Book/HelloXenProject/4-Chapter
Chapter 4: Using Xen Project
Configuring and Booting Virtual Machines
Welcome to chapter 4, I bet this chapter is, the more interested chapter for you and you can learn to run and manage your Virtual Machines via The Xen Project. In this chapter, we show you how you can configure DomU and setting up your network and storage. We knows, You are familiar with Linux but we show you some Linux tools for manage LVM and create your partitions. After this chapter, You can manage Xen Project and your Virtual Machines and Install GNU/Linux and Windows OS on your Xen Project ax DomU. We will speak about a tool with the name “ virt-manager” that is a Graphical tool for managing your Vms.
By default, Fedora Server edition not have any Graphical Environment and as you know, The Graphical Environment need a lot of resources and not a good idea for server. So, System administrator don't like to install Graphical Environment and prefer CLI. You may ask yourself, Some Graphical Environment like “XFCE” need low resources but compared with CLI they need more resources.
Manage virtual machines with virt-manager
We want to show you “virt-manager” and it is a graphical program, Thus we need to install a Graphical Environment. Popular Desktops in Fedora are “Gnome” , “KDE” , “XFCE” and “MATE” but you can use other Desktops too.
For Install a Graphical Environment use below commands :
Gnome :
# yum groupinstall gnome
or
# yum groupinstall gdm
KDE :
# yum groupinstall kde
XFCE :
# yum groupinstall xfce
MATE :
# yum groupinstall "MATE Desktop”
When installing finished just run below command :
# startx
And after it you can see your Desktop. Mine is:
Open a “Terminal” and type below command for installing “Virt-Manager” :
# yum install virt-manager
After it you can run “virt-manager”.
Usually, Xen Masters use LVM volumes to store the Xen guest virtual disks and we will explain it in the future and skip it now, But just know that you can use Virtual HDD and real partition for your Guest DomU.
The Virt-manager, provide a good GUI and you can create your DomU via it very easy :
As you see, It is easy as ABC and give you this ability to run your own DomU guest via a nice Graphical interface.
Note that, If after install Graphical Environment your Xen Project show you an error, Then don't worry and Just Reinstall the Xen Project via below command :
# dnf reinstall xen*
After, Everything back to a right point.
If you start “Virt-Manager” and see some error like “unable to connect to libvirt” then don't worry, Just type below command :
# service libvirtd restart
You as a Linux administrator know that Linux has “/var/log/” directory that record everything that happening in your system. After install Xen, The Xen Project create a directory with the name “xen” under “/var/log” and all Xen errors and...recorded under this directory. If you encounter with any error just see “/var/log/xen/”.
OK, It is enough and you are familiar with “Virt-Manager”. Now, It is a good time for speaking about “ Storage and Networking”. Let's Go!
Storage and Networking
The Xen Project can use your real partition on your HDD as a Guest HDD and you can add HDD image also. We say it “Local Storage”. The Local Storage can partitioned via a tool like “LVM” or you can save your HDD image as a file on it. Let me to speak about LVM more.
LVM or Logical Volume Manager is a device mapper in Linux. You may ask, What is Device Mapper? OK, The Device Mapper is a framework that provided by The Linux Kernel and its goal is mapping physical block devices onto high-level virtual block devices. Device Mapper passing data from a virtual block device to another block device. The LVM provide logical volume management for the Linux Kernel and a method of allocating space on storage devices. LVM is more flexible that traditional partitioning. When you install Linux, It selected a best method for partitioning by default.
You can consider “LVM” as a "dynamic partitions", Its mean that you can create/resize/delete partitions while your Linux is running and you don't need any reboot for influence. LVM have many benefits but some of important of them are :
- It can support more than one HDD.
- LVM can be created to support RAID. including RAID 1, RAID 5 and RAID 6
- It can support HA (High availability) and is a good option for Clusters.
- Save space. Small chunks of unused space from several disks can be combined to create a usable volume.
With a lot of features, It has some bad things too. For example, the initial set-up of LVM is more complex than just partitioning a disk and you must understand LVM scheme and models.
It a good figure that help you to understand LVM easily :
I guess you understand it better. I want to show you some Linux Tools that via them you can manage your HDDs.
The first tool that we cover here is “fdisk” or “cfdisk”. Just type :
# cfdisk
or
# fdisk -l
I bet you will found “cfdisk” more friendly. The “fdisk -l” command will show you all of your partitions and HDD on your system. For example, For mine is :
[root@localhost ~]# fdisk -l
Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xed8a6a75
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 662710271 662708224 316G 8e Linux LVM
Disk /dev/mapper/fedora-root00: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/fedora-swap: 16 GiB, 17179869184 bytes, 33554432 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
And “cfdisk” :
OK. The first method that we choice is using your real partition as guest HDD.
I added a new HDD to my system and linux detected it as “sdb”. Let me to tell you how Linux detect Device/Drive names. If you look at “/dev/” directory then you will find interesting names. Linux detect IDE Disk as “hd” and SCSI disks as “sd”. The complete scheme is “/dev/xxyn”. The “xx” is “hd” or “sd” as we said and :
- Y : It is a letter that indicate the number of Hard Disk. For example, “hda” mean the first IDE disk and “sda” mean the first SCSI Disk.
- N : It is a number that indicate the number of partition. Don't forget, The first four partitions (primary or extended) are in range 1..4. Logical partitions start from 5. For example, “/dev/hda2” mean the first IDE Hard Disk and The second primary or extended partition and “/dev/sdb5” mean the first logical partition on second SCSI hard disk.
We explained it because we don't like you remove other partitions and lost your data.
I added a new HDD to my system and Linus detected it as “sdb”. I use below command for look at my new HDD :
# cfdisk /dev/sdb
Create a partition via “cfdisk” is straightforward and don't need any teach, But don't forget to select “Empty” as your file system. Our suggestion is that don't create any partition and just use “parted” tool as below.
I used a tool with the name “parted” as below :
# parted /dev/sdb mklabel msdos
It show you a warning message about that all of your information will be destroy. Type “yes” and enter. Then, Write below command :
# parted /dev/sdb mkpart primary
after this command, Linux ask you some questions, I selected default file system (ext2) and enter “1” as “Start” and “100%” as “End”.
What do you see? Linux created a partition for you with the name “/dev/sdb1”. You can confirm it via below command :
# fdisk /dev/sdb -l
After it, I want enable “LVM” for this partition and use below command :
# parted -s /dev/sdb set 1 lvm on
In above command I used “set 1” that mean partition 1. Let me to see our partitions via “fdisk” command :
# fdisk /dev/sdb -l
Look carefully, You can see “LVM” at the end of line. It mean that “LVM” enable for our partition.
Type below command :
# pvcreate /dev/sdb1
# vgcreate Xen /dev/sdb1 -v
# lvcreate -L 8G -v -n Fedora Xen
In above command, You should select your size (We selected 8 Gigabyte) and logical volume name (Fedora) and a name that you typed in the previous command (Xen). The output is :
Let me to see our LVM via “vgdisplay” command :
# vgdisplay | more
OK, our storage is ready and we just need to create a config file for our VM. Before we dive into config file, Please let us for write something about “Networking”.
Networking in Xen
Create and configure network in real world is a hard job and can be complex in some areas. In VM world it can be complex too and depending on the network diagram. We never explain networking here complete byt we just show you how you can configure a simple network for your VM and your VM able to see external world.
It is true that Virtual Machines share your interfaces but it is not true that you must not concern about the security.
The Xen Project support some kind of networking that we will show you them here :
Bridge : Dom0 and DomU are connected to each other via a virtual network. It is a default and most common configuration. In this configuration, a software bridge created in the backend domain and the Virtual network connected to your physical network. For bridging you can use two schemes :1) Linux detect your NIC as “eth0” and in the first scheme, Your eth0 renamed to other name (myeth0) and bridge changed to eth0 2) in the second scheme, Your NIC remain eth0 and your bridge is mybr0.
Names are personalisation and you can select other names.
NAT : Network Address Translation or NAT is kind of routing and each guest have its own IP address. In NAT, Dom0 configured to forward your packets and iptables used to configure your Dom0 Kernel as a IP packet filter. NAT using RFC1918 addresses (http://tools.ietf.org/html/rfc1918).
Routing : In Routing your Dom0 configured for forward packets and a point-to-point link created between Dom0 and each DomU Virtual NIC. Don't forget, because of nature of routing, each of guest NIC must have static IP address.
OK, Xen need bridge for running and we want to show that you how you can create a Bridge for The Xen Project.
Depend on your Linux Distro (Dom0) you can use some tools for create a bridge in easy way. For example, On Fedora and Debian you can use “bridge-utils” for configure your Bridge.
My Current config are :
enp7s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.9.20 netmask 255.255.255.0 broadcast 172.30.9.255
inet6 fe80::52e5:49ff:fe40:c48f prefixlen 64 scopeid 0x20<link>
ether 50:e5:49:40:c4:8f txqueuelen 1000 (Ethernet)
RX packets 989 bytes 84310 (82.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 77 bytes 9575 (9.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 83 bytes 9332 (9.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 83 bytes 9332 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:8b:c5:55 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
As you see, Fedora detect my NIC as “enp7s0” and its IP address is “172.30.9.20”. I must tell you that it is a static IP and not DHCP. I will tell you something about DHCP too.
I want to create a bridge that use this static IP, So I create a file via below command :
# touch /etc/sysconfig/network-scripts/ifcfg-br0
Then open it via an editor like “nano” and fill it with below information :
DEVICE=br0
TYPE=Bridge
BOOTPROTO=none
ONBOOT=yes
IPADDR=172.30.9.20
PREFIX=24
GATEWAY=172.30.9.254
DNS1=172.30.9.1
DELAY=0
NM_CONTROLLED=no
But, If your real NIC use DHCP protocol then you should use below config for Bridge :
DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0
NM_CONTROLLED=no
After it, I must change my real NIC configuration, So I open it via an editor :
# nano /etc/sysconfig/network-scripts/ifcfg-enp7s0
HWADDR=50:E5:49:40:C4:8F
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp7s0
UUID=5e520f04-b32c-4116-b61f-89c190868edc
ONBOOT=yes
#IPADDR=172.30.9.20<
#PREFIX=24
#GATEWAY=172.30.9.254
#DNS1=172.30.9.1
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
NM_CONTROLLED=no
BRIDGE=br0
As you see, My current NIC configuration use “BOOTPROTO=none” and its mean Static IP and if it was “DHCP” then you can't see any parts like “IPADDR”, “ PREFIX”, “GATEWAY” and “ DNS1” because all of these parts gaining automatically. Because of I added all of these parts to my Bridge then I must remove these parts from real NIC configuration file and add “NM_CONTROLLED=no” and “BRIDGE=br0” at the end of file. Don't forget, “br0” is my bridge name and you can select any name that you like.
If my NIC use DHCP protocol then I didn't need any extra works.
Everything is ready and I must restart my network service for take affect :
# systemctl restart network.service
After it, you can see current configuration via “ifconfig” command as below :
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.9.20 netmask 255.255.255.0 broadcast 172.30.9.255
inet6 fe80::52e5:49ff:fe40:c48f prefixlen 64 scopeid 0x20<link>
ether 50:e5:49:40:c4:8f txqueuelen 1000 (Ethernet)
RX packets 846 bytes 62175 (60.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 129 bytes 11550 (11.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp7s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::52e5:49ff:fe40:c48f prefixlen 64 scopeid 0x20<link>
ether 50:e5:49:40:c4:8f txqueuelen 1000 (Ethernet)
RX packets 5479 bytes 468651 (457.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 558 bytes 75815 (74.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 83 bytes 9332 (9.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 83 bytes 9332 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:8b:c5:55 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
As you see, A Bridge with the name “br0” created and all information of my real NIC (enp7s0) given to my bridge. So, my Bridge work like my real NIC. Don't forget for each Physical NIC you should create a Bridge.
Our network is ready and we must create a Guest config.
Installing Windows as HVM domainU Guest
Huh, Are you ready? It is the most exciting part of our book and we to show you how you can install your Guest OS.
We choice Window 7 Enterprise for install as Guest and show you that how you can configure your Xen to use this “.iso” file and start your VM.
First of all, We must create a config file for our VM. Let me to create it via “touch” command in Linux :
# touch /etc/xen/windows.cfg
Then, I open it via an editor like “nano” as below :
# nano /etc/xen/windows.cfg
And fill this file via below information :
name = "Windows7"
builder = "hvm"
vcpus = 4
memory = "4096"
maxmex= "8192"
vif=['bridge=br0']
disk=['phy:/dev/Xen/Fedora,hda,w','file:/home/mohsen/windows7.iso,hdc:cdrom,r']
vnc="1"
vnclisten="172.30.9.20"
vncconsole="0"
boot="dc"
stdvga=1
videoram=32
on_poweroff = "destroy"
on_reboot = "restart"
on_crash = "restart"
I must tell you something about this file :
- vif=['bridge=br0'] : Because of we choice “br0” as a name of our Bridge then we must write “'bridge=br0'” ,But if your Bridge name is different then you must write your Bridge name instead of it here.
- disk=['phy:/dev/Xen/Fedora,hda,w','file:/home/mohsen/windows7.iso,hdc:cdrom,r'] : If you remember, We want to use our real partition as our Guest partition, Thus it is not a File that act as a real HDD. Because of it, We used “phy” that mean Physical. The “Xen” is our LVM and I choice “Fedora” as a name of my 20 GB partition on LVM. You may selected other name that you must put it here.
- Vnclisten="172.30.9.20" : Because I use the Xen Project on other server and not on my current PC, I must define the IP address of my Xen Server. My Xen Server IP is “172.30.9.20” and if your situation is like us then you must write your Xen Server IP here but if you run the Xen Project on your PC and not another PC or Server then you must write “vnclisten="0.0.0.0"”. When you select “0.0.0.0” as IP then it is your local host.
- boot="dc" : the “d” mean “CDROM” and “c” mean your HDD. It is your Boot order and when you Guest installed you can change it to “boot=”c””. The Xen Project parameters are boot on floppy (a), hard disk (c), Network (n) and CD-ROM (d).
Let us start our Guest. Just type below command :
# xl create /etc/xen/windows.cfg
and if you have not any error in your syntax then your xen just show “Parsing config from /etc/xen/windows.cfg” and your VM started.
After it write below command :
# xl list
and you can see something like below :
Name ID Mem VCPUsStateTime(s)
Domain-0 0 3659 8 r----- 887.6
Windows7 19 4096 4 -b---- 23.1
As you see, “Windows7” is running and we just need connect to it via “vncviewer”. But you must did something on your Firewall.
Linux use “iptables” as Firewall and VNC Port is not opened by default and we must open this port for connect to our VNC remotely. Don't forget, We doing it because our Xen server is on another PC and we must connect to it remotely thus we need open VNC Port. The VNC use Port 5900 and TCP protocol.
I use “netstat -nat” command for show our current network connections :
# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 172.30.9.20:5900 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 172.30.9.20:22 172.30.10.18:49824 ESTABLISHED
tcp6 0 0 :::9090 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
As you see, The IP “172.30.9.20” and Port “5900” are ready for connect but we must doing some steps for connect to our VM :
1- First of all, Install a tool like “vncviewer” on your Linux Distro.
2- Depend on your Linux Distro (Dom0) you must use “iptables” for open VNC Port on Dom0 :
# iptables -A INPUT -p tcp --dport 5900 -j ACCEPT
# iptables-save
After it, The Port “5900” is open on your Dom0 and you can connect to it remotely. You can use “iptables -L” for show your current iptables rules.
We ready ready to connect to our Xen Server :
# vncviewer 172.30.9.20:5900
And after it, A window displayed and we can do Windows install process:
Sometimes you don't like your VM viewed by others and you want set password for it, For do it use below parameter in your VM config file :
vncpasswd=”Your Password”
After it, When you want view your VM via VNC you must enter a password :
vncviewer 172.30.9.20:5900
Connected to RFB server, using protocol version 3.8
Performing standard VNC authentication
Password:
When you working with Your VM, You may have some problems with your Display and you must consider below Links for Troubleshooting your problems :
A good note that you must consider it is that you should install “Windows PV Drivers” for your Xen VM. Navigate to “http://www.xenproject.org/downloads/windows-pv-drivers.html” and download .TAR files and extract them, then configure your NIC IP address and see your network and copy these files and install them on your Windows Guest. In below figure you can see that I choice a static IP for my Windows Guest :
When you create another VM:
[root@localhost ~]# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 172.30.9.20:5900 0.0.0.0:* LISTEN
tcp 0 0 172.30.9.20:5901 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 172.30.9.20:5900 172.30.10.18:33869 ESTABLISHED
tcp 0 0 172.30.9.20:22 172.30.10.18:55000 ESTABLISHED
tcp 0 180 172.30.9.20:22 172.30.10.18:54616 ESTABLISHED
tcp6 0 0 :::9090 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
[root@localhost ~]# xl list
Name ID Mem VCPUsStateTime(s)
Domain-0 0 1590 8 r----- 192.7
Windows7 9 4096 4 -b---- 21.0
linux 10 2048 1 r----- 64.8
If you pay attention to the output of “ifconfig” command then you will see a default Bridge with the name “virbr0”. We can use this Bridge for add other NIC to our VM. For example, We need more than one NIC on our VM.
For adding more than one NIC you can use :
vif=['bridge=br0','mac=00:16:17:a5:6y:89,bridge=virbr0']
The “mac” defined the MAC address for our NIC.
We can use our first Bridge(br0) for adding more than NIC too but we should add a MAC address fpr each NIC :
vif=['bridge=br0,mac=00:13:r6:45:g6:9o','mac=00:16:17:a5:6y:89,bridge=br0']
In above example, We used the Bridge br0 for adding two NICs but we can't create another Bridge(br1) and attach it other Physical NIC and use it here.
In some situation, You may want to use another HDD or other Vms HDD in a VM. It is so easy and you can write the name of your other HDD in “disk” in the config file :
disk=['phy:/dev/Xen/Fedora,hda,w','phy:/dev/Xen/linux,hdb,w','file:/home/mohsen/Windows7.iso,hdc:cdrom,r']
In above example, my main HDD is “Fedora” and I mount another HDD (linux) as “hdb”. When I fire my “vm” then I can see other HDD in my vm. If you noted, Then you can see “w” and “r” word at the end of my lines. What are these? The “w” mean “read-write” and “r” mean “read-only”.
VM States in The Xen Project
When you run a VM through Xen and run “xl list” then you can see a column with the name “State” and a word that changed sometimes. What does this word mean?
For example, I run “xl list” and the output is as below :
[root@localhost ~]# xl list
Name ID Mem VCPUsStateTime(s)
Domain-0 0 1590 8 r----- 1211.3
OpenSUSE 44 2048 1 r----- 4.6
I will tel you something about these words :
- r - running
The domain is currently running.
- b - blocked
The domain is blocked, and not running or runnable. This happened when the domain is waiting on IO or going to sleep because there was nothing else for it to do.
- p - paused
The domain has been paused, and may administrator paused it via “xl pause” command. When a VM paused, The domain will still consume allocated resources like memory, but will not be eligible for scheduling by the Xen hypervisor.
- s - shutdown
The guest has requested to be shutdown, rebooted or suspended.
- c - crashed
The domain has crashed. It may if the domain has been configured not to restart on crash.
- d - dying
The domain is in process of dying, but hasn't completely shutdown or crashed.
In below example, my “OpenSUSE” VM is running and established :
[root@localhost ~]# xl list
Name ID Mem VCPUsStateTime(s)
Domain-0 0 1590 8 r----- 1245.3
OpenSUSE 44 2048 2 -b---- 127.4
Then, I run below command for “Pause” it :
# xl pause OpenSUSE
and my current state is:
[root@localhost ~]# xl list
Name ID Mem VCPUsStateTime(s)
Domain-0 0 1590 8 r----- 1248.0
OpenSUSE 44 2048 2 --p--- 127.5
For “unpause” a VM you can use below command :
# xl unpause OpenSUSE
[root@localhost ~]# xl list
Name ID Mem VCPUsStateTime(s)
Domain-0 0 1590 8 r----- 1257.0
OpenSUSE 44 2048 2 -b---- 128.3
Add USB Devices to your VM
Sometime you may want to add a USB device to your VM. Linux has a command with the name “lsudb” that show your USB controllers on your Server :
[root@localhost ~]# lsusb
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 13ba:0017 PCPlay PS/2 Keyboard+Mouse Adapter
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
As you see, My Server has USB 2.0 and USB 3,0 controllers. I plugged a HP Pendrive to my server and run command again :
[root@localhost ~]# lsusb
Bus 002 Device 003: ID 03f0:5607 Hewlett-Packard
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 13ba:0017 PCPlay PS/2 Keyboard+Mouse Adapter
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
What happened? As you see, “Hewlett-Packard “ added to my list. If you look at the output more then you see some numbers behind the device name. What are these? These are vendor and product numbers are in “[vendor]:[product]” form.
For example, My Pendrive from HP company has “03f0:5607” and If you navigate to the “http://www.linux-usb.org/usb.ids” and search “ 03f0” then you can see that it is a vendor number for HP company.
A good web site for search and find the devices models is “http://www.the-sz.com/products/usbid/”. You can browse it and enter the device “vendor” and “product” numbers and click on search then you can see the model of your device :
OK, We want add out Pendrive to our VM and copy and paste some files from/to it from our VM.
Fist of all, You must open your VM cofing file via an editor like “nano” :
# nano /etc/xen/linux.cg
Then add below lines to your config file :
usb="1"
usbdevice=['host:03f0:5607']
The “usb=”1”” cause enable USB controller and via “usbdevice” you can select your USB device for appear in VM. As you see, My HP Pendrive “vendor” and “product” numbers are “03f0:5607” and I written “usbdevice=['host:03f0:5607']” to my config file.
After it, When I start my VM then I can see my Pendrive that mounted in my VM. In below Figure, You can see that my HP Pendrive mounted in OpenSUSE Linux :
Use Physical CD/DVD ROM
It is True that installing OS from ISO files is more faster and better but sometime for some problems you can't use the .ISO file for install OS. For example, Your Dom0 not have enough space or you have CD or DVD of your OS. The Xen Project let you to use your Physical CD/DVD ROM for install OS.
Linux detect all devices as file, Thus your CD/DVD ROM is a file too. You can run “lsblk” command to find your CD/DVD ROM device :
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 316G 0 part
│ ├─fedora-root00 253:0 0 300G 0 lvm /
│ └─fedora-swap 253:1 0 16G 0 lvm [SWAP]
└─sda2 8:2 0 615.5G 0 part
├─Xen-Fedora 253:2 0 20G 0 lvm
└─Xen-linux 253:3 0 20G 0 lvm
sdb 8:16 1 15.1G 0 disk
sr0 11:0 1 158.5M 0 rom
As you see, Linux detected my DVD ROM as “sr0”, Thus my DVD is “/dev/sr0” and I can use it. If I want my VM boot via Physical CD/DVD ROM then I just add below line to my VM config :
disk=['phy:/dev/sr0,hdc:cdrom,r']
If you remember, Int he previous config we used “disk=['file:/home/mohsen/opensuse.iso,hdc:cdrom,r']” and “file” was for .ISO file but for Physical devices we use “Phy” instead of “file”. After it, My VM booted via Physical DVD ROM.
If DVD Drive not detected automatically then use below command :
# eject
This command eject DVD from the Guest and if you change CD or DVD then for detect the insertion use below command :
# eject -t
Create a VM via .img
We showed you how you can create a VM via “LVM” but sometime for some reasons you don't like to create a LVM and like to boot your VM via an image. We will show you it. It is so easy and just need some steps.
First of all, Please let me to create a directory via “mkdir” for our Virtual HDD :
# mkdir Windows
change to “Windows” directory via “cd” command :
# cd Windows/
And run below command for create a Virtual HDD about 40GB :
# dd if=/dev/zero of=windows.img bs=1M count=40000
[root@localhost Windows]# dd if=/dev/zero of=windows.img bs=1M count=40000
40000+0 records in
40000+0 records out
41943040000 bytes (42 GB) copied, 332.735 s, 126 MB/s
If you list files and directory via “ls” command then you will see something like below :
[root@localhost Windows]# ls
windows.img
As you see, A file with the name “windows.img” created for us. Now, We must change VM config file for use this file as Virtual HDD.
Open your VM config file via an editor like “Nano” and add below line as your HDD :
disk=['file:/home/mohsen/Windows/windows.img,hda,w']
As you see, We used “file” instead of “phy”.
You must note that if you install Windows 10 and see below error the don't worry, It is because of your Windows date and you must use updated .ISO file :
VGA Passthrough
A good feature and trick in Virtualization is VGA Passthrough. I don't know why someone need it but they have their reason for it. For example, Someone need run Graphical application under Vms and these application need a real VGA and can't work with a standard VGA Or some people like to Play Games :)
The Xen Project offer you this feature and you can use your real VGA in your VM but with some limitation. For example, Your CPU must support IOMMU.
Xen 4.0.0 is the first version Xen that support VGA graphics adapter passthrough. With this feature you can give the Guest Full control of graphic adapter and have Full 3D support in a VM. For enable this feature, Xen need IOMMU (Intel VT-d) support from the motherboard chipset. VT-d Pass-Through is a technique in Xen that give DomU this ability to access PCI using the IOMMU. An important note that you must consider is that “Only devices with FLR capabilities are supported” and otherwise you will see an Error. To check your PCI device for FLR feature you can run “lspci -w” on Dom0 and if in the output you see “FLReset-” then your device not supported and if you see “FLReset+” then it is OK.
We use Fedora and “-w” parameter not exist and thus we must use below commands :
First of all, Run below command to see a list of Hardware :
# lspci
Then run :
# lspci -vv -s PCI ID | grep FLR
For example :
# lspci -vv -s 00:02.0 | grep FLR
[root@localhost ~]# lspci -vv -s 00:02.0 | grep FLR
AFCap: TP+ FLR+
AFCtrl: FLR-
OK, We consider that your device support FLR and I will tell you that how you can attach it.
First of all, Execute below command :
# modprobe xen-pciback
Create a file via “touch” command and fill it with below information :
remove_device () {
BDF=$1
# Unbind a PCI function from its driver as necessary
[ ! -e /sys/bus/pci/devices/$BDF/driver/unbind ] || \
echo -n $BDF > /sys/bus/pci/devices/$BDF/driver/unbind
# Add a new slot to the PCI Backend's list
echo -n $BDF > /sys/bus/pci/drivers/pciback/new_slot
# Now that the backend is watching for the slot, bind to it
echo -n $BDF > /sys/bus/pci/drivers/pciback/bind
}
remove_device "Your PCI ID"
# For example : remove_device "0000:01:00.1"
Save the file and run this script via “sh yourfilename”.
After it, You must edit your VM config file. Open you config file via an editor like “Nano” :
# nano /etc/xen/windows.cfg
Then add below line to your config file :
pci=['Your PCI ID','Your PCI ID']
# For example : pci=['01:00.1','00:1a.0']
Then run your VM and your PCI device will attached to your VM.
Swap
Before we speak about Swap in Linux, Let me to tell something about “Paging”. The Paging is a scheme of memory management that your system use it for store and retrieves data from it. Why its named paging? It is because the OS retrieves data from a secondary storage in specific size that called pages. It is an important part of Virtual memory that implemented in modern OS and let you to run Applications that are larger than your Physical memory.
What is the Swap? Swap space usually is a disk partition but can be a file too. When you installing Linux, you can define a partition as Swap space but it can be done later too. The Swap space is recommended for users that have less than 1 GB of RAM. It is a area on your HDD which is part of your Virtual Memory. The Swap space used when your system sense that need more Physical memory for active processes and after it, Inactive pages on your Physical memory moving the Swap space and your Physical memory freed and have more free space for others. You must note that The Swap space exist on your HDD and the speed of HDDs are slower then Physical memory. It is true that HDD with new technologies like SSD has more speed but don't consider the Swap space as a replacement for the Physical memory. For define a Swap space you must define it twice of Physical memory. For example, If you have 4GB RAM then your Swap should be 8GB.
For check the Swap space of your system use below command :
# swapon --show
[root@localhost ~]# swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 16G 0B -1
[root@localhost ~]#
As you see, My system has 16GB Swap space.
You can use “free -h” command too :
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 7.5G 186M 7.0G 4.4M 325M 7.2G
Swap: 15G 0B 15G
[root@localhost ~]#
If you install the Linux as your VM then you may like to define an extended Swap space for it. It is true that you can use your Virtual HDD as a Swap space but you can define another HDD as Swap too.
First of all, We use “dd” command for create an image :
# dd if=/dev/zero of=linuxswap.swap bs=1024 count=”Your size”
For example :
[root@localhost mohsen]# dd if=/dev/zero of=linuxswap.swap bs=1024 count=4096000
4096000+0 records in
4096000+0 records out
4194304000 bytes (4.2 GB) copied, 32.8729 s, 128 MB/s
Then :
[root@localhost mohsen]# mkswap linuxswap.swap
mkswap: linuxswap.swap: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 3.9 GiB (4194299904 bytes)
no label, UUID=f2464b51-c7ef-4990-8cb6-8d50d151b52e
Now, It is a good time for attach our Swap space to our VM. Open your VM config file via an editor like “nano” and add below line to your VM config file :
file:/YourPath/linuxswap.swap,hdb,w
The complete line is something like :
disk=['phy:/dev/Xen/linux,hda,w','file:/home/mohsen/linuxswap.swap,hdb,w']
Now, Start your VM and when your VM is booted then your Swap space is “hdb” and your just need to enable it. You can use below command to enable your Swap in your VM :
# swapon /dev/hdb
As you see, My Linux had 2GB of Swap and after it my Linux VM has 5.9GB Swap Space.
For mount it automatically via boot just doing following steps :
1- Open “fstab” file from “/etc/fstab” via an editor like “nano”.
2- add below line :
/dev/XXX none swap defaults 0 0
# For example : /dev/hdb none swap defaults 0 0
3- Save file and reboot your system.
Using Xen-Tools
What is Xen-Tools? It is a collection of Perl Scripts that allow you to create a new guest Xen domain. It is compatible with Debian more but nowadays can support Ubuntu, CentOS and others. OK, Let start!
Go to “http://xen-tools.org/” and download the Xen-Tools. After it, For install “Xen-Tools” we must install some packages on CentOS. Run following command :
# yum install debootstrap perl-Text-Template perl-Config-IniFiles perl-File-Slurp perl-File-Which perl-Data-Dumper
After it go to the location that downloaded file is there and use “gunzip” and “tar” command for extract it. Then use “cd” command for change the current location and when you are in “Xen-Tools” directory run “make install” command. You will see something like below :
[root@localhost xen-tools-4.6.2]# make install
for i in hooks/*/*-*; do chmod 755 $i; done
chmod 755 hooks/common.sh
chmod 644 etc/*.conf
chmod 644 etc/xm.tmpl
chmod 644 etc/xm-nfs.tmpl
chmod 644 misc/*
mkdir -p /usr/bin
cp bin/xen-create-image /usr/bin
cp bin/xen-create-nfs /usr/bin
cp bin/xt-customize-image /usr/bin
cp bin/xt-install-image /usr/bin
cp bin/xt-create-xen-config /usr/bin
cp bin/xen-delete-image /usr/bin
cp bin/xen-list-images /usr/bin
cp bin/xen-update-image /usr/bin
cp bin/xt-guess-suite-and-mirror /usr/bin
chmod 755 /usr/bin/xen-create-image
chmod 755 /usr/bin/xen-create-nfs
chmod 755 /usr/bin/xt-customize-image
chmod 755 /usr/bin/xt-install-image
chmod 755 /usr/bin/xt-create-xen-config
chmod 755 /usr/bin/xen-delete-image
chmod 755 /usr/bin/xen-list-images
chmod 755 /usr/bin/xen-update-image
chmod 755 /usr/bin/xt-guess-suite-and-mirror
mkdir -p /etc/xen-tools
if [ -d /etc/xen-tools/hook.d ]; then mv /etc/xen-tools/hook.d/ /etc/xen-tools/hook.d.obsolete ; fi
mkdir -p /etc/xen-tools/skel/
mkdir -p /etc/xen-tools/role.d/
mkdir -p /etc/xen-tools/partitions.d/
cp etc/*.conf /etc/xen-tools/
cp etc/xm.tmpl /etc/xen-tools/
cp etc/xm-nfs.tmpl /etc/xen-tools/
cp partitions/*-* /etc/xen-tools/partitions.d/
mkdir -p /usr/share/bash-completion/completions/
cp misc/xen-tools.bash-completion /usr/share/bash-completion/completions/xen-tools
mkdir -p /etc/initramfs-tools/conf.d/
cp misc/xen-tools.initramfs-tools /etc/initramfs-tools/conf.d/xen-tools
for i in roles/* ; do if [ -f $i ]; then cp $i /etc/xen-tools/role.d; fi ; done
for i in /usr/share/xen-tools/*.d; do if [ -L "$i" ]; then rm -vf "$i"; fi; done
mkdir -p /usr/share/xen-tools/centos-4.d/
mkdir -p /usr/share/xen-tools/centos-5.d/
mkdir -p /usr/share/xen-tools/centos-6.d/
mkdir -p /usr/share/xen-tools/fedora-core-6.d/
cp -R hooks/centos-4/*-* /usr/share/xen-tools/centos-4.d
cp -R hooks/centos-5/*-* /usr/share/xen-tools/centos-5.d
cp -R hooks/centos-6/*-* /usr/share/xen-tools/centos-6.d
cp -R hooks/fedora-core-6/*-* /usr/share/xen-tools/fedora-core-6.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-4.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-5.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-7.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-8.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-9.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-10.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-11.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-12.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-13.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-14.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-15.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-16.d
cd /usr/share/xen-tools/ && ln -s fedora-core-6.d fedora-core-17.d
mkdir -p /usr/share/xen-tools/debian.d/
cp -R hooks/debian/*-* /usr/share/xen-tools/debian.d
cd /usr/share/xen-tools/ && ln -s debian.d sarge.d
cd /usr/share/xen-tools/ && ln -s debian.d etch.d
cd /usr/share/xen-tools/ && ln -s debian.d lenny.d
cd /usr/share/xen-tools/ && ln -s debian.d squeeze.d
cd /usr/share/xen-tools/ && ln -s debian.d wheezy.d
cd /usr/share/xen-tools/ && ln -s debian.d jessie.d
cd /usr/share/xen-tools/ && ln -s debian.d stretch.d
cd /usr/share/xen-tools/ && ln -s debian.d sid.d
cd /usr/share/xen-tools/ && ln -s debian.d unstable.d
cd /usr/share/xen-tools/ && ln -s debian.d testing.d
cd /usr/share/xen-tools/ && ln -s debian.d stable.d
cd /usr/share/xen-tools/ && ln -s debian.d oldstable.d
cd /usr/share/xen-tools/ && ln -s debian.d oldoldstable.d
mkdir -p /usr/share/xen-tools/gentoo.d/
cp -R hooks/gentoo/*-* /usr/share/xen-tools/gentoo.d
mkdir -p /usr/share/xen-tools/dapper.d/
cp -R hooks/dapper/*-* /usr/share/xen-tools/dapper.d/
mkdir -p /usr/share/xen-tools/edgy.d/
cp -R hooks/edgy/*-* /usr/share/xen-tools/edgy.d/
cd /usr/share/xen-tools/ && ln -s edgy.d feisty.d
cd /usr/share/xen-tools/ && ln -s edgy.d gutsy.d
cd /usr/share/xen-tools/ && ln -s edgy.d hardy.d
mkdir -p /usr/share/xen-tools/intrepid.d/
cp -R hooks/intrepid/*-* /usr/share/xen-tools/intrepid.d/
cd /usr/share/xen-tools/ && ln -s intrepid.d jaunty.d
mkdir -p /usr/share/xen-tools/karmic.d/
cp -R hooks/karmic/*-* /usr/share/xen-tools/karmic.d/
cd /usr/share/xen-tools/ && ln -s karmic.d lucid.d
cd /usr/share/xen-tools/ && ln -s karmic.d maverick.d
cd /usr/share/xen-tools/ && ln -s karmic.d natty.d
cd /usr/share/xen-tools/ && ln -s karmic.d oneiric.d
cd /usr/share/xen-tools/ && ln -s karmic.d precise.d
cd /usr/share/xen-tools/ && ln -s karmic.d quantal.d
cd /usr/share/xen-tools/ && ln -s karmic.d raring.d
cd /usr/share/xen-tools/ && ln -s karmic.d saucy.d
cd /usr/share/xen-tools/ && ln -s karmic.d trusty.d
cd /usr/share/xen-tools/ && ln -s karmic.d utopic.d
cd /usr/share/xen-tools/ && ln -s karmic.d vivid.d
cd /usr/share/xen-tools/ && ln -s karmic.d wily.d
cd /usr/share/xen-tools/ && ln -s karmic.d xenial.d
cp hooks/common.sh /usr/share/xen-tools
cp -r hooks/common /usr/share/xen-tools
mkdir -p /usr/share/perl5/Xen/Tools
cp ./lib/Xen/Tools/*.pm /usr/share/perl5/Xen/Tools
mkdir -p man
cd bin; for i in *-*[!y]; do pod2man --release=4.6.2 --official --section=8 $i ../man/$i.8; done
for i in man/*.8; do gzip --force -9 $i; done
mkdir -p /usr/share/man/man8/
cp man/*.8.gz /usr/share/man/man8/
OK, “Xen-Tools” installed successfully and you can use it.
For install under Debian Dom0 just run :
# apt-get install xen-tools
Let me to run “xen-create-image” and see what happen :
[root@localhost ~]# xen-create-image
Can't locate Env.pm in @INC (you may need to install the Env module) (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/xen-create-image line 802.
BEGIN failed--compilation aborted at /usr/bin/xen-create-image line 802.
OK, It is an error and can solved via install “perl-Env” package :
# yum install perl-Env
If after install “perl-Env” you got other errors then :
1- yum install cpan
2- Then use “cpan” with the package that you see. For example :
[root@localhost ~]# xen-create-image
Can't locate Term/UI.pm in @INC (you may need to install the Term::UI module) (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/xen-create-image line 814.
BEGIN failed--compilation aborted at /usr/bin/xen-create-image line 814.
As you see, I got an error about a package with the name “Term::UI module” then I use below command for install it :
# cpan Term::UI module
You may need to doing it for some packages but after it you are ready for work with “Xen-tools”.
OK, I consider you installed all modules and “Xen-tools” is ready for work.
I run “xen-create-image” :
[root@localhost ~]# xen-create-image
Couldn't find a useful entry in the sources.list files of the Dom0. Tried:
/etc/apt/sources.list
The '--hostname' argument is mandatory.
[root@localhost ~]#
It is a tool for automate creating. The syntax is straightforward and you can see more options just with “--help” option.
[root@localhost ~]# xen-create-image --size=4Gb --swap=512Mb --ip=172.30.9.21 --netmask=255.255.255.0 --gateway=172.30.9.254 --dir=/home/mohsen --hostname=linux.via.xentools –dist=jessie --kernel /boot/vmlinuz-4.2.3-300.fc23.x86_64 --initrd /boot/initrd-plymouth.img --role gdm
[root@localhost mohsen]# xen-create-image --size=4Gb --swap=512Mb --ip=172.30.9.21 --netmask=255.255.255.0 --gateway=172.30.9.254 --dir=/home/mohsen --hostname=linux.via.xentools --dist=jessie --kernel /boot/vmlinuz-4.2.3-300.fc23.x86_64 --initrd /boot/initrd-plymouth.img --role gdm
Couldn't find a useful entry in the sources.list files of the Dom0. Tried:
/etc/apt/sources.list
General Information
--------------------
Hostname : linux.via.xentools
Distribution : jessie
Mirror : http://httpredir.debian.org/debian
Partitions : swap 512Mb (swap)
/ 4Gb (ext3)
Image type : sparse
Memory size : 128M
Kernel path : /boot/vmlinuz-4.4.7-300.fc23.x86_64
Initrd path : /boot/initrd.img-4.4.7-300.fc23.x86_64
Networking Information
----------------------
IP Address 1 : 172.30.9.21 [MAC: 00:16:3E:52:EF:69]
Netmask : 255.255.255.0
Gateway : 172.30.9.254
Creating partition image: /home/mohsen/domains/linux.via.xentools/swap.img
Done
Creating swap on /home/mohsen/domains/linux.via.xentools/swap.img
Done
Creating partition image: /home/mohsen/domains/linux.via.xentools/disk.img
Done
Creating ext3 filesystem on /home/mohsen/domains/linux.via.xentools/disk.img
Done
Installation method: debootstrap
Running hooks
Done
No role scripts were specified. Skipping
Creating Xen configuration file
Done
No role scripts were specified. Skipping
Setting up root password
Generating a password for the new guest.
All done
Logfile produced at:
/var/log/xen-tools/linux.via.xentools.log
Installation Summary
---------------------
Hostname : linux.via.xentools
Distribution : jessie
MAC Address : 00:16:3E:52:EF:69
IP Address(es) : 172.30.9.21
SSH Fingerprint : SHA256:oRLrvjgRgIcJkVeuQKJRQEKM656GVD9t4EQOmgrAr0k (DSA)
SSH Fingerprint : SHA256:vynLLpUihRZ14l7zExPh8CDcGg/PgVjCs2RVe9QbD64 (RSA)
Root Password : z8gCMQGvyumHiQFSjWV9iXd
Everything is Done and if you look at “/etc/xen/” then you see a config file with the name of “--hostname” opting. For example, our “--hostname” was “ linux.via.xentools” and a file created with “ linux.via.xentools” under “/etc/xen”. Just run :
# xl create /etc/xen/linux.via.xentools.cfg
You can change “dist” parameter and for Linux Distros that supported, Please look at “/usr/share/xen-tools/”.
[root@localhost boot]# xl create /etc/xen/linux.via.xentools.cfg
Parsing config from /etc/xen/linux.via.xentools.cfg
libxl: error: libxl_exec.c:118:libxl_report_child_exitstatus: /etc/xen/scripts/vif-bridge online [-1] exited with error status 1
libxl: error: libxl_device.c:1078:device_hotplug_child_death_cb: script: Could not find bridge device xenbr0
libxl: error: libxl_create.c:1362:domcreate_attach_vtpms: unable to add nic devices
libxl: error: libxl_exec.c:118:libxl_report_child_exitstatus: /etc/xen/scripts/vif-bridge offline [-1] exited with error status 1
libxl: error: libxl_device.c:1078:device_hotplug_child_death_cb: script: Could not find bridge device xenbr0
libxl: info: libxl.c:1698:devices_destroy_cb: forked pid 24683 for destroy of domain 15
What happened? As you see, By default it need a Bridge with the name “xenbr0” but we defined our bridges as “br0” thus we must define it in our config file.
Open your config file that generated by “Xen-Tools” via an editor like “nano” and add below lines to it :
interface=['br0']
vif = [ 'ip=172.30.9.21 ,mac=00:16:3E:52:EF:69, bridge=br0' ]
vnc="1"
vnclisten="172.30.9.20"
I bold the parts that you must adding to your config file.
For More information about the Xen-Tools you can read the man page of it via below link :
http://man.cx/xen-create-image
Monitoring Xen project
We want talk to you about monitoring the Xen Project. I bet it is so important and you can see what happened on your Xen host. Fortunately, The Xen Project and Linux provide some tools for it and we will show you something about it.
A default tools that provided by Xen project is “XenMon”. It is a useful tool that you can use it for monitoring the performance of Xen Dom0 and finding which domains use highest I/O or processing. You can start this tool via XenMon.py command :
As you see, The XenMon provide two columns. The left hand show you statistics that captured over the preceding 10 seconds and on the right hand you can see the data for the last 1 second.
The first row (Gotten) is the amount of time that each domain spent as executing. The second row (Blocked) show the statistics for idle time and the finally row (Waited) show you the amount of time the domain has been in a wait state.
As you on the top of console, By default the XenMon display information for CPU 0and if you have more than one Physical CPU the you can use “p” and “n” keys for move forward to other CPUs. For quit from the XenMon Environment you can use “q” key and XenMon show you the summary of data like below :
[root@localhost ~]# xenmon.py
ms_per_sample = 100
Initialized with 8 cpu's
CPU Frequency = 3392.37
Event counts:
104856299Other
00000000Add Domain
00000000Remove Domain
00002256Sleep
03238839Wake
01477357Block
06431042Switch
00000000Timer Func
06431042Switch Prev
06431042Switch Next
00000000Page Map
00000000Page Unmap
00000000Page Transfer
processed 128867877 total records in 761 seconds (169340 per second)
woke up 22863 times in 761 seconds (30 per second)
[root@localhost ~]#
We recommended to look at “xenmon.py --help” command and you can see a bunch of useful parameters. For example, You just like to see the information about the first CPU then you can use “xenmon.py --cpu=1” or you just like to see the information about I/O count for each domain then you can use “xenmon.py –iocount”. The XenMon can accept The combination of parameters :
# xenmon.py --iocount --cpu=1 --allocated
Another useful tool is “xentop”. You as a Linux administrator are familiar with “top” command in Linux Environment.
# xentop
The “Xentop” command is “top” command that dedicated for the Xen Project. When you run this command, You can see a bunch of useful features at the end of window. For example, Press “n” key for display information about the networks and packets that are sent and received.
I will show you other useful tools that some of them are installed by default and some of them must be installed by yourself.
1- VmStat
It is a useful command that display statistics of virtual memory, kernerl threads, disks, system processes, I/O blocks, interrupts, CPU activity and...
[root@localhost ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 2 60 130248 36912 3107996 0 0 7 7 20 9 0 0 100 0 0
2- Tcpdump
It is good tool for network packet analyzer or packets sniffer and I bet if you work with it then it become a popular tool in your list. It provide good options for capture packets in a file.
# tcpdump -i enp7s0
05:24:08.332952 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [P.], seq 1997228:1997456, ack 417, win 309, options [nop,nop,TS val 187046219 ecr 3827389], length 228
05:24:08.333148 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [P.], seq 1997456:1997684, ack 417, win 309, options [nop,nop,TS val 187046219 ecr 3827389], length 228
05:24:08.333345 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [P.], seq 1997684:1997912, ack 417, win 309, options [nop,nop,TS val 187046220 ecr 3827389], length 228
05:24:08.333542 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [P.], seq 1997912:1998140, ack 417, win 309, options [nop,nop,TS val 187046220 ecr 3827389], length 228
05:24:08.333738 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [P.], seq 1998140:1998368, ack 417, win 309, options [nop,nop,TS val 187046220 ecr 3827389], length 228
05:24:08.333895 IP 172.30.10.18.54923 > localhost.localdomain.ssh: Flags [.], ack 1998368, win 1995, options [nop,nop,TS val 3827394 ecr 187046201], length 0
05:24:08.333949 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [P.], seq 1998368:1998596, ack 417, win 309, options [nop,nop,TS val 187046220 ecr 3827394], length 228
^C05:24:08.334048 IP localhost.localdomain.ssh > 172.30.10.18.54923: Flags [.], ack 521, win 309, options [nop,nop,TS val 187046220 ecr 3827394], length 0
9184 packets captured
9194 packets received by filter
7 packets dropped by kernel
3- Htop
It is a useful tool for monitor Linux process in real time. It is not installed by default and you can install it via “yum” or “dnf”.
# htop
4- IPTraf
It is a Real Time IP LAN Monitoring and can help you for collect useful information across the network. It is not installed by default and you must install it via “yum” or “dnf”.
# iptraf-ng
5- Nmon
Nmon or Nigel’s performance Monitor is a tool that used to monitor all Linux resources like CPU, Memory, Network and… It is not installed by default and you must use “yum” or “dnf” for install it.
XCP / XenServer
XCP means “Xen Cloud Platform” which has been replaced by XenServer from Xenserver.org. It is a Virtualization solution that provide out-of-the box virtualization and cloud computing. The XCP provide some features as below :
- Provide a way to manage Pools.
- Support of advantage storage.
- Support of SLA and mutli-tenancy.
The XCP has two flavors : 1) XCP ISO 2) XCP-XAPI Packages. The first one is like XenServer and come with same drivers and the second one is a package that exist in some Linux Distros. A good comparison exist at http://wiki.xenproject.org/wiki/Xen_/_XCP_/_XCP_on_Linux_Overview .
Unfortunately, “XAPI” not exist for Fedora anymore and if you look at https://fedoraproject.org/wiki/Features/XAPI then you see “Targeted release: Fedora 17”, But don't worry we use a trick for install it.
Download “XenServer” .iso file from “http://xenserver.org/open-source-virtualization-download.html” and move it to your Xen Project Server. You can use “scp” command for do it. For example :
# scp XenServer-7.0.0-main.iso root@172.30.9.20:/home/mohsen/
After it, We must mount this .iso file thus create a directory with the name “cd” under “/mnt/” directory :
# mkdir
cd /mnt
Then use below command to mount .iso file to “/mnt/cd” directory :
# mount -o loop /home/mohsen/XenServer-7.0.0-main.iso /mnt/cd
Then change to “/mnt/cd” directory via “cd” command :
[root@localhost ~]# cd /mnt/cd/
[root@localhost cd]# ls
boot client_install EFI EULA install.img LICENSES Packages Read_Me_First.html repodata
You can see a directory with the name “ Packages” and move to it via “cd” command. After it you can find “XAPI” package here :
[root@localhost Packages]# ls xapi-xe*
xapi-xe-1.9.57-8535.x86_64.rpm
Then use “rpm” command to install it as below :
[root@localhost Packages]# rpm -Uvh xapi-xe-1.9.57-8535.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:xapi-xe-1.9.57-8535 ################################# [100%]
After it you can see that we installed “xe” successfully and can use it :
[root@localhost Packages]# xe
Usage: xe <cmd> [-s server] [-p port] ([-u username] [-pw password] or [-pwf <password file>]) <other arguments>
A full list of commands can be obtained by running
xe help -s <server> -p <port>
You may ask why we did it, We did it because you can control a Citrix XenServer host from your Xen Project or your Linux client. We will tell something about Citrix XenServer in the next chapter.
OK, This chapter Finished and you can test your knowledge after it. At the end of this chapter I just want to show you something that maybe useful : 1-For more information about DomU and Virt-Manager Please look at “http://wiki.xenproject.org/wiki/DomU_Install_with_Virt-Manager”.
2- For learn “virsh” command and use it via Xen Project Please look at http://wiki.xenproject.org/wiki/Virsh_Commands .
3- For more information about VGA Passthrough Please look at : http://wiki.xen.org/wiki/Xen_VGA_Passthrough , http://wiki.xen.org/wiki/VTdHowTo , http://wiki.xen.org/wiki/XenGT , https://blog.xenproject.org/2014/03/11/xen-graphics-virtualization-xengt/ and http://events.linuxfoundation.org/sites/events/files/slides/XenGT-LinuxCollaborationSummit-final_1.pdf