Enjoy Slackware 15.0!
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
howtos:emulators:helper_script_for_managing_qemu_virtual_machines [2016/08/31 14:37 (UTC)] louigi600 |
howtos:emulators:helper_script_for_managing_qemu_virtual_machines [2016/09/04 20:04 (UTC)] louigi600 [Problems] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Preface ====== | ====== Preface ====== | ||
- | Qemu is a popular and powerful open-source emulator often used for running KVM virtual machines. In fact qemu supports emulating so many thins that it can be quite challenging, | + | Qemu is a popular and powerful open-source emulator often used for running KVM Virtual Machines (VMs). In fact qemu supports emulating so many things |
qemu-system-arm -name armedslack -M versatilepb -m 256 -k en-us -vnc :5,password -usb -kernel / | qemu-system-arm -name armedslack -M versatilepb -m 256 -k en-us -vnc :5,password -usb -kernel / | ||
- | Ok everyone might want console redirect on vnc and monitor redirect via telnet but non the less that's still a relatively small subset of the options supported by qemu-system-arm and only has one disk one cdrom and one Network Interface Controller (NIC) so things can be much worse then this. | + | Not everyone might want console redirect on vnc and monitor redirect via telnet but non the less that's still a relatively small subset of the options supported by qemu-system-arm and only has one disk one cdrom and one Network Interface Controller (NIC) so things can be much worse then this. |
- | Is is common | + | Is is common, for people running qemu VMs, to use some sort of software |
- | Over the years I've radically changed the helper script form having text configuration files for each VM to a centarl VM configuration database. I'd like to share my experience in doing so without presumptuously declaring that I do this any better then anyone else, letting you decide what's good or bad for your needs. It's likely that someone else has done this and a lot better then me but nevertheless I's still like to hare with you the route I took. | + | Over the years I've radically changed the helper script form having text configuration files for each VM to a centarl VM configuration database. I'd like to share my experience in doing so without presumptuously declaring that I do this any better then anyone else, letting you decide what's good or bad for your needs. It's likely that someone else has done this and a lot better then me but nevertheless I'd still like to hare with you the route I took. |
====== Problems ====== | ====== Problems ====== | ||
Line 14: | Line 14: | ||
* ability to run both x86 and ARM virtual machines | * ability to run both x86 and ARM virtual machines | ||
* ability to run several VMs simultaneously | * ability to run several VMs simultaneously | ||
+ | * have the VMs appear as a real server in the LAN | ||
* flexibility on the number of disks assigned to a VM | * flexibility on the number of disks assigned to a VM | ||
* flexibility on the number of NICs assigned to a VM | * flexibility on the number of NICs assigned to a VM | ||
- | * avoid conflicts on tap and MAC address | ||
* avoid conflicts on VM console vnc port | * avoid conflicts on VM console vnc port | ||
* avoid conflicts on VM monitor port | * avoid conflicts on VM monitor port | ||
- | Dealing with such issues on a text based configuration file per each VM started making the code unnecessarily complicated. | ||
- | Manually creating a configuration file for a new VM required looking for information across all previously configured VMs configuration files. | ||
- | Another problem that you may come across while running VMs in general is networking | + | Wanting |
+ | |||
+ | Using the initial, per VM text based configuration, | ||
====== Proposed Solution ====== | ====== Proposed Solution ====== | ||
It quickly became apparent to me that the VM configuration would need to be generated rather then manually created and that a central configuration repository would much aid the process. Again a text based central configuration file would make the code in inherently complicated (having to deal with an arbitrary number of VMs each with arbitrary number of disks and NICs). | It quickly became apparent to me that the VM configuration would need to be generated rather then manually created and that a central configuration repository would much aid the process. Again a text based central configuration file would make the code in inherently complicated (having to deal with an arbitrary number of VMs each with arbitrary number of disks and NICs). | ||
Line 252: | Line 253: | ||
As mentioned above, getting bugged with all the details required for configuring a new VM can be a a killer unless you do it really often so here's how I go about it in a similar fashion to creating the basic configuration. The idea is to reduce the amount of code written specifically to each VM option allowing for a relatively slender script and ease to add new VM options if required. | As mentioned above, getting bugged with all the details required for configuring a new VM can be a a killer unless you do it really often so here's how I go about it in a similar fashion to creating the basic configuration. The idea is to reduce the amount of code written specifically to each VM option allowing for a relatively slender script and ease to add new VM options if required. | ||
- | Unfortunately here things are a little more complicated because different qemu-system-* produce slightly different output when prompted with help and because most of the VM options require a separate dialog asking for some specific action nonetheless a lot of the code and data required is self generated the **PARAMS** variable. The only specific code is to work around the differences between the various qemu-system-*. | + | Unfortunately here things are a little more complicated because different qemu-system-* produce slightly different output when prompted with help and because most of the VM options require a separate dialog asking for some specific action nonetheless a lot of the code required is self generated |
PARAMS=" | PARAMS=" | ||
Line 286: | Line 287: | ||
EMULATOR=$(< | EMULATOR=$(< | ||
case $EMULATOR in | case $EMULATOR in | ||
- | qemu-system-i*86) | + | qemu-system-i*86|qemu-system-x86_64) |
- | MACHINE=pc | + | |
- | grep -qwE "vmx|svm" / | + | |
- | [ $KVM -eq 1 ] && CPU=host || CPU=pentium3 | + | |
- | BIOS=" | + | |
- | BOOT_ORDER=" | + | |
- | ;; | + | |
- | | + | |
MACHINE=pc | MACHINE=pc | ||
grep -qwE " | grep -qwE " | ||
Line 364: | Line 358: | ||
===== Networking ===== | ===== Networking ===== | ||
- | As mentioned above I want my VMs to look like real machines on the LAN the host server is connected on, this will require bridging the tap devices. Newer versions of qemu can automatically create and use tap0 but I don't think it would do the bridging | + | As mentioned above I want my VMs to look like real machines on the LAN the host server is connected on, this will require bridging the tap devices. Newer versions of qemu can automatically create and use tap device |
- | I like to write a single script qemu-nethelper and have qemu-ifup and qemu-ifdown linked to it. | + | I like to write a single script qemu-nethelper and have qemu-ifup and qemu-ifdown linked to it. The qemu-system-* emulators all execute / |
+ | |||
+ | As mentioned above newer versions of qemu (I think 1.1+) automatically create the tap device so the qemu-nethelper only needs to do the bridging. Now to make things a lot easier I like to have the host on which I run VMs with br0 configured at boot and then the qemu-nethelper only needs to add the tap device to the bridge, making it extremely simple. If br0 is already configured at boot then you need not restart any iptables so long as the chains use the bridge devices and the kernel has support for ebtables. | ||
#!/bin/bash | #!/bin/bash | ||
NAME=$(basename $0) | NAME=$(basename $0) | ||
- | [ $(/ | ||
tun_up () | tun_up () | ||
- | { $SUDO / | + | { / |
/ | / | ||
- | | + | /sbin/brctl addif br0 $1 |
} | } | ||
| | ||
tun_down () | tun_down () | ||
- | { $SUDO / | + | { / |
/ | / | ||
- | | + | /sbin/brctl delif br0 $1 |
} | } | ||
| | ||
Line 389: | Line 384: | ||
esac | esac | ||
+ | ===== Using Qemu from Unprivileged Users ===== | ||
+ | Using root for doing your everyday tasks is commonly discouraged so let's see how we can work around using qemu from unprivileged users. | ||
+ | Some say that it's sufficient to give sudo execution on / | ||
+ | |||
+ | User_Alias QEMUERS = al, john, jack | ||
+ | | ||
+ | Cmnd_Alias QEMUCMD = / | ||
+ | | ||
+ | QEMUERS ALL=(ALL) NOPASSWD: QEMU | ||
+ | |||
+ | This would be sufficient to run the VMs as any of the unprivileged users in QEMUERS user alias (al, john, jack) but the management script would need to run sudo qemu-system-* .... this is easy to obtain: | ||
+ | |||
+ | [ $(/ | ||
+ | eval $(echo "$CMD &" | ||
+ | |||
+ | Alternatively you could give privileges to execute the management script as root. | ||
===== Examples ===== | ===== Examples ===== | ||
Here are examples of the dialogs that user would see wile creating, starting, stopping and deleting a VM. | Here are examples of the dialogs that user would see wile creating, starting, stopping and deleting a VM. | ||
Line 607: | Line 618: | ||
====== Sources ====== | ====== Sources ====== | ||
+ | I've a blog entry on LQ where I talk a little more extensively on minimizing the code in bash scripts. | ||
+ | [[http:// | ||
<!-- If you are copying information from another source, then specify that source --> | <!-- If you are copying information from another source, then specify that source --> |