Originally Posted By: mlord
Mine is using aufs ("another unionfs") rather than the older unionfs, but apart from that it's pretty much the same setup.

Continuing in the tradition of using the internet as a huge personal backup device, here is the little script I use to convert my notebook into a RAM-based kiosk at boot time, with read/write access still available under /.rw/ for when I need it.

Save it as /sbin/aufs_init and make it executable (chmod 755 /sbin/aufs_init).

To use it, just add init=/sbin/aufs_init to the kernel command line (via the GRUB menu at boot time, or statically in the /boot/grub/menu.lst file.

It then runs and does its tricks before passing control to the real (usual) /sbin/init program.

I also hacked the system startup script /etc/init.d/checkroot.sh to detect this and not bother attempting filesystem checks when running from RAM.

And just for fun, I also added custom reboot/halt scripts to just do immediate actions rather than running through all of the usual crap, but again only when running from RAM (2-second reboots!).

The result is a *very* fast notebook, with low wear/tear on the el-cheapo solidstate CF card. But it can also be used with a plain hard disk, for situations where you want to loan it or experiment with new software, and not have anything new saved permanently.

Note that this does require a kernel with built-in aufs ("another union filesystem") support, which is not yet in mainline. Ubuntu kernels *do* have it by default, though.

Here's the primary script used to boot/run mostly from RAM:

Code:
#!/bin/bash
#
# /sbin/aufs_init
#
# Generic script to overlay a r/w rootfs with a tmpfs in RAM,
# so that writes go only to RAM rather than the real root device.
# If any step fails, we abort and continue with normal system init.
#
# The real rootfs is still accessible (r/w) as /.rw/ when needed.
#
# Invoke with "init=/sbin/aufs_init" on the kernel command line.
#

PATH="$PATH:/sbin:/usr/sbin"
INIT=/sbin/init
TMPFS=.tmpfs
AUFS=.aufs
OLDROOT=.rw
CONS=/dev/console
ROOTDEV=/dev/root

## We want/need r/w access to the real filesystem:
cd /
mount -n "${ROOTDEV}" / -oremount,rw,noatime,nodiratime,errors=remount-ro

## Create mount points (if not already there) for tmpfs and aufs filesystems:
mkdir -p "${TMPFS}" || exec "${INIT}"
mkdir -p "${AUFS}"  || exec "${INIT}"

## Create/mount the tmpfs RAM-based filesystem:
mount -n tmpfs "${TMPFS}" -t tmpfs || exec "${INIT}"

## Use aufs to overlay the tmpfs on top of the rootfs:
mount -n -t aufs -o"noatime,nodiratime,br:${TMPFS}=rw:/=ro" none ${AUFS} || exec "${INIT}"

# Clean out some leftover temporary files, to save a little space:
cd "${AUFS}"
rm var/log/* &> /dev/null
rm -r tmp/* tmp/.[A-Z] &> /dev/null
rmdir "${TMPFS}" "${AUFS}"

# Create a default /etc/mtab file on the new aufs:
echo "none / aufs rw,noatime,nodiratime 0 0" > "etc/mtab"

## Now switch to the new aufs root filesystem, and continue with normal init:
mkdir -p "${OLDROOT}" || exec "${INIT}"
pivot_root . "${OLDROOT}"
exec chroot . "${INIT}" < "./${CONS}" > "./${CONS}" 2>&1



Edited by mlord (16/06/2008 15:17)