BuildSystem: Cleanup image creation

This commit is contained in:
Bananymous 2024-04-24 01:27:59 +03:00
parent e7e1dd91c7
commit d255141bd4
3 changed files with 149 additions and 139 deletions

View File

@ -1,130 +1,66 @@
#!/bin/bash
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
if [ -z $BANAN_DISK_IMAGE_PATH ]; then
echo "You must set the BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_SYSROOT ]]; then
if [ -z $BANAN_SYSROOT ]; then
echo "You must set the BANAN_SYSROOT environment variable" >&2
exit 1
fi
if [[ -z $BANAN_TOOLCHAIN_PREFIX ]]; then
echo "You must set the BANAN_TOOLCHAIN_PREFIX environment variable" >&2
exit 1
fi
if [[ -z $BANAN_BOOTLOADER ]]; then
echo "You must set the BANAN_BOOTLOADER environment variable" >&2
exit 1
fi
if [[ -z $BANAN_ARCH ]]; then
echo "You must set the BANAN_ARCH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_UEFI_BOOT ]]; then
if [ -z $BANAN_UEFI_BOOT ]; then
echo "You must set the BANAN_UEFI_BOOT environment variable" >&2
exit 1
fi
if [[ -z $BANAN_BUILD_DIR ]]; then
if [ -z $BANAN_BUILD_DIR ]; then
echo "You must set the BANAN_BUILD_DIR environment variable" >&2
exit 1
fi
DISK_SIZE=$[500 * 1024 * 1024]
MOUNT_DIR="${MOUNT_DIR:-$BANAN_BUILD_DIR/bananmnt}"
set -u
# create mount directory
MOUNT_DIR="$BANAN_BUILD_DIR/mount"
mkdir -p $MOUNT_DIR
# create empty disk image
DISK_SIZE=$((500 * 1024 * 1024))
truncate -s 0 "$BANAN_DISK_IMAGE_PATH"
truncate -s $DISK_SIZE "$BANAN_DISK_IMAGE_PATH"
# create partition table
if (($BANAN_UEFI_BOOT)); then
sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk "$BANAN_DISK_IMAGE_PATH" > /dev/null
g # gpt
n # new partition
1 # partition number 1
# default (from the beginning of the disk)
+16M # efi system size
n # new partition
2 # partition number 2
# default (right after efi system partition)
# default (to the end of disk)
t # set type
1 # ... of partition 1
1 # efi system
t # set type
2 # ... of partition 2
20 # Linux filesystem
w # write changes
EOF
parted --script "$BANAN_DISK_IMAGE_PATH" \
mklabel gpt \
mkpart boot 1M 17M \
set 1 esp on \
mkpart root ext2 17M 100%
else
sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk "$BANAN_DISK_IMAGE_PATH" > /dev/null
g # gpt
n # new partition
1 # partition number 1
# default (from the beginning of the disk)
+1M # bios boot partition size
n # new partition
2 # partition number 2
# default (right after bios partition)
# default (to the end of disk)
t # set type
1 # ... of partition 1
4 # bios boot partition
t # set type
2 # ... of partition 2
20 # Linux filesystem
w # write changes
EOF
parted --script "$BANAN_DISK_IMAGE_PATH" \
mklabel gpt \
mkpart boot 1M 2M \
set 1 bios_grub on \
mkpart root ext2 2M 100%
fi
LOOP_DEV=$(sudo losetup -f --show "$BANAN_DISK_IMAGE_PATH")
sudo partprobe $LOOP_DEV
# create loop device
LOOP_DEV=$(sudo losetup --show -fP "$BANAN_DISK_IMAGE_PATH" || exit 1 )
PARTITION1=${LOOP_DEV}p1
PARTITION2=${LOOP_DEV}p2
sudo mkfs.ext2 -q $PARTITION2
sudo mkdir -p $MOUNT_DIR || { echo "Failed to create banan mount dir."; exit 1; }
if [[ "$BANAN_BOOTLOADER" == "GRUB" ]]; then
if (($BANAN_UEFI_BOOT)); then
sudo mkfs.fat $PARTITION1 > /dev/null
sudo mount $PARTITION1 "$MOUNT_DIR"
sudo mkdir -p "$MOUNT_DIR/EFI/BOOT"
sudo "$BANAN_TOOLCHAIN_PREFIX/bin/grub-mkstandalone" -O "$BANAN_ARCH-efi" -o "$MOUNT_DIR/EFI/BOOT/BOOTX64.EFI" "boot/grub/grub.cfg=$BANAN_TOOLCHAIN_DIR/grub-memdisk.cfg"
sudo umount "$MOUNT_DIR"
sudo mount $PARTITION2 "$MOUNT_DIR"
sudo mkdir -p "$MOUNT_DIR/boot/grub"
sudo cp "$BANAN_TOOLCHAIN_DIR/grub-uefi.cfg" "$MOUNT_DIR/boot/grub/grub.cfg"
sudo umount "$MOUNT_DIR"
else
sudo mount $PARTITION2 "$MOUNT_DIR"
sudo grub-install --no-floppy --target=i386-pc --modules="normal ext2 multiboot" --boot-directory="$MOUNT_DIR/boot" $LOOP_DEV
sudo mkdir -p "$MOUNT_DIR/boot/grub"
sudo cp "$BANAN_TOOLCHAIN_DIR/grub-legacy-boot.cfg" "$MOUNT_DIR/boot/grub/grub.cfg"
sudo umount "$MOUNT_DIR"
fi
fi
sudo losetup -d $LOOP_DEV || { echo "Failed to remove loop device for banan mount."; exit 1; }
sudo rm -rf $MOUNT_DIR || { echo "Failed to remove banan mount dir."; exit 1; }
if [[ "$BANAN_BOOTLOADER" == "GRUB" ]]; then
echo > /dev/null
elif [[ "$BANAN_BOOTLOADER" == "BANAN" ]]; then
if (($BANAN_UEFI_BOOT)); then
echo "banan bootloader does not support UEFI" >&2
exit 1
fi
$BANAN_SCRIPT_DIR/install-bootloader.sh
else
echo "unrecognized bootloader $BANAN_BOOTLOADER" >&2
if [ ! -b $PARTITION1 ] || [ ! -b $PARTITION2 ]; then
echo "Failed to probe partitions for banan disk image." >&2
sudo losetup -d $LOOP_DEV
exit 1
fi
# create root filesystem
sudo mkfs.ext2 -q $PARTITION2
# delete loop device
sudo losetup -d $LOOP_DEV
# install bootloader
$BANAN_SCRIPT_DIR/install-bootloader.sh

View File

@ -1,31 +1,36 @@
#!/bin/bash
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
if [ -z $BANAN_DISK_IMAGE_PATH ]; then
echo "You must set the BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_SYSROOT_TAR ]]; then
if [ -z $BANAN_SYSROOT_TAR ]; then
echo "You must set the BANAN_SYSROOT_TAR environment variable" >&2
exit 1
fi
if [[ -z $BANAN_BUILD_DIR ]]; then
if [ -z $BANAN_BUILD_DIR ]; then
echo "You must set the BANAN_BUILD_DIR environment variable" >&2
exit 1
fi
if [[ "$1" == "full" ]] || [[ ! -f $BANAN_DISK_IMAGE_PATH ]]; then
if [ "$1" == "full" ] || [ ! -f $BANAN_DISK_IMAGE_PATH ]; then
$BANAN_SCRIPT_DIR/image-create.sh
fi
LOOP_DEV="$(sudo losetup --show -f $BANAN_DISK_IMAGE_PATH)"
sudo partprobe $LOOP_DEV
set -u
MOUNT_DIR="$BANAN_BUILD_DIR/mount"
mkdir -p $MOUNT_DIR
LOOP_DEV="$(sudo losetup --show -Pf $BANAN_DISK_IMAGE_PATH || exit 1)"
ROOT_PARTITION="${LOOP_DEV}p2"
MOUNT_DIR="${MOUNT_DIR:-$BANAN_BUILD_DIR/bananmnt}"
sudo mkdir -p $MOUNT_DIR || { echo "Failed to create mount point dir."; exit 1; }
if [ ! -b $ROOT_PARTITION ]; then
echo "Failed to probe partitions for banan disk image." >&2
sudo losetup -d $LOOP_DEV
exit 1
fi
sudo mount $ROOT_PARTITION $MOUNT_DIR
@ -33,8 +38,6 @@ cd $MOUNT_DIR
sudo tar xf $BANAN_SYSROOT_TAR
cd
sudo umount $MOUNT_DIR || { echo "Failed to unmount banan mount."; exit 1; }
sudo umount $MOUNT_DIR
sudo losetup -d $LOOP_DEV || { echo "Failed to remove loop device for banan mount."; exit 1; }
sudo rm -rf "$MOUNT_DIR"
sudo losetup -d $LOOP_DEV

View File

@ -1,52 +1,123 @@
#!/bin/bash
set -e
if [[ -z $BANAN_ARCH ]]; then
if [ -z $BANAN_ARCH ]; then
echo "You must set the BANAN_ARCH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
if [ -z $BANAN_DISK_IMAGE_PATH ]; then
echo "You must set the BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_BUILD_DIR ]]; then
if [ -z $BANAN_BUILD_DIR ]; then
echo "You must set the BANAN_BUILD_DIR environment variable" >&2
exit 1
fi
if [[ -z $BANAN_ROOT_DIR ]]; then
if [[ -z $BANAN_UEFI_BOOT ]]; then
echo "You must set the BANAN_UEFI_BOOT environment variable" >&2
exit 1
fi
if [ -z $BANAN_ROOT_DIR ]; then
echo "You must set the BANAN_ROOT_DIR environment variable" >&2
exit 1
fi
if [[ -z $CMAKE_COMMAND ]]; then
if [ -z $CMAKE_COMMAND ]; then
echo "You must set the CMAKE_COMMAND environment variable" >&2
exit 1
fi
ROOT_PARTITION_INDEX=2
ROOT_PARTITION_INFO=$(fdisk -x $BANAN_DISK_IMAGE_PATH | grep "^$BANAN_DISK_IMAGE_PATH" | head -$ROOT_PARTITION_INDEX | tail -1)
ROOT_PARTITION_GUID=$(echo $ROOT_PARTITION_INFO | cut -d' ' -f6)
set -u
INSTALLER_BUILD_DIR=$BANAN_ROOT_DIR/bootloader/installer/build/$BANAN_ARCH
BOOTLOADER_ELF=$BANAN_BUILD_DIR/bootloader/bios/bootloader
MOUNT_DIR="$BANAN_BUILD_DIR/mount"
mkdir -p $MOUNT_DIR
if ! [ -f $BOOTLOADER_ELF ]; then
echo "You must build the bootloader first" >&2
install_grub_legacy() {
sudo mount $PARTITION2 "$MOUNT_DIR"
sudo grub-install \
--no-floppy \
--target=i386-pc \
--modules="normal ext2 multiboot" \
--boot-directory="$MOUNT_DIR/boot" \
$LOOP_DEV
sudo mkdir -p "$MOUNT_DIR/boot/grub"
sudo cp "$BANAN_TOOLCHAIN_DIR/grub-legacy-boot.cfg" "$MOUNT_DIR/boot/grub/grub.cfg"
sudo umount "$MOUNT_DIR"
}
install_grub_uefi() {
sudo mkfs.fat $PARTITION1 > /dev/null
sudo mount $PARTITION1 "$MOUNT_DIR"
sudo mkdir -p "$MOUNT_DIR/EFI/BOOT"
sudo "$BANAN_TOOLCHAIN_PREFIX/bin/grub-mkstandalone" -O "$BANAN_ARCH-efi" -o "$MOUNT_DIR/EFI/BOOT/BOOTX64.EFI" "boot/grub/grub.cfg=$BANAN_TOOLCHAIN_DIR/grub-memdisk.cfg"
sudo umount "$MOUNT_DIR"
sudo mount $PARTITION2 "$MOUNT_DIR"
sudo mkdir -p "$MOUNT_DIR/boot/grub"
sudo cp "$BANAN_TOOLCHAIN_DIR/grub-uefi.cfg" "$MOUNT_DIR/boot/grub/grub.cfg"
sudo umount "$MOUNT_DIR"
}
install_banan_legacy() {
ROOT_PARTITION_INDEX=2
ROOT_PARTITION_INFO=$(fdisk -x $BANAN_DISK_IMAGE_PATH | grep "^$BANAN_DISK_IMAGE_PATH" | head -$ROOT_PARTITION_INDEX | tail -1)
ROOT_PARTITION_GUID=$(echo $ROOT_PARTITION_INFO | cut -d' ' -f6)
INSTALLER_BUILD_DIR=$BANAN_ROOT_DIR/bootloader/installer/build/$BANAN_ARCH
BOOTLOADER_ELF=$BANAN_BUILD_DIR/bootloader/bios/bootloader
if [ ! -f $BOOTLOADER_ELF ]; then
echo "You must build the bootloader first" >&2
exit 1
fi
if [ ! -d $INSTALLER_BUILD_DIR ]; then
mkdir -p $INSTALLER_BUILD_DIR
cd $INSTALLER_BUILD_DIR
$CMAKE_COMMAND -G Ninja ../..
fi
cd $INSTALLER_BUILD_DIR
ninja
echo installing bootloader
$INSTALLER_BUILD_DIR/banan_os-bootloader-installer $BOOTLOADER_ELF $BANAN_DISK_IMAGE_PATH $ROOT_PARTITION_GUID
}
install_banan_uefi() {
echo "UEFI boot is not supported by the BANAN bootloader" >&2
exit 1
}
if [ $BANAN_BOOTLOADER = "GRUB" ]; then
LOOP_DEV=$(sudo losetup --show -fP $BANAN_DISK_IMAGE_PATH || exit 1)
PARTITION1=${LOOP_DEV}p1
PARTITION2=${LOOP_DEV}p2
if [ ! -b $PARTITION1 ] || [ ! -b $PARTITION2 ]; then
echo "Failed to probe partitions for banan disk image." >&2
sudo losetup -d $LOOP_DEV
exit 1
fi
if (($BANAN_UEFI_BOOT)); then
install_grub_uefi
else
install_grub_legacy
fi
sudo losetup -d $LOOP_DEV
elif [ $BANAN_BOOTLOADER = "BANAN" ]; then
if (($BANAN_UEFI_BOOT)); then
install_banan_uefi
else
install_banan_legacy
fi
else
echo "Unknown bootloader $BANAN_BOOTLOADER" >&2
exit 1
fi
if ! [ -d $INSTALLER_BUILD_DIR ]; then
mkdir -p $INSTALLER_BUILD_DIR
cd $INSTALLER_BUILD_DIR
$CMAKE_COMMAND ../..
fi
cd $INSTALLER_BUILD_DIR
make
echo installing bootloader
$INSTALLER_BUILD_DIR/banan_os-bootloader-installer $BOOTLOADER_ELF $BANAN_DISK_IMAGE_PATH $ROOT_PARTITION_GUID