2
0
mirror of https://github.com/mbusb/multibootusb synced 2024-11-18 15:25:46 +00:00
multibootusb/scripts/install.py

181 lines
8.3 KiB
Python
Raw Normal View History

2016-11-06 11:18:53 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Name: install.py
# Purpose: This module contain functions to install ISO files to USB disk non destructively.
# 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 shutil
import sys
import platform
import threading
import subprocess
from .usb import *
from .gen import *
2016-11-20 10:58:02 +00:00
# from .iso import *
from . import iso
2016-11-06 11:18:53 +00:00
from scripts.update_cfg_file import *
from . import config
from . import persistence
def install_distro():
"""
Install selected ISO to USB disk.
:return:
"""
2016-11-20 10:58:02 +00:00
usb_mount = config.usb_mount
2016-11-06 11:18:53 +00:00
install_dir = os.path.join(usb_mount, "multibootusb", iso_basename(config.iso_link))
2016-11-20 10:58:02 +00:00
_iso_file_list = iso.iso_file_list(config.iso_link)
2016-11-06 11:18:53 +00:00
if not os.path.exists(os.path.join(usb_mount, "multibootusb")):
print("Copying multibootusb directory to ", usb_mount)
shutil.copytree(resource_path(os.path.join("data", "tools", "multibootusb")),
os.path.join(config.usb_mount, "multibootusb"))
if not os.path.exists(install_dir):
os.makedirs(install_dir)
with open(os.path.join(install_dir, "multibootusb.cfg"), "w") as f:
f.write(config.distro)
with open(os.path.join(install_dir, "iso_file_list.cfg"), 'w') as f:
for file_path in _iso_file_list:
f.write(file_path + "\n")
print("Installing ", iso_name(config.iso_link), "on", install_dir)
if config.distro == "opensuse":
2016-11-20 10:58:02 +00:00
iso.iso_extract_file(config.iso_link, install_dir, 'boot')
2016-11-06 11:18:53 +00:00
status_text = "Copying ISO..."
if platform.system() == "Windows":
subprocess.call(["xcopy", config.iso_link, usb_mount], shell=True) # Have to use xcopy as python file copy is dead slow.
elif platform.system() == "Linux":
print(config.iso_link, usb_mount)
shutil.copy(config.iso_link, usb_mount)
2016-11-20 10:58:02 +00:00
elif config.distro == "Windows" or config.distro == "alpine":
2016-11-06 11:18:53 +00:00
print("Extracting iso to " + usb_mount)
2016-11-20 10:58:02 +00:00
iso_extract_full(config.iso_link, usb_mount)
2016-11-06 11:18:53 +00:00
elif config.distro == "trinity-rescue":
2016-11-23 17:33:07 +00:00
iso.iso_extract_file(config.iso_link, usb_mount, '*trk3')
2016-11-06 11:18:53 +00:00
elif config.distro == "ipfire":
2016-11-23 17:33:07 +00:00
iso.iso_extract_file(config.iso_link, usb_mount, '*.tlz')
2016-11-20 10:58:02 +00:00
iso.iso_extract_file(config.iso_link, usb_mount, 'distro.img')
iso.iso_extract_file(config.iso_link, install_dir, 'boot')
2016-11-06 11:18:53 +00:00
elif config.distro == "zenwalk":
2016-11-20 10:58:02 +00:00
config.status_text = "Copying ISO..."
iso.iso_extract_file(config.iso_link, install_dir, "kernel")
copy_iso(config.iso_link, install_dir)
elif config.distro == "salix-live":
2016-11-27 00:46:12 +00:00
# iso.iso_extract_file(config.iso_link, install_dir, "boot")
iso.iso_extract_file(config.iso_link, install_dir, '*syslinux')
iso.iso_extract_file(config.iso_link, install_dir, '*menus')
iso.iso_extract_file(config.iso_link, install_dir, '*vmlinuz')
iso.iso_extract_file(config.iso_link, install_dir, '*initrd*')
iso.iso_extract_file(config.iso_link, usb_mount, '*modules')
iso.iso_extract_file(config.iso_link, usb_mount, '*packages')
iso.iso_extract_file(config.iso_link, usb_mount, '*optional')
iso.iso_extract_file(config.iso_link, usb_mount, '*liveboot')
#iso.iso_extract_full(config.iso_link, usb_mount)
# config.status_text = "Copying ISO..."
# copy_iso(config.iso_link, install_dir)
2016-11-20 10:58:02 +00:00
elif config.distro == 'sgrubd2':
copy_iso(config.iso_link, install_dir)
2016-11-23 17:33:07 +00:00
elif config.distro == 'alt-linux':
iso.iso_extract_file(config.iso_link, install_dir, '-xr!*rescue')
iso.iso_extract_file(config.iso_link, config.usb_mount, 'rescue')
2016-11-06 11:18:53 +00:00
elif config.distro == "generic":
2016-11-27 00:46:12 +00:00
#with open(os.path.join(install_dir, "generic.cfg"), "w") as f:
# f.write(os.path.join(isolinux_bin_dir(config.iso_link), "generic") + ".bs")
iso_extract_full(config.iso_link, usb_mount)
elif config.distro == 'grub4dos':
iso_extract_full(config.iso_link, usb_mount)
2016-11-06 11:18:53 +00:00
else:
2016-11-20 10:58:02 +00:00
iso.iso_extract_full(config.iso_link, install_dir)
2016-11-06 11:18:53 +00:00
if platform.system() == 'Linux':
print('ISO extracted successfully. Sync is in progress...')
os.system('sync')
if config.persistence != 0:
config.status_text = 'Creating Persistence...'
persistence.create_persistence()
install_patch()
2016-11-20 10:58:02 +00:00
def copy_iso(src, dst):
if platform.system() == "Windows":
subprocess.call("xcopy " + src + " " + dst, shell=True)
elif platform.system() == "Linux":
shutil.copy(src, dst)
2016-11-06 11:18:53 +00:00
def install_progress():
"""
Function to calculate progress percentage of install.
:return:
"""
from . import progressbar
usb_details = details(config.usb_disk)
usb_mount = usb_details['mount_point']
usb_size_used = usb_details['size_used']
thrd = threading.Thread(target=install_distro, name="install_progress")
# thrd.daemon()
# install_size = usb_size_used / 1024
install_size = iso_size(config.iso_link) / 1024
final_size = (usb_size_used + iso_size(config.iso_link)) + config.persistence
thrd.start()
pbar = progressbar.ProgressBar(maxval=100).start() # bar = progressbar.ProgressBar(redirect_stdout=True)
while thrd.is_alive():
current_size = details(config.usb_disk)['size_used']
percentage = int((current_size / final_size) * 100)
if percentage > 100:
percentage = 100
config.percentage = percentage
pbar.update(percentage)
def install_patch():
2016-11-20 10:58:02 +00:00
"""
Function to certain distros which uses makeboot.sh script for making bootable usb disk.
This is required to make sure that same version (32/64 bit) of modules present is the isolinux directory
:return:
"""
2016-11-06 11:18:53 +00:00
if config.distro == 'debian':
2016-11-20 10:58:02 +00:00
if platform.system() == 'Linux': # Need to syn under Linux. Otherwise, USB disk becomes random read only.
2016-11-06 11:18:53 +00:00
os.system('sync')
iso_cfg_ext_dir = os.path.join(multibootusb_host_dir(), "iso_cfg_ext_dir")
2016-11-20 10:58:02 +00:00
isolinux_path = os.path.join(iso_cfg_ext_dir, isolinux_bin_path(config.iso_link))
2016-11-06 11:18:53 +00:00
iso_linux_bin_dir = isolinux_bin_dir(config.iso_link)
config.syslinux_version = isolinux_version(isolinux_path)
2016-11-20 10:58:02 +00:00
iso_file_list = iso.iso_file_list(config.iso_link)
os.path.join(config.usb_mount, "multibootusb", iso_basename(config.iso_link), isolinux_bin_dir(config.iso_link))
2016-11-06 11:18:53 +00:00
if any("makeboot.sh" in s.lower() for s in iso_file_list):
for module in os.listdir(os.path.join(config.usb_mount, "multibootusb", iso_basename(config.iso_link),
2016-11-20 10:58:02 +00:00
isolinux_bin_dir(config.iso_link))):
2016-11-06 11:18:53 +00:00
if module.endswith(".c32"):
if os.path.exists(os.path.join(config.usb_mount, "multibootusb", iso_basename(config.iso_link),
2016-11-20 10:58:02 +00:00
isolinux_bin_dir(config.iso_link), module)):
2016-11-06 11:18:53 +00:00
try:
os.remove(os.path.join(config.usb_mount, "multibootusb",
2016-11-20 10:58:02 +00:00
iso_basename(config.iso_link), isolinux_bin_dir(config.iso_link), module))
2016-11-06 11:18:53 +00:00
print("Copying ", module)
print((resource_path(
os.path.join(multibootusb_host_dir(), "syslinux", "modules", config.syslinux_version, module)),
os.path.join(config.usb_mount, "multibootusb", iso_basename(config.iso_link),
2016-11-20 10:58:02 +00:00
isolinux_bin_dir(config.iso_link), module)))
2016-11-06 11:18:53 +00:00
shutil.copy(resource_path(
os.path.join(multibootusb_host_dir(), "syslinux", "modules", config.syslinux_version, module)),
os.path.join(config.usb_mount, "multibootusb", iso_basename(config.iso_link),
2016-11-20 10:58:02 +00:00
isolinux_bin_dir(config.iso_link), module))
2016-11-06 11:18:53 +00:00
except Exception as err:
print(err)
print("Could not copy ", module)
else:
print('Patch not required...')
if __name__ == '__main__':
config.iso_link = '../../../DISTROS/2016/slitaz-4.0.iso'
install_distro()