Skip to content

Instantly share code, notes, and snippets.

@lpenz
Last active June 25, 2019 14:02
Show Gist options
  • Save lpenz/e7339a0b309e29698186baee92370104 to your computer and use it in GitHub Desktop.
Save lpenz/e7339a0b309e29698186baee92370104 to your computer and use it in GitHub Desktop.
Creates a live debian on the provided (already partitioned) device
#!/bin/bash
DEV=${1?must specify a device}
function finish {
for d in boot dev/pts dev proc sys; do
if test -d "$PWD/usbroot/$d" && mount | grep -q "$PWD/usbroot/$d"; then
umount "$PWD/usbroot/$d"
fi
done
if test -d usbroot; then
if mount | grep -q "$PWD/usbroot"; then
umount "./usbroot"
fi
rmdir usbroot
fi
}
ROOT_FS_TYPE=ext4
DEBIAN_RELEASE=stretch
LINUX_VERSION=4.9.0-9-amd64
set -e -x
: "Check that $DEV has partitions ${DEV}1 (boot) and ${DEV}2 (root)"
sfdisk -V "$DEV"
test -b "${DEV}1" -a -b "${DEV}2"
: Set partition table properties
sfdisk --part-type "$DEV" 1 83
sfdisk --part-type "$DEV" 2 83
sfdisk -A "$DEV" 1
: Create filesystems
mkdosfs -n LINUXBOOT "${DEV}1"
"mkfs.${ROOT_FS_TYPE}" -F "${DEV}2"
UUID1=$(blkid "${DEV}1" | sed -n 's/.*\<UUID="\([0-9a-fA-F-]\+\)".*/\1/p')
UUID2=$(blkid "${DEV}2" | sed -n 's/.*\<UUID="\([0-9a-fA-F-]\+\)".*/\1/p')
trap finish EXIT
: Mount root
mkdir -p usbroot
mount -t "$ROOT_FS_TYPE" "${DEV}2" usbroot
: Build boot partition
VMLINUX_FILENAME="vmlinuz-${LINUX_VERSION}"
INITRD_FILENAME="initrd.img-${LINUX_VERSION}"
syslinux "${DEV}1"
cat /usr/lib/SYSLINUX/mbr.bin > "$DEV"
mkdir -p usbroot/boot
mount -t vfat "${DEV}1" ./usbroot/boot
cat <<END > usbroot/boot/syslinux.cfg
default linux
timeout 1
prompt 1
label linux
kernel $VMLINUX_FILENAME
append initrd=$INITRD_FILENAME root=UUID=$UUID2 ro
END
: Build root partition
mkdir -p usbroot/etc
cat <<END > usbroot/etc/fstab
UUID=$UUID2 / $ROOT_FS_TYPE defaults,noatime 0 0
UUID=$UUID1 /boot vfat defaults 0 0
END
export DEBIAN_FRONTEND=noninteractive
if ! [ -f "${DEBIAN_RELEASE}.tar" ]; then
debootstrap --make-tarball="${DEBIAN_RELEASE}.tar" "$DEBIAN_RELEASE" tmp http://ftp.debian.org/debian
fi
debootstrap "--unpack-tarball=$PWD/${DEBIAN_RELEASE}.tar" "$DEBIAN_RELEASE" usbroot http://ftp.debian.org/debian
mount -t devtmpfs dev ./usbroot/dev
mount -t devpts devpts ./usbroot/dev/pts
mount -t proc proc ./usbroot/proc
mount -t sysfs sysfs ./usbroot/sys
chroot usbroot apt-get install --no-install-recommends -y \
rsync \
tshark tcpdump \
smartmontools \
dosfstools \
ntfs-3g \
locales \
aptitude \
lvm2 \
cryptsetup \
busybox-static \
kbd console-setup \
linux-image-amd64 \
syslinux
echo livedeb > usbroot/etc/hostname
chroot usbroot /bin/bash -c 'echo "root:root" | chpasswd'
: root password is root
: To test: qemu-system-x86_64 -m 512 -hda "$DEV"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment