2
0
mirror of https://github.com/mbusb/multibootusb synced 2024-11-01 15:40:16 +00:00
multibootusb/scripts/syslinux.py

214 lines
11 KiB
Python
Raw Normal View History

2016-11-06 11:18:53 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Name: syslinux.py
# Purpose: Module to install syslinux and extlinux on selected USB disk.
# Authors: Sundar
# Licence: This file is a part of multibootusb package. You can redistribute it or modify
# under the terms of GNU General Public License, v.2 or above
import os
import sys
import subprocess
import platform
from .gen import *
from . import usb
from .iso import *
from . import config
extlinux_path = os.path.join(multibootusb_host_dir(), "syslinux", "bin", "extlinux4")
syslinux_path = os.path.join(multibootusb_host_dir(), "syslinux", "bin", "syslinux4")
extlinux_fs = ["ext2", "ext3", "ext4", "Btrfs"]
syslinux_fs = ["vfat", "ntfs", "FAT32", "NTFS"]
mbr_bin = resource_path(os.path.join("data", "tools", "mbr.bin"))
def set_boot_flag(usb_disk):
if platform.system() == "Linux":
2016-12-26 17:53:21 +00:00
log ("\nChecking boot flag on " + usb_disk[:-1], '\n')
2016-11-06 11:18:53 +00:00
cmd_out = subprocess.check_output("parted -m -s " + usb_disk[:-1] + " print", shell=True)
if b'boot' in cmd_out:
2016-12-26 17:53:21 +00:00
log ("\nDisk " + usb_disk[:-1] + " already has boot flag.\n")
2016-11-06 11:18:53 +00:00
return True
else:
2016-12-26 17:53:21 +00:00
log ("\nExecuting ==> parted " + usb_disk[:-1] + " set 1 boot on", '\n')
2016-11-06 11:18:53 +00:00
if subprocess.call("parted " + usb_disk[:-1] + " set 1 boot on", shell=True) == 0:
2016-12-26 17:53:21 +00:00
log ("\nBoot flag set to bootable " + usb_disk[:-1], '\n')
2016-11-06 11:18:53 +00:00
return True
else:
2016-12-26 17:53:21 +00:00
log ("\nUnable to set boot flag on " + usb_disk[:-1], '\n')
2016-11-06 11:18:53 +00:00
return False
def syslinux_default(usb_disk, version=4):
"""
Install Syslinux of a selected drive
:param usb_disk: '/dev/sdx' on linux and 'E:' on Windows
:version: Default version is 4. Change it if you wish. But necessary files needs to be copied accordingly
:return: Bootable USB disk :-)
"""
usb_details = usb.details(usb_disk)
usb_fs = usb_details['file_system']
usb_mount = usb_details['mount_point']
mbr_install_cmd = 'dd bs=440 count=1 conv=notrunc if=' + mbr_bin + ' of=' + usb_disk[:-1]
2016-12-26 17:53:21 +00:00
# log (usb_fs)
2016-11-06 11:18:53 +00:00
if usb_fs in extlinux_fs:
extlinu_cmd = extlinux_path + ' --install ' + os.path.join(usb_mount, 'multibootusb')
if os.access(extlinux_path, os.X_OK) is False:
subprocess.call('chmod +x ' + extlinux_path, shell=True)
2016-12-26 17:53:21 +00:00
log ("\nExecuting ==> " + extlinu_cmd)
config.status_text = 'Installing default extlinux version 4...'
2016-11-06 11:18:53 +00:00
if subprocess.call(extlinu_cmd, shell=True) == 0:
2016-12-26 17:53:21 +00:00
log ("\nDefault Extlinux install is success...\n")
config.status_text = 'Default extlinux install is success...'
config.status_text = 'Installing mbr...'
2016-12-26 17:53:21 +00:00
log ('\nExecuting ==> ' + mbr_install_cmd)
2016-11-06 11:18:53 +00:00
if subprocess.call(mbr_install_cmd, shell=True) == 0:
2017-04-20 09:49:15 +00:00
config.status_text = 'mbr install is successful...'
log("\nmbr install is success...\n")
2016-11-06 11:18:53 +00:00
if set_boot_flag(usb_disk) is True:
return True
elif usb_fs in syslinux_fs:
if platform.system() == "Linux":
syslinux_cmd = syslinux_path + ' -i -d multibootusb ' + usb_disk
if os.access(syslinux_path, os.X_OK) is False:
subprocess.call('chmod +x ' + syslinux_path, shell=True)
2016-12-26 17:53:21 +00:00
log ("\nExecuting ==> " + syslinux_cmd + "\n")
config.status_text = 'Installing default syslinux version 4...'
2016-11-06 11:18:53 +00:00
if subprocess.call(syslinux_cmd, shell=True) == 0:
2016-12-26 17:53:21 +00:00
log ("\nDefault syslinux install is success...\n")
config.status_text = 'Default syslinux successfully installed...'
2016-11-06 11:18:53 +00:00
if subprocess.call(mbr_install_cmd, shell=True) == 0:
config.status_text = 'mbr install is success...'
2016-12-26 17:53:21 +00:00
log ("\nmbr install is success...\n")
2016-11-06 11:18:53 +00:00
if set_boot_flag(usb_disk) is True:
return True
else:
2016-12-26 17:53:21 +00:00
log ("\nFailed to install default syslinux...\n")
config.status_text = 'Failed to install default syslinux...'
2016-11-06 11:18:53 +00:00
return False
elif platform.system() == "Windows":
syslinux = resource_path(os.path.join(multibootusb_host_dir(), "syslinux", "bin", "syslinux4.exe"))
2016-12-26 17:53:21 +00:00
log ('Executing ==>' + syslinux + ' -maf -d multibootusb ' + usb_disk)
config.status_text = 'Installing default syslinux version 4...'
2016-11-06 11:18:53 +00:00
if subprocess.call(syslinux + ' -maf -d multibootusb ' + usb_disk, shell=True) == 0:
config.status_text = 'Default syslinux successfully installed...'
2016-12-26 17:53:21 +00:00
log ("\nDefault syslinux install is success...\n")
2016-11-06 11:18:53 +00:00
return True
else:
2016-12-26 17:53:21 +00:00
log ("\nFailed to install default syslinux...\n")
config.status_text = 'Failed to install default syslinux...'
2016-11-06 11:18:53 +00:00
return False
def syslinux_distro_dir(usb_disk, iso_link, distro):
"""
Install syslinux/extlinux on distro specific isolinux directory.
:param usb_disk: '/dev/sdx' on linux and 'E:' on Windows
:param iso_link: Path to ISO file
:return:
"""
usb_details = usb.details(usb_disk)
usb_fs = usb_details['file_system']
usb_mount = usb_details['mount_point']
isolinux_bin_dir(iso_link)
if isolinux_bin_exist(iso_link) is False:
2016-12-26 17:53:21 +00:00
log ('Distro does not use isolinux for booting ISO.')
2016-11-06 11:18:53 +00:00
else:
2016-11-20 10:58:02 +00:00
# iso_cfg_ext_dir = os.path.join(multibootusb_host_dir(), "iso_cfg_ext_dir")
_iso_cfg_ext_dir = iso_cfg_ext_dir()
isolinux_path = os.path.join(_iso_cfg_ext_dir, isolinux_bin_path(iso_link))
2016-11-06 11:18:53 +00:00
iso_linux_bin_dir = isolinux_bin_dir(iso_link)
config.syslinux_version = isolinux_version(isolinux_path)
if int(config.syslinux_version) < 3:
log('Distro uses really old isolinux. Installing version 3 instead of 2.')
config.syslinux_version = '3'
2016-11-06 11:18:53 +00:00
if distro == "generic" or distro == "alpine":
install_dir = usb_mount
2016-11-20 10:58:02 +00:00
distro_syslinux_install_dir = os.path.join(usb_mount, iso_linux_bin_dir.strip("/")).replace(usb_mount, "")
distro_sys_install_bs = os.path.join(install_dir, iso_linux_bin_dir.strip("/"), distro + '.bs')
2016-11-06 11:18:53 +00:00
else:
install_dir = os.path.join(usb_mount, "multibootusb", iso_basename(iso_link))
2016-11-20 10:58:02 +00:00
distro_syslinux_install_dir = os.path.join(install_dir, iso_linux_bin_dir.strip("/")).replace(usb_mount, "")
distro_sys_install_bs = os.path.join(install_dir, iso_linux_bin_dir.strip("/"), distro + '.bs')
2017-04-13 07:39:25 +00:00
# log (distro_sys_install_bs)
# log (distro_syslinux_install_dir)
2016-11-06 11:18:53 +00:00
if usb_fs in syslinux_fs:
if config.syslinux_version == str(3):
if distro == "generic" and iso_linux_bin_dir == "/":
option = ""
else:
option = " -d "
else:
if distro == "generic" and iso_linux_bin_dir == "/":
option = " -i "
else:
option = " -i -d "
if platform.system() == "Linux":
syslinux_path = os.path.join(multibootusb_host_dir(), "syslinux", "bin", "syslinux") + config.syslinux_version
if os.access(syslinux_path, os.X_OK) is False:
subprocess.call('chmod +x ' + syslinux_path, shell=True) == 0
sys_cmd = syslinux_path + option + quote(distro_syslinux_install_dir) + ' ' + usb_disk
dd_cmd = 'dd if=' + usb_disk + ' ' + 'of=' + quote(distro_sys_install_bs) + ' count=1'
2016-12-26 17:53:21 +00:00
log ("Executing ==> " + sys_cmd)
config.status_text = 'Installing distro specific syslinux...'
2016-11-06 11:18:53 +00:00
if subprocess.call(sys_cmd, shell=True) == 0:
2017-04-20 09:49:15 +00:00
config.status_text = 'Syslinux install on distro directory is successful...'
log ("\nSyslinux install on distro directory is successful...\n")
2016-12-26 17:53:21 +00:00
log ('Executing ==> ' + dd_cmd + '\n')
config.status_text = 'Copying boot sector...'
2016-11-06 11:18:53 +00:00
if subprocess.call(dd_cmd, shell=True) == 0:
2017-04-20 09:49:15 +00:00
config.status_text = 'Bootsector copy is successful...'
log ("\nBootsector copy is successful...\n")
2016-11-06 11:18:53 +00:00
else:
config.status_text = 'Failed to copy boot sector...'
2016-12-26 17:53:21 +00:00
log ("\nFailed to copy boot sector...\n")
2016-11-06 11:18:53 +00:00
else:
config.status_text = 'Failed to install syslinux on distro directory...'
2016-12-26 17:53:21 +00:00
log ("\nFailed to install syslinux on distro directory...\n")
2016-11-06 11:18:53 +00:00
elif platform.system() == "Windows":
syslinux_path = resource_path(os.path.join(multibootusb_host_dir(), "syslinux", "bin")) + \
"\syslinux" + config.syslinux_version + ".exe"
distro_syslinux_install_dir = "/" + distro_syslinux_install_dir.replace("\\", "/")
distro_sys_install_bs = distro_sys_install_bs.replace("/", "\\")
sys_cmd = syslinux_path + option + distro_syslinux_install_dir + ' ' + usb_disk + ' ' + \
distro_sys_install_bs
log("\nExecuting ==> " + sys_cmd + '\n')
config.status_text = 'Installing distro specific syslinux...'
2016-11-06 11:18:53 +00:00
if subprocess.call(sys_cmd, shell=True) == 0:
2017-04-20 09:49:15 +00:00
config.status_text = 'Syslinux install on distro directory is successful...'
2016-12-26 17:53:21 +00:00
log ("\nSyslinux install was successful on distro directory...\n")
2016-11-06 11:18:53 +00:00
else:
config.status_text = 'Failed to install syslinux on distro directory...'
2016-12-26 17:53:21 +00:00
log ("\nFailed to install syslinux on distro directory...\n")
2016-11-06 11:18:53 +00:00
elif usb_fs in extlinux_fs:
if platform.system() == "Linux":
distro_syslinux_install_dir = os.path.join(install_dir, iso_linux_bin_dir.strip("/"))
syslinux_path = os.path.join(multibootusb_host_dir(), "syslinux", "bin", "extlinux") + config.syslinux_version
ext_cmd = syslinux_path + " --install " + distro_syslinux_install_dir
dd_cmd = 'dd if=' + usb_disk + ' ' + 'of=' + quote(distro_sys_install_bs) + ' count=1'
2016-11-06 11:18:53 +00:00
if os.access(syslinux_path, os.X_OK) is False:
subprocess.call('chmod +x ' + syslinux_path, shell=True) == 0
2016-12-26 17:53:21 +00:00
log ("Executing ==> " + ext_cmd)
2016-11-06 11:18:53 +00:00
if subprocess.call(ext_cmd, shell=True) == 0:
2017-04-20 09:49:15 +00:00
log ("\nSyslinux install on distro directory is successful...\n")
2016-12-26 17:53:21 +00:00
log ('Executing ==> ' + dd_cmd, '\n')
2016-11-06 11:18:53 +00:00
if subprocess.call(dd_cmd, shell=True) == 0:
2017-04-20 09:49:15 +00:00
log ("\nBootsector copy is successful...\n")
2016-11-06 11:18:53 +00:00
else:
2016-12-26 17:53:21 +00:00
log ("\nFailed to install syslinux on distro directory...\n")
2016-11-06 11:18:53 +00:00
if __name__ == '__main__':
if os.geteuid() != 0:
2016-12-26 17:53:21 +00:00
log ('Please running this script with sudo/root/admin privilage.')
2016-11-06 11:18:53 +00:00
exit(1)
else:
syslinux_distro_dir('/dev/sdb1', '../../../DISTROS/2016/debian-live-8.3.0-amd64-lxde-desktop.iso', 'debian')
syslinux_default('/dev/sdb1')