I am trying to write a patch for some open source software because it doesn’t do quite what I’d like–except I don’t know Python, at all! I expect this is very simple, but it is defeating me.
Background: The patch I am writing is for Ganeti which I am using to manage KVM virtual machine clusters, but it doesn’t support all the possible command line options of KVM and Qemu, such as USB pass-through, so I am trying to modify it to allow this.
kvm_cmd.extend() is used to add to the array of KVM CLI args that will be passed when a VM is started. If the arguments are space-separated, each argument becomes a separate string, e.g. -usb -device usb-host,hostbus=1,hostdev=14 becomes "-usb", "-device", "usb-host,hostbus=1,hostdev=14".
I am running the following command after compiling the code:
gnt-instance modify -H usb_pass="1;14"
I want this to add to the existing list of command line arguments "-usb -device usb-host,hostbus=1,hostdev=14". This is the code I have added (as well as declaring the variables elsewhere; HV_USBPASSTHROUGH is a string, but this is where the magic happens)
usb_pass = instance.hvparams[constants.HV_USBPASSTHROUGH]
if usb_pass:
usb_pass_arr = []
usb_pass_arr = usb_pass.split(";")
kvm_cmd.extend(["-usb", "-device", "usb-host,hostbus=%s,hostaddr=%s" %
usb_pass_arr])
I am getting the following error from running the above command with the above code: Could not start instance: Error while executing backend function: not enough arguments for format string
You have two
`%sin"usb-host,hostbus=%s,hostaddr=%s"but provide only one argument, a list, where a tuple is expected.use
"usb-host,hostbus=%s,hostaddr=%s" % tuple(usb_pass_arr)tuple() builtin converts an iterable to a tuple.