Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The Problem

I have a USB flash drive which, among Windows PE and other things, has an Ubuntu 11.04 system on a virtual (file-based) partition:

<drive>/ubuntu/disks/boot.disk      mount point /boot  (contains GRUB)
<drive>/ubuntu/disks/root.disk      mount point /      (contains rest of system)

It works fine...

However, all changes persist after shutdown.

I would also like an additional option on the GRUB2 menu which will boot the same installation, but which would redirect all writes from the root file system to RAM, consequently discarding any and all changes that are made to the root file system (root.disk).

To emphasize: There must still be an option to boot the system normally.

(Of course, modifications to other file systems should not be redirected -- only the root and boot disks should stay unchanged with this option.)

What I have already done

My /etc/fstab currently looks like this:

# <file system>              <mount point> <type> <options>              <dump> <pass>
proc                         /proc         proc   nodev,noexec,nosuid    0      0
/host/ubuntu/disks/root.disk /             ext2   loop,errors=remount-ro 0      0
/host/ubuntu/disks/boot.disk /boot         ext2   loop,errors=remount-ro 0      0

And my grub.cfg looks like this:

menuentry "Ubuntu, Linux 2.6.38-13-generic" {
    insmod part_msdos
    insmod ntfs
    set root='(/dev/sdb,msdos1)'
    search --no-floppy --fs-uuid --set=root MY_DRIVE_UUID
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.38-13-generic root=UUID=MY_DRIVE_UUID loop=/ubuntu/disks/root.disk ro   acpi_sleep=nonvs acpi_osi=Linux acpi_backlight=vendor
    initrd /boot/initrd.img-2.6.38-13-generic

I have already tried changing my fstab to e.g.:

# <file system>              <mount point> <type> <options>              <dump> <pass>
proc                         /proc         proc   nodev,noexec,nosuid    0      0
/host/ubuntu/disks/root.disk /rootdisk     ext2   loop,errors=remount-ro 0      0
tmpfs                        /ramdisk      tmpfs  defaults               0      0
none                         /             aufs   br:/rootdisk=ro:/ramdisk=rw 0 1
/host/ubuntu/disks/boot.disk /boot         ext2   loop,errors=remount-ro 0      0

For some reason, it doesn't mount to / correctly (I've tried all combinations of none, tmpfs, aufs, ordering, flags, etc. That I could think of), and gives me an error on startup.

So... How would I go about setting this up?

share|improve this question

Exactly what you are asking for probably cannot be done without a custom filesystem, but there is ramfs (not ramdisk nor tmpfs) which comes close. Ramfs is sometimes used as the initial filesystem when (embedded) Linux boots, i.e. the initramfs. Since this root filesystem is in memory, any changes made to it are not propagated back to its source in flash, and are lost on shutdown or the next reboot. The contents of the initramfs comes from the initramfs.cpio archive file that is attached to the kernel binary.

A custom filesystem would probably be needed to "redirect all writes from the root file system to RAM". This sort of resembles a layered filesystem that I helped develop: the top-layer filesystem was user-accessible, and was actually comprised of two individual filesystems. User access to a file would require looking for the file first in the A filesystem; if it didn't exist in A, then the B filesystem was expected to have the file. (The A filesystem contained updated files for the B filesystem w/o having to update/replace the B filesystem on read-only media.) Your dream filesystem would be similar: accessing (i.e. opening) a file would require first checking if the file is already in ramfs. If it is, then use that copy; otherwise copy the file from the disk rootfilesystem to the ramfs and then use the ramfs copy.

Perhaps you could configure your system to boot, create & mount a ramfs, copy the rootfs to to the ramfs, and then chroot to the ramfs. This would put the entire root filesystem in memory, rather than just the modified files of your dream filesystem.

share|improve this answer
Ohh I see... +1 that's excellent info. I'm wondering though, would I need to necessarily copy the data to memory in order to do your suggestion? Because that would take a ridiculously long amount of time, since my installation is like 2 GB big. – Mehrdad Feb 14 '12 at 1:25

Ubuntu and most Debian-derived distros use an "initramfs" which contains drivers and utilites needed at boot time before the root volume can be mounted. It is loaded by the bootloader right after the kernel is loaded (which is also done by the bootloader).

Normally the initramfs is dispensed with after the real root volume is mounted over it. I believe if you edit the appropriate /etc/init.d/ script you can stop this from happening.

For that to work correctly, you need your own custom initramfs that has all the files you need to run Linux. The effect should be at the end of the boot process is that you are running completely off the initramfs. Some live CDs/ISOs really do the same thing.

Copying over say, a minimal command-line only Ubuntu install, to a new custom initramfs shouldn't be too hard. I don't know of any tools to automatically "initramfs-ify" your current system but it really really seems like the type of thing that someone else has done before. I'll update this answer if I find anything out.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .