From 24ffb20cd866d68449d8d20cf90e4292b356c4a7 Mon Sep 17 00:00:00 2001 From: Dave Freitag Date: Wed, 3 May 2017 13:22:29 -0500 Subject: [PATCH] Adding Dynamic VHD mkimage package Adding a mkimage package to create dynamic 25GB VHD images (static VHD images are already supported by LinuxKit). Signed-off-by: Dave Freitag --- tools/mkimage-dynamic-vhd/Dockerfile | 5 ++ tools/mkimage-dynamic-vhd/Makefile | 27 ++++++++++ tools/mkimage-dynamic-vhd/make-dynamic-vhd | 59 ++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 tools/mkimage-dynamic-vhd/Dockerfile create mode 100644 tools/mkimage-dynamic-vhd/Makefile create mode 100755 tools/mkimage-dynamic-vhd/make-dynamic-vhd diff --git a/tools/mkimage-dynamic-vhd/Dockerfile b/tools/mkimage-dynamic-vhd/Dockerfile new file mode 100644 index 000000000..ad23bc3ba --- /dev/null +++ b/tools/mkimage-dynamic-vhd/Dockerfile @@ -0,0 +1,5 @@ +FROM linuxkit/guestfs:2657580764d5791e103647237dac5a0276533e2e@sha256:8f99eba6720df17bce8893052d7ca9a07cdc9cd09b335a5a9c57ebd5a44934be + +COPY . . + +ENTRYPOINT [ "/make-dynamic-vhd" ] diff --git a/tools/mkimage-dynamic-vhd/Makefile b/tools/mkimage-dynamic-vhd/Makefile new file mode 100644 index 000000000..c9a7f25c9 --- /dev/null +++ b/tools/mkimage-dynamic-vhd/Makefile @@ -0,0 +1,27 @@ +.PHONY: tag push + +IMAGE=mkimage-dynamic-vhd + +default: push + +hash: Dockerfile make-dynamic-vhd + tar cf - $^ | docker build --no-cache -t $(IMAGE):build - + docker run --entrypoint sh --rm $(IMAGE):build -c "(cat $^; apt list --installed 2>/dev/null) | sha1sum" | sed 's/ .*//' > hash + +push: hash + docker pull linuxkit/$(IMAGE):$(shell cat hash) || \ + (docker tag $(IMAGE):build linuxkit/$(IMAGE):$(shell cat hash) && \ + docker push linuxkit/$(IMAGE):$(shell cat hash)) + docker rmi $(IMAGE):build + rm -f hash + +tag: hash + docker pull linuxkit/$(IMAGE):$(shell cat hash) || \ + docker tag $(IMAGE):build linuxkit/$(IMAGE):$(shell cat hash) + docker rmi $(IMAGE):build + rm -f hash + +clean: + rm -f hash + +.DELETE_ON_ERROR: diff --git a/tools/mkimage-dynamic-vhd/make-dynamic-vhd b/tools/mkimage-dynamic-vhd/make-dynamic-vhd new file mode 100755 index 000000000..5115cb8ff --- /dev/null +++ b/tools/mkimage-dynamic-vhd/make-dynamic-vhd @@ -0,0 +1,59 @@ +#!/bin/sh + +set -e + +mkdir -p /tmp/image +cd /tmp/image + +# input is a tarball of kernel and initrd.img on stdin +# output is a vmdk on stdout + +mkdir -p files + +cd files + +# extract. As guestfs base is currently Debian, no compression support +# only if stdin is a tty, if so need files volume mounted... +[ -t 0 ] || tar xf - + +INITRD="$(find . -name '*.img')" +KERNEL="$(find . -name kernel -or -name '*bzImage')" +CMDLINE="$*" + +[ "$KERNEL" = "./kernel" ] || mv "$KERNEL" vmlinuz64 +[ "$INITRD" = "./initrd.img" ] || mv "$INITRD" initrd.img + +# clean up subdirectories +find . -mindepth 1 -maxdepth 1 -type d | xargs rm -rf + +CFG="DEFAULT linux +LABEL linux + KERNEL /kernel + INITRD /initrd.img + APPEND ${CMDLINE} +" + +printf "$CFG" > syslinux.cfg + +cd .. + +tar cf files.tar -C files . + +# Disk is created in qcow format. +virt-make-fs --size=25G --type=ext4 --partition files.tar --format=qcow2 disk.qcow + +guestfish -a disk.qcow -m /dev/sda1 <&2 + +# Then create dynamic VHD +qemu-img convert -f vmdk -O vpc -o subformat=dynamic disk.vmdk disk.vhd 1>&2 + +cat disk.vhd