kvm

Using KVM Linux hypervisor (with libvirt, virtsh, virt-install and guestfish/libguestfs)

KVM (Kernel-based Virtual Machine) is a virtualization infrastructure for the Linux kernel that turns it into a hypervisor.

Installing

## check hardware virtualization supoort
$ egrep -i 'vmx|svm' --color=always /proc/cpuinfo

## disable SELinux
$ vi /etc/selinux/config
SELINUX=permissive
$ reboot

## install KVM, QEMU and user-space tools
$ apt-get install qemu-kvm qemu-utils libvirt-bin | yum install qemu-kvm qemu-img libvirt
$ sudo service libvirtd start | sudo systemctl start libvirtd 

# optionally add yourself to libvirtd group, otherwise sudo
$ sudo adduser <youruser> libvirtd
# check libvirtd daemon
$ sudo virsh -c qemu:///system list

from kvm@debian and kvm@centos

Networking

libvirt installation provides NAT based connectivity, also called default virtual network. It adds iptables rules to allow traffic to/from guests attached to the virbr0 device in the INPUT, FORWARD, OUTPUT and POSTROUTING chains, and attempt to enable ip_forward.

Advanced users will want to use full bridging, where the guest is connected directly to the LAN. Don’t attach a physical device to virbr0 (this is only for NAT connectivity).

## check default NAT virtual network
$ virsh net-list --all
default              active     yes
$ brctl show
virbr0

## bridging non-persistent using bridge-utils/brctl
$ apt-get|yum install bridge-utils
$ brctl addbr br0
$ brctl addif br0 eth0
# assign ip to bridge
$ ip link set dev br0 up
$ ip addr add dev br0 10.10.1.105/24
# delete
$ ip link set dev eth0 promisc on
$ ip link set dev eth0 master br0

## bridging using ifcfg/rhel
# disable network manager
$ sudo service NetworkManager stop ; sudo chkconfig NetworkManager off
$ sudo chkconfig network on ; sudo service network start
$ ifdown eth0
$ vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BRIDGE=br0
$ vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Bridge
# either static
BOOTPROTO=none
IPADDR=10.10.1.105
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
# or dhcp
#BOOTPROTO=dhcp
$ ifup eth0 ; ifup br0

## bridging in debian
$ vi /etc/network/interfaces
#auto eth0
#iface eth0 inet dhcp
# either static
iface br0 inet static
  bridge_ports eth0 eth1
  address 10.10.1.105
  broadcast 10.10.1.255
  netmask 255.255.255.0
  gateway 10.10.1.1
# or dhcp
auto br0
iface br0 inet dhcp
  bridge_ports eth0
  bridge_stp off
  bridge_fd 0
  bridge_maxwait 0
$ /etc/init.d/networking restart

## bridging using nmcli/networkmanager
$ nmcli con add type bridge autoconnect yes con-name br0 ifname br0
# either static
$ nmcli con mod br0 ipv4.addresses "10.10.1.105/24 10.10.1.1" ipv4.method manual 
$ nmcli con mod br0 ipv4.dns "8.8.8.8 8.8.4.4"
# or dhcp
$ nmcli con mod br0 ipv4.method auto
# remove current setting and add interface to bridge
$ nmcli c delete eth0
$ nmcli c add type bridge-slave autoconnect yes con-name eth0 ifname eth0 master br0
$ systemctl restart NetworkManager

from virtual networking and networking hints and tips

Using virsh

Virsh/virsh@man is main interface for managing (create, pause shutdown) virsh guest domains. It uses libvirt@wiki, an open source C API/lib, daemon and management tool for managing platform virtualization. It can be used to manage KVM, Xen, LXC, VMware ESX, QEMU, VirtualBox, Hyper-V, Bhyve and other virtualization technologies.

$ curl http://cdimage.ubuntu.com/ubuntu-server/daily/current/vivid-server-amd64.iso 
  -o linux.iso

## create virtual disk using qcow2 (grows as needed)
$ qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 8G

## create domain xml file either manually
$ uuid
f5b8c05b-9c7a-3211-49b9-2bd635f7e2aa
$ vi guest.xml
<domain type='kvm'>
  <name>guest</name>
  <uuid>f5b8c05b-9c7a-3211-49b9-2bd635f7e2aa</uuid>
  <memory>1048576</memory>
  <currentMemory>1048576</currentMemory>
  <vcpu>2</vcpu>
  <os>
    <type>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type="file" device="disk">
      <driver name="qemu" type="qcow2"/>
      <source file="/var/lib/libvirt/images/guest.qcow2"/>
      <target dev="vda" bus="virtio"/>
      <address type="pci" domain="0x0000" bus="0x00" slot="0x04" function="0x0"/>
    </disk>
    <disk type="file" device="cdrom">
      <driver name="qemu" type="raw"/>
      <source file=linux.iso"/>
      <target dev="hdc" bus="ide"/>
      <readonly/>
      <address type="drive" controller="0" bus="1" target="0" unit="0"/>
    </disk>
    <controller type="ide" index="0">
      <address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x1"/>
    </controller>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport="yes" listen='127.0.0.1'/>
    <console type='pty'>
      <target port='0'/>
    </console>
  </devices>
</domain>

## start vm
$ virsh create guest.xml
$ virsh list
...

## remote access vm
# either by vnc, usually kvm-host:5900
$ sudo netstat -nap | grep kvm
tcp 0 0.0.0.0:5900
# or console
$ virsh console guest

## manage VMs
$ virsh destroy|shutdown|undefined|suspend|resume|autostart guest

Using virt-install

virt-install@man is a command line tool for creating new KVM , Xen, or Linux container guests using libvirt

## install
$ apt-get install virtinst | yum install virt-install

'-r,--ram MEMORY' guest memory
'--vcpus VCPUS' guest virtual cpus
'--cpuset CPUSET' physical cpus used by guest, eg: 1-5,^3,8
'-c,--cdrom CDROM or' virtual file or device, iso usually
'--boot=BOOTOPTS' boot priority and kernel args, eg: cdrom,fd,hd,network,kernel_args="console=/dev/ttyS0"
'--disk path=PATH,size=G,format=raw|qcow2' same as '--filesystem PATH' guest storage media, created if doesnt exist
'--network bridge=BRIDGE|network=NAME'
'--graphics vnc|none' defaults to vnc
'--hvm' request full virtualization, useful if both para and full virtualizations are available
'--autostart' start domain on host boot

## start VM (from ISO locally or remotelly)
$ virt-install --name guest --ram 1024 --vcpus=2 
  --disk path=/var/lib/libvirt/images/guest.qcow2,device=disk,size=8 
  --network bridge=virbr0 --cdrom linux.iso

from Installing a virtual machine using virt-install and virt-install@tecmint

Using guestfish/libguestfs

libguestfs/Libguestfs@wiki/guestfish@man is a set of tools for accessing and modifying virtual machine (VM) disk images. You can use this for viewing and editing files inside guests, scripting changes to VMs, monitoring disk used/free statistics, creating guests, P2V, V2V, performing backups, cloning VMs, building VMs, formatting disks, resizing disks, and much more.

Libguestfs is a C library. All this functionality is available through guestfish scriptable and interactive shell, or virt-* tools (just a script wrapper).

## install
$ apt-get install libguestfs-tools | yum install libguestfs-tools | yaourt -S libguestfs

'-rw(default) or -ro' read-write or read-only
'-d libvirt-domain or -a disk.img'
'-i' interactive shell

# list files
$ virt-ls -d DOMAIN DIRECTORY

# edit file
$ virt-edit -d DOMAIN FILE -e 'SED-EXPR'
$ guestfish --rw -i -d DOMAIN
><fs> download FILE /tmp/FILE
><fs> ! sed -i 'SED-EXP' /tmp/FILE
><fs> upload /tmp/FILE FILE
# remove root password
$ virt-edit -a disk.img /etc/passwd -e 's/^root:.*?:/root::/'

# checksum file
$ guestfish --ro -a disk.img -i checksum sha256 FILE

# convert ISO to tarball
$ guestfish --ro -a in.iso -m /dev/sda tgz-out / out.tar.gz

# interactive commands, eg: delete,touch,stat a file
$ guestfish -a disk.img -i rm FILE
$ guestfish --ro -a disk.img -i stat FILE
# list all commands
$ guestfish -h

# lists differences between files in two VMs (or disk images)
$ virt-diff -d oldguest -D newguest
$ virt-diff -a old.img -A new.img

# disable systemd service
$ guestfish -a disk.img -i ln-sf /dev/null /etc/systemd/system/cloud-init.service

# export directory from VM
$ virt-copy-out -d DOMAIN VMDIR LOCALDIR

# read file
$ virt-cat disk.img FILE

# install packages
$ virt-builder fedora-20 -edit '/etc/yum.repos.d/fedora-updates-testing.repo:s/enabled=0/enabled=1/' --install emacs
# list os/templates supported
$ virt-builder --list
# same but using virt-customize
$ virt-customize -a fedora-20.img --update --install gcc

# show free space
$ virt-df -a disk.img
# list all filesystems
$ virt-filesystems -a disk.img --all --long -h

# show OS info, including version and installed packages
$ virt-inspector -d DOMAIN

# create a new image
$ virt-builder centos-7.0

# set root password
$ virt-sysprep --enable customize --root-password password:123456 -a /dev/sdX

# resize image, expand '/dev/sda2' by '+5G'
$ virt-filesystems --long -h --all -a olddisk
$ truncate -r olddisk newdisk ; truncate -s +5G newdisk
$ virt-resize --expand /dev/sda2 olddisk newdisk

from virt-tools, virt-tools@rwmj and guestfs-recipes/guestfs-recipes@ubuntu

Other KVM management tools

Virt-manager@wiki, GNOME_Boxes@wiki and OVirt@wiki are GUI versions of virtsh. See tools@kvm.