From 382b86bb8d0be375f2e877938af25ec1f39a89d2 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Sun, 31 Jan 2021 07:01:41 +0100 Subject: [PATCH 1/3] Make disk size configurable per image --- build-inside-vm.sh | 24 +++++++++++++++--------- images/basic.sh | 1 + images/cloud-image.sh | 1 + images/vagrant-libvirt.sh | 1 + images/vagrant-virtualbox.sh | 1 + 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/build-inside-vm.sh b/build-inside-vm.sh index 27c24d0..3416e7f 100755 --- a/build-inside-vm.sh +++ b/build-inside-vm.sh @@ -5,7 +5,7 @@ # errexit: "Exit immediately if [...] command exits with a non-zero status." set -o nounset -o errexit shopt -s extglob -readonly DISK_SIZE="20G" +readonly DEFAULT_DISK_SIZE="20G" readonly IMAGE="image.img" # shellcheck disable=SC2016 readonly MIRROR='https://mirror.pkgbuild.com/$repo/os/$arch' @@ -43,7 +43,7 @@ trap cleanup EXIT # Create the disk, partitions it, format the partition and mount the filesystem function setup_disk() { - truncate -s "${DISK_SIZE}" "${IMAGE}" + truncate -s "${DEFAULT_DISK_SIZE}" "${IMAGE}" sgdisk --clear \ --new 1::+1M --typecode=1:ef02 \ --new 2::-0 --typecode=2:8300 \ @@ -126,12 +126,6 @@ function unmount_image() { LOOPDEV="" } -# Copy image and mount the copied image -function copy_and_mount_image() { - cp -a "${IMAGE}" "${1}" - mount_image "${1}" -} - # Compute SHA256, adjust owner to $SUDO_UID:$SUDO_UID and move to output/ function mv_to_output() { sha256sum "${1}" >"${1}.SHA256" @@ -148,7 +142,19 @@ function mv_to_output() { function create_image() { local tmp_image tmp_image="$(basename "$(mktemp -u)")" - copy_and_mount_image "${tmp_image}" + cp -a "${IMAGE}" "${tmp_image}" + if [ -n "${DISK_SIZE}" ]; then + truncate -s "${DISK_SIZE}" "${tmp_image}" + sgdisk --delete 2 "${tmp_image}" + sgdisk --move-second-header \ + --new 2::-0 --typecode=2:8300 \ + "${tmp_image}" + fi + mount_image "${tmp_image}" + if [ -n "${DISK_SIZE}" ]; then + btrfs filesystem resize max "${MOUNT}" + fi + if [ 0 -lt "${#PACKAGES[@]}" ]; then arch-chroot "${MOUNT}" /usr/bin/pacman -S --noconfirm "${PACKAGES[@]}" fi diff --git a/images/basic.sh b/images/basic.sh index df984a9..aa47626 100644 --- a/images/basic.sh +++ b/images/basic.sh @@ -1,6 +1,7 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-basic-${build_version}.qcow2" +DISK_SIZE="" PACKAGES=() SERVICES=() diff --git a/images/cloud-image.sh b/images/cloud-image.sh index c791467..53eadcb 100644 --- a/images/cloud-image.sh +++ b/images/cloud-image.sh @@ -1,6 +1,7 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-cloudimg-${build_version}.qcow2" +DISK_SIZE="" # The growpart module[1] requires the growpart program, provided by the # cloud-guest-utils package # [1] https://cloudinit.readthedocs.io/en/latest/topics/modules.html#growpart diff --git a/images/vagrant-libvirt.sh b/images/vagrant-libvirt.sh index be2b56d..7ffac40 100644 --- a/images/vagrant-libvirt.sh +++ b/images/vagrant-libvirt.sh @@ -1,6 +1,7 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-libvirt-${build_version}.box" +DISK_SIZE="" PACKAGES=() SERVICES=() diff --git a/images/vagrant-virtualbox.sh b/images/vagrant-virtualbox.sh index e374be0..9a670f7 100644 --- a/images/vagrant-virtualbox.sh +++ b/images/vagrant-virtualbox.sh @@ -1,6 +1,7 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-virtualbox-${build_version}.box" +DISK_SIZE="" PACKAGES=(virtualbox-guest-utils-nox) SERVICES=(vboxservice) -- GitLab From 4d829cc9ccee7e0b1e16b5b2049441274aad3ebd Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Sun, 31 Jan 2021 07:04:28 +0100 Subject: [PATCH 2/3] Shrink the cloud image to the bare minimum (2GB) cloud-init resizes the disk[1] so we don't need a "big disk" with a lot of unused space + some cloud providers charges for the unused space + some cloud providers offers VM with disk < 20GB. [1] https://cloudinit.readthedocs.io/en/latest/topics/modules.html#growpart Fix #127 --- build-inside-vm.sh | 2 +- images/basic.sh | 2 +- images/vagrant-libvirt.sh | 3 ++- images/vagrant-virtualbox.sh | 3 ++- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/build-inside-vm.sh b/build-inside-vm.sh index 3416e7f..d83996c 100755 --- a/build-inside-vm.sh +++ b/build-inside-vm.sh @@ -5,7 +5,7 @@ # errexit: "Exit immediately if [...] command exits with a non-zero status." set -o nounset -o errexit shopt -s extglob -readonly DEFAULT_DISK_SIZE="20G" +readonly DEFAULT_DISK_SIZE="2G" readonly IMAGE="image.img" # shellcheck disable=SC2016 readonly MIRROR='https://mirror.pkgbuild.com/$repo/os/$arch' diff --git a/images/basic.sh b/images/basic.sh index aa47626..3d0f5ce 100644 --- a/images/basic.sh +++ b/images/basic.sh @@ -1,7 +1,7 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-basic-${build_version}.qcow2" -DISK_SIZE="" +DISK_SIZE="20G" PACKAGES=() SERVICES=() diff --git a/images/vagrant-libvirt.sh b/images/vagrant-libvirt.sh index 7ffac40..9e19098 100644 --- a/images/vagrant-libvirt.sh +++ b/images/vagrant-libvirt.sh @@ -1,7 +1,8 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-libvirt-${build_version}.box" -DISK_SIZE="" +# https://gitlab.archlinux.org/archlinux/arch-boxes/-/issues/116 +DISK_SIZE="20G" PACKAGES=() SERVICES=() diff --git a/images/vagrant-virtualbox.sh b/images/vagrant-virtualbox.sh index 9a670f7..7ce6375 100644 --- a/images/vagrant-virtualbox.sh +++ b/images/vagrant-virtualbox.sh @@ -1,7 +1,8 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-virtualbox-${build_version}.box" -DISK_SIZE="" +# https://gitlab.archlinux.org/archlinux/arch-boxes/-/issues/116 +DISK_SIZE="20G" PACKAGES=(virtualbox-guest-utils-nox) SERVICES=(vboxservice) -- GitLab From d200e0ecbfd2ebc05276c00e65e78b4441552e88 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Sun, 31 Jan 2021 07:13:42 +0100 Subject: [PATCH 3/3] Expand the basic disk image from 20G -> 40G It is meant for local usage so the disk should be "big enough". --- images/basic.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/images/basic.sh b/images/basic.sh index 3d0f5ce..48dc8e6 100644 --- a/images/basic.sh +++ b/images/basic.sh @@ -1,7 +1,8 @@ #!/bin/bash # shellcheck disable=SC2034,SC2154 IMAGE_NAME="Arch-Linux-x86_64-basic-${build_version}.qcow2" -DISK_SIZE="20G" +# It is meant for local usage so the disk should be "big enough". +DISK_SIZE="40G" PACKAGES=() SERVICES=() -- GitLab