Skip to content

Commit d36259d

Browse files
committed
Revert gRPC client dial change and test VZ block devices
Signed-off-by: Nick Sweeting <git@sweeting.me>
1 parent 7c3f8b2 commit d36259d

2 files changed

Lines changed: 117 additions & 4 deletions

File tree

hack/test-templates.sh

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

6768
case "$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+
158244
export ftp_proxy=http://localhost:2121
159245

160246
INFO "Creating \"$NAME\" from \"$FILE_HOST\""
@@ -176,14 +262,36 @@ set -x
176262
"${LIMACTL_CREATE[@]}" ${LIMACTL_CREATE_ARGS:-} "$FILE_HOST"
177263
set +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+
179287
if [[ -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"
182290
fi
183291

184292
INFO "Starting \"$NAME\""
185293
set -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
387495
fi
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
390502
nginx_image="ghcr.io/stargz-containers/nginx:1.19-alpine-org"
391503
alpine_image="ghcr.io/containerd/alpine:3.14.0"
@@ -610,7 +722,9 @@ fi
610722
if [[ -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"

pkg/driver/external/client/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/sirupsen/logrus"
1111
"google.golang.org/grpc"
1212
"google.golang.org/grpc/credentials/insecure"
13-
"google.golang.org/grpc/resolver"
1413

1514
pb "github.com/lima-vm/lima/v2/pkg/driver/external"
1615
)
@@ -33,8 +32,8 @@ func NewDriverClient(socketPath string, logger *logrus.Logger) (*DriverClient, e
3332
grpc.WithTransportCredentials(insecure.NewCredentials()),
3433
}
3534

36-
resolver.SetDefaultScheme("passthrough")
37-
conn, err := grpc.NewClient("", opts...)
35+
//nolint:staticcheck // grpc.Dial is used for compatibility reasons
36+
conn, err := grpc.Dial("unix://"+socketPath, opts...)
3837
if err != nil {
3938
logger.Errorf("failed to dial gRPC driver client connection: %v", err)
4039
return nil, err

0 commit comments

Comments
 (0)