Skip to content

Instantly share code, notes, and snippets.

@ymollard
Last active January 22, 2023 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymollard/d76ef0011fb062cd43f34622cb16b131 to your computer and use it in GitHub Desktop.
Save ymollard/d76ef0011fb062cd43f34622cb16b131 to your computer and use it in GitHub Desktop.
Create, customize, and duplicate Ubuntu Live USB sticks with persistence

1. If you are creating the image

1.1. Create the Live stick

Install DUS/MKUSB :

sudo add-apt-repository ppa:mkusb/ppa
sudo apt update
sudo apt install mkusb mkusb-nox usb-pack-efi
dus
  1. i (install)
  2. p (persistent)
  3. Select the ISO
  4. If there is a question about USB image source, answer NO
  5. Activate EFI + MSDOS options
  6. 50% of size
  7. efi-1 most current cases
  8. Go!

1.2. Enable fs journal of the casper-rw partition

sudo tune2fs -O has_journal /dev/sda5

Make sure the journal is enabled. The use_journal feature must be visible there:

sudo dumpe2fs /dev/sda5 | grep "Filesystem features"

1.3. Disable usbdata

Only applies if a partition usbdata. This partition acts as a regular USB stick, drop it to make the end image lighter.

sudo gparted

2. If you are editing an existing image on a stick (or if you have reached step 1.3.)

2.1. Customize the stick with your own need

Flash th image on a stick, boot on it and install everything that needs to be persistent.

Few interesting settings to setup:

  • Etcher
  • alias ll=ls -l
  • Terminator x4
  • VSCode
  • net-tools git-cola python3-rosinstall

Things to delete:

  • /var/cache/apt/archives/*
  • /var/lib/dhcpcd5/*
  • /var/log/*
  • /var/tmp/*
  • /tmp/*
  • ~/.bash_history
  • git config --global user.email ""
  • git config --global user.name ""

2.2. Reduce size for duplication

  1. With gparted resize partitions to a smaller size (e.g. 20GB for 32GB USB sticks. In any case keep >5GB of free space)
  2. sudo dd if=/dev/yourstick of=disk.img bs=4M status=progress
  3. sync
  4. sudo fdisk -l disk.img
  5. Read the last end sector = END
  6. truncate -s $(((END+1)*512)) disk.img
  7. Compress the img with 7z and share the archive to your recipients

2.3. Copy image to a target stick

  1. Unzip the 7z into disk.img
  2. dd if=disk.img of=/dev/yourtarget bs=4M status=progress
  3. sudo sync

3. If you are duplicating your original image to multiple target sticks

3.1. Repair backup tables on the target stick

When copying a stick to another with a slighty smaller one, dd will complain that no space left on device and end GPT/partition tables are likely to be corrupted. To fix it:

sudo gdisk /dev/sda
r    recovery menu
d    use main GPT header (rebuilding backup)
e	   load main partition table from disk (rebuilding backup)
YES
w    write table to disk and exit
YES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment