Skip to content

Commit c02a094

Browse files
authored
Merge pull request #52 from stackhpc/upstream/yoga-2023-07-17
Synchronise yoga with upstream
2 parents 8e7a6dd + 9f84c8b commit c02a094

File tree

4 files changed

+91
-63
lines changed

4 files changed

+91
-63
lines changed

ironic_python_agent/efi_utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,15 @@ def get_boot_records():
275275
276276
:return: an iterator yielding pairs (boot number, boot record).
277277
"""
278-
efi_output = utils.execute('efibootmgr', '-v')
279-
for line in efi_output[0].split('\n'):
278+
# Invokes binary=True so we get a bytestream back.
279+
efi_output = utils.execute('efibootmgr', '-v', binary=True)
280+
# Bytes must be decoded before regex can be run and
281+
# matching to work as intended.
282+
# Also ignore errors on decoding, as we can basically get
283+
# garbage out of the nvram record, this way we don't fail
284+
# hard on unrelated records.
285+
cmd_output = efi_output[0].decode('utf-16', errors='ignore')
286+
for line in cmd_output.split('\n'):
280287
match = _ENTRY_LABEL.match(line)
281288
if match is not None:
282289
yield (match[1], match[2])
@@ -293,15 +300,15 @@ def add_boot_record(device, efi_partition, loader, label):
293300
# https://linux.die.net/man/8/efibootmgr
294301
utils.execute('efibootmgr', '-v', '-c', '-d', device,
295302
'-p', str(efi_partition), '-w', '-L', label,
296-
'-l', loader)
303+
'-l', loader, binary=True)
297304

298305

299306
def remove_boot_record(boot_num):
300307
"""Remove an EFI boot record with efibootmgr.
301308
302309
:param boot_num: the number of the boot record
303310
"""
304-
utils.execute('efibootmgr', '-b', boot_num, '-B')
311+
utils.execute('efibootmgr', '-b', boot_num, '-B', binary=True)
305312

306313

307314
def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition,

ironic_python_agent/tests/unit/extensions/test_image.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
from ironic_python_agent.tests.unit import base
3232

3333

34+
EFI_RESULT = ''.encode('utf-16')
35+
36+
3437
@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True)
3538
@mock.patch.object(ilib_utils, 'execute', autospec=True)
3639
@mock.patch.object(tempfile, 'mkdtemp', lambda *_: '/tmp/fake-dir')
@@ -230,7 +233,7 @@ def test__uefi_bootloader_given_partition(
230233

231234
mock_execute.side_effect = iter([('', ''), ('', ''),
232235
('', ''), ('', ''),
233-
('', ''), ('', ''),
236+
(EFI_RESULT, ''), (EFI_RESULT, ''),
234237
('', ''), ('', '')])
235238

236239
expected = [mock.call('efibootmgr', '--version'),
@@ -239,11 +242,11 @@ def test__uefi_bootloader_given_partition(
239242
mock.call('udevadm', 'settle'),
240243
mock.call('mount', self.fake_efi_system_part,
241244
self.fake_dir + '/boot/efi'),
242-
mock.call('efibootmgr', '-v'),
245+
mock.call('efibootmgr', '-v', binary=True),
243246
mock.call('efibootmgr', '-v', '-c', '-d', self.fake_dev,
244247
'-p', '1', '-w',
245248
'-L', 'ironic1', '-l',
246-
'\\EFI\\BOOT\\BOOTX64.EFI'),
249+
'\\EFI\\BOOT\\BOOTX64.EFI', binary=True),
247250
mock.call('umount', self.fake_dir + '/boot/efi',
248251
attempts=3, delay_on_retry=True),
249252
mock.call('sync')]
@@ -278,7 +281,7 @@ def test__uefi_bootloader_find_partition(
278281
mock_efi_bl.return_value = ['EFI/BOOT/BOOTX64.EFI']
279282
mock_execute.side_effect = iter([('', ''), ('', ''),
280283
('', ''), ('', ''),
281-
('', ''), ('', ''),
284+
(EFI_RESULT, ''), (EFI_RESULT, ''),
282285
('', ''), ('', '')])
283286

284287
expected = [mock.call('efibootmgr', '--version'),
@@ -287,11 +290,11 @@ def test__uefi_bootloader_find_partition(
287290
mock.call('udevadm', 'settle'),
288291
mock.call('mount', self.fake_efi_system_part,
289292
self.fake_dir + '/boot/efi'),
290-
mock.call('efibootmgr', '-v'),
293+
mock.call('efibootmgr', '-v', binary=True),
291294
mock.call('efibootmgr', '-v', '-c', '-d', self.fake_dev,
292295
'-p', '1', '-w',
293296
'-L', 'ironic1', '-l',
294-
'\\EFI\\BOOT\\BOOTX64.EFI'),
297+
'\\EFI\\BOOT\\BOOTX64.EFI', binary=True),
295298
mock.call('umount', self.fake_dir + '/boot/efi',
296299
attempts=3, delay_on_retry=True),
297300
mock.call('sync')]
@@ -332,10 +335,11 @@ def test__uefi_bootloader_with_entry_removal(
332335
Boot0001 ironic2 HD(1,GPT,4f3c6294-bf9b-4208-9808-111111111112)File(\EFI\Boot\BOOTX64.EFI)
333336
Boot0002 VENDMAGIC FvFile(9f3c6294-bf9b-4208-9808-be45dfc34b51)
334337
""" # noqa This is a giant literal string for testing.
338+
stdout_msg = stdout_msg.encode('utf-16')
335339
mock_execute.side_effect = iter([('', ''), ('', ''),
336340
('', ''), ('', ''),
337-
(stdout_msg, ''), ('', ''),
338-
('', ''), ('', ''),
341+
(stdout_msg, ''), (EFI_RESULT, ''),
342+
(EFI_RESULT, ''), (EFI_RESULT, ''),
339343
('', ''), ('', '')])
340344

341345
expected = [mock.call('efibootmgr', '--version'),
@@ -344,12 +348,12 @@ def test__uefi_bootloader_with_entry_removal(
344348
mock.call('udevadm', 'settle'),
345349
mock.call('mount', self.fake_efi_system_part,
346350
self.fake_dir + '/boot/efi'),
347-
mock.call('efibootmgr', '-v'),
348-
mock.call('efibootmgr', '-b', '0000', '-B'),
351+
mock.call('efibootmgr', '-v', binary=True),
352+
mock.call('efibootmgr', '-b', '0000', '-B', binary=True),
349353
mock.call('efibootmgr', '-v', '-c', '-d', self.fake_dev,
350354
'-p', '1', '-w',
351355
'-L', 'ironic1', '-l',
352-
'\\EFI\\BOOT\\BOOTX64.EFI'),
356+
'\\EFI\\BOOT\\BOOTX64.EFI', binary=True),
353357
mock.call('umount', self.fake_dir + '/boot/efi',
354358
attempts=3, delay_on_retry=True),
355359
mock.call('sync')]
@@ -395,6 +399,7 @@ def test__uefi_bootloader_with_entry_removal_lenovo(
395399
Boot0003* Network VenHw(1fad3248-0000-7950-2166-a1e506fdb83a,05000000)..GO..NO............U.E.F.I.:. . . .S.L.O.T.2. .(.2.F./.0./.0.). .P.X.E. .I.P.4. . .Q.L.o.g.i.c. .Q.L.4.1.2.6.2. .P.C.I.e. .2.5.G.b. .2.-.P.o.r.t. .S.F.P.2.8. .E.t.h.e.r.n.e.t. .A.d.a.p.t.e.r. .-. .P.X.E........A....................%.4..Z...............................................................Gd-.;.A..MQ..L.P.X.E. .I.P.4. .Q.L.o.g.i.c. .Q.L.4.1.2.6.2. .P.C.I.e. .2.5.G.b. .2.-.P.o.r.t. .S.F.P.2.8. .E.t.h.e.r.n.e.t. .A.d.a.p.t.e.r. .-. .P.X.E.......BO..NO............U.E.F.I.:. . . .S.L.O.T.1. .(.3.0./.0./.0.). .P.X.E. .I.P.4. . .Q.L.o.g.i.c. .Q.L.4.1.2.6.2. .P.C.I.e. .2.5.G.b. .2.-.P.o.r.t. .S.F.P.2.8. .E.t.h.e.r.n.e.t. .A.d.a.p.t.e.r. .-.
396400
Boot0004* ironic1 HD(1,GPT,55db8d03-c8f6-4a5b-9155-790dddc348fa,0x800,0x64000)/File(\EFI\boot\shimx64.efi)
397401
""" # noqa This is a giant literal string for testing.
402+
stdout_msg = stdout_msg.encode('utf-16')
398403
mock_execute.side_effect = iter([('', ''), ('', ''),
399404
('', ''), ('', ''),
400405
(stdout_msg, ''), ('', ''),
@@ -406,13 +411,13 @@ def test__uefi_bootloader_with_entry_removal_lenovo(
406411
mock.call('udevadm', 'settle'),
407412
mock.call('mount', self.fake_efi_system_part,
408413
self.fake_dir + '/boot/efi'),
409-
mock.call('efibootmgr', '-v'),
410-
mock.call('efibootmgr', '-b', '0000', '-B'),
411-
mock.call('efibootmgr', '-b', '0004', '-B'),
414+
mock.call('efibootmgr', '-v', binary=True),
415+
mock.call('efibootmgr', '-b', '0000', '-B', binary=True),
416+
mock.call('efibootmgr', '-b', '0004', '-B', binary=True),
412417
mock.call('efibootmgr', '-v', '-c', '-d', self.fake_dev,
413418
'-p', '1', '-w',
414419
'-L', 'ironic1', '-l',
415-
'\\EFI\\BOOT\\BOOTX64.EFI'),
420+
'\\EFI\\BOOT\\BOOTX64.EFI', binary=True),
416421
mock.call('umount', self.fake_dir + '/boot/efi',
417422
attempts=3, delay_on_retry=True),
418423
mock.call('sync')]
@@ -449,8 +454,8 @@ def test__add_multi_bootloaders(
449454

450455
mock_execute.side_effect = iter([('', ''), ('', ''),
451456
('', ''), ('', ''),
452-
('', ''), ('', ''),
453-
('', ''), ('', ''),
457+
(EFI_RESULT, ''), (EFI_RESULT, ''),
458+
(EFI_RESULT, ''), ('', ''),
454459
('', '')])
455460

456461
expected = [mock.call('efibootmgr', '--version'),
@@ -459,15 +464,15 @@ def test__add_multi_bootloaders(
459464
mock.call('udevadm', 'settle'),
460465
mock.call('mount', self.fake_efi_system_part,
461466
self.fake_dir + '/boot/efi'),
462-
mock.call('efibootmgr', '-v'),
467+
mock.call('efibootmgr', '-v', binary=True),
463468
mock.call('efibootmgr', '-v', '-c', '-d', self.fake_dev,
464469
'-p', '1', '-w',
465470
'-L', 'ironic1', '-l',
466-
'\\EFI\\BOOT\\BOOTX64.EFI'),
471+
'\\EFI\\BOOT\\BOOTX64.EFI', binary=True),
467472
mock.call('efibootmgr', '-v', '-c', '-d', self.fake_dev,
468473
'-p', '1', '-w',
469474
'-L', 'ironic2', '-l',
470-
'\\WINDOWS\\system32\\winload.efi'),
475+
'\\WINDOWS\\system32\\winload.efi', binary=True),
471476
mock.call('umount', self.fake_dir + '/boot/efi',
472477
attempts=3, delay_on_retry=True),
473478
mock.call('sync')]

0 commit comments

Comments
 (0)