#!/bin/bash set -e PROG=$0 PROGS="dd curl mkfs.ext4 mkfs.vfat fatlabel parted partprobe grub-install" DISTRO=/run/haos/iso if [ "$HAOS_DEBUG" = true ]; then set -x fi get_url() { FROM=$1 TO=$2 case $FROM in ftp* | http* | tftp*) n=0 attempts=5 until [ "$n" -ge "$attempts" ]; do curl -o $TO -fL ${FROM} && break n=$((n + 1)) echo "Failed to download, retry attempt ${n} out of ${attempts}" sleep 2 done ;; *) cp -f $FROM $TO ;; esac } cleanup2() { if [ -n "${TARGET}" ]; then umount ${TARGET}/boot/efi || true umount ${TARGET} || true fi losetup -d ${ISO_DEVICE} || losetup -d ${ISO_DEVICE%?} || true umount $DISTRO || true } cleanup() { EXIT=$? cleanup2 2>/dev/null || true return $EXIT } usage() { echo "Usage: $PROG [--force-efi] [--debug] [--tty TTY] [--poweroff] [--takeover] [--no-format] [--config https://.../config.yaml] DEVICE ISO_URL" echo "" echo "Example: $PROG /dev/vda https://github.com/1898andCo/HAOS/releases/download/v0.8.0/haos.iso" echo "" echo "DEVICE must be the disk that will be partitioned (/dev/vda). If you are using --no-format it should be the device of the HAOS_STATE partition (/dev/vda2)" echo "" echo "The parameters names refer to the same names used in the cmdline, refer to README.md for" echo "more info." echo "" exit 1 } do_format() { if [ "$HAOS_INSTALL_NO_FORMAT" = "true" ]; then STATE=$(blkid -L HAOS_STATE || true) if [ -z "$STATE" ] && [ -n "$DEVICE" ]; then tune2fs -L HAOS_STATE $DEVICE STATE=$(blkid -L HAOS_STATE) fi return 0 fi dd if=/dev/zero of=${DEVICE} bs=1M count=1 parted -s ${DEVICE} mklabel ${PARTTABLE} if [ "$PARTTABLE" = "gpt" ]; then BOOT_NUM=1 STATE_NUM=2 parted -s ${DEVICE} mkpart primary fat32 0% 50MB parted -s ${DEVICE} mkpart primary ext4 50MB 750MB else BOOT_NUM= STATE_NUM=1 parted -s ${DEVICE} mkpart primary ext4 0% 700MB fi parted -s ${DEVICE} set 1 ${BOOTFLAG} on partprobe ${DEVICE} 2>/dev/null || true sleep 2 PREFIX=${DEVICE} if [ ! -e ${PREFIX}${STATE_NUM} ]; then PREFIX=${DEVICE}p fi if [ ! -e ${PREFIX}${STATE_NUM} ]; then echo Failed to find ${PREFIX}${STATE_NUM} or ${DEVICE}${STATE_NUM} to format exit 1 fi if [ -n "${BOOT_NUM}" ]; then BOOT=${PREFIX}${BOOT_NUM} fi STATE=${PREFIX}${STATE_NUM} mkfs.ext4 -F -L HAOS_STATE ${STATE} if [ -n "${BOOT}" ]; then mkfs.vfat -F 32 ${BOOT} fatlabel ${BOOT} HAOS_GRUB fi } do_mount() { TARGET=/run/haos/target mkdir -p ${TARGET} mount ${STATE} ${TARGET} mkdir -p ${TARGET}/boot if [ -n "${BOOT}" ]; then mkdir -p ${TARGET}/boot/efi mount ${BOOT} ${TARGET}/boot/efi fi mkdir -p ${DISTRO} mount -o ro ${ISO_DEVICE} ${DISTRO} || mount -o ro ${ISO_DEVICE%?} ${DISTRO} } do_copy() { tar cf - -C ${DISTRO} haos | tar xvf - -C ${TARGET} if [ -n "$STATE_NUM" ]; then echo $DEVICE $STATE_NUM >$TARGET/haos/system/growpart fi if [ -n "$HAOS_INSTALL_CONFIG_URL" ]; then get_url "$HAOS_INSTALL_CONFIG_URL" ${TARGET}/haos/system/config.yaml chmod 600 ${TARGET}/haos/system/config.yaml fi if [ "$HAOS_INSTALL_TAKE_OVER" = "true" ]; then touch ${TARGET}/haos/system/takeover if [ "$HAOS_INSTALL_POWER_OFF" = true ] || grep -q 'haos.install.power_off=true' /proc/cmdline; then touch ${TARGET}/haos/system/poweroff fi fi } install_grub() { if [ "$HAOS_INSTALL_DEBUG" ]; then GRUB_DEBUG="haos.debug" fi mkdir -p ${TARGET}/boot/grub cat >${TARGET}/boot/grub/grub.cfg <