kinsa
Posts: 457
Joined: Sat Dec 01, 2012 10:16 pm

aufs root filesystem

Thu Dec 06, 2012 5:31 am

This is the aufs version of the guide that micerinos had previously posted.

The big advantage with aufs over unionfs is the ability to update the read-only filesystem while the system is running. There is no need to boot the system to a single user or use an external methods to sync the filesystems.

Requisites
  • - a working cross compiler or native build environment (this is very slow)
    - an extra partition to hold the read-write filesystem
    - a working Raspbian setup
Steps:
  • (1) create the initramfs cpio image on the RPI:

    Code: Select all

    # temporarily install busysbox and e2fsck
    sudo apt-get install busybox-static e2fsck-static
    
    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 /bin/busybox bin/ 
    ln -s busybox bin/sh 
    
    # install e2fsck
    cp /sbin/e2fsck.static sbin/e2fsck.static
    
    # create init file
    cat > init <<EOF
    #!/bin/sh
    
    mount -t proc none /proc
    mount -t sysfs none /sys
    
    /bin/busybox --install -s
    
    # create mtab so that fsck won't complain
    /bin/ln -sf /proc/mounts /etc/mtab
    
    # wait for slow sdcards
    /bin/sleep 5
    
    # populate /dev
    /sbin/mdev -s
    
    ROOTDEV=""
    ROOTFSTYPE="ext4"
    ROOTFSOPTS="noatime"
    RWFS=""
    RWFSTYPE=""
    RWFSOPTS="noatime"
    AUFS=false
    AUFSCK=false
    
    for x in $(/bin/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 
    
    # fsck root partition
    echo "Checking root partition ${ROOTDEV}"
    /sbin/e2fsck.static -y ${ROOTDEV}
    
    # 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 ] || /bin/mkdir /aufs/ro
        [  -d /aufs/rw ] || /bin/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
    
    # uninstall busybox
    sudo apt-get remove busybox-static e2fsck-static
    (2) transfer the resulting initramfs.cpio and /proc/config.gz to the development machine

    (3) download the RPi kernel and the aufs sources

    Code: Select all

    cd <working dir>
    
    # download linux source
    git clone -b rpi-3.2.27 --depth 1 git://github.com/raspberrypi/linux.git
    cd linux
    
    # download aufs source
    git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git
    cd aufs3-standalone
    git checkout origin/aufs3.2
    
    # copy aufs source to linux tree
    cp -a fs ../
    cp include/linux/aufs_type.h ../include/linux/
    cd ..
    
    # apply patches
    patch -p1 < aufs3-standalone/aufs3-kbuild.patch
    patch -p1 < aufs3-standalone/aufs3-base.patch
    patch -p1 < aufs3-standalone/aufs3-proc_map.patch
    patch -p1 < aufs3-standalone/aufs3-standalone.patch
    
    # make sure build directory is clean
    make mrproper
    (4) extract the kernel config file and copy the initramfs image

    Code: Select all

    export SRC="<source directory>"
    
    zcat ${SRC}/config.gz > .config
    cp ${SRC}/initramfs.cpio .
    (5) configure the kernel

    Code: Select all

    # enable initramfs
    sed -i 's/# CONFIG_BLK_DEV_INITRD is not set/CONFIG_BLK_DEV_INITRD=y/' .config
    
    make ARCH=arm CROSS_COMPILE=arm-bcm2708hardfp-linux-gnueabi- oldconfig
    use the defaults when asked except for the following entries:

    Code: Select all

    Initramfs source file(s) (INITRAMFS_SOURCE) [] (NEW) initramfs.cpio
    
    Aufs (Advanced multi layered unification filesystem) support (AUFS_FS) [N/m/y/?] (NEW) y
    
    Hfsplus as an aufs branch (AUFS_BR_HFSPLUS) [Y/n/?] (NEW) n
    (6) compile and install the modules

    Code: Select all

    make ARCH=arm CROSS_COMPILE=arm-bcm2708hardfp-linux-gnueabi- -j3 -k
    make ARCH=arm modules_install INSTALL_MOD_PATH=../
    (7) transfer the kernel image and the modules to the RPi sd card

    Code: Select all

    export BOOTDIR="<sd card boot dir>"
    export ROOTDIR="<sd card root dir>"
    
    # copy kernel image
    cp arch/arm/boot/Image ${BOOTDIR}/kernel_aufs.img
    
    # backup old modules
    [ -d ${ROOTDIR}/lib/modules/3.2.27+.org ] || [ ! -d ${ROOTDIR}/lib/modules/3.2.27+ ] || mv ${ROOTDIR}/lib/modules/3.2.27+ ${ROOTDIR}/lib/modules/3.2.27+.org
    # copy new modules
    cp -a ../lib/modules/3.2.27+ ${ROOTDIR}/lib/modules
    (8) test using tmpfs as rw overlay

    Code: Select all

    # use the new image
    sed -i 's/^kernel/#kernel/' ${BOOTDIR}/config.txt
    echo "kernel=kernel_aufs.img" >> ${BOOTDIR}/config.txt
    
    # update cmdline
    sed -i 's/$/ rwfs=tmpfs/' ${BOOTDIR}/cmdline.txt
    (9) boot RPi, you should see the following entries when you run the mount command:

    Code: Select all

    /dev/mmcblk0p2 on /ro type ext4 (ro,noatime,user_xattr,barrier=1,data=ordered)
    aufs-tmpfs on /rw type tmpfs (rw,relatime)
    aufs on / type aufs (rw,relatime,si=436b232b)
Prebuilt kernel and modules Working configuration
  • Here is my working setup. Note that both the rootfs and rw overlay must not be present in /etc/fstab. The /tmp and /var/log directories are mounted on tmpfs.

    /etc/fstab

    Code: Select all

    # <file system> <mount point>   <type>          <options>               <dump>  <pass>
    proc            /proc           proc            defaults                   0    0
    /dev/mmcblk0p1  /boot           vfat            ro,defaults                0    0
    tmpfs           /tmp            tmpfs   nodev,nosuid,size=30M,mode=1777    0    0
    tmpfs           /var/log        tmpfs   nodev,nosuid,size=30M,mode=1777    0    0
    /boot/config.txt

    Code: Select all

    gpu_mem=16
    kernel=kernel_aufs.img
    /boot/cmdline.txt

    Code: Select all

    smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait rwfs=/dev/mmcblk0p3 rwfstype=ext4
    partition table

    Code: Select all

    sfdisk -d
    # partition table of /dev/mmcblk0
    unit: sectors
    
    /dev/mmcblk0p1 : start=     2048, size=   204800, Id= c
    /dev/mmcblk0p2 : start=   206848, size=  1953000, Id=83
    /dev/mmcblk0p3 : start=  2159848, size=  1788696, Id=83
    /dev/mmcblk0p4 : start=        0, size=        0, Id= 0
References Cheers.

Changelog:
03 Aug 2013 - Fixed segmentation fault in init script, always fsck the ROOT partition
Last edited by kinsa on Sat Aug 03, 2013 8:05 am, edited 4 times in total.
42

micerinos
Posts: 74
Joined: Fri Nov 09, 2012 11:15 am
Location: Madrid, Spain

Re: aufs root filesystem

Sun Dec 09, 2012 8:04 pm

Cool! Well done, you fullfilled most of my TODO list with this! :)
I'll try it next week ang give you feedback.
Now the only pieces missing for a bullet-proof boot proccess are nfs support and squashfs. Just commenting... ;)
Cheers

kinsa
Posts: 457
Joined: Sat Dec 01, 2012 10:16 pm

Re: aufs root filesystem

Sun Dec 09, 2012 11:02 pm

NFS should work out of the box.

For squashfs, it is compiled as a module so it needs to be loaded when intramfs is executed.
The other option is to compile it in the kernel; add the following step in (5):

Code: Select all

# enable squashfs
sed -i 's/CONFIG_SQUASHFS=m/CONFIG_SQUASHFS=y/' .config
42

xxx
Posts: 4
Joined: Fri Dec 21, 2012 8:19 pm

Re: aufs root filesystem

Fri Dec 21, 2012 9:26 pm

I am experimenting with Debian Live and the work presented here ( aufs / initrd support / squashfs built-in) is essentially all that is needed kernel-wise. A kernel like this , with an initramfs made on the Pi by mkinitramfs with live-boot-initramfs-tools installed, can boot a squashfs file placed on the sdcard.
This works with a squashfs image of the fs on the second partition of the standard image, I just needed to remove the boottime fsck.
The question is : has anyone experience in setting up a small project to host "debian live on pi", or does anyone know a good way to collaborate on this ?

bendenoz
Posts: 2
Joined: Mon Jan 28, 2013 8:13 am

Re: aufs root filesystem

Mon Jan 28, 2013 8:16 am

Just to add to the above, it seems with the latest version of raspbian you need to disable checkroot for this to work.
The simplest way is to add "fastboot" to the kernel command line

neiser
Posts: 2
Joined: Sun Feb 03, 2013 1:23 pm

Re: aufs root filesystem

Sun Feb 03, 2013 3:41 pm

Thank you very much for this nice HOWTO. Some additional remarks, also to make it work out of the box (as of 2013-02-03):
1) Checkout the correct kernel source branch, that is rpi-3.2.27. Use

Code: Select all

git clone -b rpi-3.2.27 --depth 1 git://github.com/raspberrypi/linux.git
2) You should add dev/console and dev/tty0 in the initramfs, otherwise the shell won't work (just in case something goes wrong...). So add to the initramfs:

Code: Select all

mknod -m 622 dev/console c 5 1
mknod -m 622 dev/tty0 c 4 0
3) I also used the static busybox (package busybox-static), so I skipped the lib copy stuff and added the folders usr/bin and usr/sbin in the initramfs (prevents some ugly complaining at boot).
4) I added a localversion in the .config, so that you can easily switch between readonly kernel and a "normal" kernel (without moving the /lib/modules in place):

Code: Select all

CONFIG_LOCALVERSION="-readonly"
5) I had a strange sound problem, found this in syslog (pasting it so someone else can google it):

Code: Select all

pulseaudio[1808]: [alsa-sink] alsa-sink.c: ALSA woke us up to write new data to the device, but there was actually nothing to write!
pulseaudio[1808]: [alsa-sink] alsa-sink.c: Most likely this is a bug in the ALSA driver 'snd_bcm2835'. Please report this issue to the ALSA developers.
pulseaudio[1808]: [alsa-sink] alsa-sink.c: We were woken up with POLLOUT set -- however a subsequent snd_pcm_avail() returned 0 or another value < min_avail.
This was (probably) resolved by using the correct toolchain "arm-bcm2708hardfp-linux-gnueabi/" instead of "gcc-linaro-arm-linux-gnueabihf-raspbian/" (from git://github.com/raspberrypi/tools.git). Updating the firmware did not help...so be careful about that.

neiser
Posts: 2
Joined: Sun Feb 03, 2013 1:23 pm

Re: aufs root filesystem

Sun Feb 03, 2013 5:37 pm

neiser wrote: 5) I had a strange sound problem. This was (probably) resolved by using the correct toolchain "arm-bcm2708hardfp-linux-gnueabi/" instead of "gcc-linaro-arm-linux-gnueabihf-raspbian/" (from git://github.com/raspberrypi/tools.git). Updating the firmware did not help...so be careful about that.
No, it wasn't. But you can fix it by restarting pulseaudio.

piplayer
Posts: 14
Joined: Thu Dec 13, 2012 8:03 pm
Location: Melbourne, Australia
Contact: Website

Re: aufs root filesystem

Mon Feb 11, 2013 4:22 pm

Thanks Kinsa,

I used gparted live to add an ext2 partition to my 4GB SD card running Occidentalis v0.2 and copied across your pre-built kernel and modules.

All appears to be well according to the mount command however if I create a text file in nano and save it to my home directory it survives a reboot. If my understanding is correct that file should have been written only to RAM and not survive a reboot. Is my understanding correct ?

Thanks for your work on this,

Mike

Code: Select all

[email protected] ~ $ mount
/dev/mmcblk0p2 on /ro type ext4 (ro,noatime,user_xattr,barrier=1,data=ordered)
/dev/mmcblk0p3 on /rw type ext2 (rw,noatime,user_xattr,barrier=1)
aufs on / type aufs (rw,relatime,si=5a4b3a28)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=18896k,mode=755)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,relatime,size=10240k,mode=755)
tmpfs on /run/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=37780k)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620)
/dev/mmcblk0p1 on /boot type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=remount-ro)

Code: Select all

more /etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       0
#/dev/mmcblk0p2  /               ext4    defaults,noatime  0       0

micerinos
Posts: 74
Joined: Fri Nov 09, 2012 11:15 am
Location: Madrid, Spain

Re: aufs root filesystem

Mon Feb 11, 2013 6:00 pm

Hi,
it seems you have enabled an on-disk rw partition:

Code: Select all

/dev/mmcblk0p3 on /rw type ext2 (rw,noatime,user_xattr,barrier=1)
you should ask for a tmpfs in kernel command line as explained by kinsha.

Cheers

piplayer
Posts: 14
Joined: Thu Dec 13, 2012 8:03 pm
Location: Melbourne, Australia
Contact: Website

Re: aufs root filesystem

Tue Feb 12, 2013 9:25 pm

Thanks micineros you are of course correct. I am skiing right at the edge of my ability here :-)

I didn't appreciate that kinsa's suggestion to test with tempfs (which is closer to what I want to do in production) is different to his actual working setup with an r/w overlay partition.

I believe that the outcome of step (8) above would yield something like this in my cmdline.txt

Code: Select all

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcbl$/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait rwfs=tempfs
Which gives

Code: Select all

[email protected] ~ $ mount
/dev/mmcblk0p2 on /ro type ext4 (ro,noatime,user_xattr,barrier=1,data=ordered)
aufs-tmpfs on /rw type tmpfs (rw,relatime)
aufs on / type aufs (rw,noatime,si=e1b6f053)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=18896k,mode=755)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,relatime,size=10240k,mode=755)
tmpfs on /run/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=37780k)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620)
/dev/mmcblk0p1 on /boot type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=remount-ro)
I believe this is giving me what I want which is to eliminate the possibility of file corruption due to an SD card write being interrupted by the Pi losing power repeatedly and at any time during the boot sequence or later. This is achieved by preventing writes to the SD card altogether.

P.S. micineros thanks for your work on read-only mounting:
http://www.raspberrypi.org/phpBB3/viewt ... 58#p217758 and using unionfs for a root union file system.

Now that kinsa has documented a working implementation of aufs on the Pi are you leaning more towards it or union-fs ?

Thanks,

Mike

micerinos
Posts: 74
Joined: Fri Nov 09, 2012 11:15 am
Location: Madrid, Spain

Re: aufs root filesystem

Tue Feb 12, 2013 10:17 pm

Hi Mike,
now you have a perfectly working read-only environment. No worries about SD corruption with this setup. However, there is a strange thing in your cmdline:
root=/dev/mmcbl$/mmcblk0p2
should read
root=/dev/mmcblk0p2
Now that kinsa has documented a working implementation of aufs on the Pi are you leaning more towards it or union-fs ?
In general, aufs is a preferred method. It is based in unionfs but implemented many different functions in a more clear and stable way. Indeed, some of those improvements are being introduced in newer versions of unionfs. Support for aufs is always more up-to-date with latests kernels, whereas unionfs tends to have very long lags. But there are good reasons to try both of them. Performance-wise, I found unionfs to generate less disk activity and perform slightly faster (in the order of magnitude of 20-30 seconds for a 10 minutes apt-get install / apt-get autoremove command).
Barry Kauler, the creator of the great distro Puppy linux, has lots of experience and insights with both, as he has been using them alternatively during the last years. Here you can find some thoughts about the topic:
http://bkhome.org/blog/?viewDetailed=01449
http://bkhome.org/blog/?viewDetailed=02713

Personally, I'm sticking to aufs now. I've prepared a new version configured through an initrd file (no kernel recompilation needed if you need to mess with the init scripts) with aufs tools included, some options to control automatic sync of ro/rw partitions on boot, a simple service to allow issuing a sync on next boot (live sync of rootfs does not work very well, and is strongly discouraged by the developer of aufs), and automatic fs checking in initrd if system halted unexpectedly (fsck in an aufs requires some extra steps and should be performed before mounting root). It's working in my raspi, but sadly it has been powered off for the last month. I need to test some corner cases and clean the code before posting it. I've been really busy last weeks, and I prefer to release a working setup. Hopefully, I'll post it before the end of the month.

Cheers,
Jorge

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: aufs root filesystem

Thu Feb 14, 2013 6:32 pm

Hi,

Thanks, useful guide.

If you are going to use a non tmpfs RW layer, then a little tweak is needed on shutdown/reboot to allow the /rw partition to unmount cleanly.

Essentially something like

mount -no remount,ro,noxino /

Is needed in the shutdown/reboot sequence. I've added this to /etc/init.d/umountroot but I'm sure there is a more elegant solution.....

Seems to work happily with 3.7.7+ as well (you have to use a different set of aufs patches of course)

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: aufs root filesystem

Wed Feb 20, 2013 10:18 am

Up to 3.7.9+ and still no problems.

I have modified the initramfs a little.

Install e2fsck-static on the PI, and copy this binary across to sbin/ in the initramfs.
Init is modified slightly:

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

# mount root
mount -t ${ROOTFSTYPE} -o ro,${ROOTFSOPTS} ${ROOTDEV} /rootfs
if [ $? -ne 0 ]; then
echo "Mounting root partition ${ROOTDEV} failed"
exec /bin/sh
exit 0
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 [ -b "${RWFS}" ]; then
/sbin/e2fsck.static -y $RWFS
fi


Additions are shown in bold.

Now we get fsck on boot.

kinsa
Posts: 457
Joined: Sat Dec 01, 2012 10:16 pm

Re: aufs root filesystem

Tue Feb 26, 2013 2:01 am

Hi,

Here's the updated STEP (1) with the enhancements from neiser and MattF. File system checking is enabled by adding aufsck to the cmdline.txt.

Code: Select all

# temporarily install busysbox and e2fsck
sudo apt-get install busybox-static e2fsck-static

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 /bin/busybox bin/ 
ln -s busybox bin/sh 

# install e2fsck
cp /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

# uninstall busybox
sudo apt-get remove busybox-static e2fsck-static
Somebody should put this up on a wiki page so changes/updates can be easily shared. ;)

Cheers!

Update:

Edited first post, I seemed to gain superpowers :D
Last edited by kinsa on Sun Mar 03, 2013 11:31 pm, edited 1 time in total.
42

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: aufs root filesystem

Wed Feb 27, 2013 2:47 pm

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

If memory serves me correctly the bold addition is also needed to let fsck work.

kinsa
Posts: 457
Joined: Sat Dec 01, 2012 10:16 pm

Re: aufs root filesystem

Wed Feb 27, 2013 10:17 pm

the mtab fix is part of the init script:

Code: Select all

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

lacha
Posts: 7
Joined: Mon Nov 19, 2012 2:54 am

Re: aufs root filesystem

Wed May 22, 2013 10:22 pm

Someone is running for newest kernel?

If you can climb, would be helpful.

probono
Posts: 9
Joined: Thu Nov 01, 2012 5:28 pm

Re: aufs root filesystem

Mon Jun 03, 2013 6:16 pm

This is what I was looking for to use in my appliance-style embedded device (an IR controlled web radio in my case).

Can someone please upload the most recent kernel and modules?

Right now I am using the 3.6.11-6-ARCH+ kernel which came preinstalled with the Arch image. This contains the LIRC infrared kernel module for the RPI which I am critically dependent on.
/lib/modules/3.6.11-6-ARCH+/kernel/drivers/staging/media/lirc/lirc_rpi.ko.gz

So it would be great if someone could upload this or a similar kernel that contains the module.

probono
Posts: 9
Joined: Thu Nov 01, 2012 5:28 pm

Re: aufs root filesystem

Mon Jun 03, 2013 8:15 pm

I found that BerryBoot includes the needed kernel. Here is my try, but it does not yet succeed because my initramfs does not get loaded. Please help:

https://gist.github.com/probonopd/5701006

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: aufs root filesystem

Thu Jun 20, 2013 4:27 pm

Hi,

Here's a fairly recent kernel built for you, it is a little stripped back however (no sound or RT for instance)

Tar ball has the kernel image, and also a tar ball of lib/modules - you'll need to expand this in /

https://dl.dropboxusercontent.com/u/151 ... 13.tar.bz2

Obviously you need to setup cmdline.txt as outlined above.

User avatar
rikas
Posts: 17
Joined: Sun Jul 31, 2011 1:28 pm
Location: Austria y Argentina

Re: aufs root filesystem

Tue Jul 09, 2013 8:21 pm

Hi MattF,

in another thread (Root union filesystem on raspian: http://www.raspberrypi.org/phpBB3/viewt ... 29&t=23154) micerinos was kind enough to point me to here, where I found your kernel - thought, my problems solved - but nope!

Trying to follow the above recipies (btw: thanks to all the contributors - great help!), simply fail doing the patch (tried Kernel version 3.6 and 3.8 with aufs 3.2 up to 3.8)

Could anyone here give me some help to get the right versions "right"? The compile part itself isn't the real problem, did and do that.

What I want is a kernel with:
- i2c, OneWire, maybe SPI
- and any type of overlay FS to have a read-only root FS

Finally the RPi will act as decentralized, remote Nagios worker (temperature, humidity and some other parameters) along with Gearman. Done that before with PC's , but the RPi is just a wonderful (tiny and cheap) replacement.
Already working fine with USB attached HD, w1-temp sensors, thermal protocol printer and display. Doing a total of 237 checks, results feeded into the central nagios monitoring. (once I got it finally working I will happily post this project)

Thanks for any help

Richard
-- rikas

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: aufs root filesystem

Wed Jul 10, 2013 9:08 am

Hi,

I'm staying at 3.8 for the moment for a variety of reasons.

Anyway, start with the standard popcorn 3.8 kernel tree.


BUILDROOT=/usr/src/linux # It's possible you have something more modern than this.

cd $BUILDROOT
git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git
cd aufs3-standalone/
git checkout origin/aufs3.8patch -p1 > aufs/aufs-aufs3-standalone/aufs3-kbuild.patch
patch -p1 < aufs/aufs-aufs3-standalone/aufs3-kbuild.patch
patch -p1 < aufs/aufs-aufs3-standalone/aufs3-base.patch
patch -p1 < aufs/aufs-aufs3-standalone/aufs3-proc_map.patch
patch -p1 < aufs/aufs-aufs3-standalone/aufs3-standalone.patch
mv fs ../
cp include/linux/aufs_type.h ../include/linux/
cp include/uapi/linux/* ../include/uapi/linux/

I think that's it from a mix of memory and .bash_history

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: aufs root filesystem

Wed Jul 10, 2013 10:17 am

I've also added i2c and 1 wire to my config.

Latest tar ball is at
https://dl.dropboxusercontent.com/u/151 ... 10.tar.bz2

User avatar
rikas
Posts: 17
Joined: Sun Jul 31, 2011 1:28 pm
Location: Austria y Argentina

Re: aufs root filesystem

Wed Jul 10, 2013 11:41 am

MatF

thanks a lot !

Whatsoever, I will try to cook the kernel - will come back with results


regards
Richard
-- rikas

User avatar
rikas
Posts: 17
Joined: Sun Jul 31, 2011 1:28 pm
Location: Austria y Argentina

Re: aufs root filesystem

Thu Jul 11, 2013 4:42 pm

MatF,

thanks again - it worked like a charm! (Most likely I did something stupid, mixing versions)

RIchard
-- rikas

Return to “Advanced users”