2
0
mirror of https://github.com/mbusb/multibootusb synced 2024-11-07 03:20:23 +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 sys
import os
import platform
import tarfile
import subprocess
from . import usb
from . import iso
from . import gen
from . import config
def persistence_distro(distro, usb_disk, iso_link):
"""
Function to detect if distro can have persistence option.
:param distro: Detected distro name.
:return: Distro name as string or None otherwise.
"""
iso_size = iso.iso_size(iso_link)
fat_max_size = (4096 * 1024 * 1024)
usb_details = usb.details(usb_disk)
usb_sf = usb_details['file_system']
usb_free_size = usb_details['size_free']
2016-11-27 00:46:12 +00:00
config.usb_uuid = usb_details['uuid']
config.usb_label = usb_details['label']
2016-11-06 11:18:53 +00:00
if usb_sf == 'vfat' or 'FAT32':
if usb_free_size > fat_max_size:
_max_size = fat_max_size
else:
_max_size = usb_free_size
else:
_max_size = usb_free_size
if distro == "ubuntu":
2016-12-26 17:53:21 +00:00
gen.log("Persistence option is available.")
2016-11-06 11:18:53 +00:00
return "ubuntu", _max_size
# FIXME to get debian persistence workable...
# Able to add successfully but unable to keep persistence data.
2016-11-20 10:58:02 +00:00
elif distro == "debian" or distro == "debian-install":
2016-12-26 17:53:21 +00:00
gen.log("Persistence option is available.")
2016-11-20 10:58:02 +00:00
return "debian", _max_size
elif distro == "fedora":
2016-12-26 17:53:21 +00:00
gen.log("Persistence option is available.")
2016-11-27 00:46:12 +00:00
return "fedora", _max_size
else:
return None, 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 ' + os.path.join(config.usb_mount, 'multibootusb',
iso.iso_basename(config.iso_link),
fs_name)
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")))
2016-11-06 11:18:53 +00:00
persistence_mkfs_cmd = 'echo y|' + mkfs + ' -b 1024 -L ' + fs_name + ' ' + os.path.join(config.usb_mount, 'multibootusb',
iso.iso_basename(config.iso_link), fs_name)
2016-11-27 00:46:12 +00:00
if config.distro == 'fedora':
persistence_dd_cmd = dd + ' if=/dev/zero ' \
'of=' + os.path.join(config.usb_mount, 'multibootusb',
iso.iso_basename(config.iso_link), 'LiveOS', fs_name) + \
' bs=1M count=' + str(int(persistence))
else:
persistence_dd_cmd = dd + ' if=/dev/zero of=' + os.path.join(config.usb_mount, 'multibootusb',
2016-11-06 11:18:53 +00:00
iso.iso_basename(config.iso_link), fs_name) +\
' 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
2016-11-27 00:46:12 +00:00
if not config.distro == 'fedora':
2016-12-26 17:53:21 +00:00
gen.log('Applying filesystem to persistence file...')
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()