BootToRAM

Revision 8 as of 2007-05-04 17:10:36

Clear message

UNDER CONSTRUCTION : This article is still under construction.

Booting Ubuntu To RAM

Preface

This article aims to document the process of creating a customized Ubuntu that loads an image from the hard disk to RAM, then boots an entire Ubuntu session out of RAM. It is intended for intermediate to advanced Ubuntu users who are familiar with the shell, and may have limited experience customizing the livecd (LiveCDCustomization) and shell scripting. We will customize a LiveCD and copy it to the hard drive, and make a few modifications to bootup scripts so that it copies to RAM via our good friend tmpfs.

WARNING: The author asserts that this procedure works for him, but cannot guarantee that this procedure works for anyone else. Although this procedure is meant to be 100% safe, it is feasible that there may be mistakes, or a chance of misunderstanding the instructions in a manner that causes loss of data. Please make a backup and do not attempt on mission critical systems. Read through this article thoroughly, and do not attempt if you do not comprehend or feel comfortable about any of the instructions!

Use Cases

There are many cases where one would like to boot Ubuntu to RAM:

  • Performance: The desktop performance is dramatically improved. A 400MB squashed filesystem in RAM, that holds 1200MB of data, is read back on a 1.6GHz Core Duo in about 3 seconds, including decompression time.
  • Power, Noise, Durability: Although modern hard disks don't use much power compared to other system components, this may still be important for some. In laptops, hard disks are often the noisiest components, so this setup can reduce system noise. With the hard disk spun down, a laptop can potentially withstand greater shocks without damage.
  • Abrupt poweroff: Since the hard disk is only momentarily used in read-only mode during boot, then never touched again, there are few or no negative consequences of an abrupt poweroff. If a system is used where power is inconsistent, or the system is regularly used in a context where fast shutoffs are required, this is very handy.

Requirements

The most obvious increased requirement is RAM. For best performance, I recommend having 256MB RAM + enough RAM to hold a customized image. Stripping Openoffice and some fonts and documentation from a stock Ubuntu LiveCD results in a 400MB compressed image, which fits in RAM comfortably on a system with 1GB RAM.

Tmpfs can fall back on swap (Ubuntu LiveCD scripts will mount any swap it finds), which is excellent for a bit of overflow, but if you regularly need to fall back on swap, performance will naturally suffer.

I have not investigated CPU requirements, but squashfs is compressed and decompressing takes some CPU power, so this is probably not a great idea on systems older than the Pentium III era.

As far as setting this system up, the requirements would be:

  1. Having a Feisty LiveCD ISO or CD handy.
  2. About 2-4GB of free space
  3. A combined 1GB or so of RAM and swap available.

The Process

Unpack LiveCD

First, we need to unpack the LiveCD for customization. For this article, I am going to make the following assumptions about paths. You can of course reject my choices and substitute your own.

  • The compressed root image is at /casper/filesystem.squashfs
  • The kernel is at /casper/vmlinuz
  • The initramfs is at /casper/initrd.gz
  • /dev/hda1 contains the /casper directory
  • We will make a temporary directory /casper/chroot where we edit this root filesystem.

For the first 3 files are located in /casper on the LiveCD. Please copy these files from the LiveCD to /casper on your hard disk. You may use the GNOME archive manager (file-roller) as root, bind-mounting, or a physical CD. I will assume you know how to do this.

Customize Live Environment

This procedure is almost identical to customizing a LiveCD (up to the generation of the .squashfs image). Please see [https://help.ubuntu.com/community/LiveCDCustomization/6%2e06 LiveCDCustomization/6.06] for detailed instructions on customizing the LiveCD. I will only be providing a basic rundown on the process.

Extract /casper/filesystem.squashfs to /casper/chroot/

sudo mount -o loop -t squashfs /casper/filesystem.squashfs /mnt
sudo mkdir /casper/chroot
sudo rsync -avx --progress /mnt/. /casper/chroot/.
sudo umount /mnt

Patch boot scripts

The stock casper "toram" functionality is broken in Feisty. In addition, even when it worked, it would completely decompress the filesystem into RAM, which requires 3-4x more RAM, and is hence undesired. As a result, we will be providing some nasty hacks on casper to make it copy to RAM the way we want it to. Casper developers, please look away.

gksu gedit /casper/chroot/usr/share/initramfs-tools/scripts/casper

Around line 533, find:

    fi
    mount_images_in_directory "${livefs_root}" "${rootmnt}"

    log_end_msg

Between fi and mount_image_in_directory, we will add our copying logic, so it looks like:

    fi


    mount /dev/hda1 /cdrom -o ro
    mkdir /store
    mount -t tmpfs -o size=1G none /store
    mkdir /store/casper
    cp /cdrom/casper/*.squashfs /store/casper
    umount /cdrom
    mount -o bind /store /cdrom


    mount_images_in_directory "${livefs_root}" "${rootmnt}"

    log_end_msg

Replace /dev/hda1 with the path to your correct hard disk. Replace "-o size=1G" with a larger size if your customized image is larger than 1GB (for example, 2G or 1500M)

Save this file, and quit the editor.

Regenerate initrd.gz