Skip to content

Instantly share code, notes, and snippets.

@AndrewSmart
Last active August 13, 2018 03:06
Show Gist options
  • Save AndrewSmart/700a68af341898bb17e11d7fd67eaae6 to your computer and use it in GitHub Desktop.
Save AndrewSmart/700a68af341898bb17e11d7fd67eaae6 to your computer and use it in GitHub Desktop.
[HOWTO] Guide to re-squash persistent live USB to reclaim space

I followed a different guide on this forum for creating the live USB in the first place. I didn't use unetbootin. This guide assumes:

  1. You have two partitions on the live USB:
    1. The first containing the squashfs, vmlinuz, and initrd; this partition has the 'boot' flag.
    2. The second containing the rw persistence partition, labeled 'persistence'.
  2. Your live-persistence.conf file contains just "union /".

Following this guide I had squashed the 2.3GB of used space to 550MB. The increased space usage is due to the updating process; if you want the space back we must re-squash everything into the squashfs. If you have a humongous USB, what you would gain from this guide is a some-what faster boot. I wish there were a way for this to be automated; but AFAIK that is not possible at this time.

  1. First, you may have noticed if you run apt-get update, you may encounter errors if update-initramfs is triggered: http://crunchbang.org/forums/viewtopic.php?id=25013

    • Follow this guide to get around that problem (so vmlinuz and initrd on the first partition can be updated): http://crunchbang.org/forums/viewtopic.php?id=38301

    • I forgot to mention earlier that /boot must be mounted, so that the squashfs, initrd.img, and vmlinuz may be updated:

sudo mount /dev/sdb1 /live/image  #Replace /dev/sdb1 with your relevant USB partition containing /boot stuff (initrd.img, vmlinuz).
  • In the above guide you must be booted with both the 'persistence' and 'toram' kernel parameters. 'toram' is necessary to be able to mount the /boot partition otherwise it will be in-use.
  1. Now, run this script to generate the new squashfs (edit TARGET_SQUASHFS_FILE to where desired, such as /tmp or an external HDD like I did): https://gist.github.com/AndrewSmart/90eb186aea08db8f1426
    • In the above guide you must be booted with the 'persistence' kernel parameter; 'toram' doesn't matter, though it would be quicker if so.
    • Two further optimizations on the above script would be to not squash /boot and /var/log; though it works fine as is.
    • In the script I chose not to squash /home.
    • I suggest you back up your original squashfs the first time you try this guide.
  2. Now copy TARGET_SQUASHFS_FILE to your first partition, over ./live/image/filesystem.squashfs I believe. Also move the filesystem.packages there if you wish to follow that convention (I believe that file is necessary if you wish to install #! to the HDD).
  3. Now most everything had been squashed into the squashfs on the first partition; but there is a problem as we have the files duplicated both there and in the persistence partition. aufs unfortunately won't clean up the duplicates from the rw branch (I wish it would). We have to do that manually. Boot without the 'persistence' kernel parameter, and run this script on your persistence partition: https://gist.github.com/AndrewSmart/2f67f79f6f1922c4556f
  4. Now boot as you normally would with the 'persistence' kernel parameter and verify you receive no new error messages on startup or shutdown. If all is well, remove /bak at your convenience. Enjoy your updated & condensed live USB stick!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment