@@ -62,6 +62,7 @@ declare -A CHECKS=(
6262 [" preserve-env" ]=" 1"
6363 [" static-port-forwards" ]=" "
6464 [" ssh-over-vsock" ]=" "
65+ [" vz-block-device" ]=" "
6566)
6667
6768case " $NAME " in
@@ -70,6 +71,7 @@ case "$NAME" in
7071 # "[hostagent] failed to confirm whether /c/Users/runneradmin [remote] is successfully mounted"
7172 [ " ${OS_HOST} " = " Msys" ] && CHECKS[" mount-home" ]=
7273 [ " ${OS_HOST} " = " Darwin" ] && CHECKS[" ssh-over-vsock" ]=" 1"
74+ [ " ${OS_HOST} " = " Darwin" ] && CHECKS[" vz-block-device" ]=" 1"
7375 ;;
7476" alpine" * )
7577 WARNING " Alpine does not support systemd"
@@ -155,6 +157,90 @@ function diagnose() {
155157 set +x -e
156158}
157159
160+ function test_vz_block_device() {
161+ local name=" $1 "
162+ local host_block_device=" $2 "
163+ local host_block_device_id=" $3 "
164+ local volume_label=" $4 "
165+ local guest_block_device=" /dev/disk/by-id/virtio-${host_block_device_id} "
166+ local guest_partition_by_id=" /dev/disk/by-id/virtio-${host_block_device_id} -part1"
167+ local host_partition
168+ local host_mount_point
169+ local expected_contents
170+
171+ expected_contents=" $( printf ' test\ntest2' ) "
172+
173+ INFO " Testing VZ block-device attachment (${host_block_device} )"
174+ set -x
175+ limactl shell " $name " bash -eus -- " $guest_block_device " " $guest_partition_by_id " " $volume_label " << 'EOF '
176+ guest_block_device="$1"
177+ guest_partition_by_id="$2"
178+ volume_label="$3"
179+ guest_partition="$guest_partition_by_id"
180+
181+ if ! command -v mkfs.exfat >/dev/null 2>&1 || ! command -v parted >/dev/null 2>&1; then
182+ sudo apt-get update
183+ sudo apt-get install -y exfatprogs parted
184+ fi
185+
186+ sudo parted -s "$guest_block_device" -- mklabel gpt mkpart primary 1MiB 100%
187+ sudo udevadm settle
188+ if [ ! -e "$guest_partition" ]; then
189+ guest_partition="$(lsblk -nrpo PATH "$guest_block_device" | sed -n '2p')"
190+ fi
191+ test -n "$guest_partition"
192+ sudo mkfs.exfat -n "$volume_label" "$guest_partition"
193+ sudo mkdir -p /mnt/test
194+ sudo mount "$guest_partition" /mnt/test
195+ printf 'test\n' | sudo tee /mnt/test/test.txt >/dev/null
196+ sudo sync
197+ sudo umount /mnt/test
198+ EOF
199+ limactl stop " $name "
200+ sudo diskutil mountDisk " $host_block_device "
201+ host_partition=" $( diskutil list " $host_block_device " | awk ' $NF ~ /^disk[0-9]+s[0-9]+$/ {print "/dev/" $NF; exit}' ) "
202+ if [ -z " $host_partition " ]; then
203+ set +x
204+ ERROR " Failed to determine host partition for ${host_block_device} "
205+ exit 1
206+ fi
207+ host_mount_point=" $( mount | awk -v dev=" $host_partition " ' $1 == dev {print $3; exit}' ) "
208+ if [ -z " $host_mount_point " ]; then
209+ set +x
210+ ERROR " Failed to determine mount point for ${host_partition} "
211+ exit 1
212+ fi
213+ printf ' test2\n' | sudo tee -a " $host_mount_point /test.txt" > /dev/null
214+ if [ " $( cat " $host_mount_point /test.txt" ) " != " $expected_contents " ]; then
215+ set +x
216+ ERROR " Host block device contents are incorrect"
217+ exit 1
218+ fi
219+ sudo diskutil unmountDisk force " $host_block_device "
220+ limactl start " $name "
221+ if [ " $(
222+ limactl shell " $name " bash -eus -- " $guest_block_device " " $guest_partition_by_id " << 'EOF '
223+ guest_block_device="$1"
224+ guest_partition_by_id="$2"
225+ guest_partition="$guest_partition_by_id"
226+
227+ if [ ! -e "$guest_partition" ]; then
228+ guest_partition="$(lsblk -nrpo PATH "$guest_block_device" | sed -n '2p')"
229+ fi
230+ test -n "$guest_partition"
231+ sudo mkdir -p /mnt/test
232+ sudo mount "$guest_partition" /mnt/test
233+ cat /mnt/test/test.txt
234+ sudo umount /mnt/test
235+ EOF
236+ ) " != " $expected_contents " ]; then
237+ set +x
238+ ERROR " Guest block device contents are incorrect after host write"
239+ exit 1
240+ fi
241+ set +x
242+ }
243+
158244export ftp_proxy=http://localhost:2121
159245
160246INFO " Creating \" $NAME \" from \" $FILE_HOST \" "
@@ -176,14 +262,36 @@ set -x
176262" ${LIMACTL_CREATE[@]} " ${LIMACTL_CREATE_ARGS:- } " $FILE_HOST "
177263set +x
178264
265+ VM_TYPE=" $( limactl ls " ${NAME} " --yq .vmType) "
266+ START_ARGS=(" $NAME " )
267+ VZ_BLOCK_DEVICE_HOST=
268+ VZ_BLOCK_DEVICE_ID=
269+ VZ_BLOCK_DEVICE_LABEL=
270+
271+ if [[ -n ${CHECKS["vz-block-device"]} && $VM_TYPE == " vz" ]]; then
272+ INFO " Creating host RAM disk for VZ block-device test"
273+ set -x
274+ VZ_BLOCK_DEVICE_HOST=" $( sudo hdiutil attach -nomount ram://524288 | awk ' NR==1 {print $1}' ) "
275+ set +x
276+ if [[ -z $VZ_BLOCK_DEVICE_HOST ]]; then
277+ ERROR " Failed to create host RAM disk"
278+ exit 1
279+ fi
280+ VZ_BLOCK_DEVICE_ID=" $( basename " $VZ_BLOCK_DEVICE_HOST " ) "
281+ VZ_BLOCK_DEVICE_LABEL=" LIMABD${RANDOM} "
282+ defer " sudo diskutil unmountDisk force \" $VZ_BLOCK_DEVICE_HOST \" >/dev/null 2>&1 || true"
283+ defer " sudo hdiutil detach \" $VZ_BLOCK_DEVICE_HOST \" -force >/dev/null 2>&1 || true"
284+ START_ARGS=(--block-device " $VZ_BLOCK_DEVICE_HOST " " $NAME " )
285+ fi
286+
179287if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
180288 mkdir -p " /tmp/lima test dir with spaces"
181289 echo " test file content" > " /tmp/lima test dir with spaces/test file"
182290fi
183291
184292INFO " Starting \" $NAME \" "
185293set -x
186- if ! limactl start " $NAME " ; then
294+ if ! limactl start " ${START_ARGS[@]} " ; then
187295 ERROR " Failed to start \" $NAME \" "
188296 diagnose " $NAME "
189297 exit 1
@@ -386,6 +494,10 @@ if [[ -n ${CHECKS["ssh-over-vsock"]} ]]; then
386494 fi
387495fi
388496
497+ if [[ -n ${CHECKS["vz-block-device"]} && -n $VZ_BLOCK_DEVICE_HOST ]]; then
498+ test_vz_block_device " $NAME " " $VZ_BLOCK_DEVICE_HOST " " $VZ_BLOCK_DEVICE_ID " " $VZ_BLOCK_DEVICE_LABEL "
499+ fi
500+
389501# Use GHCR and ECR to avoid hitting Docker Hub rate limit
390502nginx_image=" ghcr.io/stargz-containers/nginx:1.19-alpine-org"
391503alpine_image=" ghcr.io/containerd/alpine:3.14.0"
610722if [[ -n ${CHECKS["user-v2"]} ]]; then
611723 INFO " Testing user-v2 network"
612724 secondvm=" $NAME -1"
725+ limactl delete -f " $secondvm " > /dev/null 2>&1 || true
613726 " ${LIMACTL_CREATE[@]} " --set " .additionalDisks=null" " $FILE_HOST " --name " $secondvm "
727+ defer " limactl delete -f \" $secondvm \" >/dev/null 2>&1 || true"
614728 if ! limactl start " $secondvm " ; then
615729 ERROR " Failed to start \" $secondvm \" "
616730 diagnose " $secondvm "
0 commit comments