KVM VM Creation and Management

Creation

We'll use virt-install to create VMs. You should have installed this in the kvm installation page. There are other ways, but this is a good way to start.

You can create a VM at the command line, or put the following in a file, edit and execute it. The command will appear to hang on some versions of the tool as it waits for you to connect to the guest with VNC (despite using noautoconsole, it seems, though not currently in virsh version 1.3.1)

sudo virt-install \
--name=$1 \
--ram=4096 \
--vcpu=4 \
--disk path=/home/allen/VMs/$1.img,bus=virtio,size=40,cache=writeback \
--cdrom=/home/allen/VMs/ISOs/current.iso \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--os-type=linux \
--os-variant=ubuntu16.04 \
--hvm

Some notes on the options
--disk bus=virtio
This specifies that the guest will use the para-virtualized driver. This is faster if the system has the driver (linux does by default, for windows you'll need the driver below. 
--disk cache=writeback
Fastest on local disk, but because of the potential for data loss you should consider none for important data. 
--cdrom
This is used for installation only. After the initial boot this device disappears.  
--network bridge=br0
This specifies to the use the network bridge we created during installation
--graphics vnc
This specifies to make the console available at the fist address open starting at 5900
--graphics listen
This specifies to make available on all addresses. You're controlling this with your firewall (hopefully)
--os-type
      Refer to  osinfo-query os

Starting
The machine is started as part of the creation process. You can connect to the console.


Connect to the VM

If you have a Mac, you must use Chicken of the VNC or similar, as Screen Sharing will not work (for some reason). For a secure connection, tunnel over ssh and connect to localhost display 1

ssh -N -L 5901:localhost:5900 some.public.host

Add the windows 7 drivers

Download the Windows Drivers, and when the Windows installation gets to the point where it complains about no disk, mount it like so;

get https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso
virsh attach-disk someMachine virtio-win.iso hdc  --type cdrom --mode readonly
(Load Drivers in the installation console)
virsh attach-disk someMachine Windows7_Install.iso hdc  --type cdrom --mode readonly

After you're done, make sure to reattach the installation CD. If you don't, you'll get the error:

"windows is unable to the selected location. Error:0x80300001"


Reconfiguring

virsh setmaxmem <vm name> 16G --config
virsh setmem <vm name> 16G --config

# Directly edit the xml config
virsh edit someVM

Managing VMs

Showing all your VMs

virsh list --all

Starting and Stopping

At some point the installation may try to reboot and instead power off. You can start it again with a 

virsh start someMahine

virsh shutdown someMahine

You may not be able to to a graceful shutdown without the acpid package

#inside the guest
sudo apt-get install acpid

Powering Off

virsh destroy someMahine

Stopping and Starting All the VMs at Once

# provided way
service libvirt-guests stop

# scripted way - shutdown-vms ..check acpi-ness of this way
for i in `virsh list --all|awk '{print $2}'|grep -v Name`; do virsh shutdown $i;done

# startup-vms 
for i in `virsh list --all|awk '{print $2}'|grep -v Name`; do virsh start $i;done

Saving and Restoring VMs (pause and unpause)

# save-vms 
for i in `virsh list --all|awk '{print $2}'|grep -v Name`; do virsh save $i $i.vmsav;done

# restore-vms 
for i in `virsh list --all|awk '{print $2}'|grep -v Name`; do virsh restore $i.vmsav;done





Troubleshooting

If you get the error "kvm binary is deprecated, please use qemu-system-x86_64 instead" you may have another hypervisor running, such as a headless virtualbox


References


https://unix.stackexchange.com/questions/293570/libvirt-command-to-start-up-all-guest-virtual-machines-which-have-auto-start-en
Comments