kvm background
kernel based virtual machine(KVM), is a Linux kernel module, which transfer Linux to a Hypervisor, which depends on the ability of hardware virtualization. usually the physical machine is called Host
, and the virtual machine(VM) run in host is called Guest
.
kvm itself doesn’t do any hardware emulator, which needs guest space to set an address space through dev/kvm
interface, to which provides virtual I/O, e.g. QEMU.
virt-manager is a GUI tool for managing virtual machines via libvirt, mostly used by QEMU/KVM virtual machines.
- check kvm model info
|
|
- whether CPU support hardware virtualization
|
|
install kvm
- install libvirt and qemu packages
|
|
- add current user to libvirtd group
|
|
network in kvm
default network is NAT(network address transation), when you create a new virtual machine, this forwards network traffic through your host system; if the host is connected to the Internet, then your vm have Internet access.
VM manager also creates an Ethernet bridge between the host and virtual network, so can ping IP address of VM from host, also ok on the other way.
- List of network cards
go to /sys/class/net
there are a few nic:
|
|
- multi interfaces on same MAC addresss
when a switch receives a frame from an interface, it creates an entry in the mac-address table with the source mac and interface. it the source mac is known, it will update the table with the new interface. so bascially if you assign the mac address of an external-network-avialable NIC-A to the special vm, NIC-A is lost.
- virbr0
the default
bridge NIC of libvirt is virbr0
. bridge network means the guest and host share the same physical Network Cards, as well as offer the guest a special IP, which can be used to access the guest directly. the virbr0
do network address translation
(NAT), basically transfer the internal IP address to an external IP address, which means the internal IP address is un-visiable from outside.
to add the virbr0
, when it is deleted previously:
|
|
to disable or delete virbr0
:
|
|
after starting the vm, can check the bridge network by:
|
|
and we can login the vm, (after we assign current user to libvert
group), and check NAT is working:
|
|
basically the vm can access external website, but external web can’t access vm_name.
|
|
create vm
create a virtual machine, can be done either through virt-install
or config.xml
:
virt-install
virt-install
has depends on system python, pip. if current ptyhon version is 2.7, it gives warnning and return -1 due to unfound module. so make sure the #PYTHONPATH# point to the correct path if you have multi python in system. and virt-install
has to run with root
.
then can start a virtual machine with following command options)
|
|
during the installation, the process looks very much like Linux installation on a bare machine. I suppose this way, it’s like install a dual-OS in the bare machine. during the installation, there is an error failed to load installer component libc6-udeb, it’s may due to the iso or img has missing component.
config.xml
create volumes
go to /var/lib/libvirt/images, and create volume as following:
1qemu-img create -f qcow2 ubuntu.qcow2 40G
check qemu-kvm & qemu-img introduction
add vm image
cp ubuntu.iso to
/var/lib/libvirt/images
as well:12ubuntu.qcow2ubuntu-16.04.3-server-amd64.iso
vm.xml
follow an xml sample:
|
|
a few tips about the xml above:
\
component is necessary for network interface. if not assign a special
mac address
in the interface. since we had definevirbr0
, an automatic mac address will be assigned, which is unique from the host machine’s IP, but if ssh login to the guest (ssh username@guest_ip), it actually can ping host machine’s iP or any external ip(www.being.com)\
compoennt, is setting for console
.
finally run the following CLI to start vm: v1:
|
|
libvert
libvert is a software package to manage vm, including libvirtAPI, libvirtd(daemon process), and virsh tool.
|
|
only when libvirtd
service is running, can we manage vm through libvert
. all configure of the vm is stored ad /etc/libvirt/qemu
. for virsh
there are two mode:
- immediate way e.g. in host shell
virsh list
- interactive shell e.g. by
virsh
to virsh shell
common virsh commands
|
|
virsh network commands
- host configure
Every standard libvirt installation provides NAT based connectivity to virtual machines out of the box. This is the so called ‘default virtual network’
|
|
When the libvirt default networkis running, you will see an isolated bridge device. This device explicitly does NOT have any physical interfaces added, since it uses NAT + forwarding to connect to outside world. Do not add interfaces. Libvirt will add iptables rules to allow traffic to/from guests attached to the virbr0 device in the INPUT, FORWARD, OUTPUT and POSTROUTING chains.
if default.xml
is not found, check fix missing default network, default.xml
is sth like:
|
|
then run:
|
|
if bind default
to virbr0
already, need delete this brige first.
- guest configure
add the following to guest xml configure:
|
|
more details can check virsh networking doc
snapshots
snapshots used to save the state(disk mem, time..) of a domain
- create a snapshot for a vm
|
|
- list all snapshots for vm
|
|
- display info about a snapshot
|
|
manage volumes
- create a storage volume
|
|
- attach to a vm
attache test-vol
to vm test
|
|
which can be check that the vm has added a block device /dev/vdb
|
|
or directly grow disk image:
|
|
- detach from a vm
|
|
- delete a vm
|
|
fs virsh commands
|
|