In part one we created a bare minimum custom installation of the Ubuntu Server software on a USB stick, now we will set up the stick such that depending on the boot option chosen, the entire system will either be read only but live, or read/write like normal.
Sometimes you need to carry a reliable Linux system around with you but don’t want to (or can’t) use those LiveCDs, or you don’t need a graphical desktop, or you need to ensure that whatever you do while the stick is running doesn’t impact the drive itself or alter anything. These installations can be used for working on clients computers to clean or detect a virus infection on any operating system, to copy or backup files, to check the health of a hard drive or the hardware itself using commonly available tools like memtest86 or smartmontools, or repair an installation of Linux or Windows. With this setup, changes take place in ram due to the magic of aufs.
This part is much more simple than creating the USB stick was in part one, there are only a few steps and a script to copy into the right place on the system.
First lets copy the script to your system, it is available here: http://tech.xerces.com/files/rootaufs
Put that file in /etc/initramfs-tools/scripts/init-bottom/
Now fix the permissions on that file:
root@live:/# sudo chmod 0755 /etc/initramfs-tools/scripts/init-bottom/rootaufs
Then we tell initramfs-tools to include the right kernel module for aufs:
root@live:/# sudo echo aufs >> /etc/initramfs-tools/modules
Now update the initramfs:
root@live:/# sudo update-initramfs -u
This processes the changes we just made such that this script is now included in the initramfs along with the right kernel modules needed for read-only operation.
Now we alter the grub default options with nano or another editor to create the 2 separate boot modes. Use sudo so you have correct permissions to edit the file.
Find this in /boot/grub/menu.lst:
## additional options to use with the default boot option, but not with the ## alternatives ## e.g. defoptions=vga=791 resume=/dev/hda5 # defoptions=quiet splash
And make the defoptions line look like this:
# defoptions=quiet splash aufs=tmpfs
Now change this section:
## altoption boot targets option ## multiple altoptions lines are allowed ## e.g. altoptions=(extra menu suffix) extra boot options ## altoptions=(recovery) single # altoptions=(recovery mode) single
So it looks like this:
## altoption boot targets option ## multiple altoptions lines are allowed ## e.g. altoptions=(extra menu suffix) extra boot options ## altoptions=(recovery) single # altoptions=(writable mode) quiet splash # altoptions=(recovery mode) single
I found that the update-grub script didn’t like having 2 altoptions line so i removed the recovery mode from mine, so if your system doesn’t boot and give you these options try that.
Now we update grub:
sudo update-grub
When the system boots it will default to read only mode, if you need to update the system reboot in readwrite mode. You may need to hit esc during boot to see the menu. Comments are welcome :)
Great tutorial!
But i have a question…
How to manage that i have a specified mount point that is writable??? i have an application that will be deployed with a readonly system partition, but needs to have a writable partition.
for example i wanna mount /dev/sda6 as /data and this directory should be writable…
thanks in advance
Hi there,
Thanks, this is a great tutorial – but part 2 unfortunately doesn’t work on my pc. When I try to boot the read-only system, I receive the following error on boot:
/etc/initramfs-tools/scripts/init-bottom/rootaufs line 1: #! not found
I’m sure I followed your steps exactly – the script does start with #! – I have no idea why this error occurs. Do you have an idea?
thanks, regards,
dd
It sounds like the interpreter can’t be determined for some reason, that line is supposed to say “#! /bin/sh”, but perhaps the space after the exclamation point is causing problems. Try putting #!/bin/bash on that line instead if removing the space doesn’t help.
It’s also possible this is a problem of encoding, i’ve seen some Linux distros exhibit a strange bug like you seem to have found, where scripts won’t execute because it can’t read the interpreter line. Are you editing the file in Linux or Windows?
Chris is probably long gone, but for anyone else reading, you can always put a line in /etc/fstab so that another partition mounts at any place on the filesystem. A line in Chris’ case would look like this:
/dev/sda6 /data ext4 noatime,errors=remount-ro 0 0
You will have to make sure any changes to the fstab file actually get written to the drive, because by default we’re in read-only mode and writes will be discarded after a reboot.
Hi,
thanks for the reply. Well, I have already tried removing the space but that didn’t help. I’ll try using bash this evening, maybe that works.
The file is edited with vi in linux.
regards
Hi again,
I just tried using “#!/bin/bash” without success:
scripts/init-bottom/rootaufs: line 1: #! not found
scripts/init-bottom/rootaufs: line 1: #! not found
Any other idea?
Thanks,
dd
Hmm, i’m not sure what the problem is. My guess is some kind of formatting problem, or it would be telling you it can’t find #!/bin/sh and not just #!.
I did find this
I’ll look around, but if that doesn’t work try asking on the Ubuntu forums, someone might know a quick way to fix it :)
Oh man… I resolved this issue. When I typed ‘head rootaufs’ i saw, that there were some strange characters before #!, which were not displayed in VI or nano. Maybe this was a copy/paste mistake.
However, now the boot process stops when checking the root file system. I have this error:
fsck.ext2: No such device or address while trying to open /lib/init/rw/rootdev Possibly non-existent or swap device?
fsck died with exit status 8
I saw now that in this guide ext3 is used, but that should not be the problem?
thanks so far,
dd
Hmm, never seen that before. When I get home tonight i’ll run through the entire tutorial again and see if i can replicate that error.
Unfortunately i can’t replicate that fsck error here, which version of Ubuntu are you using?
Hi,
Well, I had Ubuntu 8 (can’t remeber 8.4 or 8.10), but yesterday I reinstalled the whole system with ubuntu 9.10… and now the system boots fine. No idea what the problem was.
However, I have another problem. As soon as disconnected the display from the computer and restart the system, it hangs.
I already found out that there’s a problem with the kernel mode setting which should be deactivated via “nomodeset” kernel parameter in grub, but actually doesn’t work for me.
I tried those read-only steps.
However after boot, grub menu shows 3 items, 1st is default.
I boot it from default. But I tried to install dhcp server, it can install.
Hence, what is your mean of read-only?
Thanks.
You can install whatever you want, it just won’t be written to the USB stick and will be gone after a reboot, assuming you boot the read-only option.
Hi Steve,
I found that the file rootaufs has problem applying to Karmic 9.10.
Lines #110 & #157 have different problems. Line #110: “modprobe -Qb” may be “modprobe -qb”. Line #157: should be a comment line (lack of a “#” sign).
Hence, I wondered that I still can install/remove some packages to the system even reboot.
They do seem to have updated the rootaufs script for 9.10, the original is at the link below.
https://help.ubuntu.com/community/aufsRootFileSystemOnUsbFlash#rootaufs%20Script
Thanks for the write-up, this is pretty cool!
Are there any updates for 10.04? I found that I needed to install grub2 in the chroot, since my laptop was using grub2… so grub-install was installing a version that was looking for a grub.cfg, not a menu.lst.
As far as aufs and the initrd scripts, has any of this been tested in lucid?
I haven’t tested this in Lucid, but if it doesn’t work it should be possible to fix it with minor changes.
There is a project based on the rootaufs script that seems to be up to date and perhaps easier to use but I haven’t tried it yet: http://sourceforge.net/projects/lethe/files/
Leave a comment or question