UbuntuHelp:AufsRootFileSystemOnUsbFlash
来自Ubuntu中文
点击翻译: |
English |
- title AUFS ( Another Union File System ) Root File System On Usb Flash
The following sections will describe how to use aufs on a / (root file system). Aufs allows the creation of a root file system on a read only flash device that feel just like a hard disk. This is an alternative to the livecd image based USB flash systems. Note: Aufs has many other uses. This is not a how to on aufs. This adventure was inspired by the Voyage Linux Distribution.
目录
Introduction
Flash has a limited number of write operations, 10,000 to 1,000,000. It would be nice if we could boot a system on a read only file system to protect the flash. LiveCd have done this for years. A Live cd just does not feel like a real system.
Goals:
1.The system must protect the flash.
2.The system must be ease to update and upgrade.
3.The system must feel like the real thing.
4.Keep It Simple and Small, KISS.
Requirements
Install Ubuntu 8.4, 4+ GB Flash device( USB or sdhc Class 4/6).
Install Ubuntu
Use the livecd to install to flash device.
Before rootaufs script
This is from the eeepc test. Ubuntu 8.4 was installed on a sdhc class 4 flash device.
The system is booted off of /dev/sdb. The systems main hard disk, /dev/sda1, is not mounted.
rootaufs:~$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sdb1 3916992 2044272 1673744 55% / varrun 1033584 100 1033484 1% /var/run varlock 1033584 0 1033584 0% /var/lock udev 1033584 40 1033544 1% /dev devshm 1033584 12 1033572 1% /dev/shm lrm 1033584 38176 995408 4% /lib/modules/2.6.24-12-generic/volatile /dev/sda1 3842376 3238736 408452 89% /media/sda1
After rootaufs script
After the rootaufs script is installed, the system is rebooted.
/dev/sdb1 is now mounted on /ro in a read only state. The system can not write to /ro. Aufs-tmpfs is a ram disk mounted on /rw in a read write state. The aufs is using both /ro and /rw to mount / and run the system. Everything work just the way you would expect.
Because changes are stored in ram, nothing is saved between reboots. See remountro and remountrw for more information on making changes without rebooting. Note: the system is running from a ram disk. Do not fill the ram disk with your log files.
Updates to the system are simple. Remove aufs=tmpfs from the end of the command line in grub before the system boots and the system will boot in the “Before” state.
rootaufs:~$ df Filesystem 1K-blocks Used Available Use% Mounted on varrun 1033584 104 1033480 1% /var/run varlock 1033584 0 1033584 0% /var/lock udev 1033584 40 1033544 1% /dev devshm 1033584 12 1033572 1% /dev/shm lrm 1033584 38176 995408 4% /lib/modules/2.6.24-12-generic/volatile /dev/sdb1 3913620 2130640 1584180 58% /ro aufs 1033584 3668 1029916 1% / aufs-tmpfs 1033584 3668 1029916 1% /rw}
Tested Laptop
System: Toshiba
RAM: 2GB
Flash: USB Flash 4GB
Tested eeepc
Note: Tested with Alpha 4 and apt-get dist-upgrade to Alpha 6. Tested with 8.4 beta 1.
System: eeepc
RAM: 2GB
Flash: SDHC 8GB || 4GB Class 4
Note: When installing on flash, use advanced option to put the boot loader on the flash drive, /dev/sdb .
Step by Step eeepc
Note: Backup your data. If you make a mistake, you can over write /dev/sda on the eeepc with the ubuntu installer.
This will not change the data on /dev/sda ( internal Flash )
Put the SDHC card in the side of the eeepc. Boot UBUNTU 8.4 – beta from an external USB CD-Rom.
Open Applications->Accessories->Terminal
If mounted, umount all disks.
# umount all devices before installing ubuntu sudo umount /dev/sda1 sudo umount /dev/sdb1
Click Install icon on desktop
Step 1 → Step 3
Forward
Step 4
Pick Manual
Remove all partitions from device /dev/sdb
Select /dev/sdb → free space
Click New
Select Prmary,Max size, Beginning, Ext2, /
Note: When you exit the partition editor, you will be asked to enable swap; just continue.
Or
Pick “Guided – use entire Disk”
Pick “SCSI4 (0,0,0) (sdb) – 4.1 GB USB2.0 CardReader SD0
Forward
Note: If you use "Guided", turn off swap in /etc/fstab after install.
Step 5
Name : <your name here>
Password: <passwd>
Name of this computer: rootaufs
Step 6
Forward
Step 7
Click on Advanced
Install boot loader is checked.
Device for boot loader installation:
/dev/sdb
OK
Note: Please read and understand the information in the scroll box before you click Install.
Install
Click Install.
Restart
Note: Hit ESC and boot off of the CardReader
First Boot
Update the System
apt-get update apt-get dist-upgrade apt-get install aufs-tools
Install the rootaufs Script
echo aufs >> /etc/initramfs-tools/modules # copy the script from "Script" section to rootaufs vi /etc/initramfs-tools/scripts/init-bottom/rootaufs chmod 0755 /etc/initramfs-tools/scripts/init-bottom/rootaufs
Remake the initramfs
update-grub update-initramfs -u
Edit Grub Menu
This is how rootaufs is used.
Add aufs=tmpfs to the end of the menu.lst entry you wish to use read only. Do not add aufs=tmpfs after entries with "single".
vi /boot/grub/menu.lst
title Ubuntu hardy (development branch), kernel 2.6.24-12-generic root (hd1,0) kernel /boot/vmlinuz-2.6.24-12-generic root=UUID=77a02dc5-aab7-41d5-a743-4659f2a16131 ro quiet splash aufs=tmpfs initrd /boot/initrd.img-2.6.24-12-generic quiet
Adding software to the system is easy
When the grub loader appears, hit e to edit. Remove aufs=tmpfs and hit b, to boot.
Make the needed changes and reboot.
Remountrw and Remountro
remountrw remounts /ro in a read / write mode. Files can now be copied from / to /ro.
sudo remountrw
remountro remounts /ro in a read only mode
sudo remountrw
/Ro
/ro is the real root file system. /ro is mounted ro by default and is not harmed by power cycling the system.
Use remountrw and remountro to make changes to the system without rebooting into single user mode.
/RW
/rw is a file system in RAM. As the system runs files are updated on / . /rw is the real location of the files that are changed.
/rw is mounted to allow df to show the real space used by the read / write part of the file system.
Note: Removing a file from /rw restores the file found in /ro. Removing a the file from / hides the file found in /ro
Home Accounts on /
Note: if your home account is on the root file system, your files are in ram and not saved.
rootaufs Script
This was sent as an email to Voyage Linux mailing list.
#! /bin/sh # Copyright 2008 Nicholas A. Schembri State College PA USA # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see # <http://www.gnu.org/licenses/>. # Thank you Voyage Linux for the idea, http://voyage.hk/ Great job on release 0.5 case $1 in prereqs) exit 0 ;; esac export aufs for x in $(cat /proc/cmdline); do case $x in root=*) ROOTNAME=${x#root=} ;; aufs=*) aufs=${x#aufs=} case $aufs in tmpfs-debug) aufs=tmpfs aufsdebug=1 ;; esac ;; esac done if [ "$aufs" != "tmpfs" ]; then #not set in boot loader #I'm not loved. good bye exit 0 fi # This is a simple overview of the steps needed to use aufs on the root file system and see the /rw and /ro branches. # initramfs init-botton script # move the root file system to aufs/unionfs readonly /ro # root is mounted on ${rootmnt} # create tmpfs on /rw # create a aufs using /ro and /rw # put some files on the tmpfs to fix mtab and fstab # move aufs to rootmnt to finish the init process. # No changes to the root file system are made by this script. # # Why! # This will allow you to use a usb flash drive and control what is written to the drive. # no need to rebuild the squashfs file just to add a program. # boot to single user mode. The system works the way you expect. boot aufs=tmpfs and no changes are written to the flash. # run ubuntu on an eeePC . # Install # Install ubuntu 8.4 Hardy. Hardy has aufs installed by default # apt-get update # apt-get dist-upgrade # apt-get install aufs-tools # echo aufs >> /etc/initramfs-tools/modules # put this file in /etc/initramfs-tools/scripts/init-bottom/rootaufs # chmod 0755 rootaufs # # clean up menu.lst # update-grub # update-initramfs -u # vi /boot/grub/menu.lst # add aufs=tmpfs to the default entry. # do not add this line to single user mode. # boot to single user mode in order to install software. # note: if your home account is on the root file system, your files are in ram and not saved. # echo echo aufs setup on ${rootmnt} echo modprobe -Qb aufs if [ $? -ne 0 ]; then echo root-aufs error: Failed to load aufs.ko exit 0 fi #make the mount points on the init root file system mkdir /aufs mkdir /rw mkdir /ro # mount the temp file system and move real root out of the way mount -t tmpfs aufs-tmpfs /rw mount --move ${rootmnt} /ro if [ $? -ne 0 ]; then echo root-aufs error: ${rootmnt} failed to move to /ro exit 0 fi mount -t aufs -o dirs=/rw:/ro=ro aufs /aufs if [ $? -ne 0 ]; then echo root-aufs error: Failed to mount /aufs files system exit 0 fi #test for mount points on aufs file system [ -d /aufs/ro ] || mkdir /aufs/ro [ -d /aufs/rw ] || mkdir /aufs/rw # the real root file system is hidden on /ro of the init file system. move it to /ro mount --move /ro /aufs/ro if [ $? -ne 0 ]; then echo root-aufs error: Failed to move /ro /aufs/ro exit 0 fi # tmpfs file system is hidden on /rw mount --move /rw /aufs/rw if [ $? -ne 0 ]; then echo root-aufs error: Failed to move /rw /aufs/rw exit 0 fi # this is not on the real file system. This is created on the tmpfs each time the system boots. # fix fstab # remove the root file system and swap. The init process will try to mount the root filesystem listed in fstab. # we need to have it mounted on /ro not on / cat /aufs/ro/etc/fstab|sed s/$ROOTNAME/\#$ROOTNAME/ |sed s/$resume/\#$resume/ >/aufs/etc/fstab # add the read only file system to fstab #ROOTTYPE=$(/lib/udev/vol_id -t ${ROOT}) ROOTTYPE=$(cat /proc/mounts|grep ${ROOT}|cut -d' ' -f3) ROOTOPTIONS=$(cat /proc/mounts|grep ${ROOT}|cut -d' ' -f4) echo ${ROOT} /ro $ROOTTYPE $ROOTOPTIONS 0 0 >>/aufs/etc/fstab # S22mount on debian systems is not mounting /ro correctly after boot # add to rc.local to correct what you see from df #replace last case of exit with #exit cat /aufs/ro/etc/rc.local|sed 's/\(.*\)exit/\1\#exit/' >/aufs/etc/rc.local echo mount -f /ro >>/aufs/etc/rc.local # add back the root file system. mtab seems to be created by one of the init proceses. echo "echo aufs / aufs rw,xino=/rw/.aufs.xino,br:/rw=rw:/ro=ro 0 0 >>/etc/mtab" >>/aufs/etc/rc.local echo "echo aufs-tmpfs /rw tmpfs rw 0 0 >>/etc/mtab" >>/aufs/etc/rc.local echo exit 0 >>/aufs/etc/rc.local #build remountrw echo \#!/bin/sh >/aufs/bin/remountrw echo mount -o remount,rw ${ROOT} >>/aufs/bin/remountrw chmod 0700 /aufs/bin/remountrw #build remountro echo \#!/bin/sh >/aufs/bin/remountro echo mount -o remount,ro ${ROOT} >>/aufs/bin/remountro chmod 0700 /aufs/bin/remountro # This should drop to a shell. (rewrite) if [ "$aufsdebug" -eq 1 ]; then echo root-aufs debug: mount --move /aufs /root exit 0 fi mount --move /aufs /root exit 0