Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AndrewSmart
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewSmart/90eb186aea08db8f1426 to your computer and use it in GitHub Desktop.
Save AndrewSmart/90eb186aea08db8f1426 to your computer and use it in GitHub Desktop.
Updates linux persistent squashfs
#!/usr/bin/env bash
NEW_SQUASHFS_FILE='/media/sda1/tmp/filesystem.squashfs.xz'
APT_UPDATED=false
if ! grep -qs " persistence[ |$]" /proc/cmdline
then
echo "Script intended to make squashfs from within live system, with persistence kernel parameter."
exit
fi
if [ ! -z `which apt-show-versions` ]; then
# Has a 25 MB cache, and will not recover if we delete it.
sudo apt-get -y purge apt-show-versions
fi
if [ -z `which mksquashfs` ]; then
sudo apt-get update
APT_UPDATED=true
sudo apt-get -y install squashfs-tools
fi
if [ -z `which localepurge` ]; then
# localepurge cleans files from locales you declare you won't use.
# Should clear 20-60MB (don't recall what it was originally).
if [ ! $APT_UPDATED ]; then
sudo apt-get update
fi
sudo apt-get -y install localepurge
sudo localepurge
fi
sudo apt-get autoremove
sudo apt-get clean
# Below are the excludes files; which are not compressed into the squashfs.
# /boot contents (vmlinuz/initrd) don't need to be both in the squashfs and out
# AFAIK. You can delete the contents if you like (but leave the folder).
# /dev will be regenerated each boot starting from the seed psuedo-files below.
# /proc /.pulse /run /sys /tmp will be regenerated on boot.
# /.wh* are aufs files (contains state of deleted files from ro branch).
# They are here if booted with toram kernel option; else they are in
# /live/overlay
# /live this will be present if booted without toram kernel option. Contains
# aufs runtime .wh* files.
# /lost+found I'm guessing this shouldn't go into the squashfs?
# /media /mnt nothing mounted in these should go into squashfs!
# /home I chose not to compress this via squashfs due to frequent changes.
# /initrd.img /vmlinuz are just links generated each boot.
# /live-persistence.conf will be placed there each boot by live-boot scripts.
# /selinux Empty folder for selinux, while dpkg says it isn't installed.
# /var/cache can probably be regenerated by respective utilities
# (with the exception of apt which I had accounted for using psudo-files).
# /var/lib/apt/lists not cleaned out on apt-get clean and will be re-cached
# on next apt-get update. Took 75Mb on my system.
# /var/log Can probably be excluded as well, but I encountered error messages
# on shutdown when I had done so; a tool had expected a subdirectory.
# /var/tmp guessing this shouldn't be packaged.
EXCLUDES='.wh* boot dev initrd.img live-persistence.conf live lost+found media mnt proc .pulse* run home selinux sys tmp* var/cache var/lib/apt/lists var/tmp vmlinuz'
# Below are pseudo files we must insert into the squashfs in order for the
# system to boot, and to remove error messages.
# In /dev: full, loop[0-7] port, ptmx, random, tty[1+], urandom, zero all
# appear automagically (possibly due to error checking in scripts).
# I did see an error about loop1 on startup when it was omitted below, as well
# as tty1.
# Not sure how to make xconsole, or links; these were in the original
# distrubuted squashfs.
# Other folders specifications are double checks that they are there.
cat<<-EOF > /tmp/mksquashfs.pf
boot d 755 root root
dev d 755 root root
dev/console c 600 root tty 5 1
dev/full c 666 root root 1 7
dev/kmem c 640 root kmem 1 2
dev/loop0 b 660 root disk 7 0
dev/loop1 b 660 root disk 7 1
dev/loop2 b 660 root disk 7 2
dev/loop3 b 660 root disk 7 3
dev/loop4 b 660 root disk 7 4
dev/loop5 b 660 root disk 7 5
dev/loop6 b 660 root disk 7 6
dev/loop7 b 660 root disk 7 7
dev/mem c 640 root kmem 1 1
dev/null c 666 root root 1 3
dev/port c 640 root kmem 1 4
dev/ptmx c 666 root tty 5 2
dev/pts d 755 root root
dev/ram0 b 660 root disk 1 0
dev/ram1 b 660 root disk 1 1
dev/ram2 b 660 root disk 1 2
dev/ram3 b 660 root disk 1 3
dev/ram4 b 660 root disk 1 4
dev/ram5 b 660 root disk 1 5
dev/ram6 b 660 root disk 1 6
dev/ram7 b 660 root disk 1 7
dev/ram8 b 660 root disk 1 8
dev/ram9 b 660 root disk 1 9
dev/ram10 b 660 root disk 1 10
dev/ram11 b 660 root disk 1 11
dev/ram12 b 660 root disk 1 12
dev/ram13 b 660 root disk 1 13
dev/ram14 b 660 root disk 1 14
dev/ram15 b 660 root disk 1 15
dev/ram16 b 660 root disk 1 16
dev/random c 666 root root 1 8
dev/tty b 666 root tty 5 0
dev/tty0 b 600 root tty 4 0
home d 755 root root
media d 755 root root
mnt d 755 root root
proc d 755 root root
run d 755 root root
sys d 755 root root
tmp d 755 root root
var/cache d 755 root root
var/cache/apt d 755 root root
var/cache/apt/partial d 755 root root
var/cache/apt/lock f 640 root root echo ""
var/lib/apt/lists d 755 root root
var/lib/apt/lists/partial d 755 root root
var/tmp d 777 root root
EOF
# SquashFS version 4 is included in Linux Kernel since 2.6.29 and it supports
# XZ (LZMA2) compression since 2.6.38. lzma decompresses a bit slower than gzip
# and has smaller resultant data size. If you're targeting systems with slower
# cpus and you desire quicker boot times you may want to consider using
# "-comp gzip" below.
echo Compressing into squashfs...
sudo mksquashfs / "$NEW_SQUASHFS_FILE".new -comp xz -info -always-use-fragments -noappend -wildcards -pf /tmp/mksquashfs.pf -e ${EXCLUDES} > /tmp/mksquashfs.log
# It was done before, so I'll follow the convention of generating this file.
# This file is used if you choose to install linux from the USB to the HDD.
echo Generating filesystem.packages
dpkg-query -W --showformat='${Package}\t${Version}\n' > `dirname "$NEW_SQUSAHFS_FILE"`/filesystem.packages.xz
if [ -e "$NEW_SQUASHFS_FILE".new ]; then
sudo mv "$NEW_SQUASHFS_FILE".new "$NEW_SQUASHFS_FILE"
sudo sync
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment