aufsというファイルシステムを使うと複数ディレクトリを

一つのディレクトリに重ねてマウントすることができる。

# mkdir /aufs
# mount -t aufs -o dirs=/data=rw:/=ro none /aufs
# mount --rbind /dev /aufs/dev
# mount --rbind /sys /aufs/sys
# mount -t proc none /aufs/proc
# chroot /aufs /bin/bash

試しにルートディレクトリとdataディレクトリをaufsディレクトリにマウントしchroot環境でパッケージを追加してみるとdataディレクトリにファイルが追加されていることが確認できる。

Debianだとcowbuilderがあるので上記はあまり使いどころがない気がするが起動時にaufsを使ってルートディレクトリを読込専用、dataディレクトリを書き込み用でマウントすればインストール時の環境を保ったまま使用できるのではないかと思いやってみた。(Debian Squeeze)

# aptitude install aufs-tools
# echo aufs >> /etc/initramfs-tools/modules
# vi /etc/initramfs-tools/scripts/init-bottom/mount_root_aufs

#!/bin/sh -e

case $1 in
  prereqs)
	exit 0
	;;
esac

for x in $(cat /proc/cmdline); do
  case $x in
	root=*)
	  ROOTNAME=${x#root=}
	  ;;
	aufs=off)
	  UNION=0
	  ;;
  esac
done

if [ "$UNION" -eq 0 ]; then
	exit 0
fi

# make the mount points on the init root file system
mkdir /aufs /ro /rw

# mount read-write file system
mount -t ext3 /dev/vdb1 /rw -o noatime <- ここを自分の使うデバイスに書き換えればいい

# move real root out of the way
mount --move ${rootmnt} /ro

mount -t aufs aufs /aufs -o noatime,dirs=/rw:/ro=ro

# test for mount points on union file system
[ -d /aufs/ro ] || mkdir /aufs/ro
[ -d /aufs/rw ] || mkdir /aufs/rw

mount --move /ro /aufs/ro
mount --move /rw /aufs/rw

# strip fstab off of root partition
grep -v $ROOTNAME /aufs/ro/etc/fstab > /aufs/etc/fstab

mount --move /aufs /root

exit 0
# update-initramfs -u

シェルスクリプトは下記のサイトを参考にした。

aufsRootFileSystemOnUsbFlash

起動時にro(ルートディレクトリ)とrw(書き込みディレクトリ)がaufsでマウントされるようになる。

aufsを使わない場合はブートローダの画面でaufs=offのオプションを与える。

aufs on

Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                   257364         0    257364   0% /lib/init/rw
udev                    252880        96    252784   1% /dev
tmpfs                   257364         0    257364   0% /dev/shm
rootfs                 5160272    143856   4754288   3% /
/dev/vda2              4174684    665900   3296716  17% /ro
/dev/vdb1              5160272    143856   4754288   3% /rw

aufs off

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/vda2              4174684    665920   3296696  17% /
tmpfs                   257364         0    257364   0% /lib/init/rw
udev                    252880        96    252784   1% /dev
tmpfs                   257364         0    257364   0% /dev/shm

aufs=offで起動してmkfs.ext3すればインストール時の状態に戻すことができる。

Please take a look at []() or jump right into Usage if you’d like.



blog comments powered by Disqus

Published

09 June 2012

Tags