Ubuntu – Mounting the persistence partition (casper-rw) read-only

grub2isolive-usbpersistence

I'm trying to create a fresh ISO image for use on a USB stick. I've partitioned my USB stick into 2 partitions, one vfat and one ext4. The vfat partition has the ISO and GRUB2 installed to it, such that GRUB2 boots the ISO.

The ext4 partition has the volume label casper-rw and is used as the persistent data store for the Live USB. I've modified the grub.cfg such that I get two options at boot, one to boot the ISO in persistent mode and one to boot normally.

However, when booting in "normal" mode, the casper-rw partition is mounted as a normal drive and changes made in persistent mode don't stay.

What I would like is the casper-rw partition to be mounted overlaid on the / filesystem, but when I ask Ubuntu to boot normally (i.e., without the persistent option in GRUB2), I'd like that partition to be mounted read-only so that changes made in the persistent mode persist.

What is the best way to achieve this?

Best Answer

What i did is I modify casper script inside initrd.lz.

The script will mount casper-rw as read-only and then on overlay it on top of /. It will then mount tmpfs on top of the overlay.

I use mint btw, not sure about ubuntu, hope it works for you.

Here are more details:

  1. Extract initrd using instruction from here.
  2. Edit scripts/casper, modify the setup_unionfs() function. You find the modified function here.

    ...
    p_ro_mount=""
    if [ -n "${PERSISTENT}" ]; then
          cowprobe=$(find_cow_device "${root_persistence}")
          if [ -b "${cowprobe}" ]; then
                p_ro_device=${cowprobe}
                p_ro_fstype=$(get_fstype "${cowprobe}")
                p_ro_mountopt="ro,noatime"
                p_ro_mount="/persistent"
                mkdir -p $p_ro_mount
                mount -t ${p_ro_fstype} -o ${p_ro_mountopt} \   
                  ${p_ro_device} \
                  ${p_ro_mount} || panic "Can not mount"
    ...
    case ${UNIONFS} in
    ...
       overlayfs)
           mounts=""
           for mount in /cow $p_ro_mount $rofslist
    ...
    
  3. Replace initrd.lz, or in my case, I just created a new one.

    find . | cpio --quiet -o -H newc | lzma -7 > ../ro-initrd.lz
    
  4. Add more boot option to syslinux.cfg

    append initrd=/casper/ro-initrd.lz file=/cdrom/preseed/linuxmint.seed boot=casper  splash -- persistent
    

Note: I use Mint 16 (x86) and unetbootin.