| 1 |
import tempfile |
| 2 |
|
| 3 |
from packaging.version import Version |
| 4 |
|
| 5 |
from .fs_test import FSTestCase, FSNoDevTestCase, mounted, check_output |
| 6 |
|
| 7 |
import overrides_hack |
| 8 |
import utils |
| 9 |
from utils import TestTags, tag_test |
| 10 |
|
| 11 |
from gi.repository import BlockDev, GLib |
| 12 |
|
| 13 |
|
| 14 |
class XfsNoDevTestCase(FSNoDevTestCase): |
| 15 |
pass |
| 16 |
|
| 17 |
|
| 18 |
class XfsTestCase(FSTestCase): |
| 19 |
loop_size = 500 * 1024**2 |
| 20 |
|
| 21 |
def setUp(self): |
| 22 |
super(XfsTestCase, self).setUp() |
| 23 |
|
| 24 |
self.mount_dir = tempfile.mkdtemp(prefix="libblockdev.", suffix="xfs_test") |
| 25 |
|
| 26 |
|
| 27 |
class XfsTestAvailability(XfsNoDevTestCase): |
| 28 |
|
| 29 |
def test_xfs_available(self): |
| 30 |
"""Verify that it is possible to check xfs tech availability""" |
| 31 |
available = BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, |
| 32 |
BlockDev.FSTechMode.MKFS | |
| 33 |
BlockDev.FSTechMode.QUERY | |
| 34 |
BlockDev.FSTechMode.REPAIR | |
| 35 |
BlockDev.FSTechMode.CHECK | |
| 36 |
BlockDev.FSTechMode.SET_LABEL | |
| 37 |
BlockDev.FSTechMode.RESIZE | |
| 38 |
BlockDev.FSTechMode.SET_UUID) |
| 39 |
self.assertTrue(available) |
| 40 |
|
| 41 |
BlockDev.reinit(self.requested_plugins, True, None) |
| 42 |
|
| 43 |
# now try without mkfs.xfs |
| 44 |
with utils.fake_path(all_but="mkfs.xfs"): |
| 45 |
with self.assertRaisesRegex(GLib.GError, "The 'mkfs.xfs' utility is not available"): |
| 46 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.MKFS) |
| 47 |
|
| 48 |
# now try without xfs_db |
| 49 |
with utils.fake_path(all_but="xfs_db"): |
| 50 |
with self.assertRaisesRegex(GLib.GError, "The 'xfs_db' utility is not available"): |
| 51 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.CHECK) |
| 52 |
|
| 53 |
# now try without xfs_repair |
| 54 |
with utils.fake_path(all_but="xfs_repair"): |
| 55 |
with self.assertRaisesRegex(GLib.GError, "The 'xfs_repair' utility is not available"): |
| 56 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.REPAIR) |
| 57 |
|
| 58 |
# now try without xfs_admin |
| 59 |
with utils.fake_path(all_but="xfs_admin"): |
| 60 |
with self.assertRaisesRegex(GLib.GError, "The 'xfs_admin' utility is not available"): |
| 61 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.QUERY) |
| 62 |
|
| 63 |
with self.assertRaisesRegex(GLib.GError, "The 'xfs_admin' utility is not available"): |
| 64 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.SET_LABEL) |
| 65 |
|
| 66 |
with self.assertRaisesRegex(GLib.GError, "The 'xfs_admin' utility is not available"): |
| 67 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.SET_UUID) |
| 68 |
|
| 69 |
# now try without xfs_growfs |
| 70 |
with utils.fake_path(all_but="xfs_growfs"): |
| 71 |
with self.assertRaisesRegex(GLib.GError, "The 'xfs_growfs' utility is not available"): |
| 72 |
BlockDev.fs_is_tech_avail(BlockDev.FSTech.XFS, BlockDev.FSTechMode.RESIZE) |
| 73 |
|
| 74 |
|
| 75 |
class XfsTestFeatures(XfsNoDevTestCase): |
| 76 |
|
| 77 |
def test_xfs_features(self): |
| 78 |
features = BlockDev.fs_features("xfs") |
| 79 |
self.assertIsNotNone(features) |
| 80 |
|
| 81 |
self.assertTrue(features.resize & BlockDev.FSResizeFlags.OFFLINE_GROW) |
| 82 |
self.assertTrue(features.resize & BlockDev.FSResizeFlags.ONLINE_GROW) |
| 83 |
|
| 84 |
self.assertTrue(features.mkfs & BlockDev.FSMkfsOptionsFlags.LABEL) |
| 85 |
self.assertTrue(features.mkfs & BlockDev.FSMkfsOptionsFlags.UUID) |
| 86 |
self.assertTrue(features.mkfs & BlockDev.FSMkfsOptionsFlags.DRY_RUN) |
| 87 |
self.assertTrue(features.mkfs & BlockDev.FSMkfsOptionsFlags.NODISCARD) |
| 88 |
self.assertTrue(features.mkfs & BlockDev.FSMkfsOptionsFlags.FORCE) |
| 89 |
self.assertFalse(features.mkfs & BlockDev.FSMkfsOptionsFlags.NOPT) |
| 90 |
|
| 91 |
self.assertTrue(features.fsck & BlockDev.FSFsckFlags.CHECK) |
| 92 |
self.assertTrue(features.fsck & BlockDev.FSFsckFlags.REPAIR) |
| 93 |
|
| 94 |
self.assertTrue(features.configure & BlockDev.FSConfigureFlags.LABEL) |
| 95 |
self.assertTrue(features.configure & BlockDev.FSConfigureFlags.UUID) |
| 96 |
|
| 97 |
self.assertTrue(features.features & BlockDev.FSFeatureFlags.OWNERS) |
| 98 |
self.assertFalse(features.features & BlockDev.FSFeatureFlags.PARTITION_TABLE) |
| 99 |
|
| 100 |
self.assertEqual(features.partition_id, "0x83") |
| 101 |
self.assertEqual(features.partition_type, "0fc63daf-8483-4772-8e79-3d69d8477de4") |
| 102 |
|
| 103 |
self.assertEqual(features.min_size, 300 * 1024**2) |
| 104 |
self.assertEqual(features.max_size, 16 * 1024**6 - 1) |
| 105 |
|
| 106 |
|
| 107 |
class XfsTestMkfs(XfsTestCase): |
| 108 |
@tag_test(TestTags.CORE) |
| 109 |
def test_xfs_mkfs(self): |
| 110 |
"""Verify that it is possible to create a new xfs file system""" |
| 111 |
|
| 112 |
with self.assertRaises(GLib.GError): |
| 113 |
BlockDev.fs_xfs_mkfs("/non/existing/device", None) |
| 114 |
|
| 115 |
succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None) |
| 116 |
self.assertTrue(succ) |
| 117 |
|
| 118 |
# just try if we can mount the file system |
| 119 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 120 |
pass |
| 121 |
|
| 122 |
# check the fstype |
| 123 |
fstype = BlockDev.fs_get_fstype(self.loop_devs[0]) |
| 124 |
self.assertEqual(fstype, "xfs") |
| 125 |
|
| 126 |
BlockDev.fs_wipe(self.loop_devs[0], True) |
| 127 |
|
| 128 |
|
| 129 |
class XfsTestCheck(XfsTestCase): |
| 130 |
def test_xfs_check(self): |
| 131 |
"""Verify that it is possible to check an xfs file system""" |
| 132 |
|
| 133 |
succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None) |
| 134 |
self.assertTrue(succ) |
| 135 |
|
| 136 |
succ = BlockDev.fs_xfs_check(self.loop_devs[0]) |
| 137 |
self.assertTrue(succ) |
| 138 |
|
| 139 |
# mounted RO, can be checked and nothing happened in/to the file system, |
| 140 |
# so it should be just reported as clean |
| 141 |
with mounted(self.loop_devs[0], self.mount_dir, ro=True): |
| 142 |
succ = BlockDev.fs_xfs_check(self.loop_devs[0]) |
| 143 |
self.assertTrue(succ) |
| 144 |
|
| 145 |
succ = BlockDev.fs_xfs_check(self.loop_devs[0]) |
| 146 |
self.assertTrue(succ) |
| 147 |
|
| 148 |
|
| 149 |
class XfsTestRepair(XfsTestCase): |
| 150 |
def test_xfs_repair(self): |
| 151 |
"""Verify that it is possible to repair an xfs file system""" |
| 152 |
|
| 153 |
succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None) |
| 154 |
self.assertTrue(succ) |
| 155 |
|
| 156 |
succ = BlockDev.fs_xfs_repair(self.loop_devs[0], None) |
| 157 |
self.assertTrue(succ) |
| 158 |
|
| 159 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 160 |
with self.assertRaises(GLib.GError): |
| 161 |
BlockDev.fs_xfs_repair(self.loop_devs[0], None) |
| 162 |
|
| 163 |
succ = BlockDev.fs_xfs_repair(self.loop_devs[0], None) |
| 164 |
self.assertTrue(succ) |
| 165 |
|
| 166 |
|
| 167 |
class XfsGetInfo(XfsTestCase): |
| 168 |
@tag_test(TestTags.CORE) |
| 169 |
def test_xfs_get_info(self): |
| 170 |
"""Verify that it is possible to get info about an xfs file system""" |
| 171 |
|
| 172 |
succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None) |
| 173 |
self.assertTrue(succ) |
| 174 |
|
| 175 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 176 |
self.assertEqual(fi.block_size, 4096) |
| 177 |
self.assertEqual(fi.block_count, self.loop_size / 4096) |
| 178 |
self.assertEqual(fi.label, "") |
| 179 |
# should be an non-empty string |
| 180 |
self.assertTrue(fi.uuid) |
| 181 |
|
| 182 |
|
| 183 |
class XfsSetLabel(XfsTestCase): |
| 184 |
def test_xfs_set_label(self): |
| 185 |
"""Verify that it is possible to set label of an xfs file system""" |
| 186 |
|
| 187 |
succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None) |
| 188 |
self.assertTrue(succ) |
| 189 |
|
| 190 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 191 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 192 |
self.assertTrue(fi) |
| 193 |
self.assertEqual(fi.label, "") |
| 194 |
|
| 195 |
succ = BlockDev.fs_xfs_set_label(self.loop_devs[0], "TEST_LABEL") |
| 196 |
self.assertTrue(succ) |
| 197 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 198 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 199 |
self.assertTrue(fi) |
| 200 |
self.assertEqual(fi.label, "TEST_LABEL") |
| 201 |
|
| 202 |
succ = BlockDev.fs_xfs_set_label(self.loop_devs[0], "TEST_LABEL2") |
| 203 |
self.assertTrue(succ) |
| 204 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 205 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 206 |
self.assertTrue(fi) |
| 207 |
self.assertEqual(fi.label, "TEST_LABEL2") |
| 208 |
|
| 209 |
succ = BlockDev.fs_xfs_set_label(self.loop_devs[0], "") |
| 210 |
self.assertTrue(succ) |
| 211 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 212 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 213 |
self.assertTrue(fi) |
| 214 |
self.assertEqual(fi.label, "") |
| 215 |
|
| 216 |
succ = BlockDev.fs_xfs_check_label("TEST_LABEL") |
| 217 |
self.assertTrue(succ) |
| 218 |
|
| 219 |
with self.assertRaisesRegex(GLib.GError, "at most 12 characters long."): |
| 220 |
BlockDev.fs_xfs_check_label(13 * "a") |
| 221 |
|
| 222 |
with self.assertRaisesRegex(GLib.GError, "cannot contain spaces"): |
| 223 |
BlockDev.fs_xfs_check_label("TEST LABEL") |
| 224 |
|
| 225 |
|
| 226 |
class XfsResize(XfsTestCase): |
| 227 |
def test_xfs_resize(self): |
| 228 |
"""Verify that it is possible to resize an xfs file system""" |
| 229 |
|
| 230 |
lv = self._setup_lvm(vgname="libbd_fs_tests", lvname="xfs_test", lvsize="350M") |
| 231 |
|
| 232 |
succ = BlockDev.fs_xfs_mkfs(lv, None) |
| 233 |
self.assertTrue(succ) |
| 234 |
|
| 235 |
with mounted(lv, self.mount_dir): |
| 236 |
fi = BlockDev.fs_xfs_get_info(lv) |
| 237 |
self.assertTrue(fi) |
| 238 |
self.assertEqual(fi.block_size * fi.block_count, 350 * 1024**2) |
| 239 |
|
| 240 |
# no change, nothing should happen |
| 241 |
with mounted(lv, self.mount_dir): |
| 242 |
succ = BlockDev.fs_xfs_resize(self.mount_dir, 0, None) |
| 243 |
self.assertTrue(succ) |
| 244 |
|
| 245 |
with mounted(lv, self.mount_dir): |
| 246 |
fi = BlockDev.fs_xfs_get_info(lv) |
| 247 |
self.assertTrue(fi) |
| 248 |
self.assertEqual(fi.block_size * fi.block_count, 350 * 1024**2) |
| 249 |
|
| 250 |
# (still) impossible to shrink an XFS file system |
| 251 |
xfs_version = self._get_xfs_version() |
| 252 |
if xfs_version < Version("5.12"): |
| 253 |
with mounted(lv, self.mount_dir): |
| 254 |
with self.assertRaises(GLib.GError): |
| 255 |
succ = BlockDev.fs_xfs_resize(self.mount_dir, 40 * 1024**2 // fi.block_size, None) |
| 256 |
|
| 257 |
self._lvresize("libbd_fs_tests", "xfs_test", "400M") |
| 258 |
# should grow to 400 MiB (full size of the LV) |
| 259 |
with mounted(lv, self.mount_dir): |
| 260 |
succ = BlockDev.fs_xfs_resize(self.mount_dir, 0, None) |
| 261 |
self.assertTrue(succ) |
| 262 |
with mounted(lv, self.mount_dir): |
| 263 |
fi = BlockDev.fs_xfs_get_info(lv) |
| 264 |
self.assertTrue(fi) |
| 265 |
self.assertEqual(fi.block_size * fi.block_count, 400 * 1024**2) |
| 266 |
|
| 267 |
self._lvresize("libbd_fs_tests", "xfs_test", "450M") |
| 268 |
# grow just to 430 MiB |
| 269 |
with mounted(lv, self.mount_dir): |
| 270 |
succ = BlockDev.fs_xfs_resize(self.mount_dir, 430 * 1024**2 // fi.block_size, None) |
| 271 |
self.assertTrue(succ) |
| 272 |
with mounted(lv, self.mount_dir): |
| 273 |
fi = BlockDev.fs_xfs_get_info(lv) |
| 274 |
self.assertTrue(fi) |
| 275 |
self.assertEqual(fi.block_size * fi.block_count, 430 * 1024**2) |
| 276 |
|
| 277 |
# should grow to 450 MiB (full size of the LV) |
| 278 |
with mounted(lv, self.mount_dir): |
| 279 |
succ = BlockDev.fs_xfs_resize(self.mount_dir, 0, None) |
| 280 |
self.assertTrue(succ) |
| 281 |
with mounted(lv, self.mount_dir): |
| 282 |
fi = BlockDev.fs_xfs_get_info(lv) |
| 283 |
self.assertTrue(fi) |
| 284 |
self.assertEqual(fi.block_size * fi.block_count, 450 * 1024**2) |
| 285 |
|
| 286 |
|
| 287 |
class XfsSetUUID(XfsTestCase): |
| 288 |
|
| 289 |
test_uuid = "4d7086c4-a4d3-432f-819e-73da03870df9" |
| 290 |
|
| 291 |
def test_xfs_set_uuid(self): |
| 292 |
"""Verify that it is possible to set UUID of an xfs file system""" |
| 293 |
|
| 294 |
succ = BlockDev.fs_xfs_mkfs(self.loop_devs[0], None) |
| 295 |
self.assertTrue(succ) |
| 296 |
|
| 297 |
succ = BlockDev.fs_xfs_set_uuid(self.loop_devs[0], self.test_uuid) |
| 298 |
self.assertTrue(succ) |
| 299 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 300 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 301 |
self.assertTrue(fi) |
| 302 |
self.assertEqual(fi.uuid, self.test_uuid) |
| 303 |
|
| 304 |
succ = BlockDev.fs_xfs_set_uuid(self.loop_devs[0], "nil") |
| 305 |
self.assertTrue(succ) |
| 306 |
|
| 307 |
# can't use libblockdev because XFS without UUID can't be mounted |
| 308 |
fs_type = check_output(["blkid", "-ovalue", "-sUUID", "-p", self.loop_devs[0]]).strip() |
| 309 |
self.assertEqual(fs_type, b"") |
| 310 |
|
| 311 |
succ = BlockDev.fs_xfs_set_uuid(self.loop_devs[0], "generate") |
| 312 |
self.assertTrue(succ) |
| 313 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 314 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 315 |
self.assertTrue(fi) |
| 316 |
self.assertNotEqual(fi.uuid, "") |
| 317 |
random_uuid = fi.uuid |
| 318 |
|
| 319 |
# no uuid -> random |
| 320 |
succ = BlockDev.fs_xfs_set_uuid(self.loop_devs[0], None) |
| 321 |
self.assertTrue(succ) |
| 322 |
with mounted(self.loop_devs[0], self.mount_dir): |
| 323 |
fi = BlockDev.fs_xfs_get_info(self.loop_devs[0]) |
| 324 |
self.assertTrue(fi) |
| 325 |
self.assertNotEqual(fi.uuid, "") |
| 326 |
self.assertNotEqual(fi.uuid, random_uuid) |
| 327 |
|
| 328 |
succ = BlockDev.fs_xfs_check_uuid(self.test_uuid) |
| 329 |
self.assertTrue(succ) |
| 330 |
|
| 331 |
succ = BlockDev.fs_xfs_check_uuid(self.test_uuid.upper()) |
| 332 |
self.assertTrue(succ) |
| 333 |
|
| 334 |
with self.assertRaisesRegex(GLib.GError, "not a valid RFC-4122 UUID"): |
| 335 |
BlockDev.fs_xfs_check_uuid("aaaaaaa") |