| 1 |
#!/usr/bin/env python3 |
| 2 |
# systemd integration test: Handling of storage devices |
| 3 |
# (C) 2015 Canonical Ltd. |
| 4 |
# Author: Martin Pitt <[email protected]> |
| 5 |
|
| 6 |
import os |
| 7 |
import random |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import time |
| 11 |
import unittest |
| 12 |
|
| 13 |
from glob import glob |
| 14 |
from threading import Thread |
| 15 |
|
| 16 |
|
| 17 |
TIMEOUT_SERVICE_START = 10 |
| 18 |
TIMEOUT_PASSWORD_AGENT_STOP = 10 |
| 19 |
TIMEOUT_PLAINTEXT_DEV = 30 |
| 20 |
TIMEOUT_SCSI_DEBUG_ADD_HOST = 5 |
| 21 |
|
| 22 |
SCSI_DEBUG_DIR = '/sys/bus/pseudo/drivers/scsi_debug' |
| 23 |
|
| 24 |
|
| 25 |
def have_scsi_debug(): |
| 26 |
return os.path.isdir(SCSI_DEBUG_DIR) |
| 27 |
|
| 28 |
|
| 29 |
def load_scsi_debug(): |
| 30 |
return subprocess.call(['modprobe', 'scsi_debug', 'dev_size_mb=32'], |
| 31 |
stderr=subprocess.STDOUT) == 0 |
| 32 |
|
| 33 |
|
| 34 |
# Some custom Ubuntu kernels do not build the scsi_debug module at all, |
| 35 |
# so skip the test if we can't load the module (LP: #1847816) |
| 36 |
# This doesn't (currently) apply to Debian, as scsi_debug should always |
| 37 |
# be available in Debian kernels |
| 38 |
@unittest.skipIf(not have_scsi_debug() and not load_scsi_debug(), 'Failed to load scsi_debug module') |
| 39 |
class FakeDriveTestBase(unittest.TestCase): |
| 40 |
@classmethod |
| 41 |
def setUpClass(cls): |
| 42 |
assert have_scsi_debug() |
| 43 |
|
| 44 |
def setUp(self): |
| 45 |
existing_adapters = set(glob(os.path.join(SCSI_DEBUG_DIR, 'adapter*'))) |
| 46 |
with open(os.path.join(SCSI_DEBUG_DIR, 'add_host'), 'w') as f: |
| 47 |
f.write('1') |
| 48 |
new_adapters = set(glob(os.path.join(SCSI_DEBUG_DIR, 'adapter*'))) - existing_adapters |
| 49 |
self.assertEqual(len(new_adapters), 1) |
| 50 |
self.adapter = new_adapters.pop() |
| 51 |
for timeout in range(TIMEOUT_SCSI_DEBUG_ADD_HOST): |
| 52 |
devices = set(glob(os.path.join(self.adapter, 'host*/target*/*:*/block/*'))) |
| 53 |
if len(devices) > 0: |
| 54 |
break |
| 55 |
time.sleep(1) |
| 56 |
else: |
| 57 |
self.fail('Timed out waiting for scsi_debug block device name') |
| 58 |
self.assertEqual(len(devices), 1) |
| 59 |
self.device = os.path.join('/dev/', os.path.basename(devices.pop())) |
| 60 |
|
| 61 |
def tearDown(self): |
| 62 |
existing_adapters = set(glob(os.path.join(SCSI_DEBUG_DIR, 'adapter*'))) |
| 63 |
with open(os.path.join(SCSI_DEBUG_DIR, 'add_host'), 'w') as f: |
| 64 |
f.write('-1') |
| 65 |
removed_adapters = existing_adapters - set(glob(os.path.join(SCSI_DEBUG_DIR, 'adapter*'))) |
| 66 |
self.assertEqual(len(removed_adapters), 1) |
| 67 |
adapter = removed_adapters.pop() |
| 68 |
self.assertEqual(self.adapter, adapter) |
| 69 |
self.adapter = None |
| 70 |
self.device = None |
| 71 |
|
| 72 |
|
| 73 |
class CryptsetupTest(FakeDriveTestBase): |
| 74 |
def setUp(self): |
| 75 |
testname = self.id().split('.')[-1] |
| 76 |
self.plaintext_name = 'testcrypt_%s' % testname |
| 77 |
self.plaintext_dev = '/dev/mapper/' + self.plaintext_name |
| 78 |
self.service_name = 'systemd-cryptsetup@%s.service' % self.plaintext_name |
| 79 |
if os.path.exists(self.plaintext_dev): |
| 80 |
self.fail('%s exists already' % self.plaintext_dev) |
| 81 |
|
| 82 |
super().setUp() |
| 83 |
|
| 84 |
if os.path.exists('/etc/crypttab'): |
| 85 |
os.rename('/etc/crypttab', '/etc/crypttab.systemdtest') |
| 86 |
self.password = 'pwd%i' % random.randint(1000, 10000) |
| 87 |
self.password_agent = None |
| 88 |
self.password_agent_stop = False |
| 89 |
|
| 90 |
def tearDown(self): |
| 91 |
if self.password_agent: |
| 92 |
self.password_agent_stop = True |
| 93 |
self.password_agent.join(timeout=TIMEOUT_PASSWORD_AGENT_STOP) |
| 94 |
self.assertFalse(self.password_agent.is_alive()) |
| 95 |
self.password_agent = None |
| 96 |
for timeout in range(TIMEOUT_SERVICE_START): |
| 97 |
state = subprocess.run(['systemctl', 'show', '--no-pager', self.service_name, '--property', 'ActiveState'], |
| 98 |
stdout=subprocess.PIPE, universal_newlines=True).stdout |
| 99 |
state = state.strip().replace('ActiveState=', '', 1) |
| 100 |
if state in ['active', 'failed']: |
| 101 |
break |
| 102 |
time.sleep(1) |
| 103 |
else: |
| 104 |
self.fail('Timed out waiting for %s to start (or fail)' % self.service_name) |
| 105 |
subprocess.call(['umount', self.plaintext_dev], stderr=subprocess.DEVNULL) |
| 106 |
if state == 'active': |
| 107 |
subprocess.call(['systemctl', 'stop', self.service_name], stderr=subprocess.STDOUT) |
| 108 |
if os.path.exists('/etc/crypttab'): |
| 109 |
os.unlink('/etc/crypttab') |
| 110 |
if os.path.exists('/etc/crypttab.systemdtest'): |
| 111 |
os.rename('/etc/crypttab.systemdtest', '/etc/crypttab') |
| 112 |
if os.path.exists(self.plaintext_dev): |
| 113 |
subprocess.call(['dmsetup', 'remove', self.plaintext_dev], |
| 114 |
stderr=subprocess.STDOUT) |
| 115 |
subprocess.check_call(['systemctl', 'daemon-reload']) |
| 116 |
|
| 117 |
super().tearDown() |
| 118 |
|
| 119 |
def format_luks(self): |
| 120 |
'''Format test device with LUKS''' |
| 121 |
|
| 122 |
p = subprocess.Popen(['cryptsetup', '--batch-mode', 'luksFormat', self.device, '-'], |
| 123 |
stdin=subprocess.PIPE) |
| 124 |
p.communicate(self.password.encode()) |
| 125 |
self.assertEqual(p.returncode, 0) |
| 126 |
os.sync() |
| 127 |
subprocess.check_call(['udevadm', 'settle']) |
| 128 |
|
| 129 |
def start_password_agent(self): |
| 130 |
'''Run password agent to answer passphrase request for crypt device''' |
| 131 |
|
| 132 |
# wait for incoming request |
| 133 |
found = False |
| 134 |
while not found: |
| 135 |
for ask in glob('/run/systemd/ask-password/ask.*'): |
| 136 |
with open(ask) as f: |
| 137 |
contents = f.read() |
| 138 |
if self.plaintext_name in contents: |
| 139 |
found = True |
| 140 |
break |
| 141 |
if not found: |
| 142 |
if self.password_agent_stop: |
| 143 |
return |
| 144 |
time.sleep(0.5) |
| 145 |
|
| 146 |
# parse Socket= |
| 147 |
for line in contents.splitlines(): |
| 148 |
if line.startswith('Socket='): |
| 149 |
socket = line.split('=', 1)[1] |
| 150 |
break |
| 151 |
else: |
| 152 |
self.fail('Could not find socket') |
| 153 |
|
| 154 |
# send reply |
| 155 |
p = subprocess.Popen(['/lib/systemd/systemd-reply-password', '1', socket], |
| 156 |
stdin=subprocess.PIPE) |
| 157 |
p.communicate(self.password.encode()) |
| 158 |
self.assertEqual(p.returncode, 0) |
| 159 |
|
| 160 |
def apply(self, target): |
| 161 |
'''Tell systemd to generate and run the cryptsetup units''' |
| 162 |
|
| 163 |
subprocess.check_call(['systemctl', 'daemon-reload']) |
| 164 |
|
| 165 |
self.password_agent = Thread(target=self.start_password_agent); |
| 166 |
self.password_agent.start() |
| 167 |
subprocess.check_call(['systemctl', '--no-ask-password', 'restart', target]) |
| 168 |
for timeout in range(TIMEOUT_PLAINTEXT_DEV): |
| 169 |
if os.path.exists(self.plaintext_dev): |
| 170 |
break |
| 171 |
time.sleep(1) |
| 172 |
else: |
| 173 |
self.fail('Timed out waiting for %s to appear' % self.plaintext_dev) |
| 174 |
|
| 175 |
def test_luks_by_devname(self): |
| 176 |
'''LUKS device by plain device name, empty''' |
| 177 |
|
| 178 |
self.format_luks() |
| 179 |
with open('/etc/crypttab', 'w') as f: |
| 180 |
f.write('%s %s none luks\n' % (self.plaintext_name, self.device)) |
| 181 |
self.apply('cryptsetup.target') |
| 182 |
|
| 183 |
# should not be mounted |
| 184 |
with open('/proc/mounts') as f: |
| 185 |
self.assertNotRegex(f.read(), "(?<!/run/credentials/systemd-cryptsetup@)%s" % (self.plaintext_name)) |
| 186 |
|
| 187 |
# device should not have anything on it |
| 188 |
p = subprocess.Popen(['blkid', self.plaintext_dev], stdout=subprocess.PIPE) |
| 189 |
out = p.communicate()[0] |
| 190 |
self.assertEqual(out, b'') |
| 191 |
self.assertNotEqual(p.returncode, 0) |
| 192 |
|
| 193 |
def test_luks_by_uuid(self): |
| 194 |
'''LUKS device by UUID, empty''' |
| 195 |
|
| 196 |
self.format_luks() |
| 197 |
uuid = subprocess.check_output(['blkid', '-ovalue', '-sUUID', self.device], |
| 198 |
universal_newlines=True).strip() |
| 199 |
with open('/etc/crypttab', 'w') as f: |
| 200 |
f.write('%s UUID=%s none luks\n' % (self.plaintext_name, uuid)) |
| 201 |
self.apply('cryptsetup.target') |
| 202 |
|
| 203 |
# should not be mounted |
| 204 |
with open('/proc/mounts') as f: |
| 205 |
self.assertNotRegex(f.read(), "(?<!/run/credentials/systemd-cryptsetup@)%s" % (self.plaintext_name)) |
| 206 |
|
| 207 |
# device should not have anything on it |
| 208 |
p = subprocess.Popen(['blkid', self.plaintext_dev], stdout=subprocess.PIPE) |
| 209 |
out = p.communicate()[0] |
| 210 |
self.assertEqual(out, b'') |
| 211 |
self.assertNotEqual(p.returncode, 0) |
| 212 |
|
| 213 |
def test_luks_swap(self): |
| 214 |
'''LUKS device with "swap" option''' |
| 215 |
|
| 216 |
self.format_luks() |
| 217 |
with open('/etc/crypttab', 'w') as f: |
| 218 |
f.write('%s %s none luks,swap\n' % (self.plaintext_name, self.device)) |
| 219 |
self.apply('cryptsetup.target') |
| 220 |
|
| 221 |
# should not be mounted |
| 222 |
with open('/proc/mounts') as f: |
| 223 |
self.assertNotRegex(f.read(), "(?<!/run/credentials/systemd-cryptsetup@)%s" % (self.plaintext_name)) |
| 224 |
|
| 225 |
# device should be formatted with swap |
| 226 |
out = subprocess.check_output(['blkid', '-ovalue', '-sTYPE', self.plaintext_dev]) |
| 227 |
self.assertEqual(out, b'swap\n') |
| 228 |
|
| 229 |
def test_luks_tmp(self): |
| 230 |
'''LUKS device with "tmp" option''' |
| 231 |
|
| 232 |
self.format_luks() |
| 233 |
with open('/etc/crypttab', 'w') as f: |
| 234 |
f.write('%s %s none luks,tmp\n' % (self.plaintext_name, self.device)) |
| 235 |
self.apply('cryptsetup.target') |
| 236 |
|
| 237 |
# should not be mounted |
| 238 |
with open('/proc/mounts') as f: |
| 239 |
self.assertNotRegex(f.read(), "(?<!/run/credentials/systemd-cryptsetup@)%s" % (self.plaintext_name)) |
| 240 |
|
| 241 |
# device should be formatted with ext2 or (with newer systemd) ext4 |
| 242 |
out = subprocess.check_output(['blkid', '-ovalue', '-sTYPE', self.plaintext_dev]) |
| 243 |
self.assertRegex(out, b'ext[24]') |
| 244 |
|
| 245 |
def test_luks_fstab(self): |
| 246 |
'''LUKS device in /etc/fstab''' |
| 247 |
|
| 248 |
self.format_luks() |
| 249 |
with open('/etc/crypttab', 'w') as f: |
| 250 |
f.write('%s %s none luks,tmp\n' % (self.plaintext_name, self.device)) |
| 251 |
|
| 252 |
mountpoint = '/run/crypt1.systemdtest' |
| 253 |
os.mkdir(mountpoint) |
| 254 |
self.addCleanup(os.rmdir, mountpoint) |
| 255 |
os.rename('/etc/fstab', '/etc/fstab.systemdtest') |
| 256 |
self.addCleanup(os.rename, '/etc/fstab.systemdtest', '/etc/fstab') |
| 257 |
with open('/etc/fstab', 'a') as f: |
| 258 |
with open('/etc/fstab.systemdtest') as forig: |
| 259 |
f.write(forig.read()) |
| 260 |
f.write('%s %s auto defaults 0 0\n' % (self.plaintext_dev, mountpoint)) |
| 261 |
|
| 262 |
# this should now be a requirement of local-fs.target |
| 263 |
self.apply('local-fs.target') |
| 264 |
|
| 265 |
# should be mounted |
| 266 |
found = False |
| 267 |
with open('/proc/mounts') as f: |
| 268 |
for line in f: |
| 269 |
fields = line.split() |
| 270 |
if fields[0] == self.plaintext_dev: |
| 271 |
self.assertEqual(fields[1], mountpoint) |
| 272 |
self.assertRegex(fields[2], 'ext[24]') |
| 273 |
found = True |
| 274 |
break |
| 275 |
if not found: |
| 276 |
self.fail('%s is not mounted' % self.plaintext_dev) |
| 277 |
|
| 278 |
|
| 279 |
if __name__ == '__main__': |
| 280 |
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, |
| 281 |
verbosity=2)) |