Rename tmpdir to target and always preserve it, dropping --preserve

There is nothing temporary about our "tmpdir" - its sole purpose is to
contain the final product of the bootstrap process. Thus, removing it
at the end of bootstrap amounts to doing the entire process for nothing.

To remedy this, --tmpdir is renamed --target, keeping the -t short form,
and defaulting to "target" instead of "tmp" to make its purpose clearer.
The --preserve option is removed, as the target is now always preserved.
This commit is contained in:
Gábor Stefanik 2023-12-26 04:28:47 +01:00
parent 9179f4bd01
commit 81e3123262
5 changed files with 79 additions and 91 deletions

View file

@ -1,101 +0,0 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2023 fosslinux <fosslinux@aussies.space>
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Contains a class that represents a tmpdir
"""
import enum
import getpass
import os
import shutil
from lib.utils import mount, umount, create_disk, run_as_root
class TmpType(enum.Enum):
"""Different types of tmpdirs we can have"""
NONE = 0
TMPFS = 1
class Tmpdir:
"""
Represents a tmpdir
"""
_disks = {}
_disk_filesystems = {}
_mountpoints = {}
def __init__(self, preserve, path="tmp"):
self.path = os.path.abspath(path)
self.preserve = preserve
self._type = TmpType.NONE
if not os.path.exists(self.path):
os.mkdir(self.path)
def __del__(self):
for path in self._mountpoints:
print(f"Unmounting {path}")
umount(path)
if not self.preserve:
for disk in self._disks.values():
print(f"Detaching {disk}")
run_as_root("losetup", "-d", disk)
if self._type == TmpType.TMPFS:
print(f"Unmounting tmpdir from {self.path}")
umount(self.path)
print(f"Removing {self.path}")
shutil.rmtree(self.path, ignore_errors=True)
def tmpfs(self, size="8G"):
"""Mount a tmpfs"""
print(f"Mounting tmpfs on {self.path}")
mount("tmpfs", self.path, "tmpfs", f"size={size}")
self._type = TmpType.TMPFS
# pylint: disable=too-many-arguments
def add_disk(self,
name,
size="16G",
filesystem="ext4",
tabletype="msdos",
bootable=False,
mkfs_args=None):
"""Add a disk"""
disk_path = os.path.join(self.path, f"{name}.img")
self._disks[name] = create_disk(disk_path,
tabletype,
filesystem,
size,
bootable,
mkfs_args)
self._disk_filesystems[name] = filesystem
# Allow executing user to access it
run_as_root("chown", getpass.getuser(), self._disks[name])
def mount_disk(self, name, mountpoint=None):
"""Mount the disk"""
if mountpoint is None:
mountpoint = f"{name}_mnt"
mountpoint = os.path.join(self.path, mountpoint)
os.mkdir(mountpoint)
mount(self._disks[name] + "p1", mountpoint, self._disk_filesystems[name])
# Allow executing user to access it
run_as_root("chown", getpass.getuser(), mountpoint)
self._mountpoints[name] = mountpoint
return mountpoint
def umount_disk(self, name):
"""Unmount a disk"""
umount(self._mountpoints[name])
del self._mountpoints[name]
def get_disk(self, name):
"""Get the path to a device of a disk"""
return self._disks.get(name)