- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_drive_vhost_user.py
315 lines (251 loc) · 9.92 KB
/
test_drive_vhost_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""Tests for vhost-user-block device."""
importos
importshutil
frompathlibimportPath
importhost_tools.driveasdrive_tools
fromframework.utils_driveimportpartuuid_and_disk_path
fromhost_tools.fcmetricsimportFcDeviceMetrics
def_check_block_size(ssh_connection, dev_path, size):
"""
Checks the size of the block device.
"""
_, stdout, stderr=ssh_connection.run("blockdev --getsize64 {}".format(dev_path))
assertstderr==""
assertstdout.strip() ==str(size)
def_check_drives(test_microvm, assert_dict, keys_array):
"""
Checks the info on the block devices.
"""
_, stdout, stderr=test_microvm.ssh.run("blockdev --report")
assertstderr==""
blockdev_out_lines=stdout.splitlines()
forkeyinkeys_array:
line=int(key.split("-")[0])
col=int(key.split("-")[1])
blockdev_out_line_cols=blockdev_out_lines[line].split()
assertblockdev_out_line_cols[col] ==assert_dict[key]
deftest_vhost_user_block(microvm_factory, guest_kernel, rootfs):
"""
This test simply tries to boot a VM with
vhost-user-block as a root device.
"""
vm=microvm_factory.build(guest_kernel, None, monitor_memory=False)
# We need to setup ssh keys manually because we did not specify rootfs
# in microvm_factory.build method
ssh_key=rootfs.with_suffix(".id_rsa")
vm.ssh_key=ssh_key
vm.spawn()
vm.basic_config(add_root_device=False)
vm.add_vhost_user_drive("rootfs", rootfs, is_root_device=True, is_read_only=True)
vm.add_net_iface()
vhost_user_block_metrics=FcDeviceMetrics(
"vhost_user_block", 1, aggr_supported=False
)
vm.start()
# Now check that vhost-user-block with rw is last.
# 1-0 means line 1, column 0.
assert_dict= {
"1-0": "ro",
"1-6": "/dev/vda",
}
_check_drives(vm, assert_dict, assert_dict.keys())
vhost_user_block_metrics.validate(vm)
deftest_vhost_user_block_read_write(microvm_factory, guest_kernel, rootfs):
"""
This test simply tries to boot a VM with
vhost-user-block as a root device.
This test configures vhost-user-block to be read write.
"""
vm=microvm_factory.build(guest_kernel, None, monitor_memory=False)
# We need to setup ssh keys manually because we did not specify rootfs
# in microvm_factory.build method
ssh_key=rootfs.with_suffix(".id_rsa")
vm.ssh_key=ssh_key
vm.spawn()
vm.basic_config(add_root_device=False)
# Create a rw rootfs file that is unique to the microVM
rootfs_rw=Path(vm.chroot()) /"rootfs"
shutil.copy(rootfs, rootfs_rw)
vm.add_vhost_user_drive("rootfs", rootfs_rw, is_root_device=True)
vm.add_net_iface()
vm.start()
# Now check that vhost-user-block with rw is last.
# 1-0 means line 1, column 0.
assert_dict= {
"1-0": "rw",
"1-6": "/dev/vda",
}
_check_drives(vm, assert_dict, assert_dict.keys())
deftest_vhost_user_block_disconnect(microvm_factory, guest_kernel, rootfs):
"""
Test that even if backend is killed, Firecracker is still responsive.
"""
vm=microvm_factory.build(guest_kernel, None, monitor_memory=False)
# We need to set up ssh keys manually because we did not specify rootfs
# in microvm_factory.build method
ssh_key=rootfs.with_suffix(".id_rsa")
vm.ssh_key=ssh_key
vm.spawn()
vm.basic_config(add_root_device=False)
vm.add_vhost_user_drive("rootfs", rootfs, is_root_device=True, is_read_only=True)
vm.add_net_iface()
vm.start()
# Killing the backend
vm.disks_vhost_user["rootfs"].kill()
delvm.disks_vhost_user["rootfs"]
# Verify that Firecracker is still responsive
_config=vm.api.vm_config.get().json()
deftest_device_ordering(microvm_factory, guest_kernel, rootfs):
"""
Verify device ordering.
The root device should correspond to /dev/vda in the guest and
the order of the other devices should match their configuration order.
"""
vm=microvm_factory.build(guest_kernel, None, monitor_memory=False)
# We need to setup ssh keys manually because we did not specify rootfs
# in microvm_factory.build method
ssh_key=rootfs.with_suffix(".id_rsa")
vm.ssh_key=ssh_key
vm.spawn()
vm.basic_config(add_root_device=False)
vm.add_net_iface()
# Adding first block device.
fs1=drive_tools.FilesystemFile(os.path.join(vm.fsfiles, "scratch1"), size=128)
vm.add_drive("scratch1", fs1.path)
# Adding second block device (rootfs)
vm.add_vhost_user_drive("rootfs", rootfs, is_root_device=True, is_read_only=True)
# Adding third block device.
fs2=drive_tools.FilesystemFile(os.path.join(vm.fsfiles, "scratch2"), size=512)
vm.add_drive("scratch2", fs2.path)
# Create a rw rootfs file that is unique to the microVM
rootfs_rw=Path(vm.chroot()) /"rootfs"
shutil.copy(rootfs, rootfs_rw)
# Adding forth block device.
vm.add_vhost_user_drive("dummy_rootfs", rootfs_rw)
block_metrics=FcDeviceMetrics("block", 2, aggr_supported=True)
vhost_user_block_metrics=FcDeviceMetrics(
"vhost_user_block", 2, aggr_supported=False
)
vm.start()
rootfs_size=rootfs.stat().st_size
# The devices were added in this order: fs1, rootfs, fs2. fs3
# However, the rootfs is the root device and goes first,
# so we expect to see this order: rootfs, fs1, fs2. fs3
# First check drives order by sizes.
ssh_connection=vm.ssh
_check_block_size(ssh_connection, "/dev/vda", rootfs_size)
_check_block_size(ssh_connection, "/dev/vdb", fs1.size())
_check_block_size(ssh_connection, "/dev/vdc", fs2.size())
_check_block_size(ssh_connection, "/dev/vdd", rootfs_size)
# Now check that vhost-user-block with rw is last.
# 1-0 means line 1, column 0.
assert_dict= {
"1-0": "ro",
"1-6": "/dev/vda",
"2-0": "rw",
"2-6": "/dev/vdb",
"3-0": "rw",
"3-6": "/dev/vdc",
"4-0": "rw",
"4-6": "/dev/vdd",
}
_check_drives(vm, assert_dict, assert_dict.keys())
block_metrics.validate(vm)
vhost_user_block_metrics.validate(vm)
deftest_partuuid_boot(
microvm_factory,
guest_kernel,
rootfs,
):
"""
Test the output reported by blockdev when booting with PARTUUID.
"""
vm=microvm_factory.build(guest_kernel, None, monitor_memory=False)
# We need to setup ssh keys manually because we did not specify rootfs
# in microvm_factory.build method
ssh_key=rootfs.with_suffix(".id_rsa")
vm.ssh_key=ssh_key
vm.spawn()
vm.basic_config(add_root_device=False)
# Create a rootfs with partuuid unique to this microVM
partuuid, disk_path=partuuid_and_disk_path(rootfs, Path(vm.chroot()) /"disk.img")
vm.add_vhost_user_drive(
"1", disk_path, is_root_device=True, partuuid=partuuid, is_read_only=True
)
vm.add_net_iface()
vm.start()
# Now check that vhost-user-block with rw is last.
# 1-0 means line 1, column 0.
assert_dict= {
"1-0": "ro",
"1-6": "/dev/vda",
}
_check_drives(vm, assert_dict, assert_dict.keys())
deftest_partuuid_update(microvm_factory, guest_kernel, rootfs):
"""
Test successful switching from PARTUUID boot to /dev/vda boot.
"""
vm=microvm_factory.build(guest_kernel, None, monitor_memory=False)
# We need to setup ssh keys manually because we did not specify rootfs
# in microvm_factory.build method
ssh_key=rootfs.with_suffix(".id_rsa")
vm.ssh_key=ssh_key
vm.spawn()
vm.basic_config(add_root_device=False)
vm.add_net_iface()
# Add the root block device specified through PARTUUID.
vm.add_vhost_user_drive(
"rootfs",
rootfs,
is_root_device=True,
partuuid="0eaa91a0-01",
is_read_only=True,
)
# Adding a drive with the same ID creates another backend with another socket.
vm.add_vhost_user_drive("rootfs", rootfs, is_root_device=True, is_read_only=True)
vhost_user_block_metrics=FcDeviceMetrics(
"vhost_user_block", 1, aggr_supported=False
)
vm.start()
# Now check that vhost-user-block with rw is last.
# 1-0 means line 1, column 0.
assert_dict= {
"1-0": "ro",
"1-6": "/dev/vda",
}
_check_drives(vm, assert_dict, assert_dict.keys())
vhost_user_block_metrics.validate(vm)
deftest_config_change(microvm_factory, guest_kernel, rootfs):
"""
Verify handling of block device resize.
We expect that the guest will start reporting the updated size
after Firecracker handles a PATCH request to the vhost-user block device.
"""
orig_size=10# MB
new_sizes= [20, 10, 30] # MB
mkfs_mount_cmd="mkfs.ext4 /dev/vdb && mkdir -p /tmp/tmp && mount /dev/vdb /tmp/tmp && umount /tmp/tmp"
vm=microvm_factory.build(guest_kernel, rootfs, monitor_memory=False)
vm.spawn(log_level="Info")
vm.basic_config()
vm.add_net_iface()
# Add a block device to test resizing.
fs=drive_tools.FilesystemFile(size=orig_size)
vm.add_vhost_user_drive("scratch", fs.path)
vm.start()
# Check that guest reports correct original size.
_check_block_size(vm.ssh, "/dev/vdb", orig_size*1024*1024)
# Check that we can create a filesystem and mount it
vm.ssh.check_output(mkfs_mount_cmd)
fornew_sizeinnew_sizes:
# Instruct the backend to resize the device.
# It will both resize the file and update its device config.
vm.disks_vhost_user["scratch"].resize(new_size)
# Instruct Firecracker to reread device config and notify
# the guest of a config change.
vm.patch_drive("scratch")
# Check that guest reports correct new size.
_check_block_size(vm.ssh, "/dev/vdb", new_size*1024*1024)
# Check that we can create a filesystem and mount it
vm.ssh.check_output(mkfs_mount_cmd)