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

112 lines
4.3 KiB
Python
Raw Normal View History

2016-12-26 17:53:21 +00:00
#!/usr/bin/env python3
2016-11-06 11:18:53 +00:00
# -*- coding: utf-8 -*-
# Name: persistence.py
# Purpose: Module to deal with persistence of a selected distro.
# 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 platform
import tarfile
import subprocess
from . import iso
from . import gen
from . import config
2017-04-14 10:56:11 +00:00
def max_disk_persistence(usb_disk):
2016-11-06 11:18:53 +00:00
"""
2017-04-14 10:56:11 +00:00
Detect max persistence value for filesystem on usb_disk
:param usb_disk: Disk filesystem to check
:return: Max persistence supported (bytes)
2016-11-06 11:18:53 +00:00
"""
2017-04-14 10:56:11 +00:00
assert usb_disk is not None
2016-11-06 11:18:53 +00:00
fat_max_size = (4096 * 1024 * 1024)
usb_details = config.usb_details
2016-11-27 00:46:12 +00:00
config.usb_uuid = usb_details['uuid']
config.usb_label = usb_details['label']
2017-04-14 10:56:11 +00:00
if usb_details['file_system'] in ['vfat', 'FAT32'] and usb_details['size_free'] > fat_max_size:
_max_size = fat_max_size
2016-11-06 11:18:53 +00:00
else:
2017-04-14 10:56:11 +00:00
_max_size = usb_details['size_free']
return _max_size
def persistence_distro(distro, iso_link):
"""
Function to detect if distro can have persistence option.
:param distro: Detected distro name.
:return: Distro name as string or None otherwise.
"""
assert distro is not None
assert iso_link is not None
2017-05-04 07:53:10 +00:00
# iso_size = iso.iso_size(iso_link)
2017-04-14 10:56:11 +00:00
if distro in ["ubuntu", "debian", "debian-install", "fedora"]:
2016-12-26 17:53:21 +00:00
gen.log("Persistence option is available.")
2017-04-14 10:56:11 +00:00
return distro
else:
2017-04-14 10:56:11 +00:00
return None
2016-11-06 11:18:53 +00:00
def create_persistence():
if config.distro == "ubuntu":
fs_name = 'casper-rw'
2016-11-20 10:58:02 +00:00
elif config.distro == 'debian' or config.distro == "debian-install":
2016-11-06 11:18:53 +00:00
fs_name = 'live-rw'
2016-11-27 00:46:12 +00:00
elif config.distro == 'fedora':
fs_name = 'overlay-' + config.usb_label + '-' + config.usb_uuid
2016-11-06 11:18:53 +00:00
persistence = config.persistence / 1024 / 1024
if platform.system() == 'Linux':
mkfs = 'mkfs.ext3'
dd = 'dd'
persistence_mkfs_cmd = mkfs + ' -F ' + gen.quote(os.path.join(config.usb_mount, 'multibootusb',
2017-04-13 07:39:25 +00:00
iso.iso_basename(config.image_path),
fs_name))
2016-11-06 11:18:53 +00:00
elif platform.system() == 'Windows':
2016-11-20 10:58:02 +00:00
mkfs = gen.quote(gen.resource_path(os.path.join("data", "tools", "mkfs", "mke2fs.exe")))
dd = gen.quote(gen.resource_path(os.path.join("data", "tools", "dd", "dd.exe")))
persistence_mkfs_cmd = 'echo y|' + mkfs + ' -b 1024 -L ' + fs_name + ' ' + gen.quote(os.path.join(config.usb_mount, 'multibootusb',
iso.iso_basename(config.image_path), fs_name))
2016-11-06 11:18:53 +00:00
2016-11-27 00:46:12 +00:00
if config.distro == 'fedora':
persistence_dd_cmd = dd + ' if=/dev/zero ' \
'of=' + gen.quote(os.path.join(config.usb_mount, 'multibootusb',
iso.iso_basename(config.image_path), 'LiveOS', fs_name)) + \
2016-11-27 00:46:12 +00:00
' bs=1M count=' + str(int(persistence))
else:
persistence_dd_cmd = dd + ' if=/dev/zero of=' + gen.quote(os.path.join(config.usb_mount, 'multibootusb',
iso.iso_basename(config.image_path), fs_name)) +\
2016-11-06 11:18:53 +00:00
' bs=1M count=' + str(int(persistence))
2016-12-26 17:53:21 +00:00
gen.log('Executing ==>' + persistence_dd_cmd)
2016-11-06 11:18:53 +00:00
config.status_text = 'Creating persistence file...'
if subprocess.call(persistence_dd_cmd, shell=True) == 0:
2016-12-26 17:53:21 +00:00
gen.log("\nSuccessfully created persistence file...\n")
2016-11-06 11:18:53 +00:00
2017-05-03 18:08:26 +00:00
if config.distro != 'fedora':
2016-12-26 17:53:21 +00:00
gen.log('Applying filesystem to persistence file...')
config.status_text = 'Applying filesystem to persistence file. Please wait...'
2016-12-26 17:53:21 +00:00
gen.log('Executing ==> ' + persistence_mkfs_cmd)
2016-11-27 00:46:12 +00:00
config.status_text = 'Applying filesystem to persistence file...'
if subprocess.call(persistence_mkfs_cmd, shell=True) == 0:
2016-12-26 17:53:21 +00:00
gen.log("\nSuccessfully applied filesystem...\n")
2016-11-06 11:18:53 +00:00
def extract_file(file_path, install_dir):
"""
Function to extract persistence files to distro install directory.
:param file_path: Path to persistence file.
:param install_dir: Path to distro install directory.
:return:
"""
tar = tarfile.open(file_path, "r:bz2")
tar.extractall(install_dir)
tar.close()