Create a SquashFS and a bootable ISO image
This script generates a bootable ISO image out of your currently running system. You can use this ISO to boot your system from a CD or DVD or if you want to boot it from an USB flash disk (you primarily need the file "sqsimage"). But this only works, if you boot in "Live"/Read-only mode (second Boot Manager option). Therefore, this script only makes sense if you're running a VM (Virtual Machine) which is provided by this site (topasnet.ch).
Copy this script into /root/mkusb.sh , make it executable and adapt the variables DESTDISK and DESTDISKFS according to your save location outside of the root file system tree. I normally use the second partition of an USB flash disk which is NTFS formatted - this is very personal.
#!/bin/bash
##########################################
#
# Compress the read-only root file system to a squash image file and save
# it on an external USB disk drive. Also creates a bootable ISO CD on the
# external disk.
#
# This script is very personal! Change the saving location:
# DESTDISK=...
# DESTDISKFS="-t ntfs-3g"
#
# To do: graphical installer with Zenity
#
# Note: NOP=NO Portage
#
##########################################
DESTDISK="/dev/sdb2"
DESTDISKFS="-t ntfs-3g"
BOOT="/.livesys/system/boot"
ISODIR="/mnt/work/gentoolight-iso"
ISODIRNOP="/mnt/work/gentoolight-nop"
ISOFILE="/mnt/work/gentoolight-$(date +%Y-%m-%d).iso"
ISOFILENOP="/mnt/work/gentoolight-nop-$(date +%Y-%m-%d).iso"
SQUASHFILE="${ISODIR}/sqsimage"
SQUASHFILENOP="${ISODIRNOP}/sqsimage"
KERNELREL=$(uname -r)
KERNEL=kernel-${KERNELREL}
RAMFS=initrd-${KERNELREL} # unused: .cpio.gz
ESCAPE="\033";
REDF="${ESCAPE}[31m";
GREENF="${ESCAPE}[32m";
BLUEF="${ESCAPE}[34m";
YELLOWF="${ESCAPE}[33m";
RESET="${ESCAPE}[0m";
##########################################
echo -e "${GREENF}Welcome to Squash image generation script!${RESET}"
mounted=$(mount|grep /.livesys)
if [ "${mounted}" = "" ]; then
echo -e "${REDF}Error: you have to boot the system read-only (Live mode).${RESET}"
exit 1
fi
#This check is not used, because mksquashfs doesn't include any mount points
#mounted=$(mount|grep /mnt)
#if [ "${mounted}" != "" ]; then
# echo -e "${REDF}Error: there is something mounted under /mnt:${RESET}"
# echo -e ${mounted}
# exit 1
#fi
echo -e "${YELLOWF}Don't forget to clean the kernel compile files!${RESET}"
##########################################
echo -e "Mounting USB stick ${REDF}${DESTDISK}${RESET} as '${DESTDISKFS}' to ${REDF}/mnt/work${RESET}"
echo -e "Mounting ${REDF}/dev/sda1${RESET} as ext2 to ${REDF}${BOOT}${RESET}"
echo -e "${GREENF}Press enter to continue or Ctrl-C to abort.${RESET}"
read unusedinp
# mount my USB stick to /mnt/work
mount ${DESTDISKFS} ${DESTDISK} /mnt/work
mount /dev/sda1 ${BOOT}
##########################################
# mount boot and select kernel, abort option
if [ ! -f "${BOOT}/${KERNEL}" ]; then
echo "Kernel ${BOOT}/${KERNEL} not found."
exit 1
fi
if [ ! -f "${BOOT}/${RAMFS}" ]; then
echo "Initial RAM disk file ${BOOT}/${RAMFS} not found."
exit 1
fi
echo
echo -e "Selecting ${REDF}${BOOT}/${KERNEL}${RESET} as kernel for ISO LiveCD."
echo -e "Selecting ${REDF}${BOOT}/${RAMFS}${RESET} as initial RAM disk file for ISO LiveCD."
echo -e "${GREENF}Press enter to continue or Ctrl-C to abort.${RESET}"
read unusedinp
cp ${BOOT}/${KERNEL} ${BOOT}/tgkernel
cp ${BOOT}/${RAMFS} ${BOOT}/initrd
##########################################
# Some generic excludes (relative!)
##########################################
EXCL="tmp/* tmp/.* media/* media/.* sys/* proc/* var/log/* var/tmp/*"
EXCL="${EXCL} etc/fstab etc/mtab boot/lost+found etc/udev/rules.d/70-pers* etc/hal/fdi/policy/11-x11-vmmouse.fdi"
EXCL="${EXCL} etc/X11/xorg.conf etc/X11/xorg.conf-bak"
EXCL="${EXCL} root/.xauth* root/.serverauth* root/.xsession* root/.bash_history"
EXCL="${EXCL} home/user/.xauth* home/user/.serverauth* home/user/.xsession* home/user/Downloads/*"
EXCL="${EXCL} home/user/.config/autostart/open-vm-tools.desktop home/user/.bash_history"
EXCL="${EXCL} usr/portage/distfiles/*"
EXCL="${EXCL} usr/share/hal/fdi/policy/20thirdparty/11-x11-vmmouse.fdi var/cache/hald/fdi-cache usr/bin/vmmouse_detect"
##########################################
# Normal edition with portage tree
##########################################
rm -f ${ISOFILE}
rm -f ${ISOFILE}.md5
rm -rf ${ISODIR}
echo "Building new directory for ISO files..."
mkdir ${ISODIR}
cp -a ${BOOT} ${ISODIR}
echo "Calling mksquashfs..."
# do not use exlude files, because wildcards don't work!
mksquashfs /.livesys/system ${SQUASHFILE} -wildcards -e ${EXCL}
echo "Calling mkisofs..."
mkisofs -o ${ISOFILE} -l -r -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ${ISODIR}
md5sum ${ISOFILE} > ${ISOFILE}.md5
##########################################
# Small edition without portage
##########################################
rm -f ${ISOFILENOP}
rm -f ${ISOFILENOP}.md5
rm -rf ${ISODIRNOP}
echo "NOP: No portage / Building new directory for ISO files..."
mkdir ${ISODIRNOP}
cp -a ${BOOT} ${ISODIRNOP}
echo "NOP: No portage / Calling mksquashfs..."
# do not use exclude files, because wildcards don't work!
mksquashfs /.livesys/system ${SQUASHFILENOP} -wildcards -e ${EXCL} usr/portage/* usr/portage/.git*
echo "NOP: No portage / Calling mkisofs..."
mkisofs -o ${ISOFILENOP} -l -r -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ${ISODIRNOP}
md5sum ${ISOFILENOP} > ${ISOFILENOP}.md5