1

In the days I started working on a VM manager platform, it works with virt-manager, KVM and PhP 7.2. In form.html should specify the values ​​that kvm2.php can run the VM create command.

The problem is that I give the values ​​well and fill the fields, kvm2.php (libvirt) writes this error message continuously:

"ERROR
--name is required
--memory amount in MiB is required"

What have I done wrong?

Thank you in advance for your answers,
Erik

Screenshots:
https://i.stack.imgur.com/oKXiS.jpg

form.html:

<form action="kvm2.php" method="get">
VM name: <input type="text" name="VMname" id="VMname" required><br>
vCPU number(s): <input type="text" name="VMvCPU" id="VMvCPU" required><br>
Memory (in MB): <input type="text" name="VMmem" id="VMmem" required><br>
Disk size (in GB): <input type="text" name="VMdisk" id="VMdisk" required><br>
<input type="submit" name="submit">
</form>

kvm2.php:

<?php
    $output=shell_exec("virt-install --connect qemu:///system --name=$VMname --vcpus=$VMvCPU --memory=$VMmem --disk size=$VMdisk --cdrom=/home/erik/Letöltések/debian.iso --os-type linux --vnc --network=bridge:br0 2>&1");
    echo "<pre>$output</pre>";
?>

EDIT:

The current code is:

$output=shell_exec("sudo virt-install --connect qemu:///system --name=$_GET["VMname"]; --vcpus=$_GET["VMvCPU"]; --memory=$_GET["VMmem"]; --disk size=$_GET["VMdisk"]; 2>&1");

As requested, I turned on display errors:

Parse error: syntax error, unexpected '"', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) on line 2

ADyson
  • 57,178
  • 14
  • 51
  • 63
Erik
  • 41
  • 6
  • 2
    You don't appear to have selected your variables from the $_GET array. Make take a PHP forms tutorial, it'll show you what to do – ADyson Mar 23 '19 at 14:54
  • Thanks your feedback. I've changed my code, but I get a white screen under kvm2.php: &1"); echo "
    $output
    "; ?>
    – Erik Mar 23 '19 at 15:54
  • Use quote marks for the field names - they are literal strings, not variables themselves. $_GET["VMvCPU"]; for example. Pay close attention to detail in any examples you may have seen, every character is significant. P.s. a white screen indicates a PHP error, to get details (to give you clues about the problem) you can either enable error reporting (on screen) or logging (to disk). You can easily find information on Google about how to set that up – ADyson Mar 23 '19 at 16:42
  • Thanks again for your reply :) But I use quotation marks in vain, it is not accepted by the VS code. Screenshot: https://imgur.com/a/fAglx5k – Erik Mar 23 '19 at 17:21
  • It's too blurry to see the error. What does it say? Also please paste me the code you've now written on that line. Have you got VS Code set up with a PHP interpreter? Anyway never mind the IDE just run it and see if it works – ADyson Mar 23 '19 at 17:44
  • The new code: " &1"); echo "
    $output
    "; ?>" However, the page displays a white screen.
    – Erik Mar 23 '19 at 17:52
  • Is that first and last quote mark actually part of your code? And please go and Google how to see PHP errors so you can get more info that just a white screen, as I mentioned earlier. And also please tell me what VS Code is complaining about. It's hard to help if you don't provide information. It _could_ be because you need to escape your quote marks in the fields, or just use string concatenation instead of interpolation – ADyson Mar 23 '19 at 17:54
  • I was trying to use ' or " signs, but the kvm2.php page returns the same white screen. The current code is: `$output=shell_exec("sudo virt-install --connect qemu:///system --name=$_GET["VMname"]; --vcpus=$_GET["VMvCPU"]; --memory=$_GET["VMmem"]; --disk size=$_GET["VMdisk"]; 2>&1");` . As requested, I turned on display errors: `Parse error: syntax error, unexpected '"', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) on line 2` – Erik Mar 23 '19 at 18:31
  • The exception being thrown gives you clues. The variable is not been handled correctly due to improper handling or syntax error. Good luck! – yardpenalty.com Mar 23 '19 at 19:25

1 Answers1

2

Your syntax error is because you've got double quotes within a double-quoted string. PHP cannot work out where the string really begins and ends.

There are two ways to resolve it:

1) use single quotes for the field names within the string:

$output=shell_exec("sudo virt-install --connect qemu:///system --name=$_GET['VMname']; --vcpus=$_GET['VMvCPU']; --memory=$_GET['VMmem']; --disk size=$_GET['VMdisk']; 2>&1");

2) use string concatenation instead of interpolation:

$output=shell_exec("sudo virt-install --connect qemu:///system --name=".$_GET["VMname"]."; --vcpus=".$_GET["VMvCPU"]."; --memory=".$_GET["VMmem"]."; --disk size=".$_GET["VMdisk"]>"; 2>&1");

(I'm going to assume that the semicolons after each reference to a $_GET variable are part of the syntax required by the virt-install command, but if you they're not, then you need to remove those too.)

ADyson
  • 57,178
  • 14
  • 51
  • 63