Modifying Linux Filesystem And Live Cd Via Chroot

Mar 10, 2016

ap_install debootstrap gparted squashfs-tools genisoimage
# consider using `schroot`

Download Latest Release

Obtain file system and prepare workspace

mkdir ~/livecdtmp
mv kali-linux-light-2016.1-amd64.iso ~/livecdtmp
cd ~/livecdtmp

Mount the Desktop .iso and Extract Contents

mkdir mnt
sudo mount -o loop kali-linux-light-2016.1-amd64.iso mnt

mkdir extract-cd
sudo rsync --exclude=/live/filesystem.squashfs -a mnt/ extract-cd

Either A):

sudo unsquashfs mnt/live/filesystem.squashfs
sudo mv squashfs-root edit
sudo cp /etc/resolv.conf edit/etc/

Or B):

mkdir squashfs
mkdir edit
sudo modprobe squashfs
sudo mount -t squashfs -o loop mnt/(live|casper)/filesystem.squashfs squashfs/
sudo cp -a squashfs/* edit
sudo cp /etc/resolv.conf /etc/hosts edit/etc/

Chroot into File System

sudo mount --bind /dev/ edit/dev
sudo chroot edit

mount -t proc none /proc/
mount -t sysfs none /sys/
mount -t devpts none /dev/pts
export HOME=/root
export LC_ALL=C
dbus-uuidgen > /var/lib/dbus/machine-id
dpkg-divert --local --rename --add /sbin/initctl   //not sure what this does...
ln -s /bin/true /sbin/initctl

Customize Image ..

SSH and Environment

apt-get update
ssh-keygen -t rsa -b 4096
systemctl enable sshd
apt-get install git
cd root/
git clone
git clone --origin github ./.scripts
git clone
source .bashrc
cd /etc/ssh/
ln -s /root/SERVER0/local_config/sshd_config

Remove Packages

dpkg-query -W --showformat='${Package}\n' | less  # list all packages
apt-get remove --purge libreoffice-* 
apt-get remove --purge `dpkg-query -W --showformat='${Package}\n' | grep language-pack | egrep -v '\-en'`
apt-get remove --purge gnome-games*

Update Sources and Packages

for more apt-get sources, see this sources generator

Prepare to Leave Chroot

rm -rf /tmp/* ~/.bash_history
rm /var/lib/dbus/machine-id
rm /etc/resolv.conf
rm /sbin/initctl
dpkg-divert --rename --remove /sbin/initctl
#### Clean older/non-used kernels...
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
umount /proc || umount -lf /proc
umount /sys || umount -lf /sys
umount /dev/pts  || umount -lf /dev/pts
sudo umount edit/dev || umount -lf edit/dev

Create new filesystem

sudo rm extract-cd/live/filesystem.squashfs
sudo mksquashfs edit extract-cd/live/filesystem.squashfs

Update the filesystem.size file, which is needed by the installer:

sudo su
printf $(du -sx --block-size=1 edit | cut -f1) > extract-cd/live/filesystem.size

Set an Image Name

export IMAGE_NAME="kali-light-2016.1-amd64_SSH.iso"
sudo echo $IMAGE_NAME > extract-cd/README.diskdefines

Remove old md5sum.txt and calculate new md5 sums

cd extract-cd
sudo rm md5sum.txt
sudo -s 
(find -type f -print0 | xargs -0 md5sum | \
grep -v isolinux/ | tee md5sum.txt)

Create the ISO image

sudo mkisofs -D -r -V "$IMAGE_NAME" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/ -no-emul-boot -boot-load-size 4 -boot-info-table -o ../kali-linux-light-2016.1-amd64.iso .

Create USB Drive

sudo mount -t vfat /dev/sdb1 /mnt -o uid=1000,gid=1000,umask=022

rsync -auv usb-pack_efi/ /mnt

Expected Directory Structure

Expected Directory Structure

Install Grub2 on USB

sudo grub-install --removable --boot-directory=/mnt/boot --efi-directory=/mnt/EFI/BOOT /dev/sdb

Copy File System

mkdir -p /mnt/{iso,usb}
mount -o loop kali-light-2016.1-amd64_SSH.iso /mnt/iso
mount /dev/sdb2 /mnt/usb
cp -a /mnt/iso/* /mnt/usb
umount /mnt/iso
comments powered by Disqus