Remove hard-coded path to grub binaries

Use PATH variable to find grub binaries.

Closes-Bug: 1717255

Change-Id: Ic6a2bafdc87e33c4e1c4534fa355f995c824b945
This commit is contained in:
Vasyl Saienko 2017-09-14 07:05:25 -06:00
parent d6ff5116f4
commit db5272cfea
3 changed files with 28 additions and 20 deletions

View File

@ -108,12 +108,14 @@ def _install_grub2(device, root_uuid, efi_system_part_uuid=None):
# Add /bin to PATH variable as grub requires it to find efibootmgr
# when running in uefi boot mode.
# Add /usr/sbin to PATH variable to ensure it is there as we do
# not use full path to grub binary anymore.
path_variable = os.environ.get('PATH', '')
path_variable = '%s:/bin' % path_variable
path_variable = '%s:/bin:/usr/sbin' % path_variable
# Install grub
utils.execute('chroot %(path)s /bin/sh -c '
'"/usr/sbin/%(bin)s-install %(dev)s"' %
'"%(bin)s-install %(dev)s"' %
{'path': path, 'bin': binary_name, 'dev': device},
shell=True, env_variables={'PATH': path_variable})
# Also run grub-install with --removable, this installs grub to the
@ -126,13 +128,13 @@ def _install_grub2(device, root_uuid, efi_system_part_uuid=None):
# prevents NVRAM from being updated.
if efi_partition:
utils.execute('chroot %(path)s /bin/sh -c '
'"/usr/sbin/%(bin)s-install %(dev)s --removable"' %
'"%(bin)s-install %(dev)s --removable"' %
{'path': path, 'bin': binary_name, 'dev': device},
shell=True, env_variables={'PATH': path_variable})
# Generate the grub configuration file
utils.execute('chroot %(path)s /bin/sh -c '
'"/usr/sbin/%(bin)s-mkconfig -o '
'"%(bin)s-mkconfig -o '
'/boot/%(bin)s/grub.cfg"' %
{'path': path, 'bin': binary_name}, shell=True,
env_variables={'PATH': path_variable})

View File

@ -87,14 +87,14 @@ class TestImageExtension(test_base.BaseTestCase):
mock.call('mount', '-t', 'sysfs', 'none',
self.fake_dir + '/sys'),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-install %s"' %
'"grub-install %s"' %
(self.fake_dir, self.fake_dev)), shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-mkconfig -o '
'"grub-mkconfig -o '
'/boot/grub/grub.cfg"' % self.fake_dir),
shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call('umount', self.fake_dir + '/dev',
attempts=3, delay_on_retry=True),
mock.call('umount', self.fake_dir + '/proc',
@ -132,18 +132,18 @@ class TestImageExtension(test_base.BaseTestCase):
mock.call('mount', self.fake_efi_system_part,
self.fake_dir + '/boot/efi'),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-install %s"' %
'"grub-install %s"' %
(self.fake_dir, self.fake_dev)), shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-install %s --removable"' %
'"grub-install %s --removable"' %
(self.fake_dir, self.fake_dev)), shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-mkconfig -o '
'"grub-mkconfig -o '
'/boot/grub/grub.cfg"' % self.fake_dir),
shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call('umount', self.fake_dir + '/boot/efi',
attempts=3, delay_on_retry=True),
mock.call('umount', self.fake_dir + '/dev',
@ -192,18 +192,18 @@ class TestImageExtension(test_base.BaseTestCase):
mock.call('mount', self.fake_efi_system_part,
self.fake_dir + '/boot/efi'),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-install %s"' %
'"grub-install %s"' %
(self.fake_dir, self.fake_dev)), shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-install %s --removable"' %
'"grub-install %s --removable"' %
(self.fake_dir, self.fake_dev)), shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call(('chroot %s /bin/sh -c '
'"/usr/sbin/grub-mkconfig -o '
'"grub-mkconfig -o '
'/boot/grub/grub.cfg"' % self.fake_dir),
shell=True,
env_variables={'PATH': '/sbin:/bin'}),
env_variables={'PATH': '/sbin:/bin:/usr/sbin'}),
mock.call('umount', self.fake_dir + '/boot/efi',
attempts=3, delay_on_retry=True)]
mock_execute.assert_has_calls(expected)

View File

@ -0,0 +1,6 @@
---
fixes:
- Uses the PATH environment variable to get the grub (or grub2)
binary instead of assuming it is in /usr/sbin. This provides
flexibilty for grub to be installed in any PATH directory,
on a partitioned image.