python: attaching a device to a machine

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
raj_list
Posts: 2
Joined: 17. Sep 2019, 08:34

python: attaching a device to a machine

Post by raj_list »

Hello all,

I am trying to create a machine from scratch in python which will replicate the following steps:

Code: Select all

VBoxManage createvm --name ubuntu18test --ostype Ubuntu_64 --register
VBoxManage list ostypes | less
VBoxManage storagectl ubuntu18test --name SATA --add SATA --controller IntelAhci
VBoxManage storageattach ubuntu18test --storagectl SATA --port 0 --device 0 --type hdd --medium ~/VirtualBox\ VMs/ubuntu18test/ubuntu18test.vdi
VBoxManage storagectl ubuntu18test --name IDE --add ide
VBoxManage storageattach ubuntu18test --storagectl IDE --port 0 --device 0 --type dvddrive --medium ~/Work/
projects/installer/ubuntu/back-1/ubuntu-18.04.2-server-amd64.iso
VBoxManage modifyvm ubuntu18test --memory 1024 --vram 16
VBoxManage modifyvm ubuntu18test --ioapic on
VBoxManage modifyvm ubuntu18test --boot1 dvd --boot2 disk --boot3 none --boot4 none
VBoxManage modifyvm ubuntu18test --cpus 2
VBoxManage modifyvm ubuntu18test --audio none
VBoxManage modifyvm ubuntu18test --usb off
VBoxManage modifyvm ubuntu18test --usbehci off
VBoxManage modifyvm ubuntu18test --usbxhci off
VBoxManage modifyvm ubuntu18test --nic1 bridged --bridgeadapter1 wlan0 --nic2 nat
and finally boot off an ISO.

Following is the python code I have currently.

Code: Select all

import virtualbox

vbox = virtualbox.VirtualBox()

machine_name = "test_02"
disk_location = "/home/raj/VirtualBox\ VMs/{}/{}.vdi".format(machine_name,machine_name)
hdd = vbox.create_medium(format_p="vdi",
                         location=disk_location,
                         access_mode= virtualbox.library.AccessMode.read_write,
                         a_device_type_type=virtualbox.library.DeviceType.hard_disk)
print ("Hard Disk name is {}".format(hdd.name))

machine = vbox.create_machine(name=machine_name,
                              os_type_id="Ubuntu_64",
                              settings_file="",
                              groups=['/'],
                              flags ="" )
machine.add_storage_controller("SATA", virtualbox.library.StorageBus.sata)

vbox.register_machine(machine)

mutable= vbox.find_machine(machine_name)
session = virtualbox.Session()
mutable.lock_machine(session, virtualbox.library.LockType.write)
print ("Mutable machine name is {}".format(mutable.name))

mutable.attach_device(name="SATA",
                      controller_port=0,
                      device=0,
                      type_p=virtualbox.library.DeviceType.hard_disk,
                      medium=hdd)
mutable.save_settings()
session.unlock_machine()

# progress = machine.launch_vm_process(session, "gui", "")                                                  
# progress.wait_for_completion()                                                                            
media = machine.remove()
print (media)
When I run I get:

Code: Select all

Hard Disk name is test_03.vdi
Mutable machine name is test_03
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/virtualbox/library_base.py", line 201, in _call_method
    ret = method(*in_params)
  File "<XPCOMObject method 'attachDevice'>", line 3, in attachDevice
xpcom.Exception: 0x80bb0002 (The machine is not mutable or running (state is PoweredOff))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "vbox-1.py", line 31, in <module>
    medium=hdd)
  File "/usr/local/lib/python3.4/dist-packages/virtualbox/library.py", line 12968, in attach_device
    in_p=[name, controller_port, device, type_p, medium])
  File "/usr/local/lib/python3.4/dist-packages/virtualbox/library_base.py", line 192, in _call
    return self._call_method(method, in_p=in_p)
  File "/usr/local/lib/python3.4/dist-packages/virtualbox/library_base.py", line 220, in _call_method
    raise errobj
virtualbox.library.VBoxErrorInvalidVmState: 0x80bb0002 (The machine is not mutable or running (state is PoweredOff))
attachDevice documenntation says:
This method is intended for managing storage devices in general while a machine is powered off.
In the part where I try to attach device, I believe I have followed the documentation faithfully, but there seems to be some thing amiss:

Code: Select all

mutable= vbox.find_machine(machine_name)
session = virtualbox.Session()
mutable.lock_machine(session, virtualbox.library.LockType.write)
print ("Mutable machine name is {}".format(mutable.name))

mutable.attach_device(name="SATA",
                      controller_port=0,
                      device=0,
                      type_p=virtualbox.library.DeviceType.hard_disk,
                      medium=hdd)
mutable.save_settings()
session.unlock_machine()
Any help to get this moving would be much appreciated.

raj
SmithersTheOracle
Posts: 60
Joined: 28. Dec 2019, 08:58
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: python: attaching a device to a machine

Post by SmithersTheOracle »

Hi raj_list,

Basically you're trying to write-lock the mutable machine when you should be getting a write-lock on the machine itself, then creating a mutable session from that machine. Something like this:

Code: Select all

    import virtualbox

    vbox = virtualbox.VirtualBox()

    machine_name = "test_02"
    disk_location = "/home/raj/VirtualBox\ VMs/{}/{}.vdi".format(machine_name,machine_name)
    hdd = vbox.create_medium(format_p="vdi",
                             location=disk_location,
                             access_mode= virtualbox.library.AccessMode.read_write,
                             a_device_type_type=virtualbox.library.DeviceType.hard_disk)
    print ("Hard Disk name is {}".format(hdd.name))

    machine = vbox.create_machine(name=machine_name,
                                  os_type_id="Ubuntu_64",
                                  settings_file="",
                                  groups=['/'],
                                  flags ="" )
    machine.add_storage_controller("SATA", virtualbox.library.StorageBus.sata)

    vbox.register_machine(machine)

    machine = vbox.find_machine(machine_name)
    session = virtualbox.Session()
    machine.lock_machine(session, virtualbox.library.LockType.write)
    mutable= session.get_machine(machine)
    print ("Mutable machine name is {}".format(mutable.name))

    mutable.attach_device(name="SATA",
                          controller_port=0,
                          device=0,
                          type_p=virtualbox.library.DeviceType.hard_disk,
                          medium=hdd)
    mutable.save_settings()
    session.unlock_machine()

    # progress = machine.launch_vm_process(session, "gui", "")                                                 
    # progress.wait_for_completion()                                                                           
    media = machine.remove()
    print (media)
I'm not positive that my python syntax is correct (mutable = session.get_machine(machine) might not be right) but doing it in that order should fix it.
Post Reply