1449
1449
os.makedirs(os.path.join('/mnt', 'preseed'))
1450
1450
magic.write_seed(seed, keys)
1452
#files and grub go in /mnt/efi if we're EFI and include EFI specifics
1453
#Check for a grub.cfg - replace as necessary
1456
if not os.path.exists(pivot):
1457
with misc.raised_privileges():
1459
mount = misc.execute_root('mount', self.device + EFI_ESP_PARTITION, pivot)
1461
raise RuntimeError, ("Error mounting %s%s" % (self.device, EFI_ESP_PARTITION))
1465
1452
#Check for a grub.cfg - replace as necessary
1466
1453
files = {'recovery_partition.cfg': 'grub.cfg',
1467
1454
'common.cfg' : 'common.cfg'}
1468
1455
for item in files:
1469
if os.path.exists(os.path.join(pivot, 'boot', 'grub', files[item])):
1456
full_path = os.path.join('/mnt', 'factory', files[item])
1457
if os.path.exists(full_path):
1470
1458
with misc.raised_privileges():
1471
shutil.move(os.path.join(pivot, 'boot', 'grub', files[item]),
1472
os.path.join(pivot, 'boot', 'grub', files[item]) + '.old')
1459
shutil.move(full_path, full_path + '.old')
1474
1461
with misc.raised_privileges():
1476
1462
magic.process_conf_file('/usr/share/dell/grub/' + item, \
1477
os.path.join(pivot, 'boot', 'grub', files[item]),\
1463
full_path, uuid, rp_part)
1479
1464
#Allow these to be invoked from a recovery solution launched by the BCD.
1481
shutil.copy(os.path.join(pivot, 'boot', 'grub', files[item]), \
1482
os.path.join('/tmp', files[item]))
1466
shutil.copy(full_path, os.path.join('/tmp', files[item]))
1468
#If we don't have grub binaries, build them
1470
grub_files = [ '/mnt/factory/grubx64.efi']
1472
grub_files = [ '/mnt/factory/core.img',
1473
'/mnt/factory/boot.img']
1474
for item in grub_files:
1475
if not os.path.exists(item):
1476
build = misc.execute_root('/usr/share/dell/grub/build-binaries.sh')
1478
raise RuntimeError, ("Error building grub binaries.")
1479
with misc.raised_privileges():
1480
magic.white_tree("copy", re.compile('.'), '/var/lib/dell-recovery', '/mnt/factory')
1485
1484
self.status("Installing GRUB", 88)
1487
#if we have a pre-built EFI binary, use that.
1488
if os.path.exists('/cdrom/factory/grubx64.efi') and \
1489
os.path.exists('/cdrom/factory/grub.cfg'):
1490
direct_path = pivot + '/efi/ubuntu'
1487
if not os.path.exists('/mnt/efi'):
1491
1488
with misc.raised_privileges():
1492
os.makedirs(direct_path)
1493
#copy our pre-built loader
1494
shutil.copy('/cdrom/factory/grubx64.efi', direct_path)
1496
bootmgr_output = magic.fetch_output(['efibootmgr']).split('\n')
1498
for line in bootmgr_output:
1499
if line.startswith('Boot') and 'ubuntu' in line:
1500
bootnum = line.split('Boot')[1].replace('*', '').split()[0]
1501
bootmgr = misc.execute_root('efibootmgr', '-q', '-b', bootnum, '-B')
1502
if bootmgr is False:
1503
raise RuntimeError, ("Error removing old EFI boot manager entries")
1504
bootmgr = misc.execute_root('efibootmgr', '-q', '-c', '-d',
1505
self.device, '-p', EFI_ESP_PARTITION, '-w',
1506
'-L', 'ubuntu', '-l', '\\EFI\ubuntu\\grubx64.efi')
1507
if bootmgr is False:
1508
raise RuntimeError, ("Error creating EFI boot manager entry.")
1509
#otherwise build one and install it
1511
grub = misc.execute_root('grub-install', '--root-directory=%s' % pivot)
1513
raise RuntimeError, ("Error installing grub to ESP")
1514
misc.execute_root('umount', pivot)
1489
os.makedirs('/mnt/efi')
1490
mount = misc.execute_root('mount', self.device + EFI_ESP_PARTITION, '/mnt/efi')
1492
raise RuntimeError, ("Error mounting %s%s" % (self.device, EFI_ESP_PARTITION))
1494
#find old entries and prep directory
1495
direct_path = '/mnt/efi' + '/efi/ubuntu'
1496
with misc.raised_privileges():
1497
os.makedirs(direct_path)
1498
#copy our pre-built loader
1499
shutil.copy('/mnt/factory/grubx64.efi', direct_path)
1501
bootmgr_output = magic.fetch_output(['efibootmgr']).split('\n')
1504
for line in bootmgr_output:
1505
if line.startswith('Boot') and 'ubuntu' in line:
1506
bootnum = line.split('Boot')[1].replace('*', '').split()[0]
1507
bootmgr = misc.execute_root('efibootmgr', '-q', '-b', bootnum, '-B')
1508
if bootmgr is False:
1509
raise RuntimeError, ("Error removing old EFI boot manager entries")
1510
#create new boot entry
1511
bootmgr = misc.execute_root('efibootmgr', '-q', '-c', '-d',
1512
self.device, '-p', EFI_ESP_PARTITION, '-w',
1513
'-L', 'ubuntu', '-l', '\\EFI\ubuntu\\grubx64.efi')
1514
if bootmgr is False:
1515
raise RuntimeError, ("Error creating EFI boot manager entry.")
1518
misc.execute_root('umount', '/mnt/efi')
1516
#allow red screening after MBR is replaced
1517
grub = misc.execute_root('grub-install', '--root-directory=%s' % pivot, '--force', self.device + grub_part)
1520
grub = misc.execute_root('grub-setup', '-d', '/mnt/factory', self.device)
1518
1521
if grub is False:
1519
raise RuntimeError, ("Error installing grub to %s%s" % (self.device, grub_part))
1521
#emulate factory process. place pre-built GRUB in the MBR
1522
if os.path.exists('%s/factory/core.img' % pivot) and \
1523
os.path.exists('%s/factory/boot.img' % pivot) and \
1524
os.path.exists('%s/factory/grub.cfg' % pivot):
1525
grub = misc.execute_root('grub-setup', '-d', '%s/factory' % pivot, self.device)
1527
raise RuntimeError, ("Error installing grub to %s" % (self.device))
1522
raise RuntimeError, ("Error installing grub to %s" % (self.device))
1530
1524
#dual boot needs primary #4 unmounted