From fb5383a25e6bba77dc9d969ad55344da17932b80 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Tue, 3 Oct 2017 08:35:29 +0300 Subject: [PATCH] simplify raw bios build Signed-off-by: Avi Deitcher --- tools/mkimage-raw-bios/Dockerfile | 14 ++++ tools/mkimage-raw-bios/build.yml | 2 + tools/mkimage-raw-bios/make-bios | 105 ++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 tools/mkimage-raw-bios/Dockerfile create mode 100644 tools/mkimage-raw-bios/build.yml create mode 100755 tools/mkimage-raw-bios/make-bios diff --git a/tools/mkimage-raw-bios/Dockerfile b/tools/mkimage-raw-bios/Dockerfile new file mode 100644 index 000000000..f214cde86 --- /dev/null +++ b/tools/mkimage-raw-bios/Dockerfile @@ -0,0 +1,14 @@ +FROM linuxkit/alpine:87a0cd10449d72f374f950004467737dbf440630 + +RUN \ + apk update && apk upgrade && \ + apk add --no-cache \ + dosfstools \ + libarchive-tools \ + sfdisk \ + syslinux \ + && true + +COPY . . + +ENTRYPOINT [ "/make-bios" ] diff --git a/tools/mkimage-raw-bios/build.yml b/tools/mkimage-raw-bios/build.yml new file mode 100644 index 000000000..54126ac8e --- /dev/null +++ b/tools/mkimage-raw-bios/build.yml @@ -0,0 +1,2 @@ +image: mkimage-raw-bios +arches: ["amd64"] diff --git a/tools/mkimage-raw-bios/make-bios b/tools/mkimage-raw-bios/make-bios new file mode 100755 index 000000000..95dbddb12 --- /dev/null +++ b/tools/mkimage-raw-bios/make-bios @@ -0,0 +1,105 @@ +#!/bin/sh + +set -e + +# for debugging +[ -n "$DEBUG" ] && set -x + +IMGFILE=$PWD/disk.img + + +# we want everything except the final result to stderr +( exec 1>&2; + +ESP_FILE=$PWD/boot.img + + +mkdir -p /tmp/bios +cd /tmp/bios + +# input is a tarball of filesystem on stdin with kernel in /boot +# output is an iso on stdout + +# extract. BSD tar auto recognises compression, unlike GNU tar +# only if stdin is a tty, if so need files volume mounted... +[ -t 0 ] || bsdtar xzf - + +INITRD="$(find . -name '*.img')" +KERNEL="$(find . -name kernel)" +CMDLINE_FILE="$(find . -name cmdline)" +CMDLINE="$(cat $CMDLINE_FILE )" + + +cat >> syslinux.cfg < /dev/null +echo "mtools_skip_check=1" >> /etc/mtools.conf && \ +mcopy -i $ESP_FILE syslinux.cfg ::/ +mcopy -i $ESP_FILE $KERNEL ::/kernel +mcopy -i $ESP_FILE $INITRD ::/initrd.img + +# install syslinux +syslinux --install $ESP_FILE + +# now make our actual filesystem image +# how big an image do we want? +# it should be the size of our ESP file+1MB for BIOS boot + 1MB for MBR + 1MB for backup +ONEMB=$(( 1024 * 1024 )) +SIZE_IN_BYTES=$(( $(stat -c %s "$ESP_FILE") + 4*$ONEMB )) + +# and make sure the ESP is bootable for BIOS mode +# settings +BLKSIZE=512 +MB_BLOCKS=$(( $SIZE_IN_BYTES / $ONEMB )) + +# make the image +dd if=/dev/zero of=$IMGFILE bs=1M count=$MB_BLOCKS + + +echo "w" | fdisk $IMGFILE || true +# format one large partition of the apprropriate size +echo ","$ESP_FILE_SIZE_SECTORS",;" | sfdisk $IMGFILE + +# of course, "one large partition" means minus the 2048 at the beginning +ESP_SECTOR_START=2048 + +# copy in our EFI System Partition image +dd if=$ESP_FILE of=$IMGFILE bs=$BLKSIZE count=$ESP_FILE_SIZE_SECTORS conv=notrunc seek=$ESP_SECTOR_START + +# install mbr +#dd if=/usr/share/syslinux/mbr.bin of="$IMGFILE" bs=440 count=1 conv=notrunc +dd if=/usr/share/syslinux/altmbr.bin bs=439 count=1 conv=notrunc of=$IMGFILE +printf '\1' | dd bs=1 count=1 seek=439 conv=notrunc of=$IMGFILE + +sfdisk -A "$IMGFILE" 1 + +) + +cat $IMGFILE