Debian Codegraph @

test/unit/bootloader/install/grub2_test.py

raw
1 import io
2 from unittest.mock import (
3 patch, call, MagicMock, Mock
4 )
5 from pytest import raises
6 import unittest.mock as mock
7
8 from kiwi.storage.mapped_device import MappedDevice
9 from kiwi.storage.disk import ptable_entry_type
10 from kiwi.bootloader.install.grub2 import BootLoaderInstallGrub2
11 from kiwi.defaults import Defaults
12
13 from kiwi.exceptions import (
14 KiwiBootLoaderGrubInstallError,
15 KiwiBootLoaderGrubDataError,
16 KiwiBootLoaderGrubPlatformError,
17 KiwiBootLoaderDiskPasswordError
18 )
19
20
21 class TestBootLoaderInstallGrub2:
22 @patch('os.path.exists')
23 def setup(self, mock_os_path_exists):
24 mock_os_path_exists.return_value = True
25 Defaults.set_platform_name('x86_64')
26
27 self.firmware = mock.Mock()
28 self.firmware.efi_mode = mock.Mock(
29 return_value=None
30 )
31 self.device_map = {
32 'var': MappedDevice('/dev/var-device', Mock())
33 }
34 self.custom_args = {
35 'device_map': self.device_map,
36 'boot_device': '/dev/mapper/loop0p2',
37 'root_device': '/dev/mapper/loop0p1',
38 'efi_device': '/dev/mapper/loop0p3',
39 'prep_device': '/dev/mapper/loop0p2',
40 'system_root_volume': 'root',
41 'system_volumes': {
42 'boot/grub2': {
43 'volume_options': 'subvol=@/boot/grub2',
44 'volume_device': 'device'
45 }
46 },
47 'system_partitions': {
48 'var': ptable_entry_type(
49 mbsize=100,
50 clone=1,
51 partition_name='p.lxvar',
52 partition_type='t.linux',
53 partition_id=None,
54 mountpoint='/var',
55 filesystem='ext3',
56 label='var'
57 )
58 },
59 'firmware': self.firmware,
60 'target_removable': None,
61 'install_options': [],
62 'shim_options': []
63 }
64
65 self.root_mount = mock.Mock()
66 self.root_mount.device = self.custom_args['root_device']
67 self.root_mount.mountpoint = 'tmp_root'
68
69 self.volume_mount = mock.Mock()
70 self.volume_mount.device = self.custom_args['root_device']
71 self.volume_mount.mountpoint = 'tmp_volume'
72
73 self.partition_mount = mock.Mock()
74 self.partition_mount.device = self.custom_args['root_device']
75 self.partition_mount.mountpoint = 'tmp_partition'
76
77 self.boot_mount = mock.Mock()
78 self.boot_mount.device = self.custom_args['boot_device']
79 self.boot_mount.mountpoint = 'tmp_boot'
80
81 self.efi_mount = mock.Mock()
82 self.efi_mount.device = self.custom_args['efi_device']
83 self.efi_mount.mountpoint = 'tmp_efi'
84
85 self.device_mount = mock.Mock()
86 self.device_mount.device = 'devtmpfs'
87 self.device_mount.mountpoint = 'dev'
88
89 self.proc_mount = mock.Mock()
90 self.proc_mount.device = 'proc'
91 self.proc_mount.mountpoint = 'proc'
92
93 self.sysfs_mount = mock.Mock()
94 self.sysfs_mount.device = 'sysfs'
95 self.sysfs_mount.mountpoint = 'sys'
96
97 # the order of mount manager is reverse the order in the code
98 self.mount_managers = [
99 self.sysfs_mount,
100 self.proc_mount,
101 self.device_mount,
102 self.partition_mount,
103 self.volume_mount,
104 self.efi_mount,
105 self.boot_mount,
106 self.root_mount
107 ]
108
109 device_provider = mock.Mock()
110 device_provider.get_device = mock.Mock(
111 return_value='/dev/some-device'
112 )
113
114 self.bootloader = BootLoaderInstallGrub2(
115 Mock(), 'root_dir', device_provider, self.custom_args
116 )
117
118 def setup_method(self, cls):
119 self.setup()
120
121 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
122 def test_post_init_ppc_no_prep_device(self, mock_grub_path):
123 self.bootloader.arch = 'ppc64'
124 del self.custom_args['prep_device']
125 with raises(KiwiBootLoaderGrubInstallError):
126 self.bootloader.install()
127
128 @patch('kiwi.bootloader.install.grub2.Command.run')
129 @patch('kiwi.bootloader.install.grub2.MountManager')
130 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
131 def test_grub2_bootloader_not_installed(
132 self, mock_grub_path, mock_mount_manager, mock_command
133 ):
134 mock_grub_path.return_value = None
135 self.bootloader.arch = 'x86_64'
136 with raises(KiwiBootLoaderGrubDataError):
137 self.bootloader.install()
138
139 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
140 def test_unsupported_platform(self, mock_grub_path):
141 self.bootloader.arch = 'unsupported'
142 with raises(KiwiBootLoaderGrubPlatformError):
143 self.bootloader.install()
144
145 def test_post_init_no_boot_device(self):
146 with raises(KiwiBootLoaderGrubInstallError):
147 self.bootloader.post_init({})
148
149 def test_post_init_no_root_device(self):
150 with raises(KiwiBootLoaderGrubInstallError):
151 self.bootloader.post_init({'boot_device': 'a'})
152
153 def test_post_init_secure_boot_no_efi_device(self):
154 self.firmware.efi_mode.return_value = 'uefi'
155 del self.custom_args['efi_device']
156 with raises(KiwiBootLoaderGrubInstallError):
157 self.bootloader.post_init(self.custom_args)
158
159 def test_install_required(self):
160 assert self.bootloader.install_required() is True
161
162 def test_install_required_ppc_opal(self):
163 self.bootloader.arch = 'ppc64'
164 self.bootloader.firmware = mock.Mock()
165 self.bootloader.firmware.opal_mode = mock.Mock(
166 return_value=True
167 )
168 assert self.bootloader.install_required() is False
169
170 def test_install_required_efi_no_csm(self):
171 self.bootloader.arch = 'x86_64'
172 self.bootloader.firmware = mock.Mock()
173 self.bootloader.firmware.efi_mode = mock.Mock(
174 return_value='efi'
175 )
176 self.bootloader.firmware.legacy_bios_mode = mock.Mock(
177 return_value=False
178 )
179 assert self.bootloader.install_required() is False
180
181 def test_install_required_arm64(self):
182 self.bootloader.arch = 'arm64'
183 assert self.bootloader.install_required() is False
184
185 def test_install_required_riscv64(self):
186 self.bootloader.arch = 'riscv64'
187 assert self.bootloader.install_required() is False
188
189 @patch('kiwi.bootloader.install.grub2.Path.wipe')
190 @patch('kiwi.bootloader.install.grub2.Path.which')
191 @patch('kiwi.bootloader.install.grub2.Command.run')
192 @patch('kiwi.bootloader.install.grub2.MountManager')
193 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
194 @patch('kiwi.bootloader.install.grub2.SystemSetup')
195 def test_install_with_extra_boot_partition(
196 self, mock_SystemSetup, mock_grub_path,
197 mock_mount_manager, mock_command, mock_which, mock_wipe
198 ):
199 setup = Mock()
200 mock_SystemSetup.return_value = setup
201 mock_grub_path.return_value = \
202 self.root_mount.mountpoint + '/usr/lib/grub2/i386-pc'
203
204 def side_effect(device, mountpoint=None):
205 return self.mount_managers.pop()
206
207 mock_mount_manager.side_effect = side_effect
208
209 self.bootloader.install()
210 self.bootloader.root_mount.mount.assert_called_once_with(
211 options=['subvol=root']
212 )
213 self.boot_mount.mount.assert_called_once_with()
214 mock_which.assert_called_once_with(
215 root_dir='tmp_root', filename='grub2-install'
216 )
217 mock_command.assert_called_once_with(
218 [
219 'chroot', 'tmp_root', 'grub2-install', '--skip-fs-probe',
220 '--directory', '/usr/lib/grub2/i386-pc',
221 '--boot-directory', '/boot',
222 '--target', 'i386-pc',
223 '--modules', ' '.join(
224 Defaults.get_grub_platform_modules(multiboot=True)
225 ),
226 '/dev/some-device'
227 ]
228 )
229 setup.setup_selinux_file_contexts.assert_called_once_with()
230
231 @patch('kiwi.bootloader.install.grub2.Path.wipe')
232 @patch('kiwi.bootloader.install.grub2.Path.which')
233 @patch('kiwi.bootloader.install.grub2.Command.run')
234 @patch('kiwi.bootloader.install.grub2.MountManager')
235 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
236 @patch('os.path.exists')
237 def test_install_on_dasd_disk(
238 self, mock_os_path_exists, mock_grub_path,
239 mock_mount_manager, mock_command, mock_which, mock_wipe
240 ):
241 mock_os_path_exists.return_value = True
242 self.firmware.get_partition_table_type = mock.Mock(
243 return_value='dasd'
244 )
245
246 def side_effect(device, mountpoint=None):
247 return self.mount_managers.pop()
248
249 mock_mount_manager.side_effect = side_effect
250
251 self.bootloader.install()
252 self.bootloader.root_mount.mount.assert_called_once_with(
253 options=['subvol=root']
254 )
255 self.boot_mount.mount.assert_called_once_with()
256 self.bootloader.sysfs_mount.umount.assert_called_once_with()
257 assert mock_command.call_args_list == [
258 call(
259 [
260 'chroot', 'tmp_root', 'grub2-zipl-setup', '--keep'
261 ]
262 ),
263 call(
264 [
265 'mv',
266 'tmp_root/etc/default/zipl2grub.conf.in.orig',
267 'tmp_root/etc/default/zipl2grub.conf.in'
268 ]
269 ),
270 call(
271 [
272 'mv',
273 'tmp_root/boot/zipl/config',
274 'tmp_root/boot/zipl/config.kiwi'
275 ]
276 )
277 ]
278
279 @patch('kiwi.bootloader.install.grub2.Path.wipe')
280 @patch('kiwi.bootloader.install.grub2.Path.which')
281 @patch('kiwi.bootloader.install.grub2.Command.run')
282 @patch('kiwi.bootloader.install.grub2.MountManager')
283 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
284 @patch('kiwi.bootloader.install.grub2.SystemSetup')
285 def test_install_ppc_ieee1275(
286 self, mock_SystemSetup, mock_grub_path, mock_mount_manager,
287 mock_command, mock_which, mock_wipe
288 ):
289 setup = Mock()
290 mock_SystemSetup.return_value = setup
291 mock_grub_path.return_value = \
292 self.root_mount.mountpoint + '/usr/lib/grub2/powerpc-ieee1275'
293 self.bootloader.arch = 'ppc64'
294
295 def side_effect(device, mountpoint=None):
296 return self.mount_managers.pop()
297
298 mock_mount_manager.side_effect = side_effect
299
300 self.bootloader.install()
301 self.root_mount.mount.assert_called_once_with(
302 options=['subvol=root']
303 )
304 self.boot_mount.mount.assert_called_once_with()
305 mock_command.assert_called_once_with(
306 [
307 'chroot', 'tmp_root', 'grub2-install', '--skip-fs-probe',
308 '--no-nvram', '--directory', '/usr/lib/grub2/powerpc-ieee1275',
309 '--boot-directory', '/boot',
310 '--target', 'powerpc-ieee1275',
311 '--modules', ' '.join(
312 Defaults.get_grub_ofw_modules()
313 ),
314 self.custom_args['prep_device']
315 ]
316 )
317 setup.setup_selinux_file_contexts.assert_called_once_with()
318
319 @patch('kiwi.bootloader.install.grub2.Path.wipe')
320 @patch('kiwi.bootloader.install.grub2.Path.which')
321 @patch('kiwi.bootloader.install.grub2.Command.run')
322 @patch('kiwi.bootloader.install.grub2.MountManager')
323 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
324 @patch('kiwi.bootloader.install.grub2.SystemSetup')
325 def test_install_s390_emu(
326 self, mock_SystemSetup, mock_grub_path, mock_mount_manager,
327 mock_command, mock_which, mock_wipe
328 ):
329 setup = Mock()
330 mock_SystemSetup.return_value = setup
331 mock_grub_path.return_value = \
332 self.root_mount.mountpoint + '/usr/lib/grub2/s390x-emu'
333 self.bootloader.arch = 's390x'
334
335 def side_effect(device, mountpoint=None):
336 return self.mount_managers.pop()
337
338 mock_mount_manager.side_effect = side_effect
339
340 self.bootloader.install()
341 self.root_mount.mount.assert_called_once_with(
342 options=['subvol=root']
343 )
344 self.boot_mount.mount.assert_called_once_with()
345 mock_command.assert_called_once_with(
346 [
347 'chroot', 'tmp_root', 'grub2-install', '--skip-fs-probe',
348 '--no-nvram', '--directory', '/usr/lib/grub2/s390x-emu',
349 '--boot-directory', '/boot',
350 '--target', 's390x-emu',
351 '--modules', ' '.join(
352 Defaults.get_grub_s390_modules()
353 ),
354 '/dev/some-device'
355 ]
356 )
357 setup.setup_selinux_file_contexts.assert_called_once_with()
358
359 @patch('kiwi.bootloader.install.grub2.Path.wipe')
360 @patch('kiwi.bootloader.install.grub2.Path.which')
361 @patch('kiwi.bootloader.install.grub2.Command.run')
362 @patch('kiwi.bootloader.install.grub2.MountManager')
363 @patch('kiwi.bootloader.install.grub2.Defaults.get_grub_path')
364 @patch('kiwi.bootloader.install.grub2.SystemSetup')
365 def test_install(
366 self, mock_SystemSetup, mock_grub_path, mock_mount_manager,
367 mock_command, mock_which, mock_wipe
368 ):
369 setup = Mock()
370 mock_SystemSetup.return_value = setup
371 mock_which.return_value = None
372 mock_grub_path.return_value = \
373 self.root_mount.mountpoint + '/usr/lib/grub2/i386-pc'
374 self.boot_mount.device = self.root_mount.device
375
376 def side_effect(device, mountpoint=None):
377 return self.mount_managers.pop()
378
379 mock_mount_manager.side_effect = side_effect
380 self.bootloader.target_removable = True
381
382 self.bootloader.install()
383 self.root_mount.mount.assert_called_once_with(
384 options=['subvol=root']
385 )
386 self.partition_mount.mount.assert_called_once_with()
387 self.volume_mount.mount.assert_called_once_with(
388 options=['subvol=@/boot/grub2']
389 )
390 mock_command.assert_called_once_with(
391 [
392 'chroot', 'tmp_root', 'grub2-install',
393 '--removable', '--skip-fs-probe',
394 '--directory', '/usr/lib/grub2/i386-pc',
395 '--boot-directory', '/boot',
396 '--target', 'i386-pc',
397 '--modules', ' '.join(
398 Defaults.get_grub_platform_modules(multiboot=True)
399 ),
400 '/dev/some-device'
401 ]
402 )
403 setup.setup_selinux_file_contexts.assert_called_once_with()
404
405 @patch('kiwi.bootloader.install.grub2.Command.run')
406 @patch('kiwi.bootloader.install.grub2.MountManager')
407 @patch('os.path.exists')
408 @patch('os.access')
409 @patch('shutil.copy2')
410 def test_secure_boot_install(
411 self, mock_shutil_copy2, mock_access, mock_exists,
412 mock_mount_manager, mock_command
413 ):
414 mock_access.return_value = True
415 mock_exists.return_value = True
416 self.firmware.efi_mode.return_value = 'uefi'
417 self.boot_mount.device = self.root_mount.device
418
419 def side_effect(device, mountpoint=None):
420 return self.mount_managers.pop()
421
422 mock_mount_manager.side_effect = side_effect
423
424 self.bootloader.secure_boot_install()
425
426 assert mock_shutil_copy2.call_args_list == [
427 call(
428 'tmp_root/usr/sbin/grub2-install',
429 'tmp_root/usr/sbin/grub2-install.orig'
430 ),
431 call(
432 'tmp_root/bin/true',
433 'tmp_root/usr/sbin/grub2-install'
434 )
435 ]
436
437 assert mock_command.call_args_list == [
438 call([
439 'chroot', 'tmp_root', 'shim-install', '--removable',
440 '/dev/some-device'
441 ]),
442 call([
443 'chroot', 'tmp_root',
444 'mv', '/usr/sbin/grub2-install.orig',
445 '/usr/sbin/grub2-install'
446 ])
447 ]
448 self.root_mount.mount.assert_called_once_with(
449 options=['subvol=root']
450 )
451 self.partition_mount.mount.assert_called_once_with()
452 self.volume_mount.mount.assert_called_once_with(
453 options=['subvol=@/boot/grub2']
454 )
455 self.device_mount.bind_mount.assert_called_once_with()
456 self.proc_mount.bind_mount.assert_called_once_with()
457 self.sysfs_mount.bind_mount.assert_called_once_with()
458 self.efi_mount.mount.assert_called_once_with()
459
460 @patch('kiwi.bootloader.install.grub2.Path.which')
461 @patch('kiwi.bootloader.install.grub2.MountManager')
462 def test_secure_boot_install_no_shim_install(
463 self, mock_mount_manager, mock_which
464 ):
465 mock_which.return_value = None
466 self.firmware.efi_mode.return_value = 'uefi'
467
468 def side_effect(device, mountpoint=None):
469 return self.mount_managers.pop()
470
471 mock_mount_manager.side_effect = side_effect
472
473 self.bootloader.secure_boot_install()
474 self.root_mount.mount.assert_called_once_with(
475 options=['subvol=root']
476 )
477 self.partition_mount.mount.assert_called_once_with()
478 self.volume_mount.mount.assert_called_once_with(
479 options=['subvol=@/boot/grub2']
480 )
481 self.device_mount.bind_mount.assert_called_once_with()
482 self.proc_mount.bind_mount.assert_called_once_with()
483 self.sysfs_mount.bind_mount.assert_called_once_with()
484 self.efi_mount.mount.assert_called_once_with()
485 mock_which.assert_called_once_with(
486 filename='shim-install', root_dir='tmp_root'
487 )
488
489 @patch('kiwi.bootloader.install.grub2.Command.run')
490 @patch('kiwi.bootloader.install.grub2.MountManager')
491 def test_set_disk_password(self, mock_mount_manager, mock_command):
492 self.boot_mount.device = self.root_mount.device
493
494 def side_effect(device, mountpoint=None):
495 return self.mount_managers.pop()
496
497 mock_mount_manager.side_effect = side_effect
498
499 self.bootloader.root_mount = MagicMock()
500 self.bootloader.root_mount.mountpoint = 'root_mountpoint'
501 with patch('builtins.open', create=True) as mock_open:
502 mock_open.return_value = MagicMock(spec=io.IOBase)
503 file_handle = mock_open.return_value.__enter__.return_value
504 file_handle.read.return_value = 'data__cryptomount__data'
505 self.bootloader.set_disk_password('credentials')
506 file_handle.write.assert_called_once_with(
507 'data__cryptomount -p "credentials"__data'
508 )
509
510 @patch('kiwi.bootloader.install.grub2.Command.run')
511 @patch('kiwi.bootloader.install.grub2.MountManager')
512 @patch('kiwi.bootloader.install.grub2.ExitStack')
513 def test_set_disk_password_failed_to_open_grub_config(
514 self, mock_ExitStack, mock_mount_manager, mock_command
515 ):
516 self.boot_mount.device = self.root_mount.device
517
518 def side_effect(device, mountpoint=None):
519 return self.mount_managers.pop()
520
521 mock_mount_manager.side_effect = side_effect
522 self.bootloader.root_mount = MagicMock()
523 self.bootloader.root_mount.mountpoint = 'root_mountpoint'
524
525 with patch('builtins.open', create=True) as mock_open:
526 mock_open.side_effect = IOError()
527 with raises(KiwiBootLoaderDiskPasswordError):
528 self.bootloader.set_disk_password('credentials')