February 4, 2018

Ubuntu Live USB with persistence

Creating a live USB is easy, adding persistence seems to be quite hard. After lots of research, there was alot of ways to do it, but most of these tutorials were out of date so some things weren’t relevant anymore or just didn’t work

Here are some hopefully simple steps to create a live USB with persistence, using Ubuntu. This will also be available as a normal USB flash drive to windows users

Requirements

  • Ubuntu ISO
  • USB flash drive. 4G is ideal, although you probably could have it on 2GB
  • Device already running linux

Steps

  • Firstly we will format the USB to FAT32 and wipe anything from it. You can use gparted if you want
#find your usb's device location
$ sudo fdisk -l

Disk /dev/sdb: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00766c29

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1  *     2048 30031249 30029202 14.3G  c W95 FAT32 (LBA)

#Now format as FAT32 (Make sure its unmounted)
$ sudo mkfs.vfat /dev/sdb
  • Now lets copy over the ubuntu ISO to the USB (this may take some time)
$ sudo dd if=~/Download/ubuntu-17.10.1-desktop-amd64.iso of=/dev/sdb1 status=progress
  • Once finished, mount up the drive so we can modify the boot config to tell it to be persistent. We will modify the grub config and also the isolinux config $ sudo mount /dev/sdb1 ~/usb/
    $ sudo vim usb/boot/grub/grub.conf
    .....
    menuentry "Try Ubuntu without installing" {
            set gfxpayload=keep
            linux   /casper/vmlinuz.efi persistent file=/cdrom/preseed/ubuntu.seed boot=casper quiet splash ---
            initrd  /casper/initrd.lz
    }
    .....
    # Add "persistent" after /casper/vmlinuz.efi
    
    $ sudo vim usb/isolinux/txt.cfg
    .....
    label live
      menu label ^Try Ubuntu without installing
      kernel /casper/vmlinuz.efi
      append  file=/cdrom/preseed/ubuntu.seed boot=casper persistent initrd=/casper/initrd.lz quiet splash ---
    .....
    # Add "persistent" to the append line, just after the boot=casper statement
  • We must now create a file (or you could shrink the partition and make a new partition, it does the same thing) for where the persistent data will store itself. It will store in a file (or partition) called casper-rw. I have chosen this to only be 1Gb, but you can choose your size below. There are several ways to create a pre-space-populated file, this is just one of them

sudo dd if=/dev/zero of=usb/casper-rw bs=1M count=1024

Now we will format the file into an ext4 format so the OS can read it

sudo mkfs.ext4 usb/casper-rw

  • All finished. Unmount and go boot from it. If you find that 1Gb isnt enough, you can resize that casper-rw using the following
#Create new block, the size of what you want to add. E.g 1Gb to make a 2Gb file
$ dd if=/dev/zero of=/tmp/tmpfile bs=1M count=1024
#Append new block to existing block
$ cat /tmp/tmpfile >> usb/casper-rw
# Enlarge the filesystem
$ sudo e2fsck -f usb/casper-rw
$ sudo resize2fs usb/casper-rw

Thanks alot to this tutorial that was the most helpful, altyhough I was unable to get the 2 partition USB thing to work, like in the example in that article.