Created
August 28, 2014 15:56
-
-
Save Javex/776a7c2070a104fb9eb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
import os | |
# | |
# Description | |
# This script is to be used when you have a RAW vmdk disk and fear that | |
# the drive identifier, i.e. PHYSICALDRIVEX might change during reboots. | |
# This script finds the correct name matching to a configured serial and | |
# will remove the old drive and remove it, create a new one and attach it. | |
# | |
# Configuration | |
# | |
# The serial of your drive. You can find this by running the command | |
# wmic diskdrive get name, serialnumber | |
# And find out which PHYISCALDRIVE yours is, then you can find the | |
# corresponding serial | |
DRIVE_SERIAL = "56435250323130303030384b32314c304e472020" | |
# Name of the virtual machine | |
VM_NAME = "Real Arch" | |
# Path to VboxManage.exe (this is the default path) | |
VBOX_MANAGE = "C:/Program Files/Oracle/VirtualBox/VboxManage.exe" | |
# Full path to the raw vmdk image. | |
RAW_DISK_PATH = r"C:\linux-raw.vmdk" | |
# UUID of the existing vmdk image | |
RAW_UUID = "ca32d592-09fb-4df1-bf27-1f750f207bef" | |
p = subprocess.Popen(["wmic", "diskdrive", "get", "name", ",serialnumber"], | |
stdout=subprocess.PIPE) | |
out, _ = p.communicate() | |
out = out.decode("ascii").strip() | |
for line in out.splitlines()[1:]: | |
if not line: | |
continue | |
name, serial, *_ = re.split(r'\s+', line) | |
if serial == DRIVE_SERIAL: | |
break | |
else: | |
raise ValueError("Drive not found") | |
p = subprocess.Popen([VBOX_MANAGE, "storageattach", VM_NAME, "--storagectl", | |
"SATA", "--medium", "none", "--port", "0"]) | |
if p.wait(): | |
raise ValueError("Could not detach storage") | |
os.remove(RAW_DISK_PATH) | |
p = subprocess.Popen([VBOX_MANAGE, "internalcommands", "createrawvmdk", | |
"-filename", RAW_DISK_PATH, "-rawdisk", name]) | |
if p.wait(): | |
raise ValueError("Could not create raw disk") | |
p = subprocess.Popen( | |
[VBOX_MANAGE, "internalcommands", "sethduuid", RAW_DISK_PATH, RAW_UUID]) | |
if p.wait(): | |
raise ValueError("Could not set raw disk UUID") | |
p = subprocess.Popen([VBOX_MANAGE, "storageattach", VM_NAME, "--storagectl", | |
"SATA", "--port", "0", "--type", "hdd", "--medium", | |
RAW_DISK_PATH, "--nonrotational", "on"]) | |
if p.wait(): | |
raise ValueError("Could not attach raw disk to VM") | |
p = subprocess.Popen([VBOX_MANAGE, "startvm", VM_NAME]) | |
if p.wait(): | |
raise ValueError("Could not start VM") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment