def append(self, end: str = "100%", type_: Optional[str] = None): if self.partitions: start = self.partitions[-1].end else: start = "0%" partition = Partition(self.path, len(self.partitions) + 1, start, end, type_) command = [ "parted", "-s", "-a", "optimal", str(self.path), "mkpart", "primary", ] if partition.type_ is not None: command.append(partition.type_) command += [ partition.start, partition.end, ] run(command) self.partitions.append(partition) return partition
def clone(url: str, dest: Union[Path, str]): Path(dest).mkdir(parents=True, exist_ok=True) run( ["git", "clone", url, str(dest)], { "GIT_SSH_COMMAND": "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" }, )
def unmount_filesystem(filesystem: FileSystem): cmd: List[str] = [] if filesystem.type_ == "swap": cmd = ["swapoff", filesystem.block_device.path] else: cmd = ["umount", filesystem.mount_point] run(cmd) filesystem.mounted = False
def run_reflector(country: str = "Germany", prefix: Union[Path, str] = "/mnt"): mirrorlist = Path(prefix) / "etc" / "pacman.d" / "mirrorlist" mirrorlist.parent.mkdir(parents=True, exist_ok=True) run([ "reflector", "--age", "12", "--country", country, "--protocol", "https", "--sort", "rate", "--save", str(mirrorlist), ])
def create_btrfs( block_device: BlockDevice, label: Optional[str] = None, mount_point: Optional[Union[str, Path]] = None, mount_options: Optional[List[str]] = None, ) -> BtrfsFileSystem: cmd = ["mkfs.btrfs", "-f"] if label: cmd += ["-L", label] cmd.append(block_device.path) run(cmd) return BtrfsFileSystem( block_device, label=label, mount_point=mount_point, mount_options=mount_options, )
def create_swap( block_device: BlockDevice, label: Optional[str] = None, mount_options: Optional[List[str]] = None, ) -> FileSystem: cmd = ["mkswap", "-f"] if label: cmd += ["-L", label] cmd.append(block_device.path) run(cmd) return FileSystem( block_device, "swap", label=label, mount_point="[SWAP]", mount_options=mount_options, )
def mount_filesystem(filesystem: FileSystem): if filesystem.mount_point is None: return cmd: List[str] = [] if filesystem.type_ == "swap": cmd = ["swapon"] if filesystem.mount_options: cmd += ["-o", ",".join(filesystem.mount_options)] cmd.append(filesystem.block_device.path) else: Path(filesystem.mount_point).mkdir(parents=True, exist_ok=True) cmd = ["mount", "-t", filesystem.type_] if filesystem.mount_options: cmd += ["-o", ",".join(filesystem.mount_options)] cmd += [filesystem.block_device.path, filesystem.mount_point] run(cmd) filesystem.mounted = True
def create_vfat32( block_device: BlockDevice, label: Optional[str] = None, mount_point: Optional[Union[str, Path]] = None, mount_options: Optional[List[str]] = None, ) -> FileSystem: cmd = ["mkfs.vfat", "-F", "32"] if label: cmd.append("-n") cmd.append(label[:11].upper()) cmd.append(block_device.path) run(cmd) return FileSystem( block_device, "vfat", label=label, mount_point=mount_point, mount_options=mount_options, )
def create_subvolume(self, name: str) -> FileSystem: if self.mount_point is None: raise RuntimeError("btrfs filesystem does not have mountpoint") if not self.mounted: raise RuntimeError("base filesystem is not mounted") subvolume_path = Path(self.mount_point) / name subvolume_path.parent.mkdir(parents=True, exist_ok=True) cmd = ["btrfs", "subvolume", "create", name] run(cmd, cwd=self.mount_point) mount_options = [] if self.mount_options is None else self.mount_options mount_options.append("subvol=" + name) return FileSystem( self.block_device, self.type_, mount_point=Path(self.mount_point) / name, mount_options=mount_options, )
def luks_container(block_device: BlockDevice, mapper_name: str, password: str): try: command = [ "cryptsetup", "-v", "luksFormat", block_device.path, "--type", "luks", "--key-file", "-", ] subprocess.Popen( command, stderr=sys.stderr, stdout=sys.stdout, stdin=subprocess.PIPE ).communicate(input=password.encode()) subprocess.Popen( [ "cryptsetup", "-v", "open", "--key-file", "-", block_device.path, mapper_name, ], stderr=sys.stderr, stdout=sys.stdout, stdin=subprocess.PIPE, ).communicate(input=password.encode()) yield BlockDevice(Path("/") / "dev" / "mapper" / mapper_name) finally: try: run(["cryptsetup", "-v", "close", mapper_name]) except: pass
def clear_disk(disk: Union[str, Path], label: str = "gpt"): run(["parted", "-s", str(disk), "mklabel", label])
def pacstrap(packages: Union[str, List[str]], prefix: Optional[Union[str, Path]] = "/mnt"): if isinstance(packages, str): run(["pacstrap", str(prefix), packages]) else: run(["pacstrap", str(prefix)] + packages)