probonopd
6/3/2013 - 8:14 PM

Trying to use the kernel and modules from BerryBoot to turn my ArchLinux on the Raspberry Pi into a read-only boot disk using aufs

Trying to use the kernel and modules from BerryBoot to turn my ArchLinux on the Raspberry Pi into a read-only boot disk using aufs

On my Ubuntu desktop machine, I do:

===

sudo su

wget http://downloads.sourceforge.net/project/berryboot/berryboot-20130528.zip
unzip berryboot-*.zip

kpartx -av berryboot.img --> Now I can mount/access it in Nautilus, it mounts to /media/ubuntu-gnome/BA27-9913/ in my case

dd if=/media/ubuntu-gnome/BA27-9913/berryboot.img  of=initrd skip=64 bs=1
zcat initrd | cpio -id
rm initrd


===

mkdir ../initramfs 
cd ../initramfs 

# create required directories
mkdir -p aufs bin dev etc lib proc rootfs rw sbin sys usr/{bin,sbin}
touch etc/mdev.conf 

# populate dev
mknod -m 622 dev/console c 5 1
mknod -m 622 dev/tty0 c 4 0

# install busybox
cp ../initrd/bin/busybox bin/ 
ln -s busybox bin/sh 

# install e2fsck
# cp ../initrd/sbin/e2fsck.static sbin/e2fsck.static

# create init file
cat > init <<EOF
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys

# create mtab so that fsck won't complain
ln -sf /proc/mounts /etc/mtab

/bin/busybox --install -s

# populate /dev
mdev -s

ROOTDEV=""
ROOTFSTYPE="ext4"
ROOTFSOPTS="noatime"
RWFS=""
RWFSTYPE=""
RWFSOPTS="noatime"
AUFS=false
AUFSCK=false

for x in $(cat /proc/cmdline); do 
    case $x in 
    root=*)
        ROOTDEV=${x#root=}
        ;;
    rootfstype=*)
        ROOTFSTYPE=${x#rootfstype=}
        ;;
    rootfsopts=*)
        ROOTFSOPTS=${x#rootfsopts=}
        ;;
    rwfs=*)
        RWFS=${x#rwfs=}
        ;;
    rwfstype=*)
        RWFSTYPE=${x#rwfstype=}
        ;;
    rwfsopts=*)
        RWFSOPTS=${x#rwfsopts=}
        ;;
    aufsck)
        AUFSCK=true
        ;;
    esac
done

# check root device
if [ ! -b "${ROOTDEV}" ]; then 
    echo "Root partition ${ROOTDEV} missing"
    exec /bin/sh 
    exit 0 
fi 

if ${AUFSCK}; then
    # fsck root partition
    echo "Checking root partition ${ROOTDEV}"
    /sbin/e2fsck.static -y ${ROOTDEV}
fi

# mount root
echo -n "Mounting root partition ${ROOTDEV} "
mount -t ${ROOTFSTYPE} -o ro,${ROOTFSOPTS} ${ROOTDEV} /rootfs
if [ $? -ne 0 ]; then
    echo "failed"
    exec /bin/sh 
    exit 0 
else
    echo "OK"
fi 

# check for rw partition
if [ "${RWFS}" = "tmpfs" ]; then
    RWFS="aufs-tmpfs"
    RWFSTYPE="tmpfs"
    RWFSOPTS="rw"
else
    if [ ! -b "${RWFS}" ]; then
        echo "RW partition ${RWFS} missing"
   RWFS="" 
    fi
fi

if ${AUFSCK} && [ -b "${RWFS}" ]; then
    # fsck rw partition
    echo "Checking RW partition ${ROOTDEV}"
    /sbin/e2fsck.static -y ${RWFS}
fi

if [ -n "${RWFS}" ]; then 
    # mount rw partition
    echo -n "Mounting RW partition ${RWFS} "
    mount -o ${RWFSOPTS} -t ${RWFSTYPE} ${RWFS} /rw
    if [ $? -ne 0 ]; then
        echo "failed"
        AUFS=false 
    else
        echo "OK"
        AUFS=true
    fi
else
    AUFS=false
fi

if ${AUFS}; then
    # mount aufs partition
    echo -n "Mounting AUFS "
    mount -t aufs -o dirs=/rw:/rootfs=ro aufs /aufs
    if [ $? -ne 0 ]; then
        echo "failed"
        AUFS=false
    else
        echo "OK"
    fi
fi

if ${AUFS}; then
    # mount aufs  as root partition
    # test for mount points on aufs file system
    [  -d /aufs/ro ] || mkdir /aufs/ro
    [  -d /aufs/rw ] || mkdir /aufs/rw

    # move RO and RW inside aufs 
    mount --move /rw /aufs/rw
    mount --move /rootfs /aufs/ro
    # cleanup
    umount /proc 
    umount /sys 

    # Boot the real thing
    exec switch_root /aufs /sbin/init 
else
    # revert to normal rootfs
    # remount root rw
    mount -o remount,rw ${ROOTDEV}
    # cleanup
    umount /proc 
    umount /sys 

    # Boot the real thing
    exec switch_root /rootfs /sbin/init 
fi

echo "Failed to switch_root, dropping to a shell" 
exec /bin/sh
EOF

# make init executable
chmod a+x init


####

# create the initramfs image
find . | cpio -H newc -o > ../initramfs.cpio

###

In cmdline.txt, add:
rwfs=tmpfs initramfs initramfs.cpio 0x00a00000

In config.txt, add:
ramfsfile=initramfs.cpio

---> INITRD DOES NOT REALLY GET LOADED! FIXME