[Tutorial] Build your own live usb with persistent partition

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
Laurent85
Level 17
Level 17
Posts: 7081
Joined: Tue May 26, 2015 10:11 am

[Tutorial] Build your own live usb with persistent partition

Post by Laurent85 »

Live EFI boot disk featuring the rEFInd boot manager and persistent partition in 4 fairly easy steps.

1. Partitioning a 32 GB usb stick

GPT partition table with 2 primary partitions, feel free to adapt the partition sizes to your needs:
- first partition fat32 labelled EFIBOOT, around 9 GiB
- second partition ext2 labelled casper-rw, around 19 GiB

gparted screeshots:

Image

Image

Image

Image

Note you can format the persistent partition to ext4, I chose ext2 for better speed and less overhead over ext4.

Exit gparted.


2. Grub installation
A Grub menu maintains a list of available iso images for booting with different boot options.
To mount the usb fat32 partition on the necessary mount point left click the EFIBOOT device label side pane of file manager. Then run below command from a terminal to install grub efi on the usb stick:

Code: Select all

sudo grub-install --target=x86_64-efi --efi-directory=/media/$USER/EFIBOOT --bootloader-id=loader --boot-directory=/media/$USER/EFIBOOT/boot --no-nvram --no-extra-removable --no-uefi-secure-boot
Command can take quite some time to complete.


3. rEFInd installation
rEFInd boot manager screen will show up at startup. rEFInd can detect any OS available for booting on an uefi computer. rEFInd will also chain to the grub menu that lists available iso images for booting.

Note rEFInd is installed on your live usb, no changes are made to your computer boot setup.

Create folder EFI/boot:

Code: Select all

mkdir /media/$USER/EFIBOOT/EFI/boot
Getting rEFind binary:
Get latest refind-bin zip file from https://sourceforge.net/projects/refind/files/
Current version 0.11.4 link http://sourceforge.net/projects/refind/ ... p/download

Extract zip archive and enter folder path refind-bin-0.11.4/refind
Copy below files and folders to usb folder EFI/boot:

Code: Select all

drivers_x64
icons
refind_x64.efi
refind.conf-sample
Then rename files refind_x64.efi to bootx64.efi
and refind.conf-sample to refind.conf


4. grub.cfg configuration file
Live usb tree (partial):

Code: Select all

.
├── boot
│   └── grub
│       ├── fonts
│       ├── grub.cfg
│       ├── grubenv
│       ├── locale
│       ├── themes
│       └── x86_64-efi
├── EFI
│   ├── boot
│   │   ├── bootx64.efi
│   │   ├── drivers_x64
│   │   ├── icons
│   │   └── refind.conf
│   └── loader
│       └── grubx64.efi
└── isoimages
    └── linuxmint-19-cinnamon-64bit-v2.iso
Create folder isoimages at the top of the filesystem tree and copy to that directory your Linux Mint iso image.

Create grub.cfg configuration file in folder boot/grub that will maintain the list of available iso images for booting and your customized boot options. Some grub parameters are somehow cryptic, iso images from different origins may need different parameters to boot properly but that's outside of the scope of this tutorial. Each entry in the menu is manually added and maintained.

To begin with use my configuration file as a template, here for Linux Mint 19 Cinnamon iso image:
- first menu entry "Linux Mint 19 Cinnamon live" will boot LM live as usual
- second entry "Linux Mint 19 Cinnamon persistent" will boot LM iso image using usb casper-rw partition for persistence. Notice specific grub parameter persistent
- third entry "Linux Mint 19 Cinnamon ram persistent" also uses persistence plus loads the live squashfs file system into ram. Notice specific grub parameters toram persistent

If you want to configure the same iso image then just copy paste the following contents to grub.cfg:

Code: Select all

insmod all_video
loadfont unicode
set gfxmode=auto
insmod gfxterm
terminal_output gfxterm
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

menuentry "Linux Mint 19 Cinnamon live" {
search --set -f "/isoimages/linuxmint-19-cinnamon-64bit-v2.iso"
loopback loop "/isoimages/linuxmint-19-cinnamon-64bit-v2.iso"
linux (loop)/casper/vmlinuz file=/cdrom/preseed/linuxmint.seed boot=casper iso-scan/filename=/isoimages/linuxmint-19-cinnamon-64bit-v2.iso quiet splash --
initrd (loop)/casper/initrd.lz
}

menuentry "Linux Mint 19 Cinnamon persistent" {
search --set -f "/isoimages/linuxmint-19-cinnamon-64bit-v2.iso"
loopback loop "/isoimages/linuxmint-19-cinnamon-64bit-v2.iso"
linux (loop)/casper/vmlinuz file=/cdrom/preseed/linuxmint.seed boot=casper iso-scan/filename=/isoimages/linuxmint-19-cinnamon-64bit-v2.iso quiet splash persistent --
initrd (loop)/casper/initrd.lz
}

menuentry "Linux Mint 19 Cinnamon ram persistent" {
search --set -f "/isoimages/linuxmint-19-cinnamon-64bit-v2.iso"
loopback loop "/isoimages/linuxmint-19-cinnamon-64bit-v2.iso"
linux (loop)/casper/vmlinuz file=/cdrom/preseed/linuxmint.seed boot=casper iso-scan/filename=/isoimages/linuxmint-19-cinnamon-64bit-v2.iso quiet splash toram persistent --
initrd (loop)/casper/initrd.lz
}
Finally unmount the usb stick.


First boot
Here a dual boot Linux Mint / Windows computer, note by default Linux Mint uses an Ubuntu identifier for EFI, hence the Ubuntu icon. Screenshot revamped and cropped for the purpose of this tutorial, actual display in your case may differ slightly:

Image

Notes:
- from rEFInd startup screen select the Linux Tux icon harboring a usb stick to chain to the grub menu.
- only associate the persistent partition with a single iso image of your choice. Don't try to setup menu entries with persistence for different iso images.
- once LM Cinnamon running in persistent mode disable Nemo feature "Automatically mount removeable media when inserted and on startup" in Nemo Preferences > Behavior
- rEFInd home page for further references at http://www.rodsbooks.com/refind/index.html

5. Optional: make live usb bootable in Legacy mode
Before proceeding with grub installation in legacy mode, if unsure determine the device id of your usb stick, below command returned /dev/sdc in my case:

Code: Select all

echo /dev/$(lsblk -r -o name,label | grep EFIBOOT$ | cut -d'1' -f1)
Create a 1 MB bios grub partition using free sectors 34-2047, replace <DEVICE> in below command with the device id of your usb stick:

Code: Select all

sudo sgdisk -n9:34:2047 -t9:ef02 <DEVICE>
Left click device label EFIBOOT side pane of file manager to mount EFIBOOT partition at the correct mount point.
Grub installation: replace <DEVICE> in below command with the correct device id of your usb stick:

Code: Select all

sudo grub-install --target=i386-pc --boot-directory=/media/$USER/EFIBOOT/boot --root-directory=/media/$USER/EFIBOOT --skip-fs-probe <DEVICE>
Note the live usb in legacy mode will directly load your custom grub menu, rEFInd boot manager is only available in EFI mode.

Edit :
- 29/8/2018, legacy boot update.
- 16/8/2018, make live usb bootable in legacy mode.
- 13/8/2018, first boot screenshot added, thanks to fabien85.
Last edited by Laurent85 on Tue Nov 27, 2018 7:35 am, edited 21 times in total.
Image
fabien85
Level 7
Level 7
Posts: 1877
Joined: Tue Mar 11, 2014 4:30 pm

Re: [Tutorial] Build your own live usb with persistent partition

Post by fabien85 »

Nice!
I bookmarked it for reference.
I couldn't create a screenshot of the main rEFInd screen at startup
You can use F10 to create a screenshot of refind, this is documented here : http://www.rodsbooks.com/refind/using.html#keyboard
Sir Charles

Re: [Tutorial] Build your own live usb with persistent partition

Post by Sir Charles »

Thanks for sharing!
smil3f0dcf72d1f6d.gif
Laurent85
Level 17
Level 17
Posts: 7081
Joined: Tue May 26, 2015 10:11 am

Re: [Tutorial] Build your own live usb with persistent partition

Post by Laurent85 »

fabien85 wrote: Mon Aug 13, 2018 8:21 am You can use F10 to create a screenshot of refind, this is documented here : http://www.rodsbooks.com/refind/using.html#keyboard
Tutorial updated, thanks Fabien.
Image
idle

Re: [Tutorial] Build your own live usb with persistent partition

Post by idle »

I can ask this because I'm a noob :mrgreen:

What would be really cool is a program that could do all the above with a few simple clicks, like selecting partition sizes, click yes and yes and your done!
ud6

Re: [Tutorial] Build your own live usb with persistent partition

Post by ud6 »

I had problems with this method because my system uses BIOS and not UEFI so first bash command couldn’t find files. Thus:

Thus, this is an addendum for those wanting to do persistence on USB with BIOS rather than UEFI boot loader.

This is adapted version of: http://tuxtweaks.com/2014/03/create-lin ... -live-usb/

For 8GB pen drive (change size of casper-rw and data partition for your own needs with larger USBs). Install was 19.0 but should work for previous too.

Install unetbootin, extlinux, and gparted from software manager (instead of unetbootin, mutlibooot or other software that allows persistence probably works too). Ensure you have ISO of linux mint and clean USB for your persistent live mint.

1. Partition USB

Open gparted and ensure you're looking at your USB where you want to install mint. Delete any partions. Right click unallocated space and select as primary partition. Choose ext2 format (ext2 better than FAT32 for the OS), a size of 2100MB (i.e. 2.05GB) is ideal. Label as “mint19per” or something similar that makes sense to you. This will be your boot partition. Press OK

Right click on remaining unallocated space. This MUST be labelled “casper-rw”. I allocated 4000MB (3.91 GB) but you can allocate more for bigger USB. This is your persistent partition. Also use ext2.

On the unallocated space right click and select FAT32 and label it something like “data” to create a third partition. I use FAT32 to enable files to be read in Windows.

Double check that you are making changes in the USB you want to install OS onto, and then in gparted menu: edit/apply all operations.

Before you close gparted right click on first partition (left most partition) again and choose ‘manage flags’. Click on ‘boot’ (nothing else should be selected). This allows first partition to be bootable.

Note the location of the first partition which you chose as boot partition on your USB (something like sdb1 or similar). Close gparted.

2. Installing mint to USB

Open unetbootin (or equivalent). Choose ‘disk image’ and find the 19.0 ISO file on your computer (using the “...” button in unetbootin). Choose space to preserve files as 100MB. For USB drive, select the boot partition you noted in (1), and choose OK. You may have to mount the partition, but you’ll be promoted for this if you do. It takes around 15 minutes to complete installation, and then you get an ‘exit’ button, which you choose to close unetbootin.

3. Using casper-rw partition for persistence

Open the USB partition where mint was installed (eg. mint19per) AS ADMIN. Delete the “casper-rw” file (this forces the mint OS to use your 4000MB casper-rw partition instead of the small 100MB file). Shut down the computer.

4. Testing persistence
Start computer, and choose to boot into the USB (sometimes Esc, sometimes F2 or other F key to get into boot menu). Make a minor change to your desktop, such as changing the wallpaper. Shutdown and boot back into the USB just to check your persistence works.
Screenshot at 2018-09-19 13-33-28.resized.png
Useful Notes
1. If this is not to be used as regular OS don’t update as will use alot of space in the ‘casper-rw’ partition. Instead you can turn off update in ‘startup applications’ as well as changing update manager preferences so it isn’t shown and so it doesn't give you any update options. You could also turnoff flatpak in ‘startup applications’ (unlikely you’ll use it from a USB).
2. Don’t bother removing software from the original mint: the original install is always on the USB (and if you install mint from this USB it will be the original unchanged mint)
3. I install f2fs tools (so I can read f2fs file systems), and multimedia codecs, both found in software manager.

Above is my install with my favourite ‘pop!’ icons, I also labelled menu as “Tara 19.0USBp” so I don't forget I'm in USB OS and not my desktop OS. I installed WPS and vlc; all on a tiny USB that protrudes 3mm from the USB port.
WhatsApp Image 2018-09-19 at 15.37.52.resized.jpeg
Post Reply

Return to “Tutorials”