2

Currently I am working on a project which require to create a vm from a click on the webpage.

This is my bash script which i am using to create a vm and it is working perfect but when called through php i am unable to run this script.

 #!/bin/bash
 set -e
 if [ ! -f "/tmp/value.dat" ] ; then
      value=0
 else
      value=`cat /tmp/value.dat`
 fi
 value=`expr ${value} + 1`
 echo "${value}" > /tmp/value.dat
 qemu-img create -f raw /var/lib/libvirt/images/disk$value.img 5.1G
 virt-install --name=instance$value --ram=1024 --arch=i686 --vcpus=1 --os-type=linux  --disk path=/var/lib/libvirt/images/disk$value.img,bus=virtio,format=raw --location ftp://10.21.40.227/ --extra-args "ks=ftp://10.21.40.227/ks.cfg"  --autostart

This is the php script that i am trying to run

 <?php
    $bashfile = shell_exec("sudo /root/Desktop/install.sh");
    echo "<pre>$bashfile<pre>";
 ?>

This is the entry in the sudoers file by me

apache ALL=(ALL) NOPASSWD: ALL

Still i am unable to run the bash script via php suggest some solutions.

shivams
  • 2,597
  • 6
  • 25
  • 47
  • can you post the bash script that you are using? – blissini Nov 21 '13 at 09:13
  • Post the *localized* technical problem. Not that you can't do it. Post the command(s) and the stderr/stdout and then someone'd be able to help you. the c club? – erbdex Nov 21 '13 at 09:18
  • [Read this](http://stackoverflow.com/questions/1598231/how-to-run-php-exec-as-root) and particularly [this](http://stackoverflow.com/a/5753115/2463134). Right now, you're executing '/root/Desktop/install.sh' on the same host which is running PHP/apache. Don't you want to do it *on a remote host*? – erbdex Nov 21 '13 at 10:56

1 Answers1

0

The problem may be that $PATH is not set correctly when it is not called from a shell. Try using absolute paths for qemu-img and virt-install.

Another problem I see here: you really don't want Apache to be able to run any command through sudo, do you? Change your sudoers line to this to limit it to your script only:

apache  ALL=NOPASSWD:/root/Desktop/install.sh
Konrad
  • 509
  • 5
  • 17