| 1 |
import os |
| 2 |
import sys |
| 3 |
import stat |
| 4 |
import errno |
| 5 |
|
| 6 |
# use "import libmount" for in a standard way installed python binding |
| 7 |
import pylibmount as mnt |
| 8 |
|
| 9 |
def usage(tss): |
| 10 |
print("\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0])) |
| 11 |
for i in tss: |
| 12 |
print("\t{15:-s}".format(i[0])) |
| 13 |
if i[2] != "": |
| 14 |
print(" {:s}\n".format(i[2])) |
| 15 |
|
| 16 |
print("\n") |
| 17 |
return 1 |
| 18 |
|
| 19 |
def mnt_run_test(tss, argv): |
| 20 |
rc = -1 |
| 21 |
if ((len(argv) < 2) or (argv[1] == "--help") or (argv[1] == "-h")): |
| 22 |
return usage(tss) |
| 23 |
|
| 24 |
#mnt_init_debug(0) |
| 25 |
|
| 26 |
i=() |
| 27 |
for i in tss: |
| 28 |
if i[0] == argv[1]: |
| 29 |
rc = i[1](i, argv[1:]) |
| 30 |
if rc: |
| 31 |
print("FAILED [rc={:d}]".format(rc)) |
| 32 |
break |
| 33 |
|
| 34 |
if ((rc < 0) and (i == ())): |
| 35 |
return usage(tss) |
| 36 |
return not not rc #because !!rc is too mainstream for python |
| 37 |
|
| 38 |
def test_mount(ts, argv): |
| 39 |
idx = 1 |
| 40 |
rc = 0 |
| 41 |
|
| 42 |
if len(argv) < 2: |
| 43 |
return -errno.EINVAL |
| 44 |
|
| 45 |
cxt = mnt.Context() |
| 46 |
|
| 47 |
if argv[idx] == "-o": |
| 48 |
cxt.options = argv[idx+1] |
| 49 |
idx += 2 |
| 50 |
if argv[idx] == "-t": |
| 51 |
cxt.fstype = argv[idx+1] |
| 52 |
idx += 2 |
| 53 |
if len(argv) == idx + 1: |
| 54 |
cxt.target = argv[idx] |
| 55 |
idx+=1 |
| 56 |
elif (len(argv) == idx + 2): |
| 57 |
cxt.source = argv[idx] |
| 58 |
idx += 1 |
| 59 |
cxt.target = argv[idx] |
| 60 |
idx += 1 |
| 61 |
|
| 62 |
try: |
| 63 |
cxt.mount() |
| 64 |
except Exception: |
| 65 |
print("failed to mount") |
| 66 |
return -1 |
| 67 |
print("successfully mounted") |
| 68 |
return rc |
| 69 |
|
| 70 |
def test_umount(ts, argv): |
| 71 |
idx = 1 |
| 72 |
rc = 0 |
| 73 |
if len(argv) < 2: |
| 74 |
return -errno.EINVAL |
| 75 |
|
| 76 |
cxt = mnt.Context() |
| 77 |
|
| 78 |
if argv[idx] == "-t": |
| 79 |
cxt.options = argv[idx+1] |
| 80 |
idx += 2 |
| 81 |
|
| 82 |
if argv[idx] == "-f": |
| 83 |
cxt.enable_force(True) |
| 84 |
|
| 85 |
if argv[idx] == "-l": |
| 86 |
cxt.enable_lazy(True) |
| 87 |
idx += 1 |
| 88 |
elif argv[idx] == "-r": |
| 89 |
cxt.enable_rdonly_umount(True) |
| 90 |
idx += 1 |
| 91 |
|
| 92 |
if len(argv) == idx + 1: |
| 93 |
cxt.target = argv[idx] |
| 94 |
idx += 1 |
| 95 |
else: |
| 96 |
return -errno.EINVAL |
| 97 |
try: |
| 98 |
cxt.umount() |
| 99 |
except Exception: |
| 100 |
print("failed to umount") |
| 101 |
return 1 |
| 102 |
print("successfully umounted") |
| 103 |
return rc |
| 104 |
|
| 105 |
def test_flags(ts, argv): |
| 106 |
idx = 1 |
| 107 |
rc = 0 |
| 108 |
opt = "" |
| 109 |
flags = 0 |
| 110 |
cxt = mnt.Context() |
| 111 |
|
| 112 |
if argv[idx] == "-o": |
| 113 |
cxt.options = argv[idx + 1] |
| 114 |
idx += 2 |
| 115 |
if len(argv) == idx + 1: |
| 116 |
cxt.target = argv[idx] |
| 117 |
idx += 1 |
| 118 |
|
| 119 |
try: |
| 120 |
cxt.prepare_mount() |
| 121 |
# catch ioerror here |
| 122 |
except IOError as e: |
| 123 |
print("failed to prepare mount {:s}".format(e.strerror)) |
| 124 |
|
| 125 |
opt = cxt.fs.options |
| 126 |
if (opt): |
| 127 |
print("options: {:s}", opt) |
| 128 |
|
| 129 |
print("flags: {08:lx}".format(cxt.mflags())) |
| 130 |
return rc |
| 131 |
|
| 132 |
def test_mountall(ts, argv): |
| 133 |
mntrc = 1 |
| 134 |
ignored = 1 |
| 135 |
idx = 1 |
| 136 |
cxt = mnt.Context() |
| 137 |
|
| 138 |
if len(argv) > 2: |
| 139 |
if argv[idx] == "-O": |
| 140 |
cxt.options_pattern = argv[idx+1] |
| 141 |
idx += 2 |
| 142 |
if argv[idx] == "-t": |
| 143 |
cxt.fstype_pattern = argv[idx+1] |
| 144 |
idx += 2 |
| 145 |
|
| 146 |
i = () |
| 147 |
while (cxt.next_mount()): |
| 148 |
tgt = i.target |
| 149 |
if (ignored == 1): |
| 150 |
print("{:s}: ignored: not match".format(tgt)) |
| 151 |
elif (ignored == 2): |
| 152 |
print("{:s}: ignored: already mounted".format(tgt)) |
| 153 |
elif (not cxt.status): |
| 154 |
if (mntrc > 0): |
| 155 |
# ?? errno = mntrc |
| 156 |
print("{:s}: mount failed".format(tgt)) |
| 157 |
else: |
| 158 |
print("{:s}: mount failed".format(tgt)) |
| 159 |
else: |
| 160 |
print("{:s}: successfully mounted".format(tgt)) |
| 161 |
|
| 162 |
return 0 |
| 163 |
|
| 164 |
|
| 165 |
tss = ( |
| 166 |
( "--mount", test_mount, "[-o <opts>] [-t <type>] <spec>|<src> <target>" ), |
| 167 |
( "--umount", test_umount, "[-t <type>] [-f][-l][-r] <src>|<target>" ), |
| 168 |
( "--mount-all", test_mountall, "[-O <pattern>] [-t <pattern] mount all filesystems from fstab" ), |
| 169 |
( "--flags", test_flags, "[-o <opts>] <spec>" ) |
| 170 |
) |
| 171 |
os.umask(stat.S_IWGRP | stat.S_IWOTH) #to be compatible with mount(8) |
| 172 |
|
| 173 |
sys.exit(mnt_run_test(tss, sys.argv)) |