Monday, May 23, 2016

Xen 4.4: How to install in Debian Jessie

Ideal environment:
  • sda1 - /boot 200MB
  • sda2 - / 15GB
  • sda3 - swap
  • sda4 - reserved for LVM
Check CPU support 
# egrep '(vmx|svm)' /proc/cpuinfo
Install clock synchronize
# apt-get install ntp
Install network bridge utility
# apt-get install bridge-utils
Install Xen 
# apt-get install xen-linux-system
# apt-get install xen-hypervisor-4.4-amd64
# apt-get install xen-tools
# apt-get install qemu-system-x86
Make kernel for Xen active
find ### BEGIN /etc/grub.d/20_linux_xen ### in /boot/grub/grub.cfg
# vi /boot/grub/grub.cfg
### BEGIN /etc/grub.d/20_linux_xen ### 
menuentry 'Debian GNU/Linux, with Xen hypervisor' 
Edit /etc/default/grub
# vi /etc/default/grub
GRUB_DEFAULT='Debian GNU/Linux, with Xen hypervisor'
update grub 
# update-grub
Restart to activate Xen kernel, check your Xen
# xl list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0 32167     8     r-----     110.6
Configure bridge network:
Edit /etc/network/interfaces
# vi /etc/network/interfaces
...
auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
        address [ipv4]
        netmask [netmask]
        network [network]
        broadcast [broadcast]
        gateway [ipv4gateway]
        bridge_ports eth0
        bridge_stp on
        bridge_maxwait 0
iface br0 inet6 static
        address [ipv6]
        netmask 64
        dns-nameservers [dnsipv6]
        gateway [ipv6gatewaay]
        autoconf 0
# internal
auto virbr0
iface virbr0 inet static
        address 192.168.0.1
        netmask 255.255.255.0
        broadcast 192.168.0.255
        pre-up brctl addbr virbr0
edit /etc/sysctl.conf.d
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding=1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0 
Manual adding virbr0
# brctl addbr virbr0
# ifconfig virbr0 192.168.0.1 netmask 255.255.255.0 up  
Install and configure dhcp server for domu
Install isc-dhcp-server 
# apt-get install isc-dhcp-server 
Edit /etc/default/isc-dhcp-server
INTERFACES="virbr0"
Edit /etc/dhcp/dhcpd.conf
subnet 192.168.0.0 netmask 255.255.255.0 {
  range 192.168.0.10 192.168.0.50;
  option routers 192.168.0.1;
  default-lease-time 600;
  max-lease-time 7200;
}
Restart dhcp server
# service isc-dhcp-server restart

Configuring xen-tool to create domu images
edit /etc/xen-tools/xen-tools.conf
#Begin by setting the dir = directive to the directory into which the tools are to create the Xen guest image files:
dir = /home/images
#lvm = myvolgroup -> if using LVM (logical volume management)
install-method = debootstrap
debootstrap-cmd = /usr/sbin/debootstrap
size   = 4G       # Root disk, suffix (G, M, k) required
memory = 512M # Suffix (G, M, k) required
swap   = 1G # Suffix (G, M, k) required
fs     = ext3     # Default file system for any disk
dist   = `jessie` # Default distribution is determined by Dom0's distribution
image  = sparse   # Specify sparse vs. full disk images (file based images only)
# for static
# gateway    = 192.168.1.1
# netmask    = 255.255.255.0
# broadcast  = 192.168.1.255
dhcp=1
bridge = virbr0
genpass_len = 8
passwd = 1
kernel      = /boot/vmlinuz-`uname -r`
initrd      = /boot/initrd.img-`uname -r`
pygrub = 1
mirror = http://debian.mirrors.ovh.net/debian
mirror_jessie = http://http.debian.net/debian
ext4_options     = noatime,nodiratime,errors=remount-ro 
ext3_options     = noatime,nodiratime,errors=remount-ro
ext2_options     = noatime,nodiratime,errors=remount-ro
xfs_options      = defaults
reiserfs_options = defaults
btrfs_options    = defaults
output    = /etc/xen

extension = .cfg
Creating Xen disk image (loopback image) for Xen guest
Create xen guest with hostname testa, disk 10G, swap 1G, ram/memory 512M, dhcp, vcpu 3, OS Debiean jessie:
# xen-create-image --hostname testa --size=10Gb --swap=1024Mb --memory=512Mb --vcpu=3  --dist=jessie
Can't exec "jessie": No such file or directory at /usr/share/perl5/Xen/Tools/Common.pm line 100, <FILE> line 134.
Use of uninitialized value $output in scalar chomp at /usr/share/perl5/Xen/Tools/Common.pm line 101, <FILE> line 134.
Use of uninitialized value $output in concatenation (.) or string at /usr/share/perl5/Xen/Tools/Common.pm line 104, <FILE> line 134.

General Information
--------------------
Hostname       :  testa
Distribution   :  jessie
Mirror         :  http://debian.mirrors.ovh.net/debian
Partitions     :  swap            1024Mb (swap)
                  /               10Gb  (ext3)
Image type     :  sparse
Memory size    :  512Mb
Bootloader     :  pygrub

Networking Information
----------------------
IP Address     : DHCP [MAC: 00:16:3E:73:6F:31]


Creating partition image: /home/vmimages/domains/testa/swap.img
Done

Creating swap on /home/vmimages/domains/testa/swap.img
Done

Creating partition image: /home/vmimages/domains/testa/disk.img
Done

Creating ext3 filesystem on /home/vmimages/domains/testa/disk.img
Done
Installation method: debootstrap
Done

Running hooks
Done

No role scripts were specified.  Skipping

Creating Xen configuration file
Done

No role scripts were specified.  Skipping
Setting up root password
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
All done


Logfile produced at:
         /var/log/xen-tools/testa.log

Installation Summary
---------------------
Hostname        :  testa
Distribution    :  jessie
MAC Address     :  00:16:3E:73:6F:31
IP Address(es)  :  dynamic
RSA Fingerprint :  21:12:bb:c3:ad:34:67:6f:cd:a1:7d:d3:ef:ae:95:f5
Root Password   :  N/A
This process take approximate 14 minutes.
Run xen guest 
To run xen guest 
# xl create /etc/xen/testa.cfg -c
xl command 
To list dom0 and domu
# xl list
To start domu
# xl create /etc/xen/[config].cfg 
To destroy domu
# xl destroy test
To shutdown domu
# xl shutdown test
To show xen information
# xl info
To show cpu use
# xl vcpu-list
Allocate memory and cpu for dom0
To show grub version
 # grub-install --version
grub-install (GRUB) 2.02~beta2-22+deb8u1
Allocate memory for dom0 between 1024M and maximum 4048M  
  • for grub1 (/boot/grub/grub.conf or /boot/grub/menu.lst)
    title Xen 4.1.0 / pv_ops dom0 kernel 2.6.32.36
      root (hd0,0)
      kernel /xen-4.0.gz dom0_mem=1024M,max:4048M loglvl=all guest_loglvl=all
      module /vmlinuz-2.6.32.36 ro root=/dev/sda2 console=hvc0 earlyprintk=xen nomodeset
      module /initrd-2.6.32.36.img
  • for grub2 (/etc/default/grub)
    GRUB_CMDLINE_XEN_DEFAULT="dom0_mem=1024M,max:4048M"
In general you should not assigned less than 4 vCPUs to Dom0
dom0_max_vcpus=4 dom0_vcpus_pin
Routing
Internal to internet
# iptables -t nat -A POSTROUTING -o br0 -j MASQUERADE
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# iptables -A FORWARD -i virbr0 -o br0 -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i virbr0 -o br0 -j ACCEPT  
Internet to internal in port xxx into 192.168.0.100 port yyy
# iptables -A PREROUTING -t nat -i br0 -p tcp --dport xxx -j DNAT --to 192.168.0.100:yyy
# iptables -A FORWARD -p tcp -d 192.168.0.100 --dport yyy -j ACCEPT
Make auto-start domu after boot
Create symlink into folder /etc/xen/auto
# mkdir /etc/xen/auto
# cd /etc/xen/auto/
# ln -s /etc/xen/testa.cfg

 

edit /etc/xen/testa.cfg

# vi /etc/xen/testa.cfg

on_xend_stop='shutdown' 

on_xend_start='start'


Additonal Note 



Manual create disk image and swap image
Create 10 G image disk
# fallocate -l 10G /home/vmimages/test1/disk1.img
or using dd (slower)
# dd if=if=/dev/zero of=/xen/test1/disk1.img bs=1gb count=0 seek=10
or using qemu-img
# qemu-img create -f raw -o size=8G /home/vmimages/test1/disk1.raw
Formatting '/home/vmimages/test1/disk1.raw', fmt=raw size=8589934592
Preparing image in ext4 file system
# mkfs -t ext4 /home/vmimages/test1/disk1.img
Create 1 G image swap
# fallocate -l 1G /home/vmimages/test1/swap.img
Preparing image swap
# mkswap /home/vmimages/test1/swap.img
Create partition on image disk

# fdisk /home/vmimages/domains/test/disk.img
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-8388607, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-8388607, default 8388607):

Created a new partition 1 of type 'Linux' and of size 4 GiB.

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

Command (m for help): p
Disk /home/vmimages/domains/test/disk.img: 4 GiB, 4294967296 bytes, 8388608 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: 0x05824062

Device                                Boot Start     End Sectors Size Id Type
/home/vmimages/domains/test/disk.img1 *     2048 8388607 8386560   4G 83 Linux


Command (m for help): w
The partition table has been altered.
Syncing disks.
# file /home/vmimages/domains/test/disk.img

/home/vmimages/domains/test/disk.img: DOS/MBR boot sector; partition 1 : ID=0x83, active, start-CHS (0x0,32,33), end-CHS (0x2a,42,32), startsector 2048, 8386560 sectors


References: