[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
howtos:emulators:helper_script_for_managing_qemu_virtual_machines [2016/09/01 07:11 (UTC)] – [Networking] louigi600howtos:emulators:helper_script_for_managing_qemu_virtual_machines [2016/09/04 19:47 (UTC)] – [Preface] louigi600
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, unless you do it very often, to manually start a VM by using qemu-system-* from console. Who would want to write the below command for starting a VM:+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 that it can be quite challenging, unless you do it very often, to manually start a VM from a text console. Who would want to write the below command for starting a VM ?
  
   qemu-system-arm -name armedslack -M versatilepb -m 256 -k en-us -vnc :5,password -usb -kernel /VM/armedslack/zImage-versatile -initrd /VM/armedslack/initrd-versatile -append 'root=/dev/sda1 rootfs=ext2' -monitor telnet:127.0.0.1:1035,server,nowait  -drive file=/VM/armedslack/qemu_hdu.raw,index=0,media=disk -drive file=,index=1,media=cdrom  -net nic,macaddr=52:54:57:c0:2c:bb -net tap,ifname=tap5   qemu-system-arm -name armedslack -M versatilepb -m 256 -k en-us -vnc :5,password -usb -kernel /VM/armedslack/zImage-versatile -initrd /VM/armedslack/initrd-versatile -append 'root=/dev/sda1 rootfs=ext2' -monitor telnet:127.0.0.1:1035,server,nowait  -drive file=/VM/armedslack/qemu_hdu.raw,index=0,media=disk -drive file=,index=1,media=cdrom  -net nic,macaddr=52:54:57:c0:2c:bb -net tap,ifname=tap5
  
-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 practice that people running qemu VMs use some sort of software to help them createrun and maintain them. If you want an easy way out you might consider virtmanager or something like that. Personally I chose to manage my VMs from text console because for me it's an added value to be able to do such jobs even without GUI, leading me to write my onw scripts for managing my VMs.+Is is common, for people running qemu VMs, to use some sort of software for creatingrunning and maintain the VMs. If you want an easy way out you might consider virtmanager or something like that. Personally I chose to manage my VMs from text console because for me it's an added value to be able to do such jobs even without GUI, so possibly like many others I wrote my onw scripts for managing my qemu VMs.
  
-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'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'still like to hare with you the route I took.
  
 ====== Problems ====== ====== Problems ======
Line 365: Line 365:
   #!/bin/bash   #!/bin/bash
   NAME=$(basename $0)   NAME=$(basename $0)
-    tun_up ()+  tun_up ()
   { /sbin/ifconfig $1 0.0.0.0 promisc up   { /sbin/ifconfig $1 0.0.0.0 promisc up
     /usr/bin/sleep 0.5     /usr/bin/sleep 0.5
Line 383: Line 383:
   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 /etc/qemi-if* but that not really enough because qemu-system-* needs to run as root or it will not be able to create the taps and access other resources. Although it is technically possible to give an unprivileged user sufficient privileges to execute correctly qemu-system-* emulators it is much easier to give users the sudo right to run qemu-system-* as privileged user.
 +
 +  User_Alias QEMUERS = al, john, jack
 +  
 +  Cmnd_Alias QEMUCMD = /usr/bin/qemu-system-*
 +  
 +  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:
 +
 +  [ $(/usr/bin/id -u) -ne 0 ] && CMD="sudo qemu-system-.... " || CMD="qemu-system-...."
 +  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 601: Line 617:
  
 ====== Sources ====== ====== Sources ======
 +I've a blog entry on LQ where I talk a little more extensively on minimizing the code in bash scripts.
 +[[http://www.linuxquestions.org/questions/blog/louigi600-808242/minimize-the-amount-of-code-in-your-bash-scripts-37137/|Minimize the amount of code in your bash scripts ]]
  
 <!-- If you are copying information from another source, then specify that source --> <!-- If you are copying information from another source, then specify that source -->
 howtos:emulators:helper_script_for_managing_qemu_virtual_machines ()