The purpose of this script is to make basic use of kvm-qemu easier for personal usage. It doesn't try to do everything, it does however do a broad amount of basic things that are required for basic creation and usage of virtual machines.
At times the basic configuration wont be enough, you may certainly manually edit the run_vm.conf and then you can just use the script for its other features such as the network script, virtual machine launching, disk resizing, etc.
How to use
- Lets get the source
git clone https://github.com/jeekkd/kvm-qemu-menu.git && cd kvm-qemu-menu
- This will make the script readable, writable, and executable to root and your user.
sudo chmod 770 kvm.sh qemu-ifup.sh qemu-ifdown.sh
- Now you can launch the script. Take a look below at how its intended to be used to get a start on using it.
sudo bash kvm.sh
Reminder: For your first run don't forget to do 'Install dnsmasq.conf' in the main menu otherwise dnsmasq wont have a configuration file and you wont have dhcp.
The way this script is intended to be used is
- Create a virtual disk by going to 'Create and edit virtual machines' and then 'disk creation'
- Next, Use 'Virtual machine creation' from within the same sub-menu. This will walk you through creating a virtual machine and then will add the launch settings to a file called run_vm.conf
- Run your virtual machine after the OS installation finishes from the 'Run virtual machines' menu. Networking is launched automatically and is handled by qemu-ifup.sh, and the tap device for each virtaul machine is managed at launch by a menu asking you which virtual machine you are launching.
- After you're done running your virtual machines you can tear down the network by going to the main menu and using 'if-down script to tear down network'.
By this point you are up and running, but to explore some other features you may want to use:
- To snapshot, go to 'Snapshot virtual machine' and then 'Create snapshot'
- To change the running snapshot go to 'Snapshot virtual machine' and 'Change running snapshot'
- If you're done with a snapshot you can go to 'Snapshot virtual machine' and use 'Delete snapshot'. Remember to use 'Change running snapshot' if the snapshot you deleted was being ran on your virtual machine.
- Remember to make all the scripts executable, with the correct permissions
- Run the script as 'sudo bash kvm.sh' for best results
- If you change the network settings within qemu-ifup.sh your changes should be made within the 'Install dnsmasq.conf' section of the kvm.sh script too otherwise you will run into network issues.
dnsmasq, iptables, bridge-utils, qemu
Note: Additional configuration may be required depending upon the system used, some distros require further configuration such as on Gentoo where your user must be in the appropriate group (kvm). Consult the qemu documentation for your distro.
Networking can be a bit tricky, particularly so with so many variables involved with virtual networking and multiple virtual machines, along with scripting the process. Nothing is perfect, so when connectivity is failing here are some troubleshooting steps that will help.
Tip: During troubleshooting it is recommended to configure a static IP within your virtual machine to eliminate DHCP as a potential issue.
The first four steps can done with utilising both brctl show and ifconfig
- Does the bridge exist?
- Does the bridge have an ip?
- Does the tap device exist?
- Is the tap device associated with the bridge?
- Is ip forwarding enabled? 0 means no." sudo cat /proc/sys/net/ipv4/ip_forward
Manual editing of run_vm.conf
Please be aware of the possible negative outcomes with other features within the script if you manually edit entries within run_vm.conf. Features such as snapshots, disk resizing, etc rely upon parts of the qemu configuration being in the expected place, namely positon 3 and 4 (according to cut -d " " -f 3,4). Those portions are -hda <disk path/disk name> so they must stay at those positions.
If adding things to the configuration add it to the end so it does not effect the placement of things within the beginning portion.
How the script works (notable portions, high level overview)
Going from the point where you're creating a disk using 'disk creation', the script asks some questions notably where you want your virtual machine and what to name the folder to place it in. At this point the folder is created in the appropriate directory with the given name, and the virtual disk is stored here. This is important later for snapshots.
Virtual machine creation asks some basic questions for your virtual machine setttings, these are used to create your machine and all your desired settings plus some redetermined networking settings are then redirected to a file named run_vm.conf, this is important later for running virtual machines as this is how your virtual machines settings are tracked. The label that was asked to be entered is used in a file called vm_names.txt along with the virtual machines identifying number. After this the virtual machine is launched with a barebones configuration and goes into the installation of the selected install media.
From running virtual machines, this menu reads the run_vm.conf and vm_names.txt line by line and displays the virtual machine number and label on one line and then the virtual machine settings on the next line and repeats in a loop. Each line represents a particular virtual machine. When a option is selected it is saved in a variable. At this point the network script launches and passes the numerical value from the variable to be used in the script to determine the number for the tap device to raise (eg, tap2).
After the network script runs, sed is used with the mentioned variable to grab the selected line the virtual machine resides on and is saved in a variable. That variable is echoed to launch that line, and & is used to allow multiple virtual machines to be ran from the script.
Creating a snapshot, the same vm_menu is used here to select the desired virtual machine to snapshot. A variable again holds the value of this selection, and sed is used to get the line the virtual machine resides on, and then it is piped into cut and the field where the virtual disk is is selected. An echo is used to ask what the snapshot should be called.
Next, a function is called to get the virtual machines directory. This is important as to automatically determine the location to store the snapshot, and is why a directory for each virtual machine is created. This is particularly relevant for changing snapshots mentioned in the next section. A snapshot is now created and stored in the virtual machines directory.
Change running snapshot, again vm_menu is called and asks the user to select the virtual machine to modify, and from here the virtual machines directory is determined and ls | cat -n is used to show the available disks and snapshots to select (with line numbers) for this virtual machine. The user is prompted to select one, and the response is stored in a variable. From here sed is used to change the existing disk to the selected one.
This was a high level overview of some of the primary features of this script, there are a load of other features that work in pretty neat ways. Enjoy!