Skip to content

Commit 2a2341e

Browse files
committed
tests: Build test env VM image using virt-builder
Signed-off-by: Alberto Faria <[email protected]>
1 parent e29e607 commit 2a2341e

File tree

2 files changed

+55
-78
lines changed

2 files changed

+55
-78
lines changed

plans/tests.fmf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ execute:
3838

3939
# set the test VM's Fedora version to the host's to run the tests under the
4040
# requested environment
41-
tag=$( awk -F= '/^VERSION_ID=/ {print tolower($2)}' /etc/os-release )
42-
export CRUN_VM_TEST_ENV_BASE_IMAGE=quay.io/containerdisks/fedora:$tag
41+
version=$( awk -F= '/^VERSION_ID=/ {print tolower($2)}' /etc/os-release )
42+
export CRUN_VM_TEST_ENV_FEDORA_VERSION=$version
4343

4444
tests/env.sh build
4545
tests/env.sh start

tests/env.sh

Lines changed: 53 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ set -o errexit -o pipefail -o nounset
55

66
start_time="$( date +%s%N )"
77

8-
env_image_base=${CRUN_VM_TEST_ENV_BASE_IMAGE:-"quay.io/containerdisks/fedora:40"}
98
env_image=quay.io/crun-vm/test-env:latest
109
container_name=crun-vm-test-env
1110

@@ -135,91 +134,66 @@ build)
135134

136135
__big_log 33 'Building test env image...'
137136

138-
# extract base image file
139-
140-
__log_and_run "$( __rel "$repo_root/util/extract-vm-image.sh" )" \
141-
"$env_image_base" \
142-
"$temp_dir/image"
143-
144-
# expand base image
145-
146-
__log_and_run qemu-img create -f qcow2 "$temp_dir/image.qcow2" 50G
147-
__log_and_run virt-resize \
148-
--quiet \
149-
--expand /dev/sda4 \
150-
"$temp_dir/image" \
151-
"$temp_dir/image.qcow2"
152-
153-
rm "$temp_dir/image"
154-
155-
# launch VM from base image file
156-
157-
__log_and_run podman run \
158-
--name "$container_name-build" \
159-
--runtime "$runtime" \
160-
--memory 8g \
161-
--rm -dit \
162-
--rootfs "$temp_dir" \
163-
--persistent
164-
165-
# shellcheck disable=SC2317
166-
__extra_cleanup() {
167-
__log_and_run podman stop --time 0 "$container_name-build"
168-
}
169-
170-
# customize VM
171-
172-
__exec() {
173-
__log_and_run podman exec "$container_name-build" --as fedora "$@"
174-
}
175-
176-
# generate an ssh keypair for users fedora and root so crun-vm containers
177-
# get a predictable keypair
178-
__exec 'ssh-keygen -q -f .ssh/id_rsa -N "" && sudo cp -r .ssh /root/'
179-
180-
__exec sudo dnf update -y
181-
__exec sudo dnf install -y \
182-
bash \
183-
coreutils \
184-
crun \
185-
crun-krun \
186-
docker \
187-
genisoimage \
188-
grep \
189-
htop \
190-
libselinux-devel \
191-
libvirt-client \
192-
libvirt-daemon-driver-qemu \
193-
libvirt-daemon-log \
194-
lsof \
195-
openssh-clients \
196-
podman \
197-
qemu-img \
198-
qemu-system-x86-core \
199-
shadow-utils \
200-
util-linux \
137+
# build disk image
138+
139+
packages=(
140+
bash
141+
cloud-init
142+
coreutils
143+
crun
144+
crun-krun
145+
docker
146+
genisoimage
147+
grep
148+
htop
149+
libselinux-devel
150+
libvirt-client
151+
libvirt-daemon-driver-qemu
152+
libvirt-daemon-log
153+
lsof
154+
openssh-clients
155+
podman
156+
qemu-img
157+
qemu-system-x86-core
158+
shadow-utils
159+
util-linux
201160
virtiofsd
202-
__exec sudo dnf clean all
161+
)
162+
163+
packages_joined=$( printf ",%s" "${packages[@]}" )
164+
packages_joined=${packages_joined:1}
203165

204166
daemon_json='{ "runtimes": { "crun-vm": { "path": "/home/fedora/target/debug/crun-vm" } } }'
205-
__exec sudo mkdir -p /etc/docker
206-
__exec "echo ${daemon_json@Q} | sudo tee /etc/docker/daemon.json"
207167

208-
__exec sudo cloud-init clean --logs # run cloud-init again on next boot
168+
commands=(
169+
# generate an ssh keypair for users fedora and root so crun-vm
170+
# containers get a predictable keypair
171+
'ssh-keygen -q -f /root/.ssh/id_rsa -N ""'
209172

210-
__exec sudo poweroff || true
173+
"mkdir -p /etc/docker && echo ${daemon_json@Q} > /etc/docker/daemon.json"
174+
)
211175

212-
# sparsify image file
176+
__log_and_run virt-builder \
177+
"fedora-${CRUN_VM_TEST_ENV_FEDORA_VERSION:-40}" \
178+
--smp "$( nproc )" \
179+
--memsize 4096 \
180+
--format qcow2 \
181+
--output "$temp_dir/image.qcow2" \
182+
--size 50G \
183+
--root-password password:root \
184+
--install "$packages_joined" \
185+
"${commands[@]/#/--run-command=}"
213186

214-
__log_and_run podman wait --ignore "$container_name-build"
215-
__extra_cleanup() { :; }
187+
# reduce image file size
216188

217-
__log_and_run virt-sparsify --quiet --in-place "$temp_dir/image.qcow2"
189+
__log_and_run virt-sparsify --in-place "$temp_dir/image.qcow2"
190+
__log_and_run qemu-img convert -f qcow2 -O qcow2 \
191+
"$temp_dir/image.qcow2" "$temp_dir/image-small.qcow2"
218192

219193
# package new image file
220194

221195
__log_and_run "$( __rel "$repo_root/util/package-vm-image.sh" )" \
222-
"$temp_dir/image.qcow2" \
196+
"$temp_dir/image-small.qcow2" \
223197
"$env_image"
224198

225199
__big_log 33 'Done.'
@@ -254,14 +228,17 @@ start)
254228
__log_and_run podman stop --time 0 "$container_name"
255229
}
256230

257-
# load test images onto VM
258-
259231
__exec() {
260232
__log_and_run podman exec "$container_name" --as fedora "$@"
261233
}
262234

263235
chmod a+rx "$temp_dir" # so user "fedora" in guest can access it
264236

237+
__exec sudo cp /root/.ssh/id_rsa /root/.ssh/id_rsa.pub .ssh/
238+
__exec sudo chown fedora:fedora . .ssh/id_rsa .ssh/id_rsa.pub
239+
240+
# load test images onto VM
241+
265242
for image in "${TEST_IMAGES[@]}"; do
266243
__log_and_run podman pull "$image"
267244
__log_and_run podman save "$image" -o "$temp_dir/image.tar"

0 commit comments

Comments
 (0)