Skip to content

Instantly share code, notes, and snippets.

@fsmithred
Last active May 23, 2016 17:24
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 fsmithred/87b68418207daf095efc6b3cd0aea05d to your computer and use it in GitHub Desktop.
Save fsmithred/87b68418207daf095efc6b3cd0aea05d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# live-snapshot-auto1.sh
set -x
# Source system needs live-boot* and live-config*
# Probably does not need refractasnapshot, isolinux, xorriso. Not sure about squashfs-tools.
#
work_dir="$(pwd)"
# mountpoint or directory containing the system to be copied
# DO NOT USE TRAILING SLASHES HERE
source_system=
# the rsync copy of the filesystem
myfs="${work_dir}/myfs"
# rsync excludes file
snapshot_excludes="live-snapshot_exclude.list"
# source of the isolinux files
# use the iso dir that comes with refractasnapshot
# or point this to your custom iso dir
iso_dir="/usr/lib/refractasnapshot/iso" #
# where to put the finished iso
snapshot_dir="$work_dir"
# name for the iso file, including .iso extension
filename="snapshot_$(date +%Y%m%d_%H%M).iso"
# kernel and initrd to use in the iso
kernel_image="$source_system"/vmlinuz
initrd_image="$source_system"/boot/initrd.img-3.16.0-4-686-pae ####### symlink doesn't work in mounted system
make_isohybrid="yes"
# Record errors in a logfile.
error_log="$(pwd)/errors_live-snapshot.log"
exec 2>"$error_log"
#copy stuff from the source system to the snapshot.
copy_isolinux () {
if [[ -f /usr/lib/ISOLINUX/isolinux.bin ]] ; then
isolinuxbin="/usr/lib/ISOLINUX/isolinux.bin"
elif [[ -f /usr/lib/syslinux/isolinux.bin ]] ; then
isolinuxbin="/usr/lib/syslinux/isolinux.bin"
else
echo "You need to install the isolinux package."
exit 1
fi
### Might need to add chain.c32 to this list of copied files:
if [[ -f /usr/lib/syslinux/modules/bios/vesamenu.c32 ]] ; then
vesamenu="/usr/lib/syslinux/modules/bios/vesamenu.c32"
rsync -a /usr/lib/syslinux/modules/bios/chain.c32 "$iso_dir"/isolinux/
rsync -a /usr/lib/syslinux/modules/bios/ldlinux.c32 "$iso_dir"/isolinux/
rsync -a /usr/lib/syslinux/modules/bios/libcom32.c32 "$iso_dir"/isolinux/
rsync -a /usr/lib/syslinux/modules/bios/libutil.c32 "$iso_dir"/isolinux/
else
vesamenu="/usr/lib/syslinux/vesamenu.c32"
fi
rsync -a "$isolinuxbin" "$iso_dir"/isolinux/
rsync -a "$vesamenu" "$iso_dir"/isolinux/
}
# Let iso/, vmlinuz and initrd.img get copied, even if work_dir was saved,
# in case they have changed, unless $nocopy = yes.
copy_kernel () {
rsync -a "$iso_dir"/ "$work_dir"/iso/
cp "$kernel_image" "$work_dir"/iso/live/
cp "$initrd_image" "$work_dir"/iso/live/initrd.img ########### rename initrd so it matches boot menu
}
# rsync exclude list needs items prepended with "$source_system"/
copy_filesystem () {
rsync -av "$source_system"/ "$myfs"/ --exclude-from="$snapshot_excludes" --delete-before --delete-excluded
}
### THE WORK STARTS HERE ###
cd "$work_dir"
copy_isolinux
copy_kernel
copy_filesystem
# Truncate logs, remove archived logs.
find myfs/var/log -name "*gz" -print0 | xargs -0r rm -f
find myfs/var/log/ -type f -exec truncate -s 0 {} \;
# add static links in /dev
# /etc/fstab should exist, even if it's empty,
# to prevent error messages at boot
touch "$work_dir"/myfs/etc/fstab
# Blank out systemd machine id. If it does not exist, systemd-journald
# will fail, but if it exists and is empty, systemd will automatically
# set up a new unique ID.
if [ -e "$work_dir"/myfs/etc/machine-id ]
then
rm -f "$work_dir"/myfs/etc/machine-id
: > "$work_dir"/myfs/etc/machine-id
fi
# add some basic files to /dev
mknod -m 622 "$work_dir"/myfs/dev/console c 5 1
mknod -m 666 "$work_dir"/myfs/dev/null c 1 3
mknod -m 666 "$work_dir"/myfs/dev/zero c 1 5
mknod -m 666 "$work_dir"/myfs/dev/ptmx c 5 2
mknod -m 666 "$work_dir"/myfs/dev/tty c 5 0
mknod -m 444 "$work_dir"/myfs/dev/random c 1 8
mknod -m 444 "$work_dir"/myfs/dev/urandom c 1 9
chown -v root:tty "$work_dir"/myfs/dev/{console,ptmx,tty}
ln -sv /proc/self/fd "$work_dir"/myfs/dev/fd
ln -sv /proc/self/fd/0 "$work_dir"/myfs/dev/stdin
ln -sv /proc/self/fd/1 "$work_dir"/myfs/dev/stdout
ln -sv /proc/self/fd/2 "$work_dir"/myfs/dev/stderr
ln -sv /proc/kcore "$work_dir"/myfs/dev/core
ln -sv /run/shm "$work_dir"/myfs/dev/shm
mkdir -v "$work_dir"/myfs/dev/pts
mksquashfs "$myfs"/ iso/live/filesystem.squashfs -comp xz -noappend
make_iso () {
# If isohdpfx.bin gets moved again, maybe use: isohdpfx=$(find /usr/lib/ -name isohdpfx.bin)
if [[ $make_isohybrid = "yes" ]]; then
if [[ -f /usr/lib/syslinux/mbr/isohdpfx.bin ]] ; then
isohybrid_opt="-isohybrid-mbr /usr/lib/syslinux/mbr/isohdpfx.bin"
elif [[ -f /usr/lib/syslinux/isohdpfx.bin ]] ; then
isohybrid_opt="-isohybrid-mbr /usr/lib/syslinux/isohdpfx.bin"
elif [[ -f /usr/lib/ISOLINUX/isohdpfx.bin ]] ; then
isohybrid_opt="-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin"
else
echo "Can't create isohybrid. File: isohdpfx.bin not found. The resulting image will be a standard iso file."
fi
fi
xorriso -as mkisofs -r -J -joliet-long -l ${isohybrid_opt} \
-partition_offset 16 -V "snapshot-live-cd" -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
-boot-load-size 4 -boot-info-table -o "$snapshot_dir"/"$filename" iso/
}
make_iso
### md5sum, sha256sum, gpg-sign "$filename"
md5sum "$snapshot_dir"/"$filename" > "$snapshot_dir"/"$filename".md5
chown 1000:1000 "$snapshot_dir"/"$filename"*
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment