diff --git a/data/psdk.desktop b/data/psdk.desktop new file mode 100755 index 0000000000000000000000000000000000000000..6fc45b3a80f696f81d3885acd39d096be3a4c12b --- /dev/null +++ b/data/psdk.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Version=1.0 +Name=Persistent Disk Setup +Exec=/usr/bin/psdk +Icon=drive-harddisk +Terminal=true +Type=Application +Categories=System +StartupNotify=false diff --git a/tools/psdk b/tools/psdk index d8ff3c02f3d1da1018486558c78b56f9f21f6267..6b52832535b2546b063c48fee0a51628bbf9934a 100755 --- a/tools/psdk +++ b/tools/psdk @@ -27,6 +27,8 @@ import errno BKP_EXTENSION = ".PSDK" DISK_LABEL = "sdk-persistent" +DISK_MIN_SIZE = 17 # 20 - 20*10% - 1 +HELP_LINK = "https://developer.apertis.org/latest/sdk-usage.html" MOUNT_POINT = "/run/media/persistent" PERSISTENT_ETC_PREFIX = "/home" @@ -443,6 +445,57 @@ def scan_persistent_etc(): for line in result.stdout.decode().splitlines(): persistent_to_conf(line) +def menu(empty_disk_found=False, pdisk_found=False, pdisk_in_use=False): + """Menu for user interaction""" + + print("Persistent disk setup") + print("\nFor help visit:\n\t" + HELP_LINK) + print("\nStatus:") + + if pdisk_in_use: + print("\tPersistent disk in use: {}".format(str(pdisk_in_use))) + print("\nThis SDK is currently using the persistent disk. No further action required.") + input("\nPress enter to exit") + return False + + if (not pdisk_found) and (not empty_disk_found): + print("\nNo disk found to use as persistent storage.") + input("\nPress enter to exit") + return False + + if empty_disk_found: + print("\tEmpty disk found: {}".format(str(empty_disk_found))) + answer = input("\nDo you want to initialize the empty disk {} ".format(empty_disk_found) + + "and configure the SDK to use it as persistent disk?\n" + + "(yes and press enter): ") + + if answer == "yes": + disk = Disk(empty_disk_found, DISK_LABEL) + disk.initialize() + configure_sdk() + + input("Disk initialization completed. Press enter to reboot the SDK.") + command = ["sudo", "reboot"] + _run(command) + + return True + + if pdisk_found: + print("\tPersistent disk in use: {}".format(str(pdisk_in_use))) + print("\tPersistent disk found: {}".format(str(pdisk_found))) + answer = input("\nDo you want to configure the SDK to use the persistent " + + "disk {}?\n".format(pdisk_found) + + "(yes and press enter): ") + + if answer == "yes": + configure_sdk() + + input("SDK configuration completed. Press enter to reboot the SDK.") + command = ["sudo", "reboot"] + _run(command) + + return True + def persistent_disk_in_use(): """Is the persistent disk in use?""" @@ -461,9 +514,50 @@ def persistent_disk_in_use(): return persistent_disk + +def empty_disk(min_size=DISK_MIN_SIZE): + """Find an empty disk with at least min_size""" + + command = ["sudo", "fdisk", "-l"] + result = _run(command) + + for line in result.stdout.decode().splitlines(): + if "Disk /dev" in line: + disk = "/dev/" + re.findall(r"Disk /dev/([A-Za-z]+)", line)[0] + size = re.findall(r"Disk /dev/[A-Za-z]+\: ([0-9\.]+)", line)[0] + if disk_valid(disk) and disk_empty(disk): + size = int(float(size)) + if size >= min_size: + return disk + + return False + +def probe_disks(): + """Look for disks that are currently available to check for empty_disk_found, + pdisk_found, and pdisk_in_use""" + + empty_disk_found = empty_disk() + pdisk_in_use = persistent_disk_in_use() + + # Persistent disk found? + pdisk_found = False + persistent_disk_link = "/dev/disk/by-label/" + DISK_LABEL + if os.path.islink(persistent_disk_link): + pdisk_found = os.readlink(persistent_disk_link) + pdisk_found = "/dev/" + os.path.split(pdisk_found)[1] + + return empty_disk_found, pdisk_found, pdisk_in_use + def main(): """good old main""" + if len(sys.argv) == 1: + # Interactive mode + + empty_disk_found, pdisk_found, pdisk_in_use = probe_disks() + menu(empty_disk_found, pdisk_found, pdisk_in_use) + sys.exit() + parser = argparse.ArgumentParser() etc_group = parser.add_argument_group("Configuration files management") disk_group = parser.add_argument_group("Persistent disk management")