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

197 lines
9.1 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
2017-04-25 08:33:19 +00:00
import time
2016-11-06 11:18:53 +00:00
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
install_dir = os.path.join(config.usb_mount, "multibootusb", iso_basename(config.image_path))
2017-04-13 07:39:25 +00:00
_iso_file_list = iso.iso_file_list(config.image_path)
2016-11-20 10:58:02 +00:00
2016-11-06 11:18:53 +00:00
if not os.path.exists(os.path.join(usb_mount, "multibootusb")):
2016-12-26 17:53:21 +00:00
log("Copying multibootusb directory to " + usb_mount)
2016-11-06 11:18:53 +00:00
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")
2017-04-13 07:39:25 +00:00
log("Installing " + iso_name(config.image_path) + " on " + install_dir)
2016-11-06 11:18:53 +00:00
if config.distro == "opensuse":
2017-04-13 07:39:25 +00:00
iso.iso_extract_file(config.image_path, install_dir, 'boot')
2016-11-06 11:18:53 +00:00
status_text = "Copying ISO..."
if platform.system() == "Windows":
2017-04-13 07:39:25 +00:00
subprocess.call(["xcopy", config.image_path, usb_mount], shell=True) # Have to use xcopy as python file copy is dead slow.
2016-11-06 11:18:53 +00:00
elif platform.system() == "Linux":
2017-04-13 07:39:25 +00:00
log("Copying " + config.image_path + " to " + usb_mount)
shutil.copy(config.image_path, usb_mount)
elif config.distro == "Windows" or config.distro == "alpine" or config.distro == 'pc-unlocker'\
or config.distro == 'pc-tool' or config.distro == 'grub2only':
2016-12-26 17:53:21 +00:00
log("Extracting iso to " + usb_mount)
2017-04-13 07:39:25 +00:00
iso_extract_full(config.image_path, usb_mount)
2016-11-06 11:18:53 +00:00
elif config.distro == "trinity-rescue":
2017-04-13 07:39:25 +00:00
iso.iso_extract_file(config.image_path, usb_mount, '*trk3')
2016-11-06 11:18:53 +00:00
elif config.distro == "ipfire":
2017-04-13 07:39:25 +00:00
iso.iso_extract_file(config.image_path, usb_mount, '*.tlz')
iso.iso_extract_file(config.image_path, usb_mount, 'distro.img')
iso.iso_extract_file(config.image_path, 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..."
2017-04-13 07:39:25 +00:00
iso.iso_extract_file(config.image_path, install_dir, "kernel")
copy_iso(config.image_path, install_dir)
2016-11-20 10:58:02 +00:00
elif config.distro == "salix-live":
2017-04-13 07:39:25 +00:00
# iso.iso_extract_file(config.image_path, install_dir, "boot")
iso.iso_extract_file(config.image_path, install_dir, '*syslinux')
iso.iso_extract_file(config.image_path, install_dir, '*menus')
iso.iso_extract_file(config.image_path, install_dir, '*vmlinuz')
iso.iso_extract_file(config.image_path, install_dir, '*initrd*')
iso.iso_extract_file(config.image_path, usb_mount, '*modules')
iso.iso_extract_file(config.image_path, usb_mount, '*packages')
iso.iso_extract_file(config.image_path, usb_mount, '*optional')
iso.iso_extract_file(config.image_path, usb_mount, '*liveboot')
#iso.iso_extract_full(config.image_path, usb_mount)
config.status_text = "Copying ISO..."
2017-04-13 07:39:25 +00:00
copy_iso(config.image_path, install_dir)
2016-11-20 10:58:02 +00:00
elif config.distro == 'sgrubd2':
2017-04-13 07:39:25 +00:00
copy_iso(config.image_path, install_dir)
2016-11-23 17:33:07 +00:00
elif config.distro == 'alt-linux':
2017-04-13 07:39:25 +00:00
iso.iso_extract_file(config.image_path, install_dir, '-xr!*rescue')
iso.iso_extract_file(config.image_path, 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:
2017-04-13 07:39:25 +00:00
# f.write(os.path.join(isolinux_bin_dir(config.image_path), "generic") + ".bs")
iso_extract_full(config.image_path, usb_mount)
2016-11-27 00:46:12 +00:00
elif config.distro == 'grub4dos':
2017-04-13 07:39:25 +00:00
iso_extract_full(config.image_path, usb_mount)
2016-11-30 16:29:03 +00:00
elif config.distro == 'ReactOS':
2017-04-13 07:39:25 +00:00
iso_extract_full(config.image_path, usb_mount)
elif config.distro == 'grub4dos_iso' or config.distro == 'raw_iso':
2017-04-13 07:39:25 +00:00
copy_iso(config.image_path, install_dir)
2016-11-06 11:18:53 +00:00
else:
2017-04-13 07:39:25 +00:00
iso.iso_extract_full(config.image_path, install_dir)
2016-11-06 11:18:53 +00:00
if platform.system() == 'Linux':
2016-12-26 17:53:21 +00:00
log('ISO extracted successfully. Sync is in progress...')
2016-11-06 11:18:53 +00:00
os.system('sync')
if config.persistence != 0:
2017-04-20 09:49:15 +00:00
log('Creating persistence...')
config.status_text = 'Creating persistence...'
2016-11-06 11:18:53 +00:00
persistence.create_persistence()
install_patch()
2016-11-20 10:58:02 +00:00
def copy_iso(src, dst):
2017-02-05 09:59:07 +00:00
"""
A simple wrapper for copying larger files. This is necessary as
shutil copy files is much slower under Windows platform
:param src: Path to source file
:param dst: Destination directory
:return:
"""
2016-11-20 10:58:02 +00:00
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)
config.usb_mount = usb_details['mount_point']
2016-11-06 11:18:53 +00:00
usb_size_used = usb_details['size_used']
thrd = threading.Thread(target=install_distro, name="install_progress")
# thrd.daemon()
# install_size = usb_size_used / 1024
2017-04-13 07:39:25 +00:00
install_size = iso_size(config.image_path) / 1024
final_size = (usb_size_used + iso_size(config.image_path)) + config.persistence
2016-11-06 11:18:53 +00:00
thrd.start()
pbar = progressbar.ProgressBar(maxval=100).start() # bar = progressbar.ProgressBar(redirect_stdout=True)
while thrd.is_alive():
2017-04-26 10:59:17 +00:00
current_size = shutil.disk_usage(usb_details['mount_point'])[1]
2016-11-06 11:18:53 +00:00
percentage = int((current_size / final_size) * 100)
if percentage > 100:
percentage = 100
config.percentage = percentage
pbar.update(percentage)
2017-04-25 08:33:19 +00:00
time.sleep(0.1)
2016-11-06 11:18:53 +00:00
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")
2017-04-13 07:39:25 +00:00
isolinux_path = os.path.join(iso_cfg_ext_dir, isolinux_bin_path(config.image_path))
iso_linux_bin_dir = isolinux_bin_dir(config.image_path)
2016-11-06 11:18:53 +00:00
config.syslinux_version = isolinux_version(isolinux_path)
2017-04-13 07:39:25 +00:00
iso_file_list = iso.iso_file_list(config.image_path)
os.path.join(config.usb_mount, "multibootusb", iso_basename(config.image_path), isolinux_bin_dir(config.image_path))
2016-11-06 11:18:53 +00:00
if any("makeboot.sh" in s.lower() for s in iso_file_list):
2017-04-13 07:39:25 +00:00
for module in os.listdir(os.path.join(config.usb_mount, "multibootusb", iso_basename(config.image_path),
isolinux_bin_dir(config.image_path))):
2016-11-06 11:18:53 +00:00
if module.endswith(".c32"):
2017-04-13 07:39:25 +00:00
if os.path.exists(os.path.join(config.usb_mount, "multibootusb", iso_basename(config.image_path),
isolinux_bin_dir(config.image_path), module)):
2016-11-06 11:18:53 +00:00
try:
os.remove(os.path.join(config.usb_mount, "multibootusb",
2017-04-13 07:39:25 +00:00
iso_basename(config.image_path), isolinux_bin_dir(config.image_path), module))
2016-12-26 17:53:21 +00:00
log("Copying " + module)
log((resource_path(
2016-11-06 11:18:53 +00:00
os.path.join(multibootusb_host_dir(), "syslinux", "modules", config.syslinux_version, module)),
2017-04-13 07:39:25 +00:00
os.path.join(config.usb_mount, "multibootusb", iso_basename(config.image_path),
isolinux_bin_dir(config.image_path), 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)),
2017-04-13 07:39:25 +00:00
os.path.join(config.usb_mount, "multibootusb", iso_basename(config.image_path),
isolinux_bin_dir(config.image_path), module))
2016-11-06 11:18:53 +00:00
except Exception as err:
2016-12-26 17:53:21 +00:00
log(err)
log("Could not copy " + module)
2016-11-06 11:18:53 +00:00
else:
2016-12-26 17:53:21 +00:00
log('Patch not required...')
2016-11-06 11:18:53 +00:00
if __name__ == '__main__':
2017-04-13 07:39:25 +00:00
config.image_path = '../../../DISTROS/2016/slitaz-4.0.iso'
2016-11-06 11:18:53 +00:00
install_distro()