| 1 |
import os |
| 2 |
import tempfile |
| 3 |
|
| 4 |
from .fs_test import FSTestCase |
| 5 |
|
| 6 |
import overrides_hack |
| 7 |
import utils |
| 8 |
from utils import TestTags, tag_test |
| 9 |
|
| 10 |
from gi.repository import BlockDev, GLib |
| 11 |
|
| 12 |
|
| 13 |
class MountTestCase(FSTestCase): |
| 14 |
|
| 15 |
username = "bd_mount_test" |
| 16 |
|
| 17 |
def _add_user(self): |
| 18 |
ret, _out, err = utils.run_command("useradd -M -p \"\" %s" % self.username) |
| 19 |
if ret != 0: |
| 20 |
self.fail("Failed to create user '%s': %s" % (self.username, err)) |
| 21 |
|
| 22 |
ret, uid, err = utils.run_command("id -u %s" % self.username) |
| 23 |
if ret != 0: |
| 24 |
self.fail("Failed to get UID for user '%s': %s" % (self.username, err)) |
| 25 |
|
| 26 |
ret, gid, err = utils.run_command("id -g %s" % self.username) |
| 27 |
if ret != 0: |
| 28 |
self.fail("Failed to get GID for user '%s': %s" % (self.username, err)) |
| 29 |
|
| 30 |
return (uid, gid) |
| 31 |
|
| 32 |
def _remove_user(self): |
| 33 |
ret, _out, err = utils.run_command("userdel %s" % self.username) |
| 34 |
if ret != 0: |
| 35 |
self.fail("Failed to remove user user '%s': %s" % (self.username, err)) |
| 36 |
|
| 37 |
@tag_test(TestTags.CORE) |
| 38 |
def test_mount(self): |
| 39 |
""" Test basic mounting and unmounting """ |
| 40 |
|
| 41 |
succ = BlockDev.fs_vfat_mkfs(self.loop_devs[0], None) |
| 42 |
self.assertTrue(succ) |
| 43 |
|
| 44 |
tmp = tempfile.mkdtemp(prefix="libblockdev.", suffix="mount_test") |
| 45 |
self.addCleanup(os.rmdir, tmp) |
| 46 |
|
| 47 |
self.addCleanup(utils.umount, self.loop_devs[0]) |
| 48 |
|
| 49 |
# try mounting unknown filesystem type |
| 50 |
with self.assertRaisesRegex(GLib.GError, r"Filesystem type .* not configured in kernel."): |
| 51 |
BlockDev.fs_mount(self.loop_devs[0], tmp, "nonexisting", None) |
| 52 |
|
| 53 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, "vfat", None) |
| 54 |
self.assertTrue(succ) |
| 55 |
self.assertTrue(os.path.ismount(tmp)) |
| 56 |
|
| 57 |
succ = BlockDev.fs_is_mountpoint(tmp) |
| 58 |
self.assertTrue(succ) |
| 59 |
|
| 60 |
mnt = BlockDev.fs_get_mountpoint(self.loop_devs[0]) |
| 61 |
self.assertEqual(mnt, tmp) |
| 62 |
|
| 63 |
succ = BlockDev.fs_unmount(self.loop_devs[0], False, False, None) |
| 64 |
self.assertTrue(succ) |
| 65 |
self.assertFalse(os.path.ismount(tmp)) |
| 66 |
|
| 67 |
succ = BlockDev.fs_is_mountpoint(tmp) |
| 68 |
self.assertFalse(succ) |
| 69 |
|
| 70 |
mnt = BlockDev.fs_get_mountpoint(self.loop_devs[0]) |
| 71 |
self.assertIsNone(mnt) |
| 72 |
|
| 73 |
# mount again to test unmount using the mountpoint |
| 74 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, None, None) |
| 75 |
self.assertTrue(succ) |
| 76 |
self.assertTrue(os.path.ismount(tmp)) |
| 77 |
|
| 78 |
succ = BlockDev.fs_unmount(tmp, False, False, None) |
| 79 |
self.assertTrue(succ) |
| 80 |
self.assertFalse(os.path.ismount(tmp)) |
| 81 |
|
| 82 |
# mount with some options |
| 83 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, "vfat", "ro,noexec") |
| 84 |
self.assertTrue(succ) |
| 85 |
self.assertTrue(os.path.ismount(tmp)) |
| 86 |
_ret, out, _err = utils.run_command("grep %s /proc/mounts" % tmp) |
| 87 |
self.assertTrue(out) |
| 88 |
self.assertIn("ro,noexec", out) |
| 89 |
|
| 90 |
succ = BlockDev.fs_unmount(self.loop_devs[0], False, False, None) |
| 91 |
self.assertTrue(succ) |
| 92 |
self.assertFalse(os.path.ismount(tmp)) |
| 93 |
|
| 94 |
# mount with UID=0 and GUID=0 |
| 95 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, run_as_uid="0", run_as_gid="0") |
| 96 |
self.assertTrue(succ) |
| 97 |
self.assertTrue(os.path.ismount(tmp)) |
| 98 |
|
| 99 |
with self.assertRaises(GLib.GError): |
| 100 |
BlockDev.fs_mount(self.loop_devs[0], tmp, run_as_uid="a", run_as_gid="a") |
| 101 |
|
| 102 |
succ = BlockDev.fs_unmount(self.loop_devs[0], False, False, None) |
| 103 |
self.assertTrue(succ) |
| 104 |
self.assertFalse(os.path.ismount(tmp)) |
| 105 |
|
| 106 |
def test_mount_ro_device(self): |
| 107 |
""" Test mounting an FS on a RO device """ |
| 108 |
|
| 109 |
backing_file = utils.create_sparse_tempfile("ro_mount", 50 * 1024**2) |
| 110 |
self.addCleanup(os.unlink, backing_file) |
| 111 |
self.assertTrue(BlockDev.fs_ext2_mkfs(backing_file, None)) |
| 112 |
|
| 113 |
succ, dev = BlockDev.loop_setup(backing_file, 0, 0, True, False) |
| 114 |
self.assertTrue(succ) |
| 115 |
self.addCleanup(BlockDev.loop_teardown, dev) |
| 116 |
|
| 117 |
tmp_dir = tempfile.mkdtemp(prefix="libblockdev.", suffix="mount_test") |
| 118 |
self.addCleanup(os.rmdir, tmp_dir) |
| 119 |
|
| 120 |
loop_dev = "/dev/" + dev |
| 121 |
# without any options, the mount should fall back to RO |
| 122 |
self.assertTrue(BlockDev.fs_mount(loop_dev, tmp_dir, None, None, None)) |
| 123 |
self.addCleanup(utils.umount, loop_dev) |
| 124 |
self.assertTrue(os.path.ismount(tmp_dir)) |
| 125 |
|
| 126 |
succ = BlockDev.fs_unmount(tmp_dir, False, False, None) |
| 127 |
self.assertTrue(succ) |
| 128 |
self.assertFalse(os.path.ismount(tmp_dir)) |
| 129 |
|
| 130 |
# explicit "ro" should work just fine too |
| 131 |
self.assertTrue(BlockDev.fs_mount(loop_dev, tmp_dir, None, "ro", None)) |
| 132 |
self.assertTrue(os.path.ismount(tmp_dir)) |
| 133 |
|
| 134 |
succ = BlockDev.fs_unmount(tmp_dir, False, False, None) |
| 135 |
self.assertTrue(succ) |
| 136 |
self.assertFalse(os.path.ismount(tmp_dir)) |
| 137 |
|
| 138 |
# explicit "rw" should fail |
| 139 |
with self.assertRaises(GLib.GError): |
| 140 |
BlockDev.fs_mount(loop_dev, tmp_dir, None, "rw", None) |
| 141 |
self.assertFalse(os.path.ismount(tmp_dir)) |
| 142 |
|
| 143 |
@tag_test(TestTags.UNSAFE) |
| 144 |
def test_mount_fstab(self): |
| 145 |
""" Test mounting and unmounting devices in /etc/fstab """ |
| 146 |
# this test will change /etc/fstab, we want to revert the changes when it finishes |
| 147 |
fstab = utils.read_file("/etc/fstab") |
| 148 |
self.addCleanup(utils.write_file, "/etc/fstab", fstab) |
| 149 |
|
| 150 |
succ = BlockDev.fs_vfat_mkfs(self.loop_devs[0], None) |
| 151 |
self.assertTrue(succ) |
| 152 |
|
| 153 |
tmp = tempfile.mkdtemp(prefix="libblockdev.", suffix="mount_fstab_test") |
| 154 |
self.addCleanup(os.rmdir, tmp) |
| 155 |
|
| 156 |
utils.write_file("/etc/fstab", "%s %s vfat defaults 0 0\n" % (self.loop_devs[0], tmp)) |
| 157 |
|
| 158 |
# try to mount and unmount using the device |
| 159 |
self.addCleanup(utils.umount, self.loop_devs[0]) |
| 160 |
succ = BlockDev.fs_mount(device=self.loop_devs[0]) |
| 161 |
self.assertTrue(succ) |
| 162 |
self.assertTrue(os.path.ismount(tmp)) |
| 163 |
|
| 164 |
succ = BlockDev.fs_unmount(self.loop_devs[0]) |
| 165 |
self.assertTrue(succ) |
| 166 |
self.assertFalse(os.path.ismount(tmp)) |
| 167 |
|
| 168 |
# try to mount and unmount just using the mountpoint |
| 169 |
self.addCleanup(utils.umount, self.loop_devs[0]) |
| 170 |
succ = BlockDev.fs_mount(mountpoint=tmp) |
| 171 |
self.assertTrue(succ) |
| 172 |
self.assertTrue(os.path.ismount(tmp)) |
| 173 |
|
| 174 |
succ = BlockDev.fs_unmount(tmp) |
| 175 |
self.assertTrue(succ) |
| 176 |
self.assertFalse(os.path.ismount(tmp)) |
| 177 |
|
| 178 |
@tag_test(TestTags.UNSAFE) |
| 179 |
def test_mount_fstab_user(self): |
| 180 |
""" Test mounting and unmounting devices in /etc/fstab as non-root user """ |
| 181 |
# this test will change /etc/fstab, we want to revert the changes when it finishes |
| 182 |
fstab = utils.read_file("/etc/fstab") |
| 183 |
self.addCleanup(utils.write_file, "/etc/fstab", fstab) |
| 184 |
|
| 185 |
succ = BlockDev.fs_vfat_mkfs(self.loop_devs[0], None) |
| 186 |
self.assertTrue(succ) |
| 187 |
|
| 188 |
tmp = tempfile.mkdtemp(prefix="libblockdev.", suffix="mount_fstab_user_test") |
| 189 |
self.addCleanup(os.rmdir, tmp) |
| 190 |
|
| 191 |
utils.write_file("/etc/fstab", "%s %s vfat defaults,users 0 0\n" % (self.loop_devs[0], tmp)) |
| 192 |
|
| 193 |
uid, gid = self._add_user() |
| 194 |
self.addCleanup(self._remove_user) |
| 195 |
|
| 196 |
# try to mount and unmount the device as the user |
| 197 |
self.addCleanup(utils.umount, self.loop_devs[0]) |
| 198 |
succ = BlockDev.fs_mount(device=self.loop_devs[0], run_as_uid=uid, run_as_gid=gid) |
| 199 |
self.assertTrue(succ) |
| 200 |
self.assertTrue(os.path.ismount(tmp)) |
| 201 |
|
| 202 |
succ = BlockDev.fs_unmount(self.loop_devs[0], run_as_uid=uid, run_as_gid=gid) |
| 203 |
self.assertTrue(succ) |
| 204 |
self.assertFalse(os.path.ismount(tmp)) |
| 205 |
|
| 206 |
# remove the 'users' option |
| 207 |
utils.write_file("/etc/fstab", "%s %s vfat defaults 0 0\n" % (self.loop_devs[0], tmp)) |
| 208 |
|
| 209 |
# try to mount and unmount the device as the user --> should fail now |
| 210 |
with self.assertRaises(GLib.GError): |
| 211 |
BlockDev.fs_mount(device=self.loop_devs[0], run_as_uid=uid, run_as_gid=gid) |
| 212 |
|
| 213 |
self.assertFalse(os.path.ismount(tmp)) |
| 214 |
|
| 215 |
# now mount as root to test unmounting |
| 216 |
self.addCleanup(utils.umount, self.loop_devs[0]) |
| 217 |
succ = BlockDev.fs_mount(device=self.loop_devs[0]) |
| 218 |
self.assertTrue(succ) |
| 219 |
self.assertTrue(os.path.ismount(tmp)) |
| 220 |
|
| 221 |
with self.assertRaises(GLib.GError): |
| 222 |
BlockDev.fs_unmount(self.loop_devs[0], run_as_uid=uid, run_as_gid=gid) |
| 223 |
self.assertTrue(os.path.ismount(tmp)) |
| 224 |
|
| 225 |
def test_mount_nilfs(self): |
| 226 |
""" Test basic mounting and unmounting with NILFS2 filesystem""" |
| 227 |
# using NILFS2 because it uses a helper program (mount.nilfs2) and libmount |
| 228 |
# behaves differently because of that |
| 229 |
|
| 230 |
if not self.nilfs2_avail: |
| 231 |
self.skipTest("skipping NILFS mount test: not available") |
| 232 |
|
| 233 |
succ = BlockDev.fs_nilfs2_mkfs(self.loop_devs[0], None) |
| 234 |
self.assertTrue(succ) |
| 235 |
|
| 236 |
tmp = tempfile.mkdtemp(prefix="libblockdev.", suffix="mount_test") |
| 237 |
self.addCleanup(os.rmdir, tmp) |
| 238 |
|
| 239 |
self.addCleanup(utils.umount, self.loop_devs[0]) |
| 240 |
|
| 241 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, "nilfs2", None) |
| 242 |
self.assertTrue(succ) |
| 243 |
self.assertTrue(os.path.ismount(tmp)) |
| 244 |
|
| 245 |
succ = BlockDev.fs_unmount(self.loop_devs[0], False, False, None) |
| 246 |
self.assertTrue(succ) |
| 247 |
self.assertFalse(os.path.ismount(tmp)) |
| 248 |
|
| 249 |
mnt = BlockDev.fs_get_mountpoint(self.loop_devs[0]) |
| 250 |
self.assertIsNone(mnt) |
| 251 |
|
| 252 |
# mount again to test unmount using the mountpoint |
| 253 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, None, None) |
| 254 |
self.assertTrue(succ) |
| 255 |
self.assertTrue(os.path.ismount(tmp)) |
| 256 |
|
| 257 |
succ = BlockDev.fs_unmount(tmp, False, False, None) |
| 258 |
self.assertTrue(succ) |
| 259 |
self.assertFalse(os.path.ismount(tmp)) |
| 260 |
|
| 261 |
# mount with some options |
| 262 |
succ = BlockDev.fs_mount(self.loop_devs[0], tmp, "nilfs2", "ro") |
| 263 |
self.assertTrue(succ) |
| 264 |
self.assertTrue(os.path.ismount(tmp)) |
| 265 |
_ret, out, _err = utils.run_command("grep %s /proc/mounts" % tmp) |
| 266 |
self.assertTrue(out) |
| 267 |
self.assertIn("ro", out) |
| 268 |
|
| 269 |
succ = BlockDev.fs_unmount(self.loop_devs[0], False, False, None) |
| 270 |
self.assertTrue(succ) |
| 271 |
self.assertFalse(os.path.ismount(tmp)) |