Do not try unmounting the EFI partition if it was not mounted

If mounting the root partition fails for some reason, we try to unmount
the EFI partition, which is not mounted at this point. This results in
a new exception hiding the real failure. This change fixes it.

Change-Id: I0ec636a361eda71b4149e4a7ba1538a9bbf6ec34
Closes-Bug: #1732932
This commit is contained in:
Dmitry Tantsur 2017-11-17 16:34:24 +01:00 committed by Julia Kreger
parent 831576c906
commit db4694de24
2 changed files with 33 additions and 1 deletions

View File

@ -82,6 +82,7 @@ def _install_grub2(device, root_uuid, efi_system_part_uuid=None):
root_partition = _get_partition(device, uuid=root_uuid)
efi_partition = None
efi_partition_mount_point = None
efi_mounted = False
try:
# Mount the partition and binds
@ -101,6 +102,7 @@ def _install_grub2(device, root_uuid, efi_system_part_uuid=None):
if not os.path.exists(efi_partition_mount_point):
os.makedirs(efi_partition_mount_point)
utils.execute('mount', efi_partition, efi_partition_mount_point)
efi_mounted = True
binary_name = "grub"
if os.path.exists(os.path.join(path, 'usr/sbin/grub2-install')):
@ -155,7 +157,7 @@ def _install_grub2(device, root_uuid, efi_system_part_uuid=None):
# If umount fails for efi partition, then we cannot be sure that all
# the changes were written back to the filesystem.
try:
if efi_partition:
if efi_mounted:
utils.execute('umount', efi_partition_mount_point, attempts=3,
delay_on_retry=True)
except processutils.ProcessExecutionError as e:

View File

@ -208,6 +208,36 @@ class TestImageExtension(base.IronicAgentTest):
attempts=3, delay_on_retry=True)]
mock_execute.assert_has_calls(expected)
@mock.patch.object(os, 'environ', autospec=True)
@mock.patch.object(os, 'makedirs', autospec=True)
@mock.patch.object(image, '_get_partition', autospec=True)
def test__install_grub2_uefi_mount_fails(
self, mock_get_part_uuid, mkdir_mock, environ_mock, mock_execute,
mock_dispatch):
mock_get_part_uuid.side_effect = [self.fake_root_part,
self.fake_efi_system_part]
def mount_raise_func(*args, **kwargs):
if args[0] == 'mount':
raise processutils.ProcessExecutionError('error')
mock_execute.side_effect = mount_raise_func
self.assertRaises(errors.CommandExecutionError,
image._install_grub2,
self.fake_dev, root_uuid=self.fake_root_uuid,
efi_system_part_uuid=self.fake_efi_system_part_uuid)
expected = [mock.call('mount', '/dev/fake2', self.fake_dir),
mock.call('umount', self.fake_dir + '/dev',
attempts=3, delay_on_retry=True),
mock.call('umount', self.fake_dir + '/proc',
attempts=3, delay_on_retry=True),
mock.call('umount', self.fake_dir + '/sys',
attempts=3, delay_on_retry=True),
mock.call('umount', self.fake_dir, attempts=3,
delay_on_retry=True)]
mock_execute.assert_has_calls(expected)
@mock.patch.object(image, '_get_partition', autospec=True)
def test__install_grub2_command_fail(self, mock_get_part_uuid,
mock_execute,