2007年10月11日 星期四

迷你型CF卡的Linux系統

Use a silent Compact Flash card to boot your pc, for either reduced noise, or a long life solution, with no moving parts. Ideal for firewalls, routers, or small servers.
Compact Flash is not really designed to replace your harddrive, but to just provide space you can load a filesystem image into ram from. It is not suited to the continual read/writes of normal hardrive use, so a ramdisk is loaded from the CF card, into memory, and linux run from there. This means that linux is running fast, but you need enough memory for both the filesystem image, and the operating system to use. In fact you need enough memory for over two filesystem images in order to boot, as Linux copies the filesystem image in memory when it boots.

In the example setup I used a 97Mb filesystem image (which compressed to 35Mb) , 256 MB Ram and a 256 MB CF card. A 64MB CF card would have sufficed.
So whats involved in getting linux onto the compact flash I hear you ask,
• Install Linux on a hard drive, and trim out excess stuff
• Create a small filesystem image of the hard drive
• Copy and Edit the files onto the image
• Install a boot loader, kernel and filesystem image onto the compact flash
Installing Linux
If this is to be a small server, then you don't need any X/KDE/GNOME etc stuff installed, so I'd recommend using a distribution that supports just a minimal install. My preference is for Slackware, as you can choose to just install the bare minimum, but there are lots of other distributions (including *BSD) that I'm sure offer this. Also as most of the configuration for slackware is done by hand it's easy to move the whole filesystem from one box to another, and still boot. If you are installing slackware, you should only need the first install CD, as the second contains KDE/GNOME.
To start off just install a small set of packages from the A, AP and N disk sets. This should give you enough to boot a useful box with networking, and you can install more packages as needed. I'm not going to go into detail on configuring and getting Linux installed on the disk, as there are so many distributions, and you need to be familiar with your chosen one before you embark down this road.
One important point, is that because we are going to be copying files directly from the hard drive to the filesystem image, you will want to intstall Linux without a swap disk, as when we boot from the compact flash card, there won't be any swap disk
First off we need to see how big the filesystem image is going to need to be. Use the df command to see how much disk you are using
root@lite:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda1 472392 86553 361447 20% /
From this example we can see I'm using 86553 KB, about 85 Mb. I usually create the empty filesystem image in /tmp as I then ignore this directory when copying the files. To create an empty filesystem image use the dd command. Allow a little more space than you are using , so you have room to move. I'm creating a 100,000 KB filesystem image.
root@lite:~# dd if=/dev/zero of=/tmp/lite bs=1k count=100000
100000+0 records in
100000+0 records out
You should now have a large empty filesystem image, ready for formatting. What you format it, is up to you. I use ext2, as it's basic, but needs the disk checking turned off, as the image is loaded fresh each boot, so there won't be write errors that need checking for. Because it's only a small filesystem image, we need to use a small block size, so we can have more files. I use a 1k block size.
root@lite:~# mkfs -t ext2 -i 1024 -b 1024 -F /tmp/lite
The -F parameter forces it to format the file, even though it doesn't appear to be a block device. (You can use losetup, instead if you want, and format it when mounted on the loop device).
Now create a temporary mount point and mount the file using the loopack interface
root@lite:~# mkdir /tmp/loop
root@lite:~# mount -o loop /tmp/lite /tmp/loop
If you didn't use ext2 as the format, you may need to specify the format when mounting.

Now we need to copy the files from the hard drive (except /proc) onto the filesystem image. The following commands, assume you are using slackware, and no other drives/devices are mounted under /mnt
cp -a /bin /tmp/loop
cp -a /boot /tmp/loop
cp -a /dev /tmp/loop
cp -a /etc /tmp/loop
cp -a /home /tmp/loop
cp -a /lib /tmp/loop
cp -a /mnt /tmp/loop
mkdir /tmp/loop/proc
cp -a /root /tmp/loop
cp -a /sbin /tmp/loop
cp -a /usr /tmp/loop
cp -a /var /tmp/loop
If all goes according to plan, you should not have got any disk space warning messages, otherwise, start again with a larger filesystem image file.
We now need to edit some of the files so that it boots ok, starting with the /tmp/loop/etc/fstab file. This need to be changed, so it doesn't try mounting a hard drive. We need to change the line for the root mount point from something like
/dev/hda1 / ext3 defaults 1 1
to this
/dev/fd2 / ext2 defaults 1 1
So the ramdisk loads correctly. Make sure there are no lines for a swap disk, as the CF card won't be using one. Use whatever editor you are comfortable with. I use jed, because it's easy to use. If you are using slackware, and ext2 then we need to edit the /tmp/loop/etc/rc.d/rc.S file to stop it doing disk checks You need to remove the following chunk from the file
# Test to see if the root partition is read-only...
READWRITE=no
if echo -n >> "Testing filesystem status"; then
...
...
read junk;
fi
fi # Done checking root filesystem
So the file should now look like
...
...
# enable swapping
/sbin/swapon -a

# Code You Just Deleted

# Any /etc/mtab that exists here is old, so we delete ...
/bin/rm -f /etc/mtab*
# Remounting the / partition will initialize the new /etc/mtab:
/sbin/mount -w -o remount /
...
...
Remember we are editing the files on the filesystem image (/tmp/loop), not the live files in /etc, so we can always do it again, if we get it wrong.
Now unmount the filesystem image and compress it ready for copying onto the CF card.
umount /tmp/loop
gzip -c -9 /tmp/lite > /tmp/lite.gz
Once compressed, the filesystem image is usually only a third of its original size. This allows for a smaller CF card to be used if necessary. The image is compressed so it loads into memory faster from the CF card.

Because the CF card is formatted FAT, we can use either Loadlin or SysLinux to boot the filesystem image. I'll be using SysLinux as it should work for any distribution. I'd recommend against trying to use grub or lilo to boot the cf card, as neither seem to want to work, so just leave the CF card as FAT and stick with loadlin or syslinux. If anyone gets lilo or grub to work reliably please let me know.
First up, we need to install the CF card, so we can copy files onto it. I usually put it as the secondary master with no other devices on that channel, as some disks don't seem to like it. Under linux it should be /dev/hdc. Leave your hard drive where it is, probably the primary master (/dev/hda)
Grab a copy of syslinux from syslinux.zytor.com - you can setup the CF disk from either DOS or Linux. There are ample instructions on the syslinux site, so I won't duplicate them here. I use
syslinux /dev/hdc1
Now create a mount point and mount the CF disk with
mkdir /tmp/cf
mount /dev/hdc1 /tmp/cf
Now all we need is to create a syslinux.cfg file on the cf disk and copy the filesystem image across. create a file /tmp/cf/syslinux.cfg as below
DEFAULT Lite
PROMPT 1
IMPLICIT 0
TIMEOUT 150

LABEL Lite
KERNEL vmlinuz
APPEND initrd=lite.gz ramdisk=100000 rw root=/dev/ram
Note the ramdisk size in the syslinux.cfg is the same as the size of the filesystem image we created. Now copy a kernel to the disk, this assumes you are using slackware.
cp /boot/vmlinuz /tmp/cf
And finally copy the compressed image across
cp /tmp/lite.gz /tmp/cf
Now check the first partition on the CF card is marked as bootable, using fdisk
root@lite:~# fdisk /dev/hdc

Command (m for help): p

Disk /dev/hdc: 256 MB, 256376832 bytes
16 heads, 32 sectors/track, 978 cylinders
Units = cylinders of 512 * 512 = 262144 bytes

Device Boot Start End Blocks Id System
/dev/hdc1 * 1 977 250096 6 FAT16

Command (m for help): q

root@lite:~#
Now its all ready for booting. Turn off the computer, move the CF card to the primary master, and reboot....
What you do from here is up to you. There is a lot of tweaking, and setting up still to do, but you should have a basic working bootable Compact Flash card. All of the instructions can be put in a script, so you can automate the whole process, but remember to keep the edited copies of fstab, rc.S and syslinux.cfg so you don't have to re-edit them each time.

沒有留言: