diff options
author | Helmut Grohne <helmut@subdivi.de> | 2022-12-28 10:09:06 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2022-12-28 10:09:06 +0100 |
commit | 1f16c8718bebb1f4a3065599581dad392f6afc94 (patch) | |
tree | 9046fb0fe7ec5c2e84f1b7149fec9996e4d3db20 | |
parent | 0c50e3b74ec8173d6b0b5f26250a8feee0ceda7b (diff) | |
parent | b666a2909890a7954b103388ec846cac8ce55722 (diff) | |
download | debvm-1f16c8718bebb1f4a3065599581dad392f6afc94.tar.gz |
Merge branch main into branch pod2man
This merge picks up long options and the -s to -z rename.
-rw-r--r-- | .gitlab-ci.yml | 66 | ||||
-rwxr-xr-x | 9pmounthook/customize.sh | 42 | ||||
-rwxr-xr-x | debvm-create | 133 | ||||
-rwxr-xr-x | debvm-run | 70 | ||||
-rwxr-xr-x | tests/create-and-run.sh | 35 |
5 files changed, 224 insertions, 122 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b2ce36c..6427330 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,6 @@ image: debian:sid-slim shellcheck: - stage: test script: - apt-get update - apt-get dist-upgrade --yes @@ -9,71 +8,42 @@ shellcheck: - shellcheck debvm-* codespell: - stage: test script: - apt-get update - apt-get dist-upgrade --yes - apt-get --no-install-recommends --yes install codespell - codespell debvm-* -.test_script: - script: - - timeout 240s ./debvm-run -s 2222 & - - | - timeout=5 - sshopt="-o StrictHostKeyChecking=no $(if test "$RELEASE" = jessie; then echo -o PubkeyAcceptedKeyTypes=+ssh-rsa; fi)" - ts=$(sleepenh 0 || [ $? -eq 1 ]) - for i in $(seq 30); do - rv=0 - ssh $sshopt -o ConnectTimeout="$timeout" -i ~/.ssh/id -p 2222 root@localhost echo success || rv=$? - test $rv -eq 0 && break - ts=$(sleepenh "$ts" "$timeout" || [ $? -eq 1 ]); - if test $i -eq 30; then - echo "timeout reached" >&2 - exit 1 - fi - done - - ssh $sshopt -i ~/.ssh/id -p 2222 root@localhost poweroff - release_test: - extends: .test_script - stage: test parallel: matrix: - RELEASE: - - sid - - bookworm - - bullseye - - buster - - stretch - - jessie - before_script: + - sid + - bookworm + - bullseye + - buster + - stretch + - jessie + script: - apt-get update - apt-get dist-upgrade --yes - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client sleepenh qemu-kvm - - ssh-keygen -f ~/.ssh/id -N '' - - ./debvm-create -k ~/.ssh/id.pub -r "$RELEASE" + - PATH=.:$PATH ./tests/create-and-run.sh $(dpkg --print-architecture) "$RELEASE" arch_test: - extends: .test_script - stage: test parallel: matrix: - ARCHITECTURE: - - arm64 - - armhf - - i386 - - mips64el - - mipsel - - ppc64el - - s390x - before_script: - - | - if [ ! -e /proc/sys/fs/binfmt_misc/status ]; then - mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc - fi + - arm64 + - armhf + - i386 + - mips64el + - mipsel + - ppc64el + - s390x + script: + - test -e /proc/sys/fs/binfmt_misc/status || mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc - apt-get update - apt-get dist-upgrade --yes - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client sleepenh qemu-system binfmt-support arch-test qemu-user-static - - ssh-keygen -f ~/.ssh/id -N '' - - ./debvm-create -k ~/.ssh/id.pub -a $ARCHITECTURE + - PATH=.:$PATH ./tests/create-and-run.sh "$ARCHITECTURE" sid diff --git a/9pmounthook/customize.sh b/9pmounthook/customize.sh new file mode 100755 index 0000000..67c4240 --- /dev/null +++ b/9pmounthook/customize.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# Copyright 2022 Helmut Grohne <helmut@subdivi.de> +# SPDX-License-Identifier: MIT +# +# This is a mmdebstrap customize hook that adds a systemd-generator that causes +# 9p filesystems to be automatically mounted to /media/$SOMETAG during boot. +# You can enable it by passing the containing directory to --hook-dir. +# In order to add a 9p filesystem to your VM, pass +# -virtfs local,security_model=none,mount_tag=$SOMETAG,path=$SOMEDIR +# Note that the linux-image-cloud-* does not include a 9p driver. + +set -eu +GENERATOR_PATH="$1/etc/systemd/system-generators/9p-generator" +mkdir -p "${GENERATOR_PATH%/*}" +cat >"$GENERATOR_PATH" << 'ENDOFGENERATOR' +#!/bin/sh + +UNITDIR=$1 + +modprobe 9pnet_virtio || exit 0 + +for tagfile in /sys/bus/virtio/devices/*/mount_tag; do + tag=$(cat "$tagfile") || continue + test -z "$tag" && continue + mountpoint="/media/$tag" + mkdir -p "$mountpoint" + unitname="$(systemd-escape -p "$mountpoint").mount" + cat > "$UNITDIR/$unitname" <<ENDOFUNIT +[Unit] +Description=9p mount for tag $tag + +[Mount] +What=$tag +Where=$mountpoint +Type=9p +Options=trans=virtio +ENDOFUNIT + mkdir -p "$UNITDIR/remote-fs.target.wants" + ln -s "../$unitname" "$UNITDIR/remote-fs.target.wants/$unitname" +done +ENDOFGENERATOR +chmod 755 "$GENERATOR_PATH" diff --git a/debvm-create b/debvm-create index 65b3c54..37fa514 100755 --- a/debvm-create +++ b/debvm-create @@ -94,67 +94,88 @@ SSHKEY= SUITE=unstable VMNAME=testvm +nth_arg() { + shift "$1" + printf "%s" "$1" +} + die() { echo "$*" 1>&2 exit 1 } - usage() { - die "usage: $0 [-a architecture] [-h hostname] [-k sshkey] [-m mirror] [-o output] [-p packages] [-r release] [-s size_in_GB] [-- mmdebstrap options]" + die "usage: $0 [-a architecture] [-h hostname] [-k sshkey] [-m mirror] [-o output] [-p packages] [-r release] [-z size_in_GB] [-- mmdebstrap options]" +} +usage_error() { + echo "error: $*" 1>&2 + usage } +opt_architecture() { + ARCHITECTURE=$1 +} +opt_hostname() { + VMNAME=$1 +} +opt_mirror() { + MIRROR=$1 +} +opt_sshkey() { + SSHKEY=$1 +} +opt_output() { + IMAGE=$1 +} +opt_package() { + INCLUDE_PACKAGES="$INCLUDE_PACKAGES,$1" +} +opt_release() { + SUITE=$1 +} +opt_size() { + SIZE=$(($1*1024*1024*1024)) +} -while test "$#" -gt 0; do - case "$1" in - -a) - test "$#" -eq 1 && usage - ARCHITECTURE=$2 - shift 2 - ;; - -h) - test "$#" -eq 1 && usage - VMNAME=$2 - shift 2 +while getopts :a:h:k:m:o:p:r:z:-: OPTCHAR; do + case "$OPTCHAR" in + a) opt_architecture "$OPTARG" ;; + h) opt_hostname "$OPTARG" ;; + k) opt_sshkey "$OPTARG" ;; + m) opt_mirror "$OPTARG" ;; + o) opt_output "$OPTARG" ;; + p) opt_package "$OPTARG" ;; + r) opt_release "$OPTARG" ;; + z) opt_size "$OPTARG" ;; + -) + case "$OPTARG" in + help) + usage + ;; + architecture|hostname|mirror|output|package|release|size|sshkey) + test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG" + "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")" + OPTIND=$((OPTIND+1)) + ;; + architecture=*|hostname=*|mirror=*|output=*|package=*|release=*|size=*|sshkey=*) + "opt_${OPTARG%%=*}" "${OPTARG#*=}" + ;; + *) + usage_error "unrecognized option --$OPTARG" + ;; + esac ;; - -k) - test "$#" -eq 1 && usage - SSHKEY=$2 - shift 2 + :) + usage_error "missing argument for -$OPTARG" ;; - -m) - test "$#" -eq 1 && usage - MIRROR=$2 - shift 2 - ;; - -o) - test "$#" -eq 1 && usage - IMAGE=$2 - shift 2 - ;; - -p) - test "$#" -eq 1 && usage - INCLUDE_PACKAGES="$INCLUDE_PACKAGES,$2" - shift 2 - ;; - -r) - test "$#" -eq 1 && usage - SUITE=$2 - shift 2 - ;; - -s) - test "$#" -eq 1 && usage - SIZE=$(($2*1024*1024*1024)) - shift 2 - ;; - --) - shift - break + '?') + usage_error "unrecognized option -$OPTARG" ;; *) - usage + die "internal error while parsing command options, please report a bug" ;; esac done +shift "$((OPTIND - 1))" if test -n "$SSHKEY" && ! test -f "$SSHKEY"; then die "error: ssh keyfile '$SSHKEY' not found" @@ -190,8 +211,6 @@ case "$SUITE" in ;; esac - - KERNEL_SUFFIX=-$ARCHITECTURE case "$ARCHITECTURE" in amd64|arm64) @@ -217,7 +236,13 @@ case "$ARCHITECTURE" in ;; esac -INCLUDE_PACKAGES="$INCLUDE_PACKAGES,linux-image$KERNEL_SUFFIX" +case ",$INCLUDE_PACKAGES," in + *,linux-image-*) + ;; + *) + INCLUDE_PACKAGES="$INCLUDE_PACKAGES,linux-image$KERNEL_SUFFIX" + ;; +esac if test -n "$SSHKEY"; then INCLUDE_PACKAGES="$INCLUDE_PACKAGES,openssh-server" @@ -284,8 +309,7 @@ if test "$DEBVER" -le 8; then # Use obsolete and expired keys. set -- '--keyring=/usr/share/keyrings/debian-archive-removed-keys.gpg' "$@" set -- --aptopt='Apt::Key::gpgvcommand "/usr/libexec/mmdebstrap/gpgvnoexpkeysig"' "$@" - # chfn does not work, because libpam-runtime.postinst is late setting up /etc/pam.d/common-auth et al, see #1026765 - set -- --extract-hook='chroot "$1" pam-auth-update --package --force' "$@" + set -- --hook-dir=/usr/share/mmdebstrap/hooks/jessie-or-older "$@" fi if test "$DEBVER" -ge 12; then @@ -300,8 +324,11 @@ set -ex mmdebstrap "$@" -truncate -s "$SIZE" "$IMAGE" -/sbin/resize2fs "$IMAGE" +IMAGESIZE=$(stat -c %s "$IMAGE") +if test "$IMAGESIZE" -lt "$SIZE"; then + truncate -s "$SIZE" "$IMAGE" + /sbin/resize2fs "$IMAGE" +fi /sbin/tune2fs -L debvm -i 0 -O extents,uninit_bg,dir_index,has_journal "$IMAGE" # Must fsck after tune2fs: https://ext4.wiki.kernel.org/index.php/UpgradeToExt4 /sbin/fsck.ext4 -fDp "$IMAGE" @@ -66,40 +66,68 @@ IMAGE=rootfs.ext4 SSHPORT= GRAPHICAL= +nth_arg() { + shift "$1" + printf "%s" "$1" +} + die() { echo "$*" 1>&2 exit 1 } - usage() { die "usage: $0 [-g] [-i image] [-s sshport] [-- qemu options]" } +usage_error() { + echo "error: $*" 1>&2 + usage +} -while test "$#" -gt 0; do - case "$1" in - -g) - GRAPHICAL=1 - shift - ;; - -i) - test "$#" -eq 1 && usage - IMAGE=$2 - shift 2 +opt_graphical() { + GRAPHICAL=1 +} +opt_image() { + IMAGE=$1 +} +opt_sshport() { + SSHPORT=$1 +} + +while getopts :gi:s:-: OPTCHAR; do + case "$OPTCHAR" in + g) opt_graphical ;; + i) opt_image "$OPTARG" ;; + s) opt_sshport "$OPTARG" ;; + -) + case "$OPTARG" in + help) + usage + ;; + graphical|image|sshport) + test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG" + "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")" + OPTIND=$((OPTIND+1)) + ;; + image=*|sshport=*) + "opt_${OPTARG%%=*}" "${OPTARG#*=}" + ;; + *) + usage_error "unrecognized option --$OPTARG" + ;; + esac ;; - -s) - test "$#" -eq 1 && usage - SSHPORT=$2 - shift 2 + :) + usage_error "missing argument for -$OPTARG" ;; - --) - shift - break + '?') + usage_erro "unrecognized option -$OPTARG" ;; *) - usage + die "internal error while parsing command options, please report a bug" ;; esac done +shift "$((OPTIND - 1))" test -f "$IMAGE" || die "image '$IMAGE' not found" test -s "$IMAGE" || die "image '$IMAGE' is empty" @@ -198,9 +226,9 @@ else ;; esac fi -if test "$MAX_SMP" -gt 1; then +if test -z "$MAX_SMP" || test "$MAX_SMP" -gt 1; then NPROC=$(nproc) - test "$NPROC" -gt "$MAX_SMP" && NPROC=$MAX_SMP + test -n "$MAX_SMP" && test "$NPROC" -gt "$MAX_SMP" && NPROC=$MAX_SMP set -- -smp "$NPROC" "$@" fi diff --git a/tests/create-and-run.sh b/tests/create-and-run.sh new file mode 100755 index 0000000..0e2a3ca --- /dev/null +++ b/tests/create-and-run.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +# shellcheck disable=SC2086 + +if test "$#" -ne 2; then + echo "$(basename $0) takes two positional arguments. architecture and release" 1>&2 + exit 1 +fi + +set -ex + +cleanup() { + rm -f ssh_id ssh_id.pub test.ext4 +} + +trap cleanup EXIT INT TERM QUIT + +ssh-keygen -f ssh_id -N '' +debvm-create -k ssh_id.pub -o test.ext4 -a "$1" -r "$2" + +timeout 240s debvm-run -s 2222 -i test.ext4 & +timeout=5 +sshopt="-o StrictHostKeyChecking=no $(if test "$2" = jessie; then echo -o PubkeyAcceptedKeyTypes=+ssh-rsa; fi)" +ts=$(sleepenh 0 || [ $? -eq 1 ]) +for i in $(seq 30); do + rv=0 + ssh $sshopt -o ConnectTimeout="$timeout" -i ssh_id -p 2222 root@localhost echo success || rv=$? + test $rv -eq 0 && break + ts=$(sleepenh "$ts" "$timeout" || [ $? -eq 1 ]); + if test "$i" -eq 30; then + echo "timeout reached" >&2 + exit 1 + fi +done +ssh $sshopt -i ssh_id -p 2222 root@localhost poweroff |