From f67e14c0130f7025813fa230534a89d678c9c660 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 23 Aug 2018 06:27:51 +0000 Subject: [PATCH] KVM --disksize option to grow target disk image This is needed for users with larger caches. --- bin/gbuild | 31 +++++++++----- libexec/grow-target-vm | 78 +++++++++++++++++++++++++++++++++++ target-bin/complete-resize.sh | 11 +++++ 3 files changed, 110 insertions(+), 10 deletions(-) create mode 100755 libexec/grow-target-vm create mode 100644 target-bin/complete-resize.sh diff --git a/bin/gbuild b/bin/gbuild index 24e4e2a..128ee0a 100755 --- a/bin/gbuild +++ b/bin/gbuild @@ -55,6 +55,11 @@ def build_one_configuration(suite, arch, build_desc) unless @options[:skip_image] info "Making a new image copy" system! "make-clean-vm --suite #{suite} --arch #{arch}" + + if @options[:disksize] + info "Growing target disk image" + system! "grow-target-vm --suite #{suite} --arch #{arch} --size #{@options[:disksize]}G" + end end info "Starting target" @@ -76,7 +81,20 @@ def build_one_configuration(suite, arch, build_desc) %#{ENV['DISTRO'] || 'ubuntu'} ALL=(ALL) NOPASSWD: ALL EOF" if build_desc["sudo"] and @options[:allow_sudo] + if build_desc["multiarch"] + info "Adding multiarch support (log in var/install.log)" + for a in build_desc["multiarch"] + system! "on-target -u root dpkg --add-architecture #{a} > var/install.log 2>&1" + end + end + + info "Updating apt-get repository (log in var/install.log)" + system! "on-target -u root apt-get update > var/install.log 2>&1" + info "Preparing build environment" + if @options[:disksize] + system! "on-target -u root bash < target-bin/complete-resize.sh > var/install.log 2>&1" + end system! "on-target setarch #{@arches[arch]} bash < target-bin/init-build.sh" build_desc["files"].each do |filename| @@ -94,16 +112,6 @@ EOF" if build_desc["sudo"] and @options[:allow_sudo] end end - if build_desc["multiarch"] - info "Adding multiarch support (log in var/install.log)" - for a in build_desc["multiarch"] - system! "on-target -u root dpkg --add-architecture #{a} > var/install.log 2>&1" - end - end - - info "Updating apt-get repository (log in var/install.log)" - system! "on-target -u root apt-get update > var/install.log 2>&1" - info "Installing additional packages (log in var/install.log)" system! "on-target -u root -e DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install #{build_desc["packages"].join(" ")} > var/install.log 2>&1" @@ -186,6 +194,9 @@ OptionParser.new do |opts| opts.on("-m MEM", "--memory MEM", "memory to allocate in MiB") do |v| @options[:memory] = v end + opts.on("--disksize SIZE", "grow disk image to size in GiB") do |v| + @options[:disksize] = v + end opts.on("-c PAIRS", "--commit PAIRS", "comma separated list of DIRECTORY=COMMIT pairs") do |v| @options[:commit] = v end diff --git a/libexec/grow-target-vm b/libexec/grow-target-vm new file mode 100755 index 0000000..d0e693c --- /dev/null +++ b/libexec/grow-target-vm @@ -0,0 +1,78 @@ +#!/bin/sh +set -e + +SUITE=lucid +ARCH=amd64 +SIZE=10G + +VMSW=KVM +if [ -n "$USE_LXC" ]; then + VMSW=LXC +elif [ -n "$USE_VBOX" ]; then + VMSW=VBOX +fi + +usage() { + echo "Usage: ${0##*/} [OPTION]..." + echo "Resize target copy of the base client's disk image." + echo + cat << EOF + --help display this help and exit + --suite U build suite U instead of lucid + --arch A build architecture A (e.g. i386) instead of amd64 + --size N new disk image size, in +EOF +} + +if [ $# != 0 ] ; then + while true ; do + case "$1" in + --help|-h) + usage + exit 0 + ;; + --suite|-s) + SUITE="$2" + shift 2 + ;; + --arch|-a) + ARCH="$2" + shift 2 + ;; + --size) + SIZE="$2" + shift 2 + ;; + --*) + echo "unrecognized option $1" + exit 1 + ;; + *) + break + ;; + esac + done +fi + +export LXC_SUITE=$SUITE +export LXC_ARCH=$ARCH + +OUT=target-$SUITE-$ARCH + +case $VMSW in + KVM) + out="$(qemu-img resize -f qcow2 "$OUT.qcow2" "$SIZE" || echo FAILED)" + if grep -q "shrink" <<<"$out"; then + echo "Disk image already large enough." + exit 0 + elif grep -q "FAILED" <<<"$out"; then + exit 1 + else + echo "$out" + fi + ;; + *) + echo "Growing disk images not supported with $VMSW" >&2 + exit 1 + ;; +esac diff --git a/target-bin/complete-resize.sh b/target-bin/complete-resize.sh new file mode 100644 index 0000000..ac74eda --- /dev/null +++ b/target-bin/complete-resize.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install expect-dev parted tcl +root_partno=$(perl -nle 'm[^/dev/vda(\d+)\s+/\s]&&print $1' /proc/mounts) +# TODO: Delete all swap and use space +{ cat <