[Tutorial] Build your own live usb with persistent partition

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Post Reply
Laurent85
Level 15
Level 15
Posts: 5539
Joined: Tue May 26, 2015 10:11 am

[Tutorial] Build your own live usb with persistent partition

Post by Laurent85 » Mon Aug 13, 2018 6:54 am

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.3 link http://sourceforge.net/projects/refind/ ... p/download

Extract zip file and enter folder path refind-bin-0.11.3/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 :
- 13/8/2018, first boot screenshot added, thanks to fabien85.
- 16/8/2018, make live usb bootable in legacy mode.
- 29/8/2018, legacy boot update.
Last edited by Laurent85 on Tue Sep 11, 2018 1:02 pm, edited 18 times in total.
Image

fabien85
Level 6
Level 6
Posts: 1340
Joined: Tue Mar 11, 2014 4:30 pm

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

Post by fabien85 » Mon Aug 13, 2018 8:21 am

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

User avatar
Sir Charles
Level 7
Level 7
Posts: 1832
Joined: Thu Jan 04, 2018 1:00 pm

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

Post by Sir Charles » Mon Aug 13, 2018 8:30 am

Thanks for sharing!
smil3f0dcf72d1f6d.gif
smil3f0dcf72d1f6d.gif (958 Bytes) Viewed 344 times
I suppose that's one of the ironies of life, doing the wrong thing at the right moment -C.C.

Laurent85
Level 15
Level 15
Posts: 5539
Joined: Tue May 26, 2015 10:11 am

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

Post by Laurent85 » Mon Aug 13, 2018 9:47 am

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

User avatar
idle
Level 3
Level 3
Posts: 171
Joined: Tue Aug 18, 2015 8:25 am
Location: Aka-kabunga West

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

Post by idle » Mon Aug 13, 2018 9:49 pm

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!

Post Reply

Return to “Tutorials”