Way to pass parameters or share a directory/file to a qemu-kvm launched VM on Centos 7.0
Asked Answered
C

4

5

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.

Coincident answered 11/6, 2019 at 0:56 Comment(0)
B
7

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.

Bo answered 3/2, 2022 at 7:22 Comment(0)
F
1

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.

Faris answered 31/12, 2021 at 20:45 Comment(0)
F
1

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:\qemuiso\sample.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
Facies answered 9/1, 2023 at 14:3 Comment(0)
S
0

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.

Solange answered 9/7, 2019 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.