Skip to content

Instantly share code, notes, and snippets.

@maxux
Last active October 11, 2018 00:22
Show Gist options
  • Save maxux/7b23af1bfe448fa6027f3f3cd5446c8c to your computer and use it in GitHub Desktop.
Save maxux/7b23af1bfe448fa6027f3f3cd5446c8c to your computer and use it in GitHub Desktop.
Zero-OS VirtualBox Bootdisk (offline tool, doesn't download kernel on each boot)
#!/bin/bash
set -e
usage() {
echo "Zero-OS VirtualBox Offline Bootdisk Tool"
echo ""
echo " ./zos-vbox-offline.sh <image-file.vdi> [branch] [zerotier] [kernel arguments]"
echo ""
echo "All arguments except image-file are optional, see default value below"
echo ""
echo " - Image file: filename of the rawdisk you'll need to boot (eg: /tmp/zos.img)"
echo ""
echo " - Branch is the kernel branch revision (see bootstrap to get a list)"
echo " Default: development"
echo ""
echo " - Zerotier argument is zerotier network id to join on boot"
echo " Default: 0 (no zerotier network)"
echo ""
echo " - Kernel arguments needs to be set as single argument (use quotes if you"
echo " want to use multiple arguments)"
echo " Default: 'development debug'"
echo ""
exit 1
}
checkcmd() {
which $1 2>&1 > /dev/null || (echo "[-] cannot find '$1' command required"; exit 1)
}
prepare() {
echo "[+] checking system dependencies"
[[ "$UID" != "0" ]] && (echo "[-] please run as root (need to mount stuff)"; exit 1)
# syslinux root is not at same location on different distribution
syslinuxroot="/usr/lib/syslinux"
if [ ! -d ${syslinuxroot} ]; then
syslinuxroot="/usr/share/syslinux"
if [ ! -d ${syslinuxroot} ]; then
echo "[-] cannot find syslinux data path"
exit 1
fi
fi
checkcmd mkfs.ext2
checkcmd wget
checkcmd sfdisk
checkcmd losetup
bootstrap="https://bootstrap.grid.tf"
}
mkdisk() {
echo "[+] removing previous possible image disk file"
rm -rf ${imagefile}.raw ${imagefile}
echo "[+] creating and mounting disk image"
truncate -s 128M ${imagefile}.raw
# create linux (type 83) partition
echo ',,83;' | sfdisk -q ${imagefile}.raw
# mark partition 1 active
sfdisk -q -A ${imagefile}.raw 1
# mounting image as loop device
loopdev=$(losetup -f --show -P ${imagefile}.raw)
echo "[+] loop device populated: ${loopdev}"
}
mkdiskfs() {
echo "[+] creating disk partition"
mkfs.ext2 -q ${loopdev}p1
}
mounter() {
echo "[+] creating temporary working directory"
workdir=$(mktemp -d)
echo "[+] mounting disk to: ${workdir}"
mount ${loopdev}p1 ${workdir}
}
download() {
echo "[+] downloading kernel file"
url="${bootstrap}/kernel/zero-os-${branch}.efi"
wget -q --show-progress -O ${workdir}/zero-os-kernel $url || (echo "[-] cannot download: $url"; exit 1)
}
bootloader() {
echo "[+] installing bootmanager"
extlinux --install ${workdir} 2> /dev/null
cat ${syslinuxroot}/mbr.bin > ${loopdev}
echo "[+] writing configuration file"
extconf="${workdir}/extlinux.conf"
echo "DEFAULT zos" > $extconf
echo "LABEL zos" >> $extconf
echo " KERNEL /zero-os-kernel" >> $extconf
echo " APPEND $kargs" >> $extconf
}
convert() {
echo "[+] converting disk into vdi file"
sync
VBoxManage convertdd ${loopdev} ${imagefile} --format VDI 2> /dev/null
}
cleaner() {
echo "[+] unmounting stuff and cleaning"
umount ${workdir}
rm -rf ${workdir} ${imagefile}.raw
losetup -d ${loopdev}
}
main() {
# display usage on --help argument
[[ "$1" == "--help" ]] && usage
echo "[+] initializing virtualbox offline bootloader"
prepare
imagefile=$1
branch=${2:-development}
zerotier=${3:-0}
kargs=${4:-development debug}
echo "[+] "
echo "[+] image file: $imagefile"
echo "[+] zero-os branch: $branch"
echo "[+] zerotier network: $zerotier"
echo "[+] kernel arguments: $kargs"
echo "[+] syslinux path: $syslinuxroot"
echo "[+] bootstrap server: $bootstrap"
echo "[+] "
mkdisk
mkdiskfs
mounter
download
bootloader
convert
cleaner
echo "[+] "
echo "[+] zero-os virtualbox bootdisk ready: $imagefile"
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment