skip to Main Content

I need to be able to pass some parameters to my virtual machine during it’s bootup so it sets itself properly. To do that I either have to bake the info into the image or somehow pass it as parameters to my qemu-kvm command. These parameters are just few, and if it was VMware, we would just pass it as ova paramas and when the VM launches we would call the ova-environment to get these params. But launching it from qemu-kvm I have no such options. I did some homework and found that I could use virtio-9p driver for sharing files across host and guest. Unfortuantely RHEL/Centos has decided not to support 9p.

With no option of rebuilding my RHEL kernel with the 9p options enabled, how do I solve my above problem? Either solution would work, which is, pass/share some kind of json file to the VM(pre-populated on the host), which will read this and do it’s setup OR set some kind of “environment variables” which I can query from within the VM to get these params and continue with setup. Any pointers would help.

4

Answers


  1. When you boot your guest with -kernel and -initrd you should be able to pass environment variables with -append.

    The downside is that you have to keep track of your current kernel and initrd outside of your disk image.

    Other possibilities could be a small prepared disk image (as you said) or via network/dhcp or a serial link into your guest or … this really depends on your environment.

    Login or Signup to reply.
  2. I was just searching to see if this situation had improved and came across this question. Apparently it has not improved.

    What I do is output my variable data to a temp file (eg. /tmp/xxFoo). Usually I write text or a tar straight to that file then truncate it to a minimum size and 512 byte multiple like 64K otherwise the disk controller won’t configure it. Then the VM starts with a raw drive as that file. After the VM is started the temp file is deleted. From within the guest you can read/cat the raw block device and get the variable data (in BSD use the c partition as the raw drive).

    In Windows guests it’s tricky to get to the data. In theory you can read \.PhysicalDriveN but I have not ever been able to get that to work. Cygwin can do it and it works like Linux. The other option is to make your temp file a partitioned and formatted image but that’s a pain to create and update.

    As far as sharing a folder I use Samba which works in just about anything. I usually use several instances of smbd running with different configurations.

    Login or Signup to reply.
  3. If your version of QEMU supports it, you could use its -fw_cfg option to pass information to the guest. If that guest is running a Linux kernel with CONFIG_FW_CFG_SYSFS enabled, you will be able to read out the information from sysfs. An example:

    If you launch your VM like so:

    qemu-system-x86_64 <OPTIONS> -fw_cfg name=opt/com.example.test,string=qwerty
    

    From inside the guest, you can then get the value back from sysfs:

    cat /sys/firmware/qemu_fw_cfg/by_name/opt/com.example.test/raw
    

    There appears to be some driver for Windows as well, but I’ve never used it.

    Login or Signup to reply.
  4. One option is to create a ISO file and pass as parameter. This works for both host Win and Ubuntu and Guest Win and Ubuntu. You can read the mounted CD ROM inside the guest OS

    >>qemu-system-x86_64  -drive file=c:/qemuiso/winlive1.qcow2,format=qcow2 -m 8G  -drive file=c:qemuisosample.iso,index=1,media=cdrom
    
    
    On Guest Linux Mount CDROM in Ubuntu:-
    >>blkid     //to check if media is there
    >>sudo mkdir /mnt/cdrom
    >>sudo mount /dev/sr0 /mnt/cdrom    //this step can also be put in crontab
    >>cd /mnt/cdrom
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search