ow do I setup and manage a virtualized environment with Kernel based Virtual Machine (KVM) in CentOS or Red Hat Enterpise Linux version 6 server on IBM server? Can you provide step-by-step commands of how to install and manage Virtual Machines (VMs) on a physical server using KVM for RHEL/CentOS version 6.4?
KVM is part of RHEL and CentOS Linux. You can easily install the same using the following commands. Please note that only the package names have changed in RHEL/CentOS 6.x. If you are using RHEL / CentOS 5.x, read our previous CentOS / Redhat (RHEL) v.5.x KVM Virtulization guide for more information.
Install required KVM RPMs/packages
Type the following yum command
# yum groupinstall Virtualisation Tools Virtualization Platform
# yum install python-virtinst
OR
# yum install kvm qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools
Sample outputs:
Loaded plugins: product-id, protectbase, rhnplugin
This system is receiving updates from RHN Classic or RHN Satellite.
0 packages excluded due to repository protections
Setting up Install Process
Package libvirt-0.10.2-18.el6_4.15.x86_64 already installed and latest version
Resolving Dependencies
–> Running transaction check
—> Package libguestfs-tools.x86_64 1:1.16.34-2.el6 will be installed
–> Processing Dependency: libguestfs-tools-c = 1:1.16.34-2.el6 for package: 1:libguestfs-tools-1.16.34-2.el6.x86_64
….
..
..
spice-glib.x86_64 0:0.14-7.el6_4.3
spice-gtk.x86_64 0:0.14-7.el6_4.3
spice-gtk-python.x86_64 0:0.14-7.el6_4.3
spice-server.x86_64 0:0.12.0-12.el6_4.5
vgabios.noarch 0:0.6b-3.7.el6
vte.x86_64 0:0.25.1-8.el6_4
Complete!
Turn on libvirtd service
The libvirtd program is the server side daemon component of the libvirt virtualization management system. Type the following chkconfig command to turn it on:
# chkconfig libvirtd on
Start the libvirtd service by typing the following service command:
# service libvirtd start
Sample outputs:
Starting libvirtd daemon: [ OK ]
You can verify the libvirtd service by tying the following commands:
# service libvirtd status
libvirtd (pid 31128) is running…
# virsh -c qemu:///system list
Id Name State
Sample setup
+————-+
| CentOS/RHEL | |—–|
–>ISP router—+ Box Host w/ +— eth0 –>-+ br0 + Private IP
| KVM | | |
+————-+— eth1 –>-+ br1 | Public IP
|—–|
|
vm1-+—vm2—-vm3—vm4
*** NOTE: each VMs will have two interface i.e. Private and Public *****
Where,
eth0 – KVM servers’ LAN interface.
eth1 – KVM servers’ WAN/Internet interface.
br0 – KVM servers’ bridge interface to give VM direct access to all the resources on the LAN.
br1 – KVM servers’ bridge interface to give VM direct access to all the resources on the WAN/Internet. Users can ssh into VMs or you can host HTTPD/SMTPD using this interface.
obsd-vm1 (vm1) – OpenBSD VM name.
rhel-vm1 (vm2) – RHEL VM name.
Install and configure a network bridge
All VMs will only have network access to host and other VMs on same physical server via private network. You need to crate a network bridge so that the VMs can access your LAN and possible the Internet/WAN from outside. Type the following yum command to install bridge-utils package:
# yum install bridge-utils
Setup a default gateway
Edit /etc/sysconfig/network as follows
# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=kvm-42.cyberciti.biz
GATEWAYDEV=br1
Configure bridging
Update /etc/sysconfig/network-scripts/ifcfg-eth0 (private) as follows:
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
HWADDR=00:30:48:C6:0A:D8
BRIDGE=br0
Update /etc/sysconfig/network-scripts/ifcfg-eth1 (public) as follows:
# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
HWADDR=00:30:48:C6:0A:D9
BRIDGE=br1
Create/edit the /etc/sysconfig/network-scripts/ifcfg-br0 file to setup private/lan ip address for br0:
# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.10.29.66
NETMASK=255.255.255.192
DELAY=0
Create/edit the /etc/sysconfig/network-scripts/ifcfg-br1 file to setup public/wan/internet ip address for br1:
# cat /etc/sysconfig/network-scripts/ifcfg-br1
DEVICE=br1
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
IPADDR=74.ww.xx.yy
NETMASK=255.255.255.248
GATEWAY=74.ww.xx.yy
DELAY=0
I need to route all lan traffic (subnet 10.0.0.0/8) via 10.10.29.65 gateway. Create/edit file /etc/sysconfig/network-scripts/route-br0 as follows:
# cat /etc/sysconfig/network-scripts/route-br0
10.0.0.0/8 via 10.10.29.65
Warning: Restarting network service over the ssh session may result into total loss of the connectivity to the server. So make sure br0 and br1 configuration including routing set correctly.
A note about SELinux
I have not disabled SELinux on CentOS / RHEL. I do not recommend disabling SELinux. So make sure the config file has correct SELinux permissions:
# ls -Z /etc/sysconfig/network-scripts/{route-br0,ifcfg-eth?,ifcfg-br?}
Sample outputs:
-rw-r–r–. root root system_u:object_r:net_conf_t:s0 /etc/sysconfig/network-scripts/ifcfg-br0
-rw-r–r–. root root system_u:object_r:net_conf_t:s0 /etc/sysconfig/network-scripts/ifcfg-br1
-rw-r–r–. root root system_u:object_r:net_conf_t:s0 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r–r–. root root system_u:object_r:net_conf_t:s0 /etc/sysconfig/network-scripts/ifcfg-eth1
-rw-r–r–. root root system_u:object_r:net_conf_t:s0 /etc/sysconfig/network-scripts/route-br0
Use restorecon command to set or restore file(s) default SELinux security contexts:
# restorecon -Rv /etc/sysconfig/
If you are going to reboot the SELinux enabled server, make sure you type the following command:
# touch /.autorelabel
# reboot
Restart the networking service
Type the following command to restart networking on RHEL/CentOS/SL:
# service network restart
Verify br0/br1 settings
Type the following commands:
# brctl show
# ip addr show br0
# ip addr show br1
# ip route
# ping cyberciti.biz
Creating VMs
You need to use virt-install command.
Example: Create OpenBSD VM
Grab, installation media:
# cd /var/lib/libvirt/boot/
# wget http://ftp.openbsd.org/pub/OpenBSD/5.4/amd64/install54.iso
In this example, I am using virt-install to create a OpenBSD 5.4_amd64 VM named obsd-vm1 with one virtual CPU, 1 GB memory and 10 GB of disk space:
# virt-install \
-n obsd-vm1 \
–description cyberciti.biz OpenBSD 5.4 64 bit VM1 \
–ram=1024 \
–vcpus=1 \
–cpu host \
–os-variant=openbsd4 \
–accelerate \
–hvm \
–cdrom /var/lib/libvirt/boot/install54.iso \
–network bridge:br0,model=virtio –network bridge:br1,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/openbsd-vm1-cyberciti.biz.img,bus=virtio,size=10
VIRT-INSTALL OPTIONS
-n obsd-vm1 : The name of the VM.
–description cyberciti.biz OpenBSD 5.4 64 bit VM1 : The long description of the VM.
–ram=1024 : 1024MB is the amount of memory allocated to the VM.
–vcpus=1 : The number of virtual CPU(s) for the VM.
–cpu host : Optimize CPU properties for the VM.
–os-variant=openbsd4 : This is the VM OS type.
–accelerate : Prefer KVM or KQEMU (in that order) if installing a QEMU guest. This option is no longer required.
–hvm : Request the use of full virtualization.
–cdrom /var/lib/libvirt/boot/install54.iso : Install OpeBSD v5.4 from an iso the location parameter.
–network bridge:br0,model=virtio –network bridge:br1,model=virtio : Create a network bridge using br0 and br1 i.e. the VM will have two network interface for LAN and Internet.
–graphics vnc : Use VNC to access installation using vnc server/viewer from your local Linux/OSX/Unix/Windows desktop.
–disk path=/var/lib/libvirt/images/openbsd-vm1-cyberciti.biz.img,bus=virtio,size=10 : This is is the image file for the VM, the size is specified in GBs and I am forcing vio driver. The virtio driver provides support service for paravirtualized devices using the VirtIO protocol.
How do I connect to OpenBSD installer from my local desktop?
Type the following command on your local desktop:
# ssh -L 5900:127.0.0.1:5900 root@KVM-Server-IP-Here
OR
# ssh -L 5900:127.0.0.1:5900 -N -f -l root kvm-42.cyberciti.biz
Use VNC client to connect to 127.0.0.1:5900. Now, just follow on-screen instructions:
Fig.01: OpenBSD installation started over the VNC session
Before rebooting the installer make sure you setup com0 console for the VM. For example, for a OpenBSD VM, append the following parameters to the file /etc/boot.conf and then reboot the VM:
stty com0 115200
set tty com0
See how to stup SSH to tunnel VNC traffic though the Internets for more information.
How do I start my VM after OpenBSD install is finished?
The virt-install will create a config file for VM at /etc/libvirt/qemu/obsd-vm1.xml. To start VM called obsd-vm1, enter:
# virsh start obsd-vm1
How do I access a VMs console?
Login to KVM host and type the following command:
# virsh console obsd-vm1
Sample outputs:
Fig.02: Getting access to a OpenBSD VM console when networking is down
Final example: Create RHEL VM using DVD
Type the following command:
# virt-install \
–name RHEL-vm1 \
–description cyberciti.biz RHEL 6.4 64 bit VM1 \
–ram=2048 \
–vcpus=2 \
–disk path=/var/lib/libvirt/images/rhel-vm1-cyberciti.biz.img,size=20 \
–cdrom /var/lib/libvirt/boot/RHEL.6.4.Server-DVD1.iso \
–network bridge:br0 –network bridge:br1 \
–graphics vnc
Before rebooting the installer make sure you setup com0 console for the VM. For example, for a RHEL/CentOS VM, append the following parameters to the kernel boot line in /boot/grub.conf file and then reboot the VM:
console=tty0 console=ttyS1,19200n8
Stay tuned for the following advanced topics in RHEL/CentOS v6.x KVM (rss) series:
Firewall, kVM & VMs security configuration.
Attaching storage device to a VM.
Changing VM parameters such as RAM, CPU, Disk and more.
Managing and cloning VMs and more.
]]>
ernel-based Virtual Machine (KVM) is a virtualization module for the Linux kernel that turns it into a hypervisor. How can I install KVM, setup guest operating system as the back-end virtualization technology for non-graphic Ubuntu Linux 14.04 LTS server?
You can use KVM to run multiple operating systems such as Windows, *BSD, Linux distro using virtual machines. Each virtual machine has its private disk, graphics card, a network card and more.
What is a hypervisor?
KVM is a hypervisor that creates and run virtual machines. A server on which a hypervisor is running is called as a host machine. Each virtual machine is referred to as a guest machine. Using KVM, you can run multiple operating systems such as CentOS, OpenBSD, FreeBSD, MS-Windows running unmodified.
Fig.01: What is KVM hypervisor?
Steps for installing KVM on Ubuntu Linux 14.04 LTS
The host server located in the remote data center and it is a headless server.
All commands in this tutorial typed over the ssh based session.
You need a vnc client to install the guest operating system.
In this tutorial, you will learn how to install KVM software on Ubuntu and use KVM to setup your first guest VM.
Find out if server is capable of running hardware accelerated KVM vm
You need to use the kvm-ok command to determine if the server can host hardware accelerated KVM virtual machines. First install cpu-checker package using the following apt-get command:
$ sudo apt-get install cpu-checker
Sample outputs:
Fig.02: Installing cpu-checker on Ubuntu Linux
Next, run the following command:
$ sudo kvm-ok
Sample outputs:
INFO: /dev/kvm exists
KVM acceleration can be used
Install kvm on Ubuntu Linux
Type the following apt-get command to install kvm and related software:
$ sudo apt-get install qemu-kvm libvirt-bin virtinst bridge-utils
Sample outputs:
Fig.03: Installing kvm on Ubuntu Linux using apt-get
Default configuration locations
Default directory: /var/lib/libvirt/
ISO images for installation: /var/lib/libvirt/boot/
VM installation directory: /var/lib/libvirt/images/
Libvirt configuration directory for LVM/LXC/qemu: /etc/libvirt/
More about the default networking for VM
The default networking is called ‘default’. To list networks, enter:
$ sudo virsh net-list
Sample outputs:
Name State Autostart Persistent
default active yes yes
For network information, enter:
$ sudo virsh net-info default
Sample outputs:
Name: default
UUID: 1c0abaa3-8d17-45b5-85e9-c0d48cec94f9
Active: yes
Persistent: yes
Autostart: yes
Bridge: virbr0
To dump network information in XML format, enter:
$ sudo virsh net-dumpxml default
Sample outputs:
<network connections= 1 >
<name>default</name>
<uuid>1c0abaa3-8d17-45b5-85e9-c0d48cec94f9</uuid>
<forward mode= nat >
<nat>
<port start= 1024 end= 65535 />
</nat>
</forward>
<bridge name= virbr0 stp= on delay= 0 />
<ip address= 192.168.122.1 netmask= 255.255.255.0 >
<dhcp>
<range start= 192.168.122.2 end= 192.168.122.254 />
</dhcp>
</ip>
</network>
Create a CentOS Linux VM
First, grab the CentOS Linux 7.x DVD ISO file:
$ cd /var/lib/libvirt/boot/
$ sudo wget http://mirrors.kernel.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
$ ls
Sample outputs:
CentOS-7-x86_64-Minimal-1511.iso install58.iso
To provision new virtual machines use virt-install command. In this example, I’m creating a CentOS 7.x VM with 1GB RAM, 1 CPU core, and 20GB disk space, enter:
$ sudo virt-install \
–virt-type=kvm \
–name centos7 \
–ram 1024 \
–vcpus=1 \
–os-variant=rhel7 \
–hvm \
–cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1511.iso \
–network network=default,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/centos7.img,size=20,bus=virtio
Sample outputs:
Starting install…
Allocating centos7.img | 20 GB 00:00
Creating domain… | 0 B 00:00
WARNING Unable to connect to graphical console: virt-viewer not installed. Please install the virt-viewer package.
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
Understanding virt-install options
–virt-type=kvm : Use kvm as the hypervisor to install CentOS7 guest.
–name centos7 : Name of the new guest virtual machine instance.
–ram 1024 : Memory to allocate for guest instance in megabytes.
–vcpus=1 : Number of virtual cpus to configure for the guest.
–os-variant=rhel7 : Optimize the guest configuration for a specific operating system variant. Use ‘virt-install –os-variant list‘ to see the full OS list.
–hvm : Request the use of full virtualization.
–cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1511.iso : File or device use as a virtual CD-ROM device for fully virtualized guests. It can be path to an ISO image, or to a CDROM device.
–network network=default,model=virtio : Connect the guest to the host network. In this example, connect to a virtual network in the host called “default” with nic model called virtio.
–graphics vnc : Setup a virtual console in the guest and export it as a VNC server in the host. This is useful for our headless server (see below).
–disk path=/var/lib/libvirt/images/centos7.img,size=20,bus=virtio : Specifies media to use as storage for the guest. A path to /var/lib/libvirt/images/centos7.img storage media to use with size (20 GB) to use if creating new storage and disk bus type set to virtio.
A note about VNC and a headless server
This is a headless server i.e. a server without a local interface or GUI. There is no monitor or peripherals, such as a keyboard and mouse attached to this server. To continue installation you need to use the vnc client from your own laptop or desktop. To find out information about the vnc server port, enter:
$ sudo virsh dumpxml centos7 | grep vnc
Sample outputs:
<graphics type= vnc port= 5901 autoport= yes listen= 127.0.0.1 >
Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server.
To access guest domain’s VNC console
Type the following SSH port forwarding command:
$ ssh vivek@server1.cyberciti.biz -L 5901:127.0.0.1:5901
Where,
ssh vivek@server1.cyberciti.biz – Establishes the SSH session to the remote KVM host at server1.cyberciti.biz host.
-L – Start local port forwarding.
5901:127.0.0.1:5901 – Setup tunnel i.e. pass traffic over the internet to access remote server 127.0.0.1 and port 5901. See “Setup SSH To Tunnel VNC Traffic Though Internet” for more information.
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5901 as follows:
Fig.04: Accessing VNC consoles of KVM guests via SSH
You should see CentOS Linux 7 guest installation screen as follows:
Fig.05: CentOS 7 guest vm installation
Just follow on screen instructions to install CentOS 7. After some time CentOS 7 installed successfully on my server and ready to use. Go ahead and click reboot button. The remote server closed the connection to our VNC client. Type the following command to boot up VM for the first time:
$ sudo virsh start centos7
Sample outputs:
Domain centos7 started
Verify it:
$ sudo virsh list
Sample outputs:
Id Name State
3 puffy running
5 centos7 running
Again use an SSH client to setup tunnel and a VNC client to access CentOS 7 vm via the vnc server:
Fig.07: Sample CentOS 7 vm session using VNC
And, there you have it a CentOS 7 vm running KVM on top of the Ubuntu Linux 14.04 LTS server. In the default configuration, the CentOS 7 guest operating system will have access to network services, but will not be visible to other machines on the network or from the Internets. The CentOS VM will not be able to host an accessible web server. In next part of this tutorial I will talk about port forwarding and setting up bridged networking to connect to the outside network transparently.
]]>
am using KVM/libvirt on Linux operating system and how do I assign static IP address using dnsmasq dhcpd server for my default virtual network switch?
By default, an instance of dnsmasq dhcpd server is automatically configured and started by libvirt for each virtual network switch needing it. Each virtual network switch can given a range of IP addresses provided to guests through DHCP. The default networking switch uses dnsmasq server.
Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.
View the current dnsmasq DHCP configuration
Type the following command to list networks
# virsh net-list
Sample outputs:
Name State Autostart Persistent
default active yes yes
To see the default network information, enter:
# virsh net-dumpxml default
Sample outputs:
<network connections= 2 >
<name>default</name>
<uuid>e346291e-f86b-4f2f-a16e-654136441805</uuid>
<forward mode= nat >
<nat>
<port start= 1024 end= 65535 />
</nat>
</forward>
<bridge name= virbr0 stp= on delay= 0 />
<mac address= 52:54:00:12:fe:35 />
<ip address= 192.168.122.1 netmask= 255.255.255.0 >
<dhcp>
<range start= 192.168.122.100 end= 192.168.122.254 />
</dhcp>
</ip>
</network>
The DHCP range is between 192.168.122.100 and 192.168.122.254.
How to configure static guest IP addresses on the VM host
First find out your guest VM’s MAC addresses, enter:
# virsh dumpxml {VM-NAME-HERE} | grep -i <mac
# virsh dumpxml xenial | grep -i <mac
Sample outputs:
<mac address= 52:54:00:4c:40:1c />
Please note down the MAC addresses of the xenial VM that you want to assign static IP addresses.
Edit the default network
Type the following command:
# virsh net-edit default
Find the following section:
<dhcp>
<range start= 192.168.122.100 end= 192.168.122.254 />
Append the static IP as follows after range:
<host mac= 52:54:00:4c:40:1c name= xenial ip= 192.168.122.4 />
Where,
mac= 52:54:00:4c:40:1c – VMs mac address
name= xenial – VMs name.
ip= 192.168.122.4 – VMs static IP.
Here is my complete file with three static DHCP entries for three VMs:
<network>
<name>default</name>
<uuid>e346291e-f86b-4f2f-a16e-654136441805</uuid>
<forward mode= nat />
<bridge name= virbr0 stp= on delay= 0 />
<mac address= 52:54:00:12:fe:35 />
<ip address= 192.168.122.1 netmask= 255.255.255.0 >
<dhcp>
<range start= 192.168.122.100 end= 192.168.122.254 />
<host mac= 52:54:00:a0:cc:19 name= centos7 ip= 192.168.122.2 />
<host mac= 52:54:00:f7:a1:c8 name= puffy ip= 192.168.122.3 />
<host mac= 52:54:00:4c:40:1c name= xenial ip= 192.168.122.4 />
</dhcp>
</ip>
</network>
Restart DHCP service:
# virsh net-destroy default
# virsh net-start default
Sample outputs:
Network default destroyed
Network default started
If you are running the guest/VM called xenial shutdown it:
# virsh shutdown xenial
# /etc/init.d/libvirt-bin restart
# virsh start xenial
# ping -a 192.168.122.4
Sample outputs:
PING 192.168.122.4 (192.168.122.4) 56(84) bytes of data.
64 bytes from 192.168.122.4: icmp_seq=1 ttl=64 time=0.518 ms
64 bytes from 192.168.122.4: icmp_seq=2 ttl=64 time=0.202 ms
64 bytes from 192.168.122.4: icmp_seq=3 ttl=64 time=0.327 ms
^C
— 192.168.122.4 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.202/0.349/0.518/0.129 ms
Each time the guest or VM called xenial comes online (or rebooted for the kernel update) it will get 192.168.122.4 as static IP address by dnsmasq DHCP server.
]]>
have a cloud-based virtual machine running Ubuntu and CentOS Linux server. It may or may not be virtualized. How do I determine what kind of virtualization technology (VMWARE/ KVM/ XEN/ VirtualBox/ Container/ lxc/Hyper-V etc.) a Linux guest system running on? How do I find out the virtualization type of a CentOS 7 Linux VPS?
You need to use the virt-what program (shell script) to detect the type of virtualization being used (or none at all if we’re running on bare-metal). It prints out one of more lines each being a ‘fact’ about the virtualization.
Install virt-what on Debian or Ubuntu Linux VM
$ sudo apt-get install virt-what
OR
$ sudo apt install virt-what
Fig.01: Debian/Ubuntu Linux install virt-what command
Install virt-what on RHEL/CentOS/Scientific Linux VM
$ sudo yum install virt-what
Fig.02: Fedora/RHEL/CentOS Linux install virt-what command
Install virt-what on Fedora Linux VM
$ sudo dnf install virt-what
Check if your remote server runs in virtual environment
Simply type the following command:
$ sudo virt-what
xen
xen-domU
Another output:
$ sudo virt-what
kvm
Another output:
$ sudo virt-what
lxc
Other possible values
hyperv : This is Microsoft Hyper-V hypervisor.
parallels : The guest is running inside Parallels Virtual Platform (Parallels Desktop, Parallels Server).
powervm_lx86 : The guest is running inside IBM PowerVM Lx86 Linux/x86 emulator.
qemu : This is QEMU hypervisor using software emulation.
virtualpc : The guest appears to be running on Microsoft VirtualPC.
xen-hvm : This is a Xen guest fully virtualized (HVM).
uml : This is a User-Mode Linux (UML) guest.
openvz : The guest appears to be running inside an OpenVZ or Virtuozzo container.
linux_vserver : This process is running in a Linux VServer container.
ibm_systemz : This is an IBM SystemZ (or other S/390) hardware partitioning system.
If nothing is printed, then it can mean *either* that the program is running on bare-metal *or* the program is running inside a type of virtual machine which we don’t know about or cannot detect. For more info see virt-what – detect if we are running in a virtual machine and here is the source code of the script:
#!/bin/bash –
# virt-what. Generated from virt-what.in by configure.
# Copyright (C) 2008-2011 Red Hat Inc.
# Do not allow unset variables, and set defaults.
set -u
root=
skip_qemu_kvm=false
VERSION= 1.13
function fail {
echo virt-what: $1 >&2
exit 1
}
function usage {
echo virt-what [options]
echo Options:
echo –help Display this help
echo –version Display version and exit
exit 0
}
# Handle the command line arguments, if any.
TEMP=$(getopt -o v –long help –long version –long test-root: -n virt-what — $@ )
if [ $? != 0 ]; then exit 1; fi
eval set — $TEMP
while true; do
case $1 in
–help) usage ;;
–test-root)
# Deliberately undocumented: used for make check .
root= $2
shift 2
;;
-v|–version) echo $VERSION ; exit 0 ;;
–) shift; break ;;
*) fail internal error ($1) ;;
esac
done
# Add /sbin and /usr/sbin to the path so we can find system
# binaries like dmicode.
# Add /usr/libexec to the path so we can find the helper binary.
prefix=/usr
exec_prefix=${prefix}
PATH= ${root}${prefix}/lib/virt-what:${root}/sbin:${root}/usr/sbin:${PATH}
# Check we re running as root.
if [ x$root = x ] && [ $EUID -ne 0 ]; then
fail this script must be run as root
fi
# Many fullvirt hypervisors give an indication through CPUID. Use the
# helper program to get this information.
cpuid=$(virt-what-cpuid-helper)
# Check for various products in the BIOS information.
# Note that dmidecode doesn t exist on non-PC architectures. On these,
# this will return an error which is ignored (error message redirected
# into $dmi variable).
dmi=$(LANG=C dmidecode 2>&1)
# Architecture.
# Note for the purpose of testing, we only call uname with -p option.
arch=$(uname -p)
# Check for VMware.
# cpuid check added by Chetan Loke.
if [ $cpuid = VMwareVMware ]; then
echo vmware
elif echo $dmi | grep -q Manufacturer: VMware ; then
echo vmware
fi
# Check for Hyper-V.
# http://blogs.msdn.com/b/sqlosteam/archive/2010/10/30/is-this-real-the-metaphysics-of-hardware-virtualization.aspx
if [ $cpuid = Microsoft Hv ]; then
echo hyperv
fi
# Check for VirtualPC.
# The negative check for cpuid is to distinguish this from Hyper-V
# which also has the same manufacturer string in the SM-BIOS data.
if [ $cpuid != Microsoft Hv ] &&
echo $dmi | grep -q Manufacturer: Microsoft Corporation ; then
echo virtualpc
fi
# Check for VirtualBox.
# Added by Laurent Léonard.
if echo $dmi | grep -q Manufacturer: innotek GmbH ; then
echo virtualbox
fi
# Check for OpenVZ / Virtuozzo.
# Added by Evgeniy Sokolov.
# /proc/vz – always exists if OpenVZ kernel is running (inside and outside
# container)
# /proc/bc – exists on node, but not inside container.
if [ -d ${root}/proc/vz -a ! -d ${root}/proc/bc ]; then
echo openvz
fi
# Check for LXC containers
# http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
# Added by Marc Fournier
if [ -e ${root}/proc/1/environ ] &&
cat ${root}/proc/1/environ | tr \000 \n | grep -Eiq ^container= ; then
echo lxc
fi
# Check for Linux-VServer
if cat ${root}/proc/self/status | grep -q VxID: [0-9]* ; then
echo linux_vserver
fi
# Check for UML.
# Added by Laurent Léonard.
if grep -q UML ${root}/proc/cpuinfo ; then
echo uml
fi
# Check for IBM PowerVM Lx86 Linux/x86 emulator.
if grep -q ^vendor_id.*PowerVM Lx86 ${root}/proc/cpuinfo ; then
echo powervm_lx86
fi
# Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
if echo $dmi | grep -q Manufacturer.*HITACHI &&
echo $dmi | grep -q Product.* LPAR ; then
echo virtage
fi
# Check for IBM SystemZ.
if grep -q ^vendor_id.*IBM/S390 ${root}/proc/cpuinfo ; then
echo ibm_systemz
if [ -f ${root}/proc/sysinfo ]; then
if grep -q VM.*Control Program.*z/VM ${root}/proc/sysinfo ; then
echo ibm_systemz-zvm
elif grep -q ^LPAR ${root}/proc/sysinfo ; then
echo ibm_systemz-lpar
else
# This is unlikely to be correct.
echo ibm_systemz-direct
fi
fi
fi
# Check for Parallels.
if echo $dmi | grep -q Vendor: Parallels ; then
echo parallels
skip_qemu_kvm=true
fi
# Check for Xen.
if [ $cpuid = XenVMMXenVMM ]; then
echo xen; echo xen-hvm
skip_qemu_kvm=true
elif [ -f ${root}/proc/xen/capabilities ]; then
echo xen
if grep -q control_d ${root}/proc/xen/capabilities ; then
echo xen-dom0
else
echo xen-domU
fi
skip_qemu_kvm=true
elif [ -f ${root}/sys/hypervisor/type ] &&
grep -q xen ${root}/sys/hypervisor/type ; then
# Ordinary kernel with pv_ops. There does not seem to be
# enough information at present to tell whether this is dom0
# or domU. XXX
echo xen
elif [ $arch = ia64 ]; then
if [ -d ${root}/sys/bus/xen -a ! -d ${root}/sys/bus/xen-backend ]; then
# PV-on-HVM drivers installed in a Xen guest.
echo xen
echo xen-hvm
else
# There is no virt leaf on IA64 HVM. This is a last-ditch
# attempt to detect something is virtualized by using a
# timing attack.
virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
case $? in
0) ;; # not virtual
1) # Could be some sort of virt, or could just be a bit slow.
echo virt
esac
fi
fi
# Check for QEMU/KVM.
#
# Parallels exports KVMKVMKVM leaf, so skip this test if we ve already
# seen that it s Parallels. Xen uses QEMU as the device model, so
# skip this test if we know it is Xen.
if ! $skip_qemu_kvm ; then
if [ $cpuid = KVMKVMKVM ]; then
echo kvm
else
# XXX This is known to fail for qemu with the explicit -cpu
# option, since /proc/cpuinfo will not contain the QEMU
# string. The long term fix for this would be to export
# another CPUID leaf for non-accelerated qemu.
if grep -q QEMU ${root}/proc/cpuinfo ; then
echo qemu
fi
fi
fi
]]>
ernel-based Virtual Machine (KVM) is a virtualization module for the Linux kernel that turns it into a hypervisor. How can I install KVM with bridged networking, setup guest operating system as the back-end virtualization technology for non-graphic Ubuntu Linux 16.04 LTS server?
You can use KVM to run multiple operating systems such as Windows, *BSD, Linux distro using virtual machines. Each virtual machine has its private disk, graphics card, a network card and more.
Steps for installing KVM on Ubuntu Linux 16.04 LTS server
The host server located in the remote data center and it is a headless server.
All commands in this tutorial typed over the ssh based session.
You need a vnc client to install the guest operating system.
In this tutorial, you will learn how to install KVM software on Ubuntu 16.04 LTS server and use KVM to setup your first guest VM.
Follow installation steps of KVM on Ubuntu 16.04 LTS headless sever
Step 1: Install kvm
$ sudo apt-get install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker
Step 2: Verify kvm installation
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
Step 3: Configure bridged networking
$ sudo cp /etc/network/interfaces /etc/network/interfaces.bakup-1-july-2016
$ sudo vi /etc/network/interfaces
Edit/append as follows:
auto br0
iface br0 inet static
address 10.18.44.26
netmask 255.255.255.192
broadcast 10.18.44.63
dns-nameservers 10.0.80.11 10.0.80.12
# set static route for LAN
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.18.44.1
post-up route add -net 161.26.0.0 netmask 255.255.0.0 gw 10.18.44.1
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
# br1 setup with static wan IPv4 with ISP router as a default gateway
auto br1
iface br1 inet static
address 208.43.222.51
netmask 255.255.255.248
broadcast 208.43.222.55
gateway 208.43.222.49
bridge_ports eth1
bridge_stp off
bridge_fd 0
bridge_maxwait 0
Save and close the file. Restart the networking service, enter:
$ sudo systemctl restart networking
Verify it:
$ sudo brctl show
Step 4: Create your first virtual machine
I am going to create a CentOS 7.x VM. First, grab CentOS 7.x latest ISO image:
$ cd /var/lib/libvirt/boot/
$ sudo wget https://mirrors.kernel.org/centos/7.2.1511/isos/x86_64/CentOS-7-x86_64-DVD-1511.iso
CREATE CENTOS 7 VM
In this example, I’m creating CentOS 7.x VM with 2GB RAM, 2 CPU core, 2 nics (1 for lan and 1 for wan) and 40GB disk space, enter:
$ sudo virt-install \
–virt-type=kvm \
–name centos7 \
–ram 2048 \
–vcpus=2 \
–os-variant=rhel7 \
–virt-type=kvm \
–hvm \
–cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-DVD-1511.iso \
–network=bridge=br0,model=virtio \
–network=bridge=br1,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/centos7.qcow2,size=40,bus=virtio,format=qcow2
To configure vnc login from another terminal over ssh and type:
$ sudo virsh dumpxml centos7 | grep vnc
<graphics type= vnc port= 5901 autoport= yes listen= 127.0.0.1 >
You can also use the following command:
$ sudo virsh vncdisplay centos7
Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your client/desktop:
$ ssh vivek@server1.cyberciti.biz -L 5901:127.0.0.1:5901
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5901 as follows:
Fig.01 : VNC client to complete CentOS 7.x installation
You should see CentOS Linux 7 guest installation screen as follows:
Fig.02: CentOS 7.x installation on KVM based VM
Now jist follow on screen instructions and install CentOS 7. Once installed, go ahead and click reboot button. The remote server closed the connection to our VNC client. You can reconnect via KVM client to configure the rest of the server including SSH based session or firewall.
CREATE FREEBSD 10 OR 11 VM
In this example, I’m creating FreeBSD 10 VM with 2GB RAM, 2 CPU core, 2 nics (1 for lan and 1 for wan) and 40GB disk space, enter:
$ cd /var/lib/libvirt/boot/
$ sudo wget ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/amd64/ISO-IMAGES/10.3/FreeBSD-10.3-RELEASE-amd64-disc1.iso
$ sudo virt-install \
–virt-type=kvm \
–name freebsd \
–ram 2048 \
–vcpus=2 \
–os-variant=freebsd10.0 \
–virt-type=kvm \
–hvm \
–cdrom=/var/lib/libvirt/boot/FreeBSD-10.3-RELEASE-amd64-disc1.iso \
–network=bridge=br0,model=virtio \
–network=bridge=br1,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/freebsd10.qcow2,size=40,bus=virtio,format=qcow2
In this example, I’m creating FreeBSD 11 VM with 1GB RAM (expandable to 2GB), 1 vCPU core (expandable to 2vCPU), 1 nic and 40GB disk space, enter:
$ sudo virt-install \
–virt-type=kvm \
–name freebsd \
–memory=1024,maxmemory=2048 \
–vcpus=1,maxvcpus=2 \
–os-variant=freebsd10.0 \
–virt-type=kvm \
–hvm \
–cdrom=/var/lib/libvirt/boot/FreeBSD-11.0-RELEASE-amd64-disc1.iso \
–network=bridge=br0,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/freebsd11.qcow2,size=40,bus=virtio,format=qcow2
To configure vnc login from another terminal over ssh and type:
$ sudo virsh dumpxml freebsd | grep vnc
<graphics type= vnc port= 5902 autoport= yes listen= 127.0.0.1 >
Please note down the port value (i.e. 5902). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your client/desktop:
$ ssh vivek@server1.cyberciti.biz -L 5902:127.0.0.1:5902
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5902 to continue with FreeBSD 10.3 installation.
CREATE OPENBSD 5.9 VM
In this example, I’m creating OpenBSD 5.9 VM with 2GB RAM, 2 CPU core, 2 nics (1 for lan and 1 for wan) and 40GB disk space, enter:
$ cd /var/lib/libvirt/boot/
$ sudo wget http://mirror.esc7.net/pub/OpenBSD/5.9/amd64/install59.iso
$ sudo virt-install \
–virt-type=kvm \
–name openbsd \
–ram 2048 \
–vcpus=2 \
–os-variant=openbsd5.6 \
–virt-type=kvm \
–hvm \
–cdrom=/var/lib/libvirt/boot/install59.iso \
–network=bridge=br0,model=virtio \
–network=bridge=br1,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/openbsd59.qcow2,size=40,bus=virtio,format=qcow2
To configure vnc login from another terminal over ssh and type:
$ sudo virsh dumpxml openbsd | grep vnc
<graphics type= vnc port= 5903 autoport= yes listen= 127.0.0.1 >
Please note down the port value (i.e. 5903). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your client/desktop:
$ ssh vivek@server1.cyberciti.biz -L 5903:127.0.0.1:5903
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5903 to continue with OpenBSD 5.9 installation.
CREATE DEBIAN 8.X (DEBIAN JESSIE) VM
In this example, I’m creating Debian 8.5 VM with 2GB RAM, 2 CPU core, 2 nics (1 for lan and 1 for wan) and 40GB disk space, enter:
$ cd /var/lib/libvirt/boot/
$ sudo wget https://mirrors.kernel.org/debian-cd/current/amd64/iso-dvd/debian-8.5.0-amd64-DVD-1.iso
$ sudo virt-install \
–virt-type=kvm \
–name=debina8 \
–ram=2048 \
–vcpus=2 \
–os-variant=debian8 \
–virt-type=kvm \
–hvm \
–cdrom=/var/lib/libvirt/boot/debian-8.5.0-amd64-DVD-1.iso \
–network=bridge=br0,model=virtio \
–network=bridge=br1,model=virtio \
–graphics vnc \
–disk path=/var/lib/libvirt/images/debian8.qcow2,size=40,bus=virtio,format=qcow2
To configure vnc login from another terminal over ssh and type:
$ sudo virsh dumpxml debian8 | grep vnc
<graphics type= vnc port= 5904 autoport= yes listen= 127.0.0.1 >
Please note down the port value (i.e. 5904). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your client/desktop:
$ ssh vivek@server1.cyberciti.biz -L 5904:127.0.0.1:5904
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5904 to continue with Debian Linux 8.5 installation.
Useful commands
Let us see some useful commands.
Find the list of the accepted OS variants
$ osinfo-query os | less
$ osinfo-query os | grep debian
$ osinfo-query os | grep freebsd
List a running vms/domains
$ sudo virsh list
Shutodwn a vm/domain called openbsd
$ sudo virsh shutdown openbsd
Start a vm/domain called openbsd
$ sudo virsh start openbsd
Suspend a vm/domain called openbsd
$ sudo virsh suspend openbsd
Reboot (soft & safe reboot) a vm/domain called openbsd
$ sudo virsh reboot openbsd
Reset (hard reset/not safe) a vm/domain called openbsd
$ sudo virsh reset openbsd
Delete/remove a vm/domain called openbsd
$ sudo virsh undefine openbsd
$ sudo virsh destroy openbsd
To see a complete list of virsh command type
$ virsh help | less
$ virsh help | grep reboot
]]>
Is there a command to create a snapshot of virtual machine (VM) while it is running? Does qemu-kvm support live snapshot creation?
Yes, KVM (Kernel Virtual Machine) does support both live and normal snapshots. The snapshot must base on qcow2 type disk. You can create a snapshot (disk and RAM) from arguments too. Snapshots are supported on KVM VM Host servers only. You can not create snapshots within KVM VM. You need to use any one of the following command:
qemu-img command – QEMU disk image utility. Never use qemu-img to modify images in use by a running virtual machine or any other process. Machine must be in shutdown state to use qemu-img command.
virsh command – The virsh program is the main interface for managing virsh guest domains including KVM. In this tutorial we are going to use the virsh command.
Syntax
The virsh command can create a snapshot from XML file using virsh snapshot-create or directly from a set of options using virsh snapshot-create-as command. The syntax is:
virsh snapshot-create-as –domain {VM-NAME} –name {SNAPSHOT-NAME}
Where,
–domain {VM-NAME}: Domain name/VM name/id/uuid
–name {SNAPSHOT-NAME} : Name of snapshot
Examples
First list running VMS/guests/domain from host os:
# virsh list
Id Name State
1 freebsd running
2 openbsd running
3 centos7 running
To see existing snapshots (if any) for a domain called openbsd, enter:
# virsh snapshot-list –domain openbsd
Name Creation Time State
3sep2016 2016-09-02 13:38:18 -0500 shutoff
3sep2016u1 2016-09-02 15:04:50 -0500 shutoff
Let us create a snapshot for freebsd domain. First, make sure freebsd domain using qcow2 disk:
# virsh dumpxml freebsd | grep -i qemu
Sample outputs:
<driver name= qemu type= qcow2 />
<driver name= qemu type= raw />
To create a snapshot for a domain/VM called freebsd, enter:
# virsh snapshot-create-as –domain freebsd \
–name 5sep2016s0 \
–description Snapshot before upgrading to FreeBSD 11 \
–live
Domain snapshot 5sep2016s0 created
You just took a snapshot from a running guest. This only captures the state of the disk and not the state of the memory. To take a new snapshot of a VM Guest called freebsd which currently not running:
# virsh shutdown freebsd
# virsh snapshot-create-as –domain freebsd \
–name 5Sep2016_S1 \
–description My First Snapshpot
# virsh start freebsd
To list snapshots for a domain called freebsd, enter:
# virsh snapshot-list –domain freebsd
OR for ubuntu-box2 vm:
$ virsh snapshot-list –domain ubuntu-box2
Fig.01: How to list snapshot with virsh KVM on Linux
To see detailed snapshot info for a domain called freebsd, enter:
# virsh snapshot-info –domain freebsd –snapshotname 5Sep2016_S1
Sample outputs:
Fig.02: Creating and managing KVM snapshots with virsh
How do I use/revert/restore a snapshot?
To revert a domain to a snapshot, enter:
# virsh shutdown –domain freebsd
# virsh snapshot-revert –domain freebsd –snapshotname 5Sep2016_S1 –running
Sample outputs:
Fig.03: Revert domain to snapshot
How do I delete a domain snapshot?
Use the following syntax:
# virsh snapshot-delete –domain freebsd –snapshotname 5Sep2016_S2
]]>
am a new Linux and KVM user. How do I delete a VM guest with virsh command line on Linux operating system? How do I delete a virtual machine called vps42 on Ubuntu or CentOS Linux server?
Deleting a VM Guest removes its XML configuration by default stored in /etc/ directory. You may also delete a guest’s storage files to completely erase the guest. In this quick tutorial you will learn how to delete a VM guest with virsh on Debian/Ubuntu/RHEL/CentOS Linux servers.
To Delete a KVM Guest Using Virsh:
First, list all running KVM guests using “virsh list” command.
Next, you need to shut down a guest virtual machine using the “virsh shutdown VM” command.
Finally, delete a VM Guest with “virsh undefine VM” command.
Let us see steps in details.
Step 1: List all a VM guests
Type the following command:
# virsh list
Sample outputs:
Fig.01: Virsh list a KVM vm
To see info about domain called openbsd, enter:
# virsh dumpxml VM_NAME
# virsh dumpxml –domain VM_NAME
# virsh dumpxml –domain openbsd
Note down a guest’s storage files
# virsh dumpxml –domain openbsd | grep source file
<source file= /nfswheel/kvm/openbsd.qcow2 />
Step 2: Shutdown the guest
Type the following command to shutdown the vm called openbsd:
# virsh shutdown VM_NAME
# virsh shutdown –domain VM_NAME
# virsh shutdown –domain openbsd
You can force a guest virtual machine to stop with the virsh destroy command:
# virsh destroy VM_NAME
# virsh destroy –domain VM_NAME
# virsh destroy –domain openbsd
Step 3: Deleting a virtual machine
To delete a VM Guest with virsh run
# virsh undefine VM_NAME
# virsh undefine –domain VM_NAME
# virsh undefine –domain openbsd
Sample outputs:
Domain openbsd has been undefined
There is no option to automatically delete the attached storage file called /nfswheel/kvm/openbsd.qcow2. To delete it, enter:
# rm -rf /nfswheel/kvm/openbsd.qcow2
A note about error: “cannot delete inactive domain with snapshots”
You cannot delete a VM with snapshots. For example:
# virsh undefine –domain openbsd
Sample outputs:
error: Failed to undefine domain openbsd
error: Requested operation is not valid: cannot delete inactive domain with 2 snapshots
To list snapshots, enter:
# virsh snapshot-list –domain VM_NAME
# virsh snapshot-list –domain openbsd
Sample outputs:
Name Creation Time State
3sep2016 2016-09-02 13:38:18 -0500 shutoff
3sep2016u1 2016-09-02 15:04:50 -0500 shutoff
The syntax is as follows to delete snapshot:
# virsh snapshot-delete –domain VM_NAME –snapshotname SNAPSHOT_NAME
To delete both snapshots, enter:
# virsh snapshot-delete –domain openbsd –snapshotname 3sep2016
Domain snapshot 3sep2016 deleted
# virsh snapshot-delete –domain openbsd –snapshotname 3sep2016u1
Domain snapshot 3sep2016u1 deleted
Verify it:
# virsh snapshot-list –domain openbsd
Name Creation Time State
Now delete the vm:
# virsh undefine –domain openbsd
Domain openbsd has been undefined
# rm -f /nfswheel/kvm/openbsd.qcow2
]]>
installed KVM on Ubuntu server as described here. However, I am unable to get out of a ‘virsh console domain’ command. How can I exit from a “virsh console” connection over ssh based session?
You can use ‘virsh console vm’ to connect the virtual serial console for the guest machine. The syntax is:
virsh console domain
virsh console vmNameHere
virsh console –domain vmNameHere –safe
To exit a virsh console from a shell prompt:
Open a shell prompt or login using ssh.
Login to a host server called server1.
Use the virsh console command to log in to a running VM called ‘centos7’ type: virsh console centos7
To exit a virsh console session, type CTRL+Shift followed by ].
How to exit a “virsh console”?
The keyboard shortcuts are as follows if you are using English keyboard i.e. Escape character is ^]:
CTRL+Shift+5
CTRL+Shift+]
Fig.01: Use special keyboard shortcuts to exit from a virsh console
More on virsh-console options
Type the following command:
$ virsh help console
Sample outputs:
NAME
console – connect to the guest console
SYNOPSIS
console <domain> [–devname <string>] [–force] [–safe]
DESCRIPTION
Connect the virtual serial console for the guest
OPTIONS
[–domain] <string> domain name, id or uuid
–devname <string> character device name
–force force console connection (disconnect already connected sessions)
–safe only connect if safe console handling is supported
]]>
wanted to clone my Debian or Ubuntu Linux KVM VM for testing purpose. How do I clone existing virtual machine images under KVM?
You can use a simple command named virt-clone. It is a command line utility for cloning existing virtual machine images using the “libvirt” hypervisor management library. It will copy the disk images of any existing virtual machine, and define a new guest with an identical virtual hardware configuration. Elements which require uniqueness will be updated to avoid a clash between old and new guests.
How to clone your VM and spawn new instances in KVM
The syntax is
# virt-clone –original {Domain-Vm-Name-Here} –auto-clone
OR
# virt-clone –original {Domain-Vm-Name-Here} \
–name {New-Domain-Vm-Name-Here}
Examples
First VM/domain with devices to clone must be paused or shutoff. To gracefully shutdown a domain named ubuntu-box1, run:
$ sudo virsh shutdown ubuntu-box1
OR you can paused it as follows:
$ sudo virsh suspend ubuntu-box1
$ virsh list
Sample outputs:
Domain ubuntu-box1 suspended
Id Name State
1 freebsd running
5 ubuntu-box1 paused
Let us generate a new guest name, and paths for new storage automatically for a vm called ubuntu-box1
$ sudo virt-clone –original ubuntu-box1 –auto-clone
Sample outputs:
WARNING Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating ubuntu-box-1-clone.qcow2 | 40 GB 00:00:04
Clone ubuntu-box1-clone created successfully.
The above command cloned the guest called “demo” on the default connection, auto generating a new name called ubuntu-box1-clone and disk clone path. You can start or resume original domain:
$ sudo virsh start ubuntu-box1
OR
$ sudo virsh resume ubuntu-box1
Next, start ubuntu-box1-clone, enter:
$ sudo virsh start ubuntu-box1-clone
Verify it:
$ virsh list
My dhcpd server gave 192.168.2.147 IP address to ubuntu-box1-clone VM, run:
$ ping -c2 192.168.2.147
Finally, ssh into the box:
$ ssh vivek@192.168.2.147
Sample sessions:
Fig.01 virt-clone command cloned existing virtual machine images
Please note, virt-clone does not change anything inside the guest OS, it only duplicates disks and does host side changes. So things like changing passwords, changing static IP address, ssh-keys, hostnames etc are outside the scope of this tool. Once login using to cloned VM using ssh, you can change those:
$ ssh vivek@192.168.2.147
$ sudo -s
# echo ubuntu-box1-clone > /etc/hostname
# sed -i s/ubuntu-box1/ubuntu-box1-clone/g /etc/hosts
# reboot
# /bin/rm -v /etc/ssh/ssh_host_*
# dpkg-reconfigure openssh-server
# passwd vivek
You can use virt-sysprep instead of virt-clone if you need to clone the VM and make/reset anything inside the guest OS.
]]>
ow do I rename KVM-powered virtual machine from foo to bar using virsh command line option on Linux?
There are two ways to rename a domain/VM. Let us see how to change the name of a domain or vm under KVM.
Method 1 – Rename KVM VM with virsh
The syntax is:
# virsh domrename {domain} {new-name}
To rename VM from foo to bar, type:
# virsh shutdown foo
Domain foo is being shutdown
Now rename a VM, run:
# virsh domrename foo bar
Domain successfully renamed
Start a VM/domain, enter:
# virsh start bar
Domain bar started
Method 2 – Rename KVM domain with virsh
First get domain information in XML and save it to a new file:
# virsh dumpxml foo > bar.xml
Edit the XML file and change the name between the <name></name>
# vi bar.xml
Update it as follows from foo to bar:
<name>bar</name>
Shutdown the foo domain/vm:
# virsh shutdown foo
Domain foo is being shutdown
You need to undefine the old VM name:
# virsh undefine foo
Domain foo has been undefined
Finally, import the edited XML file to define the VM bar:
# virsh define bar.xml
Domain bar defined from bar.xml
Start the domain bar, enter:
# virsh start bar
Domain bar started
]]>