File onie-installer of Package openSUSE-MicroOS

#!/bin/sh
# SPDX-FileCopyrightText: 2021 Fabian Vogt <fvogt@suse.de>
# SPDX-License-Identifier: GPL-2.0-or-later

# busybox supports pipefail
set -euo pipefail

BASEURL="http://192.168.43.1/"

targetdisk=$(blkid | awk -F: '/LABEL="ONIE-BOOT"/ { print $1 }' | sed 's/[0-9]*$//')
echo "Installing MicroOS on ${targetdisk}3"

echo "Creating partition"

if [ "$(onie-sysinfo -t)" = "gpt" ] || [ "$(onie-sysinfo -t)" = "uefi" ]; then
	if [ -e "${targetdisk}3" ]; then
		# Drop the old partition
		gdisk "${targetdisk}" <<EOF >/dev/null
d
3
w
Y
q
EOF
	fi

	# Create a new partition with max size.
	gdisk "${targetdisk}" <<EOF >/dev/null
p
n
3
+0
-0
8300
p
w
Y
q
EOF
else
	# msdos not implemented yet
	exit 1
fi
partprobe

if ! [ -e "${targetdisk}3" ]; then
	echo "No partition 3?"
	exit 1
fi

echo "Writing root partition..."
wget -O - "${BASEURL}/rootpart.xz" | xz -d | dd "of=${targetdisk}3" bs=1M 2>/dev/null

echo "Writing bootloader..."

if [ "$(onie-sysinfo -l)" = "bios" ]; then
	# Write the grub core image into the bios boot partition
	wget -O /tmp/biospart "${BASEURL}/biospart"
	dd if=/tmp/biospart of=${targetdisk}1 2>/dev/null
else
	# Call shim-install inside a chroot
	mnt="$(mktemp -d)"
	mount -o ro "${targetdisk}3" "${mnt}"
	dirs="tmp dev sys proc sys/firmware/efi/efivars boot/efi"
	for i in $dirs; do
		mount --bind "/${i}" "${mnt}/${i}"
	done
	# grub2-install needs write access, but calling it is actually not needed here
	mount --bind "${mnt}/usr/bin/true" "${mnt}/usr/sbin/grub2-install"
	chroot $mnt shim-install >/dev/null

	# Busybox does not support umount -R :-(
	umount "${mnt}/usr/sbin/grub2-install"
	for i in $(printf '%s\n' $dirs | tac); do
		umount "${mnt}/${i}"
	done
	umount "${mnt}"
fi

onie-nos-mode -s

echo "Installation complete!"
openSUSE Build Service is sponsored by