Skip to content

Instantly share code, notes, and snippets.

@pfichtner
Created April 19, 2017 21:39
Show Gist options
  • Save pfichtner/08aba76d67116f67bf715da6dc4c3072 to your computer and use it in GitHub Desktop.
Save pfichtner/08aba76d67116f67bf715da6dc4c3072 to your computer and use it in GitHub Desktop.
#!/bin/bash
# copied from https://help.ubuntu.com/community/MakeALiveCD/DVD/BootableFlashFromHarddiskInstall
export WORK=$HOME/work
export CD=$HOME/cd
export FORMAT=squashfs
export FS_DIR=casper
sudo mkdir -p ${CD}/${FS_DIR} ${CD}/boot/grub ${WORK}/rootfs
sudo apt-get update
sudo apt-get install -y grub2 xorriso squashfs-tools qemu
sudo rsync -av --one-file-system --exclude=/proc/* --exclude=/dev/* \
--exclude=/sys/* --exclude=/tmp/* --exclude=/home/* --exclude=/lost+found \
--exclude=/var/tmp/* --exclude=/boot/grub/* --exclude=/root/* \
--exclude=/var/mail/* --exclude=/var/spool/* --exclude=/media/* \
--exclude=/etc/fstab --exclude=/etc/mtab --exclude=/etc/hosts \
--exclude=/etc/timezone --exclude=/etc/shadow* --exclude=/etc/gshadow* \
--exclude=/etc/X11/xorg.conf* --exclude=/etc/gdm/custom.conf \
--exclude=/etc/lightdm/lightdm.conf --exclude=${WORK}/rootfs / ${WORK}/rootfs
sudo cp -av /boot/* ${WORK}/rootfs/boot
# Copy settings in your home dir:
# If you want to preseve your user account settings which are stored in your home directory, you can copy them to ${WORK}/rootfs/etc/skel/. But first we have to define what files we want to copy. For example I am using xcfe4 as my DE, and it stores all it settings in a directory called .config in my home directory, so I am going to add .config to the variable $CONFIG:
CONFIG='.config .bashrc'
cd $HOME && for i in $CONFIG; do
sudo cp -rpv --parents $i ${WORK}/rootfs/etc/skel
done
sudo mount --bind /dev/ ${WORK}/rootfs/dev
sudo mount -t proc proc ${WORK}/rootfs/proc
sudo mount -t sysfs sysfs ${WORK}/rootfs/sys
sudo mount -o bind /run ${WORK}/rootfs/run
sudo chroot ${WORK}/rootfs /bin/bash <<EOF
LANG=
apt-get update
apt-get install -y casper lupin-casper
# If you want your live cd to have an installer, install the Ubuntu installer:
# kde: apt-get install -y ubiquity ubiquity-frontend-kde
apt-get install -y ubiquity ubiquity-frontend-gtk
# Install any packages you want to be in the CD. Some of the following packages are useful in emergency situations:
sudo apt-get install -y gparted testdisk wipe partimage xfsprogs reiserfsprogs jfsutils ntfs-3g dosfstools mtools
depmod -a $(uname -r)
update-initramfs -u -k $(uname -r)
# Remove non system users
for i in `cat /etc/passwd | awk -F":" '{print $1}'`; do
uid=`cat /etc/passwd | grep "^${i}:" | awk -F":" '{print $3}'`
[ "$uid" -gt "998" -a "$uid" -ne "65534" ] && userdel --force ${i} 2> /dev/null
done
apt-get clean
find /var/log -regex '.*?[0-9].*?' -exec rm -v {} \;
find /var/log -type f | while read file; do
cat /dev/null | tee $file
done
rm /etc/resolv.conf /etc/hostname
EOF
export kversion=`cd ${WORK}/rootfs/boot && ls -1 vmlinuz-* | tail -1 | sed 's@vmlinuz-@@'`
sudo cp -vp ${WORK}/rootfs/boot/vmlinuz-${kversion} ${CD}/${FS_DIR}/vmlinuz
sudo cp -vp ${WORK}/rootfs/boot/initrd.img-${kversion} ${CD}/${FS_DIR}/initrd.img
sudo cp -vp ${WORK}/rootfs/boot/memtest86+.bin ${CD}/boot
# This step is only needed if you installed the Ubuntu installer ubiquity. This step generates two files (filesystem.manifest & filesystem.manifest-desktop).
sudo chroot ${WORK}/rootfs dpkg-query -W --showformat='${Package} ${Version}\n' | sudo tee ${CD}/${FS_DIR}/filesystem.manifest
sudo cp -v ${CD}/${FS_DIR}/filesystem.manifest ${CD}/${FS_DIR}/filesystem.manifest-desktop
REMOVE='ubiquity casper user-setup os-prober libdebian-installer4'
for i in $REMOVE; do
sudo sed -i "/${i}/d" ${CD}/${FS_DIR}/filesystem.manifest-desktop
done
sudo umount ${WORK}/rootfs/proc/sys/fs/binfmt_misc
sudo umount ${WORK}/rootfs/run
sudo umount ${WORK}/rootfs/sys
sudo umount ${WORK}/rootfs/proc
sudo umount ${WORK}/rootfs/dev
sudo mksquashfs ${WORK}/rootfs ${CD}/${FS_DIR}/filesystem.${FORMAT} -noappend
echo -n $(sudo du -s --block-size=1 ${WORK}/rootfs | tail -1 | awk '{print $1}') | sudo tee ${CD}/${FS_DIR}/filesystem.size
find ${CD} -type f -print0 | xargs -0 md5sum | sed "s@${CD}@.@" | grep -v md5sum.txt | sudo tee -a ${CD}/md5sum.txt
sudo tee ${CD}/boot/grub/grub.cfg >/dev/null <<EOF
set default="0"
set timeout=10
menuentry "Ubuntu GUI" {
linux /casper/vmlinuz boot=casper quiet splash
initrd /casper/initrd.img
}
menuentry "Ubuntu in safe mode" {
linux /casper/vmlinuz boot=casper xforcevesa quiet splash
initrd /casper/initrd.img
}
menuentry "Ubuntu CLI" {
linux /casper/vmlinuz boot=casper textonly quiet splash
initrd /casper/initrd.img
}
menuentry "Ubuntu GUI persistent mode" {
linux /casper/vmlinuz boot=casper persistent quiet splash
initrd /casper/initrd.img
}
menuentry "Ubuntu GUI from RAM" {
linux /casper/vmlinuz boot=casper toram quiet splash
initrd /casper/initrd.img
}
menuentry "Check Disk for Defects" {
linux /casper/vmlinuz boot=casper integrity-check quiet splash
initrd /casper/initrd.img
}
menuentry "Memory Test" {
linux16 /boot/memtest86+.bin
}
menuentry "Boot from the first hard disk" {
set root=(hd0)
chainloader +1
}
EOF
sudo grub-mkrescue -o $HOME/live-cd.iso ${CD}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment