Debian Codegraph @

tests/fs_tests/generic_test.py

raw
1 import os
2 import time
3 import tempfile
4 import re
5
6 from packaging.version import Version
7
8
9 from .fs_test import FSTestCase, FSNoDevTestCase, mounted, check_output
10
11 import overrides_hack
12 import utils
13 from utils import TestTags, tag_test
14
15 from gi.repository import BlockDev, GLib
16
17
18 class GenericNoDevTestCase(FSNoDevTestCase):
19 pass
20
21
22 class GenericTestCase(FSTestCase):
23
24 loop_size = 500 * 1024**2
25
26 def setUp(self):
27 super(GenericTestCase, self).setUp()
28
29 self.mount_dir = tempfile.mkdtemp(prefix="libblockdev.", suffix="generic_test")
30
31 self._vfat_version = self._get_dosfstools_version()
32
33 def _get_dosfstools_version(self):
34 _ret, out, _err = utils.run_command("mkfs.vfat --help")
35 # mkfs.fat 4.1 (2017-01-24)
36 m = re.search(r"mkfs\.fat ([\d\.]+)", out)
37 if not m or len(m.groups()) != 1:
38 raise RuntimeError("Failed to determine dosfstools version from: %s" % out)
39 return Version(m.groups()[0])
40
41
42 class TestGenericWipe(GenericTestCase):
43 @tag_test(TestTags.CORE)
44 def test_generic_wipe(self):
45 """Verify that generic signature wipe works as expected"""
46
47 with self.assertRaises(GLib.GError):
48 BlockDev.fs_wipe("/non/existing/device", True)
49
50 ret = utils.run("pvcreate -ff -y %s --config \"devices {use_devicesfile = 0}\" >/dev/null 2>&1" % self.loop_devs[0])
51 self.assertEqual(ret, 0)
52
53 succ = BlockDev.fs_wipe(self.loop_devs[0], True)
54 self.assertTrue(succ)
55
56 # now test the same multiple times in a row
57 for i in range(10):
58 ret = utils.run("pvcreate -ff -y %s --config \"devices {use_devicesfile = 0}\" >/dev/null 2>&1" % self.loop_devs[0])
59 self.assertEqual(ret, 0)
60
61 succ = BlockDev.fs_wipe(self.loop_devs[0], True)
62 self.assertTrue(succ)
63
64 # vfat has multiple signatures on the device so it allows us to test the
65 # 'all' argument of fs_wipe()
66 if self._vfat_version >= Version("4.2"):
67 ret = utils.run("mkfs.vfat -I %s >/dev/null 2>&1 --mbr=n" % self.loop_devs[0])
68 else:
69 ret = utils.run("mkfs.vfat -I %s >/dev/null 2>&1" % self.loop_devs[0])
70 self.assertEqual(ret, 0)
71
72 time.sleep(0.5)
73 succ = BlockDev.fs_wipe(self.loop_devs[0], False)
74 self.assertTrue(succ)
75
76 # the second signature should still be there
77 # XXX: lsblk uses the udev db so it we need to make sure it is up to date
78 utils.run("udevadm settle")
79 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
80 self.assertEqual(fs_type, b"vfat")
81
82 # get rid of all the remaining signatures (there could be vfat + PMBR for some reason)
83 succ = BlockDev.fs_wipe(self.loop_devs[0], True)
84 self.assertTrue(succ)
85
86 utils.run("udevadm settle")
87 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
88 self.assertEqual(fs_type, b"")
89
90 # now do the wipe all in a one step
91 if self._vfat_version >= Version("4.2"):
92 ret = utils.run("mkfs.vfat -I %s >/dev/null 2>&1 --mbr=n" % self.loop_devs[0])
93 else:
94 ret = utils.run("mkfs.vfat -I %s >/dev/null 2>&1" % self.loop_devs[0])
95 self.assertEqual(ret, 0)
96
97 succ = BlockDev.fs_wipe(self.loop_devs[0], True)
98 self.assertTrue(succ)
99
100 utils.run("udevadm settle")
101 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
102 self.assertEqual(fs_type, b"")
103
104 # try to wipe empty device
105 with self.assertRaisesRegex(GLib.GError, "No signature detected on the device"):
106 BlockDev.fs_wipe(self.loop_devs[0], True)
107
108 @tag_test(TestTags.CORE)
109 def test_generic_wipe_force(self):
110 ret = utils.run("mkfs.ext2 %s >/dev/null 2>&1" % self.loop_devs[0])
111 self.assertEqual(ret, 0)
112
113 with mounted(self.loop_devs[0], self.mount_dir):
114 # default should be force=False
115 with self.assertRaisesRegex(GLib.GError, "Failed to open the device"):
116 BlockDev.fs_wipe(self.loop_devs[0])
117
118 succ = BlockDev.fs_wipe(self.loop_devs[0], force=True)
119 self.assertTrue(succ)
120
121 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
122 self.assertEqual(fs_type, b"")
123
124
125 class TestClean(GenericTestCase):
126 def test_clean(self):
127 """Verify that device clean works as expected"""
128
129 with self.assertRaises(GLib.GError):
130 BlockDev.fs_clean("/non/existing/device")
131
132 # empty device shouldn't fail
133 succ = BlockDev.fs_clean(self.loop_devs[0])
134 self.assertTrue(succ)
135
136 ret = utils.run("pvcreate -ff -y %s --config \"devices {use_devicesfile = 0}\" >/dev/null 2>&1" % self.loop_devs[0])
137 self.assertEqual(ret, 0)
138
139 succ = BlockDev.fs_clean(self.loop_devs[0])
140 self.assertTrue(succ)
141
142 # XXX: lsblk uses the udev db so it we need to make sure it is up to date
143 utils.run("udevadm settle")
144 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
145 self.assertEqual(fs_type, b"")
146
147 # vfat has multiple signatures on the device so it allows us to test
148 # that clean removes all signatures
149 if self._vfat_version >= Version("4.2"):
150 ret = utils.run("mkfs.vfat -I %s >/dev/null 2>&1 --mbr=n" % self.loop_devs[0])
151 else:
152 ret = utils.run("mkfs.vfat -I %s >/dev/null 2>&1" % self.loop_devs[0])
153 self.assertEqual(ret, 0)
154
155 time.sleep(0.5)
156 succ = BlockDev.fs_clean(self.loop_devs[0])
157 self.assertTrue(succ)
158
159 utils.run("udevadm settle")
160 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
161 self.assertEqual(fs_type, b"")
162
163 @tag_test(TestTags.CORE)
164 def test_generic_clean_force(self):
165 ret = utils.run("mkfs.ext2 %s >/dev/null 2>&1" % self.loop_devs[0])
166 self.assertEqual(ret, 0)
167
168 with mounted(self.loop_devs[0], self.mount_dir):
169 # default should be force=False
170 with self.assertRaisesRegex(GLib.GError, "Failed to open the device"):
171 BlockDev.fs_clean(self.loop_devs[0])
172
173 succ = BlockDev.fs_clean(self.loop_devs[0], force=True)
174 self.assertTrue(succ)
175
176 fs_type = check_output(["blkid", "-ovalue", "-sTYPE", "-p", self.loop_devs[0]]).strip()
177 self.assertEqual(fs_type, b"")
178
179
180 class CanResizeRepairCheckLabel(GenericNoDevTestCase):
181 def test_can_resize(self):
182 """Verify that tooling query works for resize"""
183
184 avail, mode, util = BlockDev.fs_can_resize("ext4")
185 self.assertTrue(avail)
186 self.assertEqual(util, None)
187
188 old_path = os.environ.get("PATH", "")
189 os.environ["PATH"] = ""
190 avail, mode, util = BlockDev.fs_can_resize("ext4")
191 os.environ["PATH"] = old_path
192 self.assertFalse(avail)
193 self.assertEqual(util, "resize2fs")
194 self.assertEqual(mode, BlockDev.FSResizeFlags.ONLINE_GROW |
195 BlockDev.FSResizeFlags.OFFLINE_GROW |
196 BlockDev.FSResizeFlags.OFFLINE_SHRINK)
197
198 avail = BlockDev.fs_can_resize("vfat")
199 self.assertTrue(avail)
200
201 with self.assertRaises(GLib.GError):
202 BlockDev.fs_can_resize("non-existing-fs")
203
204 with self.assertRaises(GLib.GError):
205 BlockDev.fs_can_resize("udf")
206
207 def test_can_repair(self):
208 """Verify that tooling query works for repair"""
209
210 avail, util = BlockDev.fs_can_repair("xfs")
211 self.assertTrue(avail)
212 self.assertEqual(util, None)
213
214 old_path = os.environ.get("PATH", "")
215 os.environ["PATH"] = ""
216 avail, util = BlockDev.fs_can_repair("xfs")
217 os.environ["PATH"] = old_path
218 self.assertFalse(avail)
219 self.assertEqual(util, "xfs_repair")
220
221 avail = BlockDev.fs_can_repair("vfat")
222 self.assertTrue(avail)
223
224 with self.assertRaises(GLib.GError):
225 BlockDev.fs_can_repair("nilfs2")
226
227 with self.assertRaises(GLib.GError):
228 BlockDev.fs_can_repair("udf")
229
230 def test_can_check(self):
231 """Verify that tooling query works for consistency check"""
232
233 avail, util = BlockDev.fs_can_check("xfs")
234 self.assertTrue(avail)
235 self.assertEqual(util, None)
236
237 old_path = os.environ.get("PATH", "")
238 os.environ["PATH"] = ""
239 avail, util = BlockDev.fs_can_check("xfs")
240 os.environ["PATH"] = old_path
241 self.assertFalse(avail)
242 self.assertEqual(util, "xfs_db")
243
244 avail = BlockDev.fs_can_check("vfat")
245 self.assertTrue(avail)
246
247 with self.assertRaises(GLib.GError):
248 BlockDev.fs_can_check("nilfs2")
249
250 with self.assertRaises(GLib.GError):
251 BlockDev.fs_can_check("udf")
252
253 def test_can_set_label(self):
254 """Verify that tooling query works for setting the label"""
255
256 avail, util = BlockDev.fs_can_set_label("xfs")
257 self.assertTrue(avail)
258 self.assertEqual(util, None)
259
260 old_path = os.environ.get("PATH", "")
261 os.environ["PATH"] = ""
262 avail, util = BlockDev.fs_can_set_label("xfs")
263 os.environ["PATH"] = old_path
264 self.assertFalse(avail)
265 self.assertEqual(util, "xfs_admin")
266
267 with self.assertRaises(GLib.GError):
268 BlockDev.fs_can_set_label("non-existing-fs")
269
270 def test_can_get_size(self):
271 """Verify that tooling query works for getting size"""
272
273 avail, util = BlockDev.fs_can_get_size("xfs")
274 self.assertTrue(avail)
275 self.assertEqual(util, None)
276
277 old_path = os.environ.get("PATH", "")
278 os.environ["PATH"] = ""
279 avail, util = BlockDev.fs_can_get_size("xfs")
280 os.environ["PATH"] = old_path
281 self.assertFalse(avail)
282 self.assertEqual(util, "xfs_admin")
283
284 with self.assertRaises(GLib.GError):
285 BlockDev.fs_can_get_size("non-existing-fs")
286
287 def test_can_get_free_space(self):
288 """Verify that tooling query works for getting free space"""
289
290 avail, util = BlockDev.fs_can_get_free_space("ext4")
291 self.assertTrue(avail)
292 self.assertEqual(util, None)
293
294 old_path = os.environ.get("PATH", "")
295 os.environ["PATH"] = ""
296 avail, util = BlockDev.fs_can_get_free_space("ext4")
297 os.environ["PATH"] = old_path
298 self.assertFalse(avail)
299 self.assertEqual(util, "dumpe2fs")
300
301 with self.assertRaises(GLib.GError):
302 BlockDev.fs_can_get_free_space("xfs")
303
304 with self.assertRaises(GLib.GError):
305 BlockDev.fs_can_get_free_space("non-existing-fs")
306
307 with self.assertRaises(GLib.GError):
308 BlockDev.fs_can_get_free_space("exfat")
309
310 with self.assertRaises(GLib.GError):
311 BlockDev.fs_can_get_free_space("udf")
312
313 def test_can_get_info(self):
314 """Verify that tooling query works for getting info"""
315
316 avail, util = BlockDev.fs_can_get_info("ext4")
317 self.assertTrue(avail)
318 self.assertEqual(util, None)
319
320 old_path = os.environ.get("PATH", "")
321 os.environ["PATH"] = ""
322 avail, util = BlockDev.fs_can_get_info("ext4")
323 os.environ["PATH"] = old_path
324 self.assertFalse(avail)
325 self.assertEqual(util, "dumpe2fs")
326
327 with self.assertRaises(GLib.GError):
328 BlockDev.fs_can_get_info("non-existing-fs")
329
330 def test_can_get_min_size(self):
331 """Verify that tooling query works for getting min size"""
332
333 avail, util = BlockDev.fs_can_get_min_size("ext4")
334 self.assertTrue(avail)
335 self.assertEqual(util, None)
336
337 old_path = os.environ.get("PATH", "")
338 os.environ["PATH"] = ""
339 avail, util = BlockDev.fs_can_get_min_size("ext4")
340 os.environ["PATH"] = old_path
341 self.assertFalse(avail)
342 self.assertEqual(util, "resize2fs")
343
344 avail, util = BlockDev.fs_can_get_min_size("ntfs")
345 if self.ntfs_avail:
346 self.assertTrue(avail)
347 self.assertEqual(util, None)
348 else:
349 self.assertFalse(avail)
350 self.assertEqual(util, "ntfsresize")
351
352 with self.assertRaises(GLib.GError):
353 BlockDev.fs_can_get_min_size("xfs")
354
355 with self.assertRaises(GLib.GError):
356 BlockDev.fs_can_get_min_size("non-existing-fs")
357
358 with self.assertRaises(GLib.GError):
359 BlockDev.fs_can_get_min_size("exfat")
360
361 with self.assertRaises(GLib.GError):
362 BlockDev.fs_can_get_min_size("udf")
363
364 def test_can_set_uuid(self):
365 """Verify that tooling query works for setting"""
366 avail, util = BlockDev.fs_can_set_uuid("ext4")
367 self.assertTrue(avail)
368 self.assertEqual(util, None)
369
370 old_path = os.environ.get("PATH", "")
371 os.environ["PATH"] = ""
372 avail, util = BlockDev.fs_can_set_uuid("ext4")
373 os.environ["PATH"] = old_path
374 self.assertFalse(avail)
375 self.assertEqual(util, "tune2fs")
376
377 with self.assertRaises(GLib.GError):
378 BlockDev.fs_can_set_uuid("non-existing-fs")
379
380 with self.assertRaises(GLib.GError):
381 BlockDev.fs_can_set_uuid("f2fs")
382
383
384 class GenericMkfs(GenericTestCase):
385
386 def _test_ext_generic_mkfs(self, fsname, info_fn, label=None, uuid=None, force=False, extra=None, default_label=None):
387 # clean the device
388 succ = BlockDev.fs_clean(self.loop_devs[0])
389 self.assertTrue(succ)
390
391 supported, flags, _util = BlockDev.fs_can_mkfs(fsname)
392 if not supported:
393 self.skipTest("skipping %s: not available" % fsname)
394
395 if flags & BlockDev.FSMkfsOptionsFlags.DRY_RUN:
396 # try dry run first
397 options = BlockDev.FSMkfsOptions(None, None, True, False)
398 succ = BlockDev.fs_mkfs(self.loop_devs[0], fsname, options)
399 self.assertTrue(succ)
400
401 fstype = BlockDev.fs_get_fstype (self.loop_devs[0])
402 self.assertIsNone(fstype)
403
404 options = BlockDev.FSMkfsOptions(label, uuid, False, False)
405
406 succ = BlockDev.fs_mkfs(self.loop_devs[0], fsname, options, extra)
407 self.assertTrue(succ)
408
409 fstype = BlockDev.fs_get_fstype (self.loop_devs[0])
410 self.assertEqual(fstype, fsname)
411
412 info = info_fn(self.loop_devs[0])
413 self.assertIsNotNone(info)
414 if label is not None:
415 if label == "":
416 if default_label:
417 self.assertEqual(info.label, default_label)
418 else:
419 self.assertEqual(info.label, "")
420 else:
421 self.assertEqual(info.label, label)
422 if uuid:
423 self.assertEqual(info.uuid, uuid)
424
425 if force:
426 # try overwriting the existing filesystem, should fail without force
427 with self.assertRaises(GLib.GError):
428 BlockDev.fs_mkfs(self.loop_devs[0], fsname)
429
430 # now add the option
431 options = BlockDev.FSMkfsOptions(force=True)
432 succ = BlockDev.fs_mkfs(self.loop_devs[0], fsname, options, extra)
433 self.assertTrue(succ)
434
435 def test_exfat_generic_mkfs(self):
436 """ Test generic mkfs with exFAT """
437 if not self.exfat_avail:
438 self.skipTest("skipping exFAT: not available")
439 label = "label"
440 self._test_ext_generic_mkfs("exfat", BlockDev.fs_exfat_get_info, label, None)
441
442 def test_ext2_generic_mkfs(self):
443 """ Test generic mkfs with ext2 """
444 label = "label"
445 uuid = "8802574c-587b-43b9-a6be-9de77759d2c5"
446 self._test_ext_generic_mkfs("ext2", BlockDev.fs_ext2_get_info, label, uuid, False)
447
448 # clean the device
449 succ = BlockDev.fs_clean(self.loop_devs[0])
450 self.assertTrue(succ)
451
452 options = BlockDev.FSMkfsOptions(label, uuid, False, False)
453
454 # and try with a custom extra arg (we can get block size from the info)
455 extra = BlockDev.ExtraArg("-b", "4096")
456
457 succ = BlockDev.fs_mkfs(self.loop_devs[0], "ext2", options, [extra])
458 self.assertTrue(succ)
459
460 fstype = BlockDev.fs_get_fstype (self.loop_devs[0])
461 self.assertEqual(fstype, "ext2")
462
463 info = BlockDev.fs_ext2_get_info(self.loop_devs[0])
464 self.assertEqual(info.label, label)
465 self.assertEqual(info.uuid, uuid)
466 self.assertEqual(info.block_size, 4096)
467
468 # try with -F, it doesn't really do anything with stdin closed so just a sanity check
469 options = BlockDev.FSMkfsOptions(force=True)
470 succ = BlockDev.fs_mkfs(self.loop_devs[0], "ext2", options)
471 self.assertTrue(succ)
472
473 def test_ext3_generic_mkfs(self):
474 """ Test generic mkfs with ext3 """
475 label = "label"
476 uuid = "8802574c-587b-43b9-a6be-9de77759d2c5"
477 self._test_ext_generic_mkfs("ext3", BlockDev.fs_ext3_get_info, label, uuid, False)
478
479 # now try with empty label and UUID
480 label = ""
481 uuid = ""
482 self._test_ext_generic_mkfs("ext3", BlockDev.fs_ext3_get_info, label, uuid, False)
483
484 def test_ext4_generic_mkfs(self):
485 """ Test generic mkfs with ext4 """
486 label = "label"
487 uuid = "8802574c-587b-43b9-a6be-9de77759d2c5"
488 self._test_ext_generic_mkfs("ext4", BlockDev.fs_ext4_get_info, label, uuid, False)
489
490 # now try with empty label and UUID
491 label = ""
492 uuid = ""
493 self._test_ext_generic_mkfs("ext4", BlockDev.fs_ext4_get_info, label, uuid, False)
494
495 def test_f2fs_generic_mkfs(self):
496 """ Test generic mkfs with F2FS """
497 label = "label"
498 self._test_ext_generic_mkfs("f2fs", BlockDev.fs_f2fs_get_info, label, None, True)
499
500 # now try with empty label
501 label = ""
502 self._test_ext_generic_mkfs("f2fs", BlockDev.fs_f2fs_get_info, label, None, True)
503
504 def test_nilfs2_generic_mkfs(self):
505 """ Test generic mkfs with nilfs2 """
506 if not self.nilfs2_avail:
507 self.skipTest("skipping NILFS2: not available")
508 label = "label"
509 self._test_ext_generic_mkfs("nilfs2", BlockDev.fs_nilfs2_get_info, label, None, True)
510
511 # now try with empty label
512 label = ""
513 self._test_ext_generic_mkfs("nilfs2", BlockDev.fs_nilfs2_get_info, label, None, True)
514
515 def test_ntfs_generic_mkfs(self):
516 """ Test generic mkfs with NTFS """
517 if not self.ntfs_avail:
518 self.skipTest("skipping NTFS: not available")
519 label = "label"
520 self._test_ext_generic_mkfs("ntfs", BlockDev.fs_ntfs_get_info, label, None)
521
522 # now try with empty label
523 label = ""
524 self._test_ext_generic_mkfs("ntfs", BlockDev.fs_ntfs_get_info, label, None)
525
526 def test_vfat_generic_mkfs(self):
527 """ Test generic mkfs with vfat """
528 label = "LABEL"
529 if self._vfat_version >= Version("4.2"):
530 extra = [BlockDev.ExtraArg.new("--mbr=n", "")]
531 else:
532 extra = None
533 self._test_ext_generic_mkfs("vfat", BlockDev.fs_vfat_get_info, label, None, False, extra)
534
535 # now try with empty label
536 label = ""
537 self._test_ext_generic_mkfs("vfat", BlockDev.fs_vfat_get_info, label, None, False, extra)
538
539 # now try with UUID
540 self._test_ext_generic_mkfs("vfat", BlockDev.fs_vfat_get_info, None, "2E24-EC82", False, extra)
541
542 def test_vfat_generic_mkfs_no_pt(self):
543 """ Test generic mkfs with vfat and mbr=no """
544
545 options = BlockDev.FSMkfsOptions(no_pt=True)
546
547 succ = BlockDev.fs_mkfs(self.loop_devs[0], "vfat", options, None)
548 self.assertTrue(succ)
549
550 fstype = BlockDev.fs_get_fstype (self.loop_devs[0])
551 self.assertEqual(fstype, "vfat")
552
553 # there should be no partition
554 self.assertFalse(os.path.exists(self.loop_devs[0] + "1"))
555
556 def test_xfs_generic_mkfs(self):
557 """ Test generic mkfs with XFS """
558 label = "label"
559 uuid = "8802574c-587b-43b9-a6be-9de77759d2c5"
560
561 def _xfs_info(device):
562 with mounted(device, self.mount_dir):
563 info = BlockDev.fs_xfs_get_info(device)
564 return info
565
566 self._test_ext_generic_mkfs("xfs", _xfs_info, label, uuid, True)
567
568 # now try with empty label and UUID
569 label = ""
570 uuid = ""
571 self._test_ext_generic_mkfs("xfs", _xfs_info, label, uuid, True)
572
573 def test_btrfs_generic_mkfs(self):
574 """ Test generic mkfs with Btrfs """
575 if not self.btrfs_avail:
576 self.skipTest("skipping Btrfs: not available")
577 label = "label"
578 uuid = "8802574c-587b-43b9-a6be-9de77759d2c5"
579
580 def _btrfs_info(device):
581 with mounted(device, self.mount_dir):
582 info = BlockDev.fs_btrfs_get_info(self.mount_dir)
583 return info
584
585 self._test_ext_generic_mkfs("btrfs", _btrfs_info, label, uuid, True)
586
587 # now try with empty label and UUID
588 label = ""
589 uuid = ""
590 self._test_ext_generic_mkfs("btrfs", _btrfs_info, label, uuid, True)
591
592 def test_udf_generic_mkfs(self):
593 """ Test generic mkfs with udf """
594 if not self.udf_avail:
595 self.skipTest("skipping UDF: not available")
596 label = "LABEL"
597 self._test_ext_generic_mkfs("udf", BlockDev.fs_udf_get_info, label, None)
598
599 # now try with empty label
600 label = ""
601 self._test_ext_generic_mkfs("udf", BlockDev.fs_udf_get_info, label, None, default_label="LinuxUDF")
602
603 def test_generic_mkfs_no_options(self):
604 """ Test that fs_mkfs works without options specified """
605 succ = BlockDev.fs_mkfs(self.loop_devs[0], "ext2")
606 self.assertTrue(succ)
607
608 info = BlockDev.fs_ext2_get_info(self.loop_devs[0])
609 self.assertIsNotNone(info)
610 self.assertFalse(info.label) # label should be empty by default
611
612 def test_fail_generic_mkfs(self):
613 """ Test that generic mkfs fails correctly with unknown/unsupported filesystem """
614
615 with self.assertRaisesRegex(GLib.GError, "Filesystem 'non-existing-fs' is not supported"):
616 BlockDev.fs_mkfs(self.loop_devs[0], "non-existing-fs")
617
618 def test_can_mkfs(self):
619 """ Test checking whether mkfs is supported """
620 # lets pick a filesystem that supports everything and is always available
621 # in the CI
622 supported, flags, _util = BlockDev.fs_can_mkfs("ext2")
623 self.assertTrue(supported)
624 self.assertTrue(flags & BlockDev.FSMkfsOptionsFlags.DRY_RUN)
625 self.assertTrue(flags & BlockDev.FSMkfsOptionsFlags.LABEL)
626 self.assertTrue(flags & BlockDev.FSMkfsOptionsFlags.UUID)
627 self.assertTrue(flags & BlockDev.FSMkfsOptionsFlags.NODISCARD)
628 self.assertTrue(flags & BlockDev.FSMkfsOptionsFlags.FORCE)
629
630
631 class GenericCheck(GenericTestCase):
632 log = []
633
634 def _test_generic_check(self, mkfs_function, fstype=None):
635 # clean the device
636 succ = BlockDev.fs_clean(self.loop_devs[0])
637
638 succ = mkfs_function(self.loop_devs[0], None)
639 self.assertTrue(succ)
640
641 self.log = []
642 # check for consistency (expected to be ok)
643 succ = BlockDev.fs_check(self.loop_devs[0])
644 self.assertTrue(succ)
645
646 if fstype:
647 succ = BlockDev.fs_check(self.loop_devs[0], fstype)
648 self.assertTrue(succ)
649
650 def _my_progress_func(self, task, status, completion, msg):
651 self.assertTrue(isinstance(completion, int))
652 self.log.append(completion)
653
654 def _verify_progress(self, log):
655 # at least 2 members
656 self.assertLessEqual(2, len(log))
657 # non-decreasing members
658 self.assertTrue(all(x<=y for x, y in zip(log, log[1:])))
659
660 def test_ext4_generic_check(self):
661 """Test generic check function with an ext4 file system"""
662 self._test_generic_check(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
663
664 def test_ext4_progress_check(self):
665 """Test check function with an ext4 file system and progress reporting"""
666
667 succ = BlockDev.utils_init_prog_reporting(self._my_progress_func)
668 self.assertTrue(succ)
669
670 self._test_generic_check(mkfs_function=BlockDev.fs_ext4_mkfs)
671 self._verify_progress(self.log)
672
673 succ = BlockDev.utils_init_prog_reporting(None)
674
675 def test_xfs_generic_check(self):
676 """Test generic check function with an xfs file system"""
677 self._test_generic_check(mkfs_function=BlockDev.fs_xfs_mkfs, fstype="xfs")
678
679 def test_ntfs_generic_check(self):
680 """Test generic check function with an ntfs file system"""
681 if not self.ntfs_avail:
682 self.skipTest("skipping NTFS: not available")
683 self._test_generic_check(mkfs_function=BlockDev.fs_ntfs_mkfs, fstype="ntfs")
684
685 def _check_fsck_f2fs_version(self):
686 # if it can run -V to get version it can do the check
687 ret, _out, _err = utils.run_command("fsck.f2fs -V")
688 return ret == 0
689
690 def test_f2fs_generic_check(self):
691 """Test generic check function with an f2fs file system"""
692 if not self.f2fs_avail:
693 self.skipTest("skipping F2FS: not available")
694 if not self._check_fsck_f2fs_version():
695 with self.assertRaisesRegex(GLib.GError, "Too low version of fsck.f2fs. At least 1.11.0 required."):
696 self._test_generic_check(mkfs_function=BlockDev.fs_f2fs_mkfs, fstype="f2fs")
697 else:
698 self._test_generic_check(mkfs_function=BlockDev.fs_f2fs_mkfs, fstype="f2fs")
699
700 def test_nilfs2_generic_check(self):
701 """Test generic check function with an nilfs2 file system"""
702 if not self.nilfs2_avail:
703 self.skipTest("skipping NILFS2: not available")
704 with self.assertRaises(GLib.GError):
705 # nilfs2 doesn't support check
706 self._test_generic_check(mkfs_function=BlockDev.fs_nilfs2_mkfs, fstype="nilfs2")
707
708 def test_exfat_generic_check(self):
709 """Test generic check function with an exfat file system"""
710 if not self.exfat_avail:
711 self.skipTest("skipping exFAT: not available")
712 self._test_generic_check(mkfs_function=BlockDev.fs_exfat_mkfs, fstype="exfat")
713
714 def test_btrfs_generic_check(self):
715 """Test generic check function with an btrfs file system"""
716 if not self.btrfs_avail:
717 self.skipTest("skipping Btrfs: not available")
718 self._test_generic_check(mkfs_function=BlockDev.fs_btrfs_mkfs, fstype="btrfs")
719
720
721 class GenericRepair(GenericTestCase):
722 def _test_generic_repair(self, mkfs_function, fstype):
723 # clean the device
724 succ = BlockDev.fs_clean(self.loop_devs[0])
725
726 succ = mkfs_function(self.loop_devs[0], None)
727 self.assertTrue(succ)
728
729 # repair (expected to succeed)
730 succ = BlockDev.fs_repair(self.loop_devs[0])
731 self.assertTrue(succ)
732
733 # repair (expected to succeed)
734 succ = BlockDev.fs_repair(self.loop_devs[0], fstype)
735 self.assertTrue(succ)
736
737 def test_ext4_generic_repair(self):
738 """Test generic repair function with an ext4 file system"""
739 self._test_generic_repair(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
740
741 def test_xfs_generic_repair(self):
742 """Test generic repair function with an xfs file system"""
743 self._test_generic_repair(mkfs_function=BlockDev.fs_xfs_mkfs, fstype="xfs")
744
745 def test_ntfs_generic_repair(self):
746 """Test generic repair function with an ntfs file system"""
747 if not self.ntfs_avail:
748 self.skipTest("skipping NTFS: not available")
749 self._test_generic_repair(mkfs_function=BlockDev.fs_ntfs_mkfs, fstype="ntfs")
750
751 def test_f2fs_generic_repair(self):
752 """Test generic repair function with an f2fs file system"""
753 if not self.f2fs_avail:
754 self.skipTest("skipping F2FS: not available")
755 self._test_generic_repair(mkfs_function=BlockDev.fs_f2fs_mkfs, fstype="f2fs")
756
757 def test_nilfs2_generic_repair(self):
758 """Test generic repair function with an nilfs2 file system"""
759 if not self.nilfs2_avail:
760 self.skipTest("skipping NILFS2: not available")
761 with self.assertRaises(GLib.GError):
762 # nilfs2 doesn't support repair
763 self._test_generic_repair(mkfs_function=BlockDev.fs_nilfs2_mkfs, fstype="nilfs2")
764
765 def test_exfat_generic_repair(self):
766 """Test generic repair function with an exfat file system"""
767 if not self.exfat_avail:
768 self.skipTest("skipping exFAT: not available")
769 self._test_generic_repair(mkfs_function=BlockDev.fs_exfat_mkfs, fstype="exfat")
770
771 def test_btrfs_generic_repair(self):
772 """Test generic repair function with an btrfs file system"""
773 if not self.btrfs_avail:
774 self.skipTest("skipping Btrfs: not available")
775 self._test_generic_repair(mkfs_function=BlockDev.fs_btrfs_mkfs, fstype="btrfs")
776
777
778 class GenericSetLabel(GenericTestCase):
779 def _test_generic_set_label(self, mkfs_function, fstype, test_label="new_label", expected_label="new_label", invalid_label=513*"a"):
780 # clean the device
781 succ = BlockDev.fs_clean(self.loop_devs[0])
782
783 succ = mkfs_function(self.loop_devs[0], None)
784 self.assertTrue(succ)
785
786 succ = BlockDev.fs_check_label(fstype, test_label)
787 self.assertTrue(succ)
788
789 with self.assertRaises(GLib.GError):
790 BlockDev.fs_check_label(fstype, invalid_label)
791
792 # set label (expected to succeed)
793 succ = BlockDev.fs_set_label(self.loop_devs[0], test_label)
794 self.assertTrue(succ)
795
796 fs_label = check_output(["blkid", "-ovalue", "-sLABEL", "-p", self.loop_devs[0]]).decode().strip()
797 self.assertEqual(fs_label, expected_label)
798
799 # set label (expected to succeed)
800 succ = BlockDev.fs_set_label(self.loop_devs[0], test_label, fstype)
801 self.assertTrue(succ)
802
803 def test_ext4_generic_set_label(self):
804 """Test generic set_label function with an ext4 file system"""
805 self._test_generic_set_label(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
806
807 def test_xfs_generic_set_label(self):
808 """Test generic set_label function with a xfs file system"""
809 self._test_generic_set_label(mkfs_function=BlockDev.fs_xfs_mkfs, fstype="xfs")
810
811 def test_ntfs_generic_set_label(self):
812 """Test generic set_label function with a ntfs file system"""
813 if not self.ntfs_avail:
814 self.skipTest("skipping NTFS: not available")
815 self._test_generic_set_label(mkfs_function=BlockDev.fs_ntfs_mkfs, fstype="ntfs")
816
817 def test_f2fs_generic_set_label(self):
818 """Test generic set_label function with a f2fs file system"""
819 if not self.f2fs_avail:
820 self.skipTest("skipping F2FS: not available")
821 with self.assertRaises(GLib.GError):
822 # f2fs doesn't support relabeling
823 self._test_generic_set_label(mkfs_function=BlockDev.fs_f2fs_mkfs, fstype="f2fs")
824
825 def test_nilfs2_generic_set_label(self):
826 """Test generic set_label function with a nilfs2 file system"""
827 if not self.nilfs2_avail:
828 self.skipTest("skipping NILFS2: not available")
829 self._test_generic_set_label(mkfs_function=BlockDev.fs_nilfs2_mkfs, fstype="nilfs2")
830
831 def test_exfat_generic_set_label(self):
832 """Test generic set_label function with a exfat file system"""
833 if not self.exfat_avail:
834 self.skipTest("skipping exFAT: not available")
835 self._test_generic_set_label(mkfs_function=BlockDev.fs_exfat_mkfs, fstype="exfat")
836
837 def test_btrfs_generic_set_label(self):
838 """Test generic set_label function with a btrfs file system"""
839 if not self.btrfs_avail:
840 self.skipTest("skipping btrfs: not available")
841 self._test_generic_set_label(mkfs_function=BlockDev.fs_btrfs_mkfs, fstype="btrfs")
842
843 def test_udf_generic_set_label(self):
844 """Test generic set_label function with a udf file system"""
845 if not self.udf_avail:
846 self.skipTest("skipping udf: not available")
847 self._test_generic_set_label(mkfs_function=BlockDev.fs_udf_mkfs, fstype="udf")
848
849 def test_vfat_generic_set_label(self):
850 """Test generic set_label function with a vfat file system"""
851 if self._vfat_version < Version("4.2"):
852 self.skipTest("dosfstools >= 4.2 needed to set label")
853
854 def mkfs_vfat(device, options=None):
855 if self._vfat_version >= Version("4.2"):
856 return BlockDev.fs_vfat_mkfs(device, [BlockDev.ExtraArg.new("--mbr=n", "")])
857 else:
858 return BlockDev.fs_vfat_mkfs(device, options)
859
860 self._test_generic_set_label(mkfs_function=mkfs_vfat, fstype="vfat", expected_label="NEW_LABEL")
861
862 def test_fail_generic_check_label(self):
863 """ Test that generic check label fails correctly with unknown/unsupported filesystem """
864
865 with self.assertRaisesRegex(GLib.GError, "Checking label format for filesystem 'non-existing-fs' is not supported"):
866 BlockDev.fs_check_label("non-existing-fs", "label")
867
868 with self.assertRaisesRegex(GLib.GError, "Filesystem type must be specified to check label format"):
869 BlockDev.fs_check_label("", "label")
870
871
872 class GenericSetUUID(GenericTestCase):
873 def _test_generic_set_uuid(self, mkfs_function, fstype, test_uuid="4d7086c4-a4d3-432f-819e-73da03870df9", expected_uuid=None):
874 # clean the device
875 succ = BlockDev.fs_clean(self.loop_devs[0])
876
877 succ = mkfs_function(self.loop_devs[0], None)
878 self.assertTrue(succ)
879
880 # set uuid (expected to succeed)
881 succ = BlockDev.fs_set_uuid(self.loop_devs[0], test_uuid)
882 self.assertTrue(succ)
883
884 fs_uuid = check_output(["blkid", "-ovalue", "-sUUID", "-p", self.loop_devs[0]]).decode().strip()
885 if expected_uuid:
886 self.assertEqual(fs_uuid, expected_uuid)
887 else:
888 self.assertEqual(fs_uuid, test_uuid)
889
890 # set empty/random UUID
891 succ = BlockDev.fs_set_uuid(self.loop_devs[0], None, fstype)
892 self.assertTrue(succ)
893
894 # check uuid format
895 succ = BlockDev.fs_check_uuid(fstype, test_uuid)
896 self.assertTrue(succ)
897
898 def test_ext4_generic_set_uuid(self):
899 """Test generic set_uuid function with an ext4 file system"""
900 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
901
902 def test_xfs_generic_set_uuid(self):
903 """Test generic set_uuid function with a xfs file system"""
904 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_xfs_mkfs, fstype="xfs")
905
906 def test_ntfs_generic_set_uuid(self):
907 """Test generic set_uuid function with a ntfs file system"""
908 if not self.ntfs_avail:
909 self.skipTest("skipping NTFS: not available")
910 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_ntfs_mkfs, fstype="ntfs", test_uuid="1C2716ED53F63962")
911
912 def test_vfat_generic_set_uuid(self):
913 """Test generic set_uuid function with a vfat file system"""
914 if self._vfat_version < Version("4.2"):
915 self.skipTest("dosfstools >= 4.2 needed to set UUID")
916
917 def mkfs_vfat(device, options=None):
918 if self._vfat_version >= Version("4.2"):
919 return BlockDev.fs_vfat_mkfs(device, [BlockDev.ExtraArg.new("--mbr=n", "")])
920 else:
921 return BlockDev.fs_vfat_mkfs(device, options)
922
923 self._test_generic_set_uuid(mkfs_function=mkfs_vfat, fstype="vfat", test_uuid="2E24EC82",
924 expected_uuid="2E24-EC82")
925
926 def test_f2fs_generic_set_uuid(self):
927 """Test generic set_uuid function with a f2fs file system"""
928 if not self.f2fs_avail:
929 self.skipTest("skipping F2FS: not available")
930 with self.assertRaises(GLib.GError):
931 # f2fs doesn't support setting UUID
932 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_f2fs_mkfs, fstype="f2fs")
933
934 def test_nilfs2_generic_set_uuid(self):
935 """Test generic set_uuid function with a nilfs2 file system"""
936 if not self.nilfs2_avail:
937 self.skipTest("skipping NILFS2: not available")
938 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_nilfs2_mkfs, fstype="nilfs2")
939
940 def test_exfat_generic_set_uuid(self):
941 """Test generic set_uuid function with a exfat file system"""
942 if not self.exfat_avail:
943 self.skipTest("skipping exFAT: not available")
944 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_exfat_mkfs, fstype="exfat", test_uuid="2E24EC82",
945 expected_uuid="2E24-EC82")
946
947 def test_btrfs_generic_set_uuid(self):
948 """Test generic set_uuid function with a btrfs file system"""
949 if not self.btrfs_avail:
950 self.skipTest("skipping Btrfs: not available")
951 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_btrfs_mkfs, fstype="btrfs")
952
953 def test_udf_generic_set_uuid(self):
954 """Test generic set_uuid function with a udf file system"""
955 if not self.udf_avail:
956 self.skipTest("skipping UDF: not available")
957 self._test_generic_set_uuid(mkfs_function=BlockDev.fs_udf_mkfs, fstype="udf", test_uuid="5fae9ade7938dfc8")
958
959 def test_fail_generic_check_uuid(self):
960 """ Test that generic check_uuid fails correctly with unknown/unsupported filesystem """
961
962 with self.assertRaisesRegex(GLib.GError, "Checking UUID format for filesystem 'non-existing-fs' is not supported"):
963 BlockDev.fs_check_uuid("non-existing-fs", "uuid")
964
965 with self.assertRaisesRegex(GLib.GError, "Filesystem type must be specified to check UUID format"):
966 BlockDev.fs_check_uuid("", "uuid")
967
968
969 class GenericResize(GenericTestCase):
970 log = ""
971
972 def my_log_func(self, level, msg):
973 # not much to verify here
974 self.assertTrue(isinstance(level, int))
975 self.assertTrue(isinstance(msg, str))
976
977 self.log += msg + "\n"
978
979 def _test_generic_resize(self, mkfs_function, fstype, size_delta=0, min_size=130*1024**2):
980 # clean the device
981 succ = BlockDev.fs_clean(self.loop_devs[0])
982
983 succ = mkfs_function(self.loop_devs[0], None)
984 self.assertTrue(succ)
985 size = BlockDev.fs_get_size(self.loop_devs[0])
986
987 # shrink
988 succ = BlockDev.fs_resize(self.loop_devs[0], min_size)
989 self.assertTrue(succ)
990 new_size = BlockDev.fs_get_size(self.loop_devs[0])
991 self.assertAlmostEqual(new_size, min_size, delta=size_delta)
992
993 # resize to maximum size
994 succ = BlockDev.fs_resize(self.loop_devs[0], 0, fstype)
995 self.assertTrue(succ)
996 new_size = BlockDev.fs_get_size(self.loop_devs[0])
997 # should be back to original size
998 self.assertAlmostEqual(new_size, size, delta=size_delta)
999
1000 def test_ext2_generic_resize(self):
1001 """Test generic resize function with an ext2 file system"""
1002 self._test_generic_resize(mkfs_function=BlockDev.fs_ext2_mkfs, fstype="ext2")
1003
1004 def test_ext3_check_generic_resize(self):
1005 """Test generic resize function with an ext3 file system"""
1006 self._test_generic_resize(mkfs_function=BlockDev.fs_ext3_mkfs, fstype="ext3")
1007
1008 def test_ext4_generic_resize(self):
1009 """Test generic resize function with an ext4 file system"""
1010 self._test_generic_resize(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
1011
1012 def test_ntfs_generic_resize(self):
1013 """Test generic resize function with an ntfs file system"""
1014 if not self.ntfs_avail:
1015 self.skipTest("skipping NTFS: not available")
1016 def mkfs_prepare(drive):
1017 return BlockDev.fs_ntfs_mkfs(drive, None) and BlockDev.fs_repair(drive)
1018 def ntfs_size(drive):
1019 # man ntfsresize says "The filesystem size is set to be at least one
1020 # sector smaller" (maybe depending on alignment as well?), thus on a
1021 # loop device as in this test it is 4096 bytes smaller than requested
1022 BlockDev.fs_repair(drive)
1023 fi = BlockDev.fs_ntfs_get_info(drive)
1024 return fi.size + 4096
1025
1026 # clean the device
1027 succ = BlockDev.fs_clean(self.loop_devs[0])
1028
1029 succ = mkfs_prepare(self.loop_devs[0])
1030 self.assertTrue(succ)
1031
1032 size = ntfs_size(self.loop_devs[0])
1033
1034 # shrink
1035 succ = BlockDev.fs_resize(self.loop_devs[0], 80 * 1024**2)
1036 self.assertTrue(succ)
1037 new_size = ntfs_size(self.loop_devs[0])
1038 self.assertEqual(new_size, 80 * 1024**2)
1039
1040 # resize to maximum size
1041 succ = BlockDev.fs_resize(self.loop_devs[0], 0, "ntfs")
1042 self.assertTrue(succ)
1043 new_size = ntfs_size(self.loop_devs[0])
1044 # should be back to original size
1045 self.assertEqual(new_size, size)
1046
1047 @tag_test(TestTags.UNSTABLE)
1048 @utils.required_plugins(("tools",))
1049 def test_vfat_generic_resize(self):
1050 """Test generic resize function with a vfat file system"""
1051 def mkfs_vfat(device, options=None):
1052 if self._vfat_version >= Version("4.2"):
1053 if options:
1054 return BlockDev.fs_vfat_mkfs(device, options + [BlockDev.ExtraArg.new("--mbr=n", "")])
1055 else:
1056 return BlockDev.fs_vfat_mkfs(device, [BlockDev.ExtraArg.new("--mbr=n", "")])
1057 else:
1058 return BlockDev.fs_vfat_mkfs(device, options)
1059
1060 self._test_generic_resize(mkfs_function=mkfs_vfat, size_delta=1024**2, fstype="vfat")
1061
1062 def _test_xfs_generic_resize(self, mount):
1063 lv = self._setup_lvm(vgname="libbd_fs_tests", lvname="generic_test", lvsize="350M")
1064
1065 succ = BlockDev.fs_xfs_mkfs(lv, None)
1066 self.assertTrue(succ)
1067
1068 if mount:
1069 with mounted(lv, self.mount_dir):
1070 fi = BlockDev.fs_xfs_get_info(lv)
1071 else:
1072 fi = BlockDev.fs_xfs_get_info(lv)
1073 self.assertTrue(fi)
1074 self.assertEqual(fi.block_size * fi.block_count, 350 * 1024**2)
1075
1076 # no change, nothing should happen
1077 if mount:
1078 with mounted(lv, self.mount_dir):
1079 succ = BlockDev.fs_resize(lv, 0)
1080 else:
1081 succ = BlockDev.fs_resize(lv, 0)
1082 self.assertTrue(succ)
1083
1084 if mount:
1085 with mounted(lv, self.mount_dir):
1086 fi = BlockDev.fs_xfs_get_info(lv)
1087 else:
1088 fi = BlockDev.fs_xfs_get_info(lv)
1089 self.assertTrue(fi)
1090 self.assertEqual(fi.block_size * fi.block_count, 350 * 1024**2)
1091
1092 # (still) impossible to shrink an XFS file system
1093 xfs_version = self._get_xfs_version()
1094 if xfs_version < Version("5.12"):
1095 with mounted(lv, self.mount_dir):
1096 with self.assertRaises(GLib.GError):
1097 succ = BlockDev.fs_resize(lv, 40 * 1024**2)
1098
1099 self._lvresize("libbd_fs_tests", "generic_test", "380M")
1100 # should grow to 380 MiB (full size of the LV)
1101 if mount:
1102 with mounted(lv, self.mount_dir):
1103 succ = BlockDev.fs_resize(lv, 0)
1104 else:
1105 succ = BlockDev.fs_resize(lv, 0)
1106 self.assertTrue(succ)
1107 if mount:
1108 with mounted(lv, self.mount_dir):
1109 fi = BlockDev.fs_xfs_get_info(lv)
1110 else:
1111 fi = BlockDev.fs_xfs_get_info(lv)
1112 self.assertTrue(fi)
1113 self.assertEqual(fi.block_size * fi.block_count, 380 * 1024**2)
1114
1115 self._lvresize("libbd_fs_tests", "generic_test", "400M")
1116 # grow just to 390 MiB
1117 if mount:
1118 with mounted(lv, self.mount_dir):
1119 succ = BlockDev.fs_resize(lv, 390 * 1024**2)
1120 else:
1121 succ = BlockDev.fs_resize(lv, 390 * 1024**2)
1122 self.assertTrue(succ)
1123 if mount:
1124 with mounted(lv, self.mount_dir):
1125 fi = BlockDev.fs_xfs_get_info(lv)
1126 else:
1127 fi = BlockDev.fs_xfs_get_info(lv)
1128 self.assertTrue(fi)
1129 self.assertEqual(fi.block_size * fi.block_count, 390 * 1024**2)
1130
1131 # should grow to 400 MiB (full size of the LV)
1132 if mount:
1133 with mounted(lv, self.mount_dir):
1134 succ = BlockDev.fs_resize(lv, 0, "xfs")
1135 else:
1136 succ = BlockDev.fs_resize(lv, 0, "xfs")
1137 self.assertTrue(succ)
1138 if mount:
1139 with mounted(lv, self.mount_dir):
1140 fi = BlockDev.fs_xfs_get_info(lv)
1141 else:
1142 fi = BlockDev.fs_xfs_get_info(lv)
1143 self.assertTrue(fi)
1144 self.assertEqual(fi.block_size * fi.block_count, 400 * 1024**2)
1145
1146 self._lvresize("libbd_fs_tests", "generic_test", "420M")
1147 # unmounted resize (should get automounted)
1148 succ = BlockDev.fs_resize(lv, 0, "xfs")
1149 self.assertTrue(succ)
1150 if mount:
1151 with mounted(lv, self.mount_dir):
1152 fi = BlockDev.fs_xfs_get_info(lv)
1153 else:
1154 fi = BlockDev.fs_xfs_get_info(lv)
1155 self.assertTrue(fi)
1156 self.assertEqual(fi.block_size * fi.block_count, 420 * 1024**2)
1157
1158 # enable XFS quotas
1159 with mounted(lv, self.mount_dir, options="uquota,gquota,pquota"):
1160 pass
1161
1162 self._lvresize("libbd_fs_tests", "generic_test", "450M")
1163 succ = BlockDev.utils_init_logging(self.my_log_func)
1164 self.assertTrue(succ)
1165 BlockDev.utils_set_log_level(BlockDev.UTILS_LOG_INFO)
1166
1167 succ = BlockDev.fs_resize(lv, 0, "xfs")
1168 self.assertTrue(succ)
1169 if mount:
1170 with mounted(lv, self.mount_dir):
1171 fi = BlockDev.fs_xfs_get_info(lv)
1172 else:
1173 fi = BlockDev.fs_xfs_get_info(lv)
1174 self.assertTrue(fi)
1175 self.assertEqual(fi.block_size * fi.block_count, 450 * 1024**2)
1176
1177 # quota options should be preserved during temp mount
1178 self.assertIn("with options: uquota,gquota,pquota", self.log)
1179
1180 BlockDev.utils_init_logging(None)
1181
1182 def test_xfs_generic_resize(self):
1183 """Test generic resize function with an xfs file system with automatic mounting"""
1184 # FS is not mounted by the test, libblockdev should mount it when needed
1185 self._test_xfs_generic_resize(mount=False)
1186
1187 def test_xfs_generic_resize_mounted(self):
1188 """Test generic resize function with an xfs file system with manual mounting"""
1189 # FS is mounted by the test, libblockdev should use the existing mountpoint
1190 self._test_xfs_generic_resize(mount=True)
1191
1192 def _can_resize_f2fs(self):
1193 ret, out, _err = utils.run_command("resize.f2fs -V")
1194 if ret != 0:
1195 # we can't even check the version
1196 return False
1197
1198 m = re.search(r"resize.f2fs ([\d\.]+)", out)
1199 if not m or len(m.groups()) != 1:
1200 raise RuntimeError("Failed to determine f2fs version from: %s" % out)
1201 version = Version(m.groups()[0])
1202 # XXX resize works with f2fs-tools 1.15 but dump doesn't
1203 return version >= Version("1.12.0") and version < Version("1.15.0")
1204
1205 def test_f2fs_generic_resize(self):
1206 """Verify that it is possible to resize an f2fs file system"""
1207 if not self.f2fs_avail:
1208 self.skipTest("skipping F2FS: not available")
1209 if not self._can_resize_f2fs():
1210 self.skipTest("skipping F2FS: f2fs-tools version doesn't support resizing")
1211 else:
1212 self._test_generic_resize(mkfs_function=BlockDev.fs_f2fs_mkfs, fstype="f2fs")
1213
1214 def test_nilfs2_generic_resize(self):
1215 """Test generic resize function with an nilfs2 file system"""
1216 if not self.nilfs2_avail:
1217 self.skipTest("skipping NILFS2: not available")
1218 self._test_generic_resize(mkfs_function=BlockDev.fs_nilfs2_mkfs, fstype="nilfs2")
1219
1220 def test_exfat_generic_resize(self):
1221 """Test generic resize function with an exfat file system"""
1222 if not self.exfat_avail:
1223 self.skipTest("skipping exFAT: not available")
1224
1225 # clean the device
1226 succ = BlockDev.fs_clean(self.loop_devs[0])
1227
1228 succ = BlockDev.fs_exfat_mkfs(self.loop_devs[0], None)
1229 self.assertTrue(succ)
1230
1231 # no resize support for exFAT
1232 with self.assertRaisesRegex(GLib.GError, "Resizing filesystem 'exfat' is not supported."):
1233 BlockDev.fs_resize(self.loop_devs[0], 80 * 1024**2)
1234
1235 def test_btrfs_generic_resize(self):
1236 """Test generic resize function with an btrfs file system"""
1237 if not self.btrfs_avail:
1238 self.skipTest("skipping Btrfs: not available")
1239
1240 temps1 = set([temp for temp in os.listdir("/tmp") if temp.startswith("blockdev.")])
1241 self._test_generic_resize(mkfs_function=BlockDev.fs_btrfs_mkfs, min_size=300*1024**2, fstype="btrfs")
1242 temps2 = set([temp for temp in os.listdir("/tmp") if temp.startswith("blockdev.")])
1243
1244 # make sure we didn't leak a temporary mount directory
1245 self.assertFalse(temps2 - temps1)
1246
1247 def test_btrfs_generic_resize_mounted(self):
1248 """Test generic resize function with a mounted btrfs file system"""
1249 if not self.btrfs_avail:
1250 self.skipTest("skipping Btrfs: not available")
1251
1252 # clean the device
1253 succ = BlockDev.fs_clean(self.loop_devs[0])
1254
1255 succ = BlockDev.fs_btrfs_mkfs(self.loop_devs[0], None)
1256 self.assertTrue(succ)
1257 size = BlockDev.fs_get_size(self.loop_devs[0])
1258
1259 with mounted(self.loop_devs[0], self.mount_dir):
1260 # shrink
1261 succ = BlockDev.fs_resize(self.loop_devs[0], 300*1024**2)
1262 self.assertTrue(succ)
1263 new_size = BlockDev.fs_get_size(self.loop_devs[0])
1264 self.assertAlmostEqual(new_size, 300*1024**2)
1265
1266 # resize to maximum size
1267 succ = BlockDev.fs_resize(self.loop_devs[0], 0, "btrfs")
1268 self.assertTrue(succ)
1269 new_size = BlockDev.fs_get_size(self.loop_devs[0])
1270 # should be back to original size
1271 self.assertAlmostEqual(new_size, size)
1272
1273 def test_udf_generic_resize(self):
1274 """Test generic resize function with an udf file system"""
1275 if not self.udf_avail:
1276 self.skipTest("skipping UDF: not available")
1277
1278 # clean the device
1279 succ = BlockDev.fs_clean(self.loop_devs[0])
1280
1281 succ = BlockDev.fs_udf_mkfs(self.loop_devs[0], None)
1282 self.assertTrue(succ)
1283
1284 # no resize support for UDF
1285 with self.assertRaisesRegex(GLib.GError, "Resizing filesystem 'udf' is not supported."):
1286 BlockDev.fs_resize(self.loop_devs[0], 80 * 1024**2)
1287
1288
1289 class GenericGetFreeSpace(GenericTestCase):
1290 def _test_get_free_space(self, mkfs_function, fstype, size_delta=0):
1291 # clean the device
1292 succ = BlockDev.fs_clean(self.loop_devs[0])
1293
1294 succ = mkfs_function(self.loop_devs[0], None)
1295 self.assertTrue(succ)
1296 size = BlockDev.fs_get_size(self.loop_devs[0])
1297 free = BlockDev.fs_get_free_space(self.loop_devs[0])
1298 self.assertNotEqual(free, 0)
1299 self.assertLessEqual(free, size)
1300
1301 size = BlockDev.fs_get_size(self.loop_devs[0], fstype)
1302 free = BlockDev.fs_get_free_space(self.loop_devs[0], fstype)
1303 self.assertNotEqual(free, 0)
1304 self.assertLessEqual(free, size)
1305
1306 def test_ext2_get_free_space(self):
1307 """Test generic get_free_space function with an ext2 file system"""
1308 self._test_get_free_space(mkfs_function=BlockDev.fs_ext2_mkfs, fstype="ext2")
1309
1310 def test_ext3_check_get_free_space(self):
1311 """Test generic get_free_space function with an ext3 file system"""
1312 self._test_get_free_space(mkfs_function=BlockDev.fs_ext3_mkfs, fstype="ext3")
1313
1314 def test_ext4_get_free_space(self):
1315 """Test generic get_free_space function with an ext4 file system"""
1316 self._test_get_free_space(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
1317
1318 def test_ntfs_get_free_space(self):
1319 """Test generic get_free_space function with an ntfs file system"""
1320 if not self.ntfs_avail:
1321 self.skipTest("skipping NTFS: not available")
1322 self._test_get_free_space(mkfs_function=BlockDev.fs_ntfs_mkfs, fstype="ntfs")
1323
1324 def test_vfat_get_free_space(self):
1325 """Test generic get_free_space function with a vfat file system"""
1326 def mkfs_vfat(device, options=None):
1327 if self._vfat_version >= Version("4.2"):
1328 if options:
1329 return BlockDev.fs_vfat_mkfs(device, options + [BlockDev.ExtraArg.new("--mbr=n", "")])
1330 else:
1331 return BlockDev.fs_vfat_mkfs(device, [BlockDev.ExtraArg.new("--mbr=n", "")])
1332 else:
1333 return BlockDev.fs_vfat_mkfs(device, options)
1334
1335 self._test_get_free_space(mkfs_function=mkfs_vfat, fstype="vfat")
1336
1337 def test_nilfs2_get_free_space(self):
1338 """Test generic get_free_space function with an nilfs2 file system"""
1339 if not self.nilfs2_avail:
1340 self.skipTest("skipping NILFS2: not available")
1341 self._test_get_free_space(mkfs_function=BlockDev.fs_nilfs2_mkfs, fstype="nilfs2")
1342
1343 def test_btrfs_get_free_space(self):
1344 """Test generic get_free_space function with a btrfs file system"""
1345 if not self.btrfs_avail:
1346 self.skipTest("skipping Btrfs: not available")
1347 self._test_get_free_space(mkfs_function=BlockDev.fs_btrfs_mkfs, fstype="btrfs")
1348
1349 def test_udf_get_free_space(self):
1350 """Test generic get_free_space function with a udf file system"""
1351 if not self.udf_avail:
1352 self.skipTest("skipping UDF: not available")
1353
1354 # clean the device
1355 succ = BlockDev.fs_clean(self.loop_devs[0])
1356 self.assertTrue(succ)
1357
1358 succ = BlockDev.fs_udf_mkfs(self.loop_devs[0])
1359 self.assertTrue(succ)
1360
1361 with self.assertRaisesRegex(GLib.GError, "Getting free space on filesystem 'udf' is not supported."):
1362 BlockDev.fs_get_free_space(self.loop_devs[0])
1363
1364
1365 class GenericGetMinSize(GenericTestCase):
1366 def _test_get_min_size(self, mkfs_function, fstype):
1367 # clean the device
1368 succ = BlockDev.fs_clean(self.loop_devs[0])
1369
1370 succ = mkfs_function(self.loop_devs[0], None)
1371 self.assertTrue(succ)
1372
1373 size = BlockDev.fs_get_min_size(self.loop_devs[0])
1374 self.assertNotEqual(size, 0)
1375
1376 def test_ext2_test_get_min_size(self):
1377 """Test generic min size function with an ext2 file system"""
1378 self._test_get_min_size(mkfs_function=BlockDev.fs_ext2_mkfs, fstype="ext2")
1379
1380 def test_ext3_check_test_get_min_size(self):
1381 """Test generic min size function with an ext3 file system"""
1382 self._test_get_min_size(mkfs_function=BlockDev.fs_ext3_mkfs, fstype="ext3")
1383
1384 def test_ext4_test_get_min_size(self):
1385 """Test generic min size function with an ext4 file system"""
1386 self._test_get_min_size(mkfs_function=BlockDev.fs_ext4_mkfs, fstype="ext4")
1387
1388 def test_ntfs_test_get_min_size(self):
1389 """Test generic min size function with an ntfs file system"""
1390 if not self.ntfs_avail:
1391 self.skipTest("skipping NTFS: not available")
1392 self._test_get_min_size(mkfs_function=BlockDev.fs_ntfs_mkfs, fstype="ntfs")
1393
1394 def test_xfs_get_get_min_size(self):
1395 """Test generic min size function with a xfs file system"""
1396 # clean the device
1397 succ = BlockDev.fs_clean(self.loop_devs[0])
1398 self.assertTrue(succ)
1399
1400 succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0])
1401 self.assertTrue(succ)
1402
1403 with self.assertRaisesRegex(GLib.GError, "Getting minimum size of filesystem 'xfs' is not supported."):
1404 BlockDev.fs_get_min_size(self.loop_devs[0])
1405
1406
1407 class FSFreezeTest(GenericTestCase):
1408
1409 def _clean_up(self):
1410 try:
1411 BlockDev.fs_unfreeze(self.loop_devs[0])
1412 except:
1413 pass
1414
1415 BlockDev.fs_wipe(self.loop_devs[0], True)
1416
1417 super(FSFreezeTest, self)._clean_up()
1418
1419 def test_freeze_xfs(self):
1420 """ Test basic freezing and un-freezing with XFS """
1421
1422 succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None)
1423 self.assertTrue(succ)
1424
1425 # try to freeze with non-existing mountpoint
1426 with self.assertRaises(GLib.GError):
1427 BlockDev.fs_freeze("/not/a/mountpoint")
1428
1429 tmp = tempfile.mkdtemp(prefix="libblockdev.", suffix="freeze_test")
1430 self.addCleanup(os.rmdir, tmp)
1431
1432 self.addCleanup(utils.umount, self.loop_devs[0])
1433 succ = BlockDev.fs_mount(self.loop_devs[0], tmp, "xfs", None)
1434 self.assertTrue(succ)
1435 self.assertTrue(os.path.ismount(tmp))
1436
1437 succ = BlockDev.fs_freeze(tmp)
1438 self.assertTrue(succ)
1439
1440 # try to freeze again (should fail)
1441 with self.assertRaises(GLib.GError):
1442 BlockDev.fs_freeze(tmp)
1443
1444 # and unfreeze
1445 succ = BlockDev.fs_unfreeze(tmp)
1446 self.assertTrue(succ)
1447
1448 def test_freeze_vfat(self):
1449 """ Test basic freezing and un-freezing with FAT """
1450
1451 succ = BlockDev.fs_vfat_mkfs(self.loop_devs[0], None)
1452 self.assertTrue(succ)
1453
1454 tmp = tempfile.mkdtemp(prefix="libblockdev.", suffix="freeze_test")
1455 self.addCleanup(os.rmdir, tmp)
1456
1457 self.addCleanup(utils.umount, self.loop_devs[0])
1458 succ = BlockDev.fs_mount(self.loop_devs[0], tmp, "vfat", None)
1459 self.assertTrue(succ)
1460 self.assertTrue(os.path.ismount(tmp))
1461
1462 # FAT doesn't support freezing
1463 with self.assertRaises(GLib.GError):
1464 BlockDev.fs_freeze(tmp)
1465
1466
1467 class SupportedFilesystemsTest(GenericNoDevTestCase):
1468 def test_supported_filesystems(self):
1469 filesystems = BlockDev.fs_supported_filesystems()
1470 self.assertListEqual(filesystems,
1471 ["ext2", "ext3", "ext4", "xfs", "vfat", "ntfs",
1472 "f2fs", "nilfs2", "exfat", "btrfs", "udf"])