Wednesday, September 26, 2012

Using UnionfsFuse on Debian/Ubuntu Linux

Unionfs-fuse is a user-space implementation of unionfs that makes it possible to run Linux over a read-only medium, such as CD-ROM. Unionfs-fuse is a convenient alternative to kernel implementations (unionfs, aufs and overlayfs), so users don't have to patch the kernel in order to try unionfs. There are many possible uses of unionfs:




  • Create a live CD
  • Run Linux from a read-only filesystem image, such as a squashfs file
  • Freeze an existing Linux system and save all changes in the memory


Unionfs achieves all this by merging a read-only filesystem and a writeable filesystem into a single virtual filesystem and mount it at a certain mount point. Let's try and apply unionfs-fuse to the third situation above. Basically, we need to create an initramfs file containing unionfs-fuse and a custom init script. The basic procedure is like this:






  1. Load kernel drivers necessary to access the underlying storage device
  2. Mount the read-only filesystem image at /opt
  3. Mount tmpfs at /opt/tmp and create a directory /opt/tmp/.change
  4. Use unionfs-fuse to merge the two and mount it at /mnt
  5. Use chroot and start /sbin/init to boot the new unionfs filesystem


To carry about the above steps, an init script might look like this:



#!/bin/dash
# Use the small but functional dash to process this script

# Mount /proc, /sys, /dev and /dev/pts just in case
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mkdir -m 755 /dev/pts
mount -t devpts -o gid=5,mode=620 none /dev/pts

# Write commands here to load modules necessary to access a hard drive
modprobe pata_via
modprobe sd_mod

# Mount the Linux filesystem read-only at /opt
mount -r /dev/sda6 /opt

# Mount tmpfs at /opt/tmp
mount -t tmpfs none /opt/tmp
mkdir /opt/tmp/.change

# Create a unionfs mount at /mnt
modprobe fuse
unionfs-fuse -o allow_other,use_ino,suid,dev,nonempty,kernel_cache \
-o cow,chroot=/opt,max_files=32768 /tmp/.change=RW:/=RO /mnt

# Make sure that init exists and is executable
if [ -x /mnt/sbin/init ]; then
mount --move /dev /mnt/dev
mount --move /proc /mnt/proc
mount --move /sys /mnt/sys

# Start init from the root filesystem with runlevel 5.
exec chroot /mnt /sbin/init 5
fi


The computer will boot into a virtual unionfs, consisting of writable tmpfs on top of read-only root filesystem. Make sure to modify /etc/rc.local so that it contains the following snippet of code.



for i in `ps ax | grep unionfs | grep -v grep | awk '{print $1}'`; do
echo $i > /var/run/sendsigs.omit.d/unionfs.$i;
done


This prevents Linux from killing unionfs-fuse during shutdown so the system will properly shut down.



Related Posts



No comments:

Post a Comment

About This Blog

KBlog logo This blog is about current events and issues concerning general population. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Followers

Total Pageviews

icon
Powered By Blogger