Create the compact flash image

So now that you got your filesystem size down to below 100megs it’s time to create the compressed image to copy over to the CF.  It’s important to note that the larger the compressed image, the longer it takes to load into RAM.  At least I think that is the problem.  It does not seem to pause nearly as long when I tried loading the compressed image from a hard drive so perhaps there is something else going on that I haven’t quite figured out yet.  All I know is that the smaller the compressed image we create in this step, the faster the system boots from CF.  That is one of the reasons it is so important to get the image as small as possible.

Note about tmpfs

An alternative and potentially more optimal method to compressed CF image to ramdisk could be to use the tmpfs command in the fstab file.  It has a number of advantages over ramdisk.  It dynamically allocates RAM based on needed which is a more efficient use of RAM.  Ramdisk usage remains static even if you don't need all the RAM allocated.  Tmpfs is the method used by Astlinux which is a highly optimized for embedded CF Linux/Asterisk distribution.  If one were to employ the methods used by Astlinux just about everything from this point forward will differ.  I could very well choose to follow Astlinux's lead in the near future as it seems to be a better way to do this.  Keep an eye on future revisions of this document for updates.

First get the actual image size:

# df -h

Assuming it is 75Meg that means we need a image size of about 100Meg because of additional overhead.

At this point we will be working from partition #2 since it still has all the Linux commands and all the Asterisk install.

Reboot the system and select partition #2 from the GRUB boot screen

# mkdir /mnt/cfimage

Now create the actual image file

# dd if=/dev/zero of=/mnt/cfimage bs=1k count=100000

You should now have a 100,000KB empty filesystem image.  Now you need to format it.

# mkfs -t ext2 -i 1024 -b 1024 -F /mnt/cfimage

Now create a temporary mount point and mount the file using the loopback interface.

# mkdir /mnt/loop

# mount -o loop /mnt/cfimage /mnt/loop

Now we need to copy the files from partition #3 (except /proc) onto the filesystem image.

# mkdir /mnt/hda3

# mount /dev/hda3 /mnt/hda3

# cp -a /mnt/hda3/bin /mnt/loop

# cp -a /mnt/hda3/boot /mnt/loop
# cp -a /mnt/hda3/dev /mnt/loop
# cp -a /mnt/hda3/etc /mnt/loop
# cp -a /mnt/hda3/home /mnt/loop
# cp -a /mnt/hda3/lib /mnt/loop
# cp -a /mnt/hda3/root /mnt/loop
# cp -a /mnt/hda3/sbin /mnt/loop
# cp -a /mnt/hda3/usr /mnt/loop
# cp -a /mnt/hda3/var /mnt/loop
# mkdir /mnt/loop/mnt
# mkdir /mnt/loop/mnt/hda1
# mkdir /mnt/loop/tmp
# mkdir /mnt/loop/proc

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 /mnt/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/hda3 / ext2 defaults 1 1
to this 

/dev/fd2 / ext2 defaults 1 0

Remove any lines for swap disk, CD drive and any other drive paths.

Copy any config, log and voicemail folders from hda3 to a temp folder on hda2, maintaining file system structure. These folders/files will go on Partition #2 and #3 on our CF drive. Now we want to sim-link to these files so that we can write to them on the CF drive even though the software is all running in a ramdisk.

For example, to sym-link to the asterisk extensions.conf file

# ln –s /etc/asterisk/extensions.conf /dev/hda2/extensions.conf

When the system is running in RAM and you want to make a change to extensions.conf, you will proceed normally and the file will actually be written and read from partition #2 on the flash. By doing this, the changes will still be there when we reboot. Repeat this step for every folder/file you want to be able to make permanent changes to while running from RAM.
Now unmount the filesystem image and compress it ready for copying onto the CF card.
 
# umount /mnt/loop
# gzip -c -9 /mnt/cfimage > /mnt/cfimage.gz