ladybird/Kernel/build-image-grub.sh
Conrad Pankoff e1c982e4db Build: Remove grub from default build process
This removes grub and all the loopback device business from the default
build process. Running grub takes about a second, and it turns out it's
inconsistently packaged in different distributions, which has led to
at least one confusing issue so far (grub-install vs grub2-install).
Removing it from the basic path will make it easier for people to try
Serenity out.

There are now two scripts that can be used to build a disk image:

1. `build-image-grub.sh` - this will build an image suitable for writing
   to the IDE hard drive of a physical machine, complete with a partition
   table and bootloader. This can be run in qemu with the `qgrub` target
   for the `run` script.
2. `build-image-qemu.sh` - this is a simpler script which creates a bare
   filesystem image rather than a full MBR disk.

Both of these call out to `build-root-filesystem.sh` to do most of the
work setting up... the root filesystem.

For completeness' sake, I've retained the `sync.sh` script as a simple
forwarding to `build-image-qemu.sh`.

This relies on the functionality from #194 and #195. #195 allows us to
use `/dev/hda` as the root device when nothing else is specified, and #194
works around a strange feature of qemu that appends a space to the kernel
command line.
2019-06-04 07:15:44 -07:00

88 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
set -e
die() {
echo "die: $@"
exit 1
}
if [ $(id -u) != 0 ]; then
die "this script needs to run as root"
fi
grub=$(which grub-install 2>/dev/null) || true
if [[ -z "$grub" ]]; then
grub=$(which grub2-install 2>/dev/null) || true
fi
if [ -z "$grub" ]; then
echo "can't find a grub-install or grub2-install binary, oh no"
exit 1
fi
echo "using grub-install at ${grub}"
echo "setting up disk image..."
dd if=/dev/zero of=_disk_image bs=1M count=${DISK_SIZE:-500} status=none || die "couldn't create disk image"
chown 1000:1000 _disk_image || die "couldn't adjust permissions on disk image"
echo "done"
echo -n "creating loopback device... "
dev=$(losetup --find --partscan --show _disk_image)
if [ -z $dev ]; then
die "couldn't mount loopback device"
fi
echo "loopback device is at ${dev}"
cleanup() {
if [ -d mnt ]; then
echo -n "unmounting filesystem... "
umount mnt || ( sleep 1 && sync && umount mnt )
rm -rf mnt
echo "done"
fi
if [ -e ${dev} ]; then
echo -n "cleaning up loopback device... "
losetup -d ${dev}
echo "done"
fi
}
trap cleanup EXIT
echo -n "creating partition table... "
parted -s ${dev} mklabel msdos mkpart primary ext2 32k 100% -a minimal set 1 boot on || die "couldn't partition disk"
echo "done"
echo -n "destroying old filesystem... "
dd if=/dev/zero of=${dev}p1 bs=1M count=1 status=none || die "couldn't destroy old filesystem"
echo "done"
echo -n "creating new filesystem... "
mke2fs -q -I 128 ${dev}p1 || die "couldn't create filesystem"
echo "done"
echo -n "mounting filesystem... "
mkdir -p mnt
mount ${dev}p1 mnt/ || die "couldn't mount filesystem"
echo "done"
./build-root-filesystem.sh
echo -n "creating /boot... "
mkdir -p mnt/boot
echo "done"
echo "installing grub using $grub..."
$grub --boot-directory=mnt/boot --target=i386-pc --modules="ext2 part_msdos" ${dev}
if [ -d mnt/boot/grub2 ]; then
cp grub.cfg mnt/boot/grub2/grub.cfg
else
cp grub.cfg mnt/boot/grub/grub.cfg
fi
echo "done"
echo -n "installing kernel in /boot... "
cp kernel mnt/boot
echo "done"