6.1. Building Live key

As we want to put a whole system on little space, we will squash some directories to have some more free space on the key.

Before squashing, we have some work to do

[Note]

Note

The following needs to be done as user root, to ensure a successful build

Issue the following:

mkdir -v /mnt/livekey
mount /dev/<sdx1> /mnt/livekey
export LIVEKEY=/mnt/livekey
cd $LIVEKEY

Where /dev/sdx1 is the partition we created previously, eg. /dev/sda1

As we don't intend our system to be writable, we have to provide a directory that will be mounted on tmpfs and will, therefore, be writable to ensure a successful boot.

mkdir -v ro rw dev

Make sure you have support for loopback device in your kernel:

ls -l /dev | grep loop

You should see a few /dev/loopX, where X is a number from 0 to 7

Create some essential files in the dev directory

mknod -m 600 dev/console c 5 1
mknod -m 666 dev/null c 1 3
cp -av /dev/loop[0-7] dev/
mknod dev/ram0 b 1 0
mknod dev/ram1 b 1 1

Copy the /etc directory

cp -av /etc .

Modify some files in it

echo "" > etc/mtab
cat > etc/fstab << "EOF"
# Begin /etc/fstab

# file system  mount-point  type   options         dump  fsck
#                                                        order

proc           /proc        proc   defaults        0     0
sysfs          /sys         sysfs  defaults        0     0
devpts         /dev/pts     devpts gid=4,mode=620  0     0
tmpfs            /dev/shm     tmpfs  defaults        0     0
usbfs        /proc/bus/usb usbfs   devgid=14,devmode=0660 0 0

# End /etc/fstab
EOF

While booting, we will have to copy some directories on a ramdisk, and mount a root filesystem on it. This has to be done in the first bootscript.

6.1.1. Creating bootscript

issue the following to create a new bootscript:

cd etc/rc.d/init.d
cat > mountrootfs << "EOF"
#!/bin/sh

# Set up variables

dir_rw=/rw
dir_ro=/ro

# Source functions

source /etc/rc.d/init.d/functions

case "$1" in
  start)
          
# Mount temporary files on $dir_rw

    echo "Mounting tmpfs on $dir_rw..."
    mount -t tmpfs tmpfs $dir_rw
    evaluate_retval
    sleep 1
    
# Create essential dirs in dir_rw

# If you plan to put some other dirs in $LIVEKEY/ like sources
# Add them to that list, eg: mkdir -v $dir_rw/sources
# You will have to mount the corresponding dirs with unionfs
    mkdir -v $dir_rw/{dev,etc,home,media,mnt}
    mkdir -v $dir_rw/{opt,root,srv,tmp,usr,var}
    
mount -n -t squashfs root.sqsh ro -o loop
mount -t unionfs -o dirs=$dir_rw/etc=rw:$dir_ro/etc=ro unionfs etc
mount -t unionfs -o dirs=$dir_rw/usr=rw:$dir_ro/usr=ro unionfs usr
mount -t unionfs -o dirs=$dir_rw/tmp=rw:$dir_ro/tmp=ro unionfs tmp
mount -t unionfs -o dirs=$dir_rw/var=rw:$dir_ro/var=ro unionfs var
mount -t unionfs -o dirs=$dir_rw/dev=rw:$dir_ro/dev=ro unionfs dev
mount -t unionfs -o dirs=$dir_rw/home=rw:$dir_ro/home=ro unionfs home
mount -t unionfs -o dirs=$dir_rw/media=rw:$dir_ro/media=ro unionfs media
mount -t unionfs -o dirs=$dir_rw/mnt=rw:$dir_ro/mnt=ro unionfs mnt
mount -t unionfs -o dirs=$dir_rw/opt=rw:$dir_ro/opt=ro unionfs opt
mount -t unionfs -o dirs=$dir_rw/root=rw:$dir_ro/root=ro unionfs root
mount -t unionfs -o dirs=$dir_rw/srv=rw:$dir_ro/srv=ro unionfs srv
chmod 1777 /tmp
         
  ;;
  *)
    echo "Usage: $0 {start}"
    exit 1
  ;;
esac
EOF
chmod 0754 mountrootfs

Inspect the contents of etc/rc.d/rcsysinit.d

ls -l ../rcsysinit.d

You should see a list beginning with S00mountkernfs, S02consolelog, ...

Now, issue:

cd ../rcsysinit.d
mv -v S00mountkernfs S01mountkernfs
ln -sv ../init.d/mountrootfs S00mountrootfs
cd $LIVEKEY

6.1.2. About the var directory

As we are just copying our system, we will need the var directory while booting. We have to remove pid and sockets file from it.

cp -av /var .
cd var
rm log/*.log
for FILE in `find . -type f -name '*pid'`
  do rm $FILE
done
for FILE in `find . -type s`
  do rm $FILE
done
rm -rf tmp/*
cd $LIVEKEY

6.1.3. Squashing directories

Create tmp, root and other directories, then squash all. When squashing, you might not want to add some file or directory. Add files to the -e option of mksquashfs if you wish that.

mkdir -pv home media mnt opt proc root srv sys tmp usr ro/{dev,etc,home,media,mnt,opt,root,srv,tmp,usr,var}
chmod 1777 tmp
mksquashfs dev etc /home /media mnt /opt /root /srv tmp /usr var root.sqsh -keep-as-directory -info
rm -r var/*

Copy dirs that won't be squashed:

cp -av /bin /boot /lib /sbin .

Now, our tree is built. In next chapter we will build an initrd file for the boot process