Skip to content

Instantly share code, notes, and snippets.

@biggers
Last active October 18, 2021 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biggers/6477046 to your computer and use it in GitHub Desktop.
Save biggers/6477046 to your computer and use it in GitHub Desktop.
Am example (expanded) of using Python 'clom' library, to "abstract" Virtualbox command-line. Thanks to the "Refs!" It's not a runnable script by itself; will probably make it so, soon.
#!/usr/bin/env python
from clom import clom
from pprint import pprint as pp
# Install, into a virtualenv: pip install clom
#
# Reference:
# http://clom.readthedocs.org/en/latest/
# Derived from:
# http://devdetails.com/2011/11/07/clom-python-command-line-the-easy-way/
#
vbox = clom.VBoxManage # create a VBoxManage command-object
def dump_one_vm(vbox, rpt_idx=0):
""" Report info, on the first Virtualbox VM found
"""
res = vbox.list.vms.shell.all() # list all Virtualbox VMs
vm, vm_id = None, None
for vm_spec in res:
if vm_spec.find('{') != -1: # find the first actual VBox VM
vm, vm_id = vm_spec.split()
vm = vm.strip('"')
break
return vbox.showvminfo(vm, machinereadable=True, details=True).shell.all()
def dump_all_linux_os(vbox)
res = vbox.list.ostypes.shell.all()
return res
# "display" the Shell command that would be executed, by '.shell.all()'
vbox.createvm( name='Ubuntu_64', ostype='Linux', register=True,)
res = vbox.createvm( name=VM, ostype=OS, register=True, ).shell.all()
pp( res )
"""
out = ["Virtual machine 'CentOS_6-3_x64' is created and registered.",
'UUID: 3dc57441-cb51-4644-969a-f91aea962c7a',
"Settings file: '/var/lib/MyData/mbiggers/virtualbox/VirtualBox VMs/CentOS_6-3_x64/CentOS_6-3_x64.vbox'"]
"""
VDI = "{}/{}/{}/{}.vdi".format('/var/lib/MyData/mbiggers/virtualbox', 'VirtualBox VMs', VM, VM)
res = vbox.createhd( filename=VDI, size=8192 ).shell.all()
pp(res)
res = vbox.storagectl.with_opts( VM, name='SATA Controller', add='sata', controller='IntelAHCI' ).shell.all()
pp(res)
res = vbox.storageattach.with_opts( VM, storagectl="SATA Controller", port=0, device=0, type='hdd', medium=VDI ).shell.all()
pp(res)
# Change amt of memory, et cetera...
# PXE-boot: change NIC from (default) 'e1000,' as "plain" VirtualBox install
# has no firmware necessary to net-boot from that device
res = vbox.modifyvm.with_opts( VM, memory=768, acpi='on', nictype1='Am79C973', usb='on', vrde='on', ).shell.all()
pp(res)
pp( vbox.showvminfo(VM, machinereadable=True, details=True).shell.all() )
res = vbox.modifyvm.with_opts( VM, boot1='disk', boot2='dvd', boot3='none', boot4='none',
dvd='/var/lib/MyData/CentOS-6.3-x86_64-netinstall.iso').shell.all()
pp(res)
# attach storage controllers to it
res = vbox.storagectl.with_opts( VM, name='IDE Controller', add='ide', controller='PIIX4').shell.all()
pp(res)
res = vbox.storageattach.with_opts( VM, storagectl='IDE Controller', port=1, device=0, type='dvddrive',
medium='/var/lib/MyData/CentOS-6.3-x86_64-netinstall.iso' ).shell.all()
pp(res)
# clom.VBoxHeadless.with_opts(startvm=VM).background().shell()
# INFO:clom.shell:Executing command (capture off): nohup VBoxHeadless --startvm 'Test VM' &> /dev/null &
# vbox.controlvm.with_opts( VM ).with_args('poweroff').shell()
# CLONING! WARNING!
CLONE_ID = 'dpt-min-01'
VDI_clone = "{}/{}/{}/{}-{}.vdi".format('/var/lib/MyData/mbiggers/virtualbox', 'VirtualBox VMs', VM, VM, CLONE_ID)
pp("{} clone =>> {}".format( VDI, VDI_clone ))
vbox.clonehd(VDI, VDI_clone).shell.all()
VM2 = "depot-clone-01"
vbox.createvm( name=VM2, ostype=OS, register=True,)
res = vbox.createvm( name=VM2, ostype=OS, register=True, ).shell.all()
pp( res )
res = vbox.storagectl.with_opts( VM2, name='SATA Controller', add='sata', controller='IntelAHCI' ).shell.all()
pp(res)
res = vbox.storageattach.with_opts( VM2, storagectl="SATA Controller", port=0, device=0, type='hdd', medium=VDI_clone ).shell.all()
pp(res)
res = vbox.modifyvm.with_opts( VM2, memory=768, acpi='on', nictype1='Am79C973', usb='on', vrde='on', ).shell.all()
pp(res)
pp( vbox.showvminfo(VM2, machinereadable=True, details=True).shell.all() )
res = vbox.modifyvm.with_opts( VM2, boot1='disk', boot2='dvd', boot3='none', boot4='none',).shell.all()
pp(res)
# attach storage controllers to it
res = vbox.storageattach.with_opts( VM2, storagectl='IDE Controller', port=1, device=0, type='dvddrive',medium='/var/lib/MyData/CentOS-6.3-x86_64-netinstall.iso' ).shell.all()
pp(res)
res = vbox.modifyvm.with_opts( VM2, memory=768, acpi='on', nictype1='Am79C973', usb='on', vrde='on', ).shell.all()
pp(res)
# "affix" the CentOS DVD #1
res = vbox.storageattach.with_opts( VM2, storagectl='IDE Controller', port=1, device=0, type='dvddrive', medium='/var/lib/MyData/CentOS-6.3-x86_64-bin-DVD1.iso').shell.all()
pp(res)
#. vboxmanage modifyvm --natpf<1-N> [<rulename>],tcp|udp,[<hostip>],<hostport>,[<guestip>],<guestport>]
#. VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22"
#. VBoxManage modifyvm "VM name" --natpf1 delete "guestssh"
#res = vbox.modifyvm.with_opts( VM2, natpf1='guestssh,tcp,,2201,,22' ).shell.all()
vbox.modifyvm.with_opts(VM2,
natpf1='g_ssh,tcp,127.0.0.1,2201,,22',
).shell.all()
vbox.modifyvm.with_opts(VM2,
natpf1='g_web,tcp,127.0.0.1,8001,,80',
).shell.all()
vbox.modifyvm.with_opts(VM2,
natpf1='g_twisted,tcp,127.0.0.1,8801,,8080',
).shell.all()
vbox.modifyvm.with_opts(VM2,
natpf1='g_webmin,tcp,127.0.0.1,10001,,10000',
).shell.all()
# res = vbox.modifyvm.with_opts( VM2, natpf1='delete' 'guestssh' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment