Kumi
b09bec7a70
Introduced a new set of Python classes under `classes/` directory, enabling functionality to edit and configure virtual machines (VMs) through a text-based user interface (TUI). Key components include handling for Linux distribution-specific configurations, partition mounting, and VM disk manipulation using QEMU tools. This structure allows for scalable VM management tasks such as user modification, network configuration, hostname setting, and disk conversion from VMDK to raw image format. Additionally, basic infrastructure such as `.gitignore`, `README.md`, and `requirements.txt` setup is included to ensure a clean development environment and provide necessary instructions and dependencies for running the script. The TUI, powered by npyscreen, facilitates user interaction for configuring newly cloned VMs, focusing on ease of use and automation of repetitive tasks. This comprehensive suite marks a pivotal step towards automating VM management and customization processes, significantly reducing manual overhead for system administrators and developers dealing with virtual environments.
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import subprocess
|
|
import tempfile
|
|
import os
|
|
import sys
|
|
|
|
class Mountpoint(tempfile.TemporaryDirectory):
|
|
pass
|
|
|
|
|
|
class PartitionMounter:
|
|
def __init__(self, img_file, partition_number):
|
|
self.img_file = img_file
|
|
self.partition_number = partition_number
|
|
self.mountpoint = None
|
|
self.loop_device = None
|
|
|
|
def setup_loop_device(self):
|
|
self.loop_device = subprocess.check_output(['losetup', '--show', '-fP', self.img_file], text=True).strip()
|
|
print(f"Loop device created: {self.loop_device}")
|
|
|
|
def mount_partition(self):
|
|
if self.loop_device is None:
|
|
self.setup_loop_device()
|
|
|
|
# Use partprobe to make the kernel aware of the partition
|
|
subprocess.run(['partprobe', self.loop_device], check=True)
|
|
|
|
# Calculate partition path
|
|
partition_path = f"{self.loop_device}p{self.partition_number}"
|
|
|
|
# Create a temporary directory to mount the partition
|
|
self.mountpoint = Mountpoint()
|
|
|
|
# Mount the partition to the temporary directory
|
|
subprocess.run(['mount', partition_path, self.mountpoint.name], check=True)
|
|
|
|
def cleanup(self):
|
|
if self.mountpoint:
|
|
subprocess.run(['umount', self.mountpoint.name], check=True)
|
|
self.mountpoint.cleanup()
|
|
|
|
if self.loop_device:
|
|
subprocess.run(['losetup', '-d', self.loop_device], check=True)
|
|
|
|
def __enter__(self):
|
|
self.mount_partition()
|
|
return self.mountpoint.name
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
self.cleanup()
|