根据 Microsoft 文档,我能够创建数据磁盘并将其附加到 RHEL azure 虚拟机,我可以将数据磁盘附加到我的 Linux Azure VM,并通过登录 VM 手动初始化磁盘。一切都适合我。
但这还不够,因为我想通过代码自动完成整个过程。您能给我一些代码示例或相关指南吗?谢谢!
要在通过 python 将数据磁盘附加到 rhel Linux azure VM 后自动创建 PV、VG、LV、格式、挂载文件系统,您可以使用以下脚本使用 paramiko
import paramiko
HOST = "123.456.789.105" #updatewith ur own VM details
USERNAME = "arko"
PASSWORD = "Madhuri@123"
VG_NAME = "vg_data_new"
LV_NAME = "lv_data_new"
MOUNT_POINT = "/mnt/data_new"
commands = [
"echo y | sudo pvcreate -ff /dev/sdb",
f"sudo vgcreate {VG_NAME} /dev/sdb",
f"sudo lvcreate -y -l 100%FREE -n {LV_NAME} {VG_NAME}",
f"sudo mkfs.ext4 -F /dev/{VG_NAME}/{LV_NAME}",
f"sudo mkdir -p {MOUNT_POINT}",
f"sudo mount /dev/{VG_NAME}/{LV_NAME} {MOUNT_POINT}",
f"echo '/dev/{VG_NAME}/{LV_NAME} {MOUNT_POINT} ext4 defaults 0 0' | sudo tee -a /etc/fstab"
]
def execute_commands(commands):
print("Debug: Starting command execution on /dev/sdb with new volume group and logical volume names")
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(HOST, username=USERNAME, password=PASSWORD)
for command in commands:
print(f"Executing: {command}")
stdin, stdout, stderr = client.exec_command(command)
output = stdout.read().decode()
error = stderr.read().decode()
if output:
print(f"Output: {output}")
if error:
print(f"Error: {error}")
print("Disk setup completed successfully with new names.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
client.close()
execute_commands(commands)
您可以通过登录虚拟机并执行 lsblk 来验证这一点