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/grubGRUB_DEFAULT='Debian GNU/Linux, with Xen hypervisor'
update grub
# update-grub
Restart to activate Xen kernel, check your Xen
# xl listName 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 eth0iface eth0 inet manualauto br0iface br0 inet staticaddress [ipv4]netmask [netmask]network [network]broadcast [broadcast]gateway [ipv4gateway]bridge_ports eth0bridge_stp onbridge_maxwait 0iface br0 inet6 staticaddress [ipv6]netmask 64dns-nameservers [dnsipv6]gateway [ipv6gatewaay]autoconf 0# internalauto virbr0iface virbr0 inet staticaddress 192.168.0.1netmask 255.255.255.0broadcast 192.168.0.255pre-up brctl addbr virbr0
edit /etc/sysctl.conf.d
net.ipv4.ip_forward = 1net.ipv6.conf.all.forwarding=1net.bridge.bridge-nf-call-ip6tables = 0net.bridge.bridge-nf-call-iptables = 0net.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 = debootstrapdebootstrap-cmd = /usr/sbin/debootstrapsize = 4G # Root disk, suffix (G, M, k) requiredmemory = 512M # Suffix (G, M, k) requiredswap = 1G # Suffix (G, M, k) requiredfs = ext3 # Default file system for any diskdist = `jessie` # Default distribution is determined by Dom0's distributionimage = 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.255dhcp=1bridge = virbr0genpass_len = 8passwd = 1kernel = /boot/vmlinuz-`uname -r`initrd = /boot/initrd.img-`uname -r`pygrub = 1mirror = http://debian.mirrors.ovh.net/debianmirror_jessie = http://http.debian.net/debianext4_options = noatime,nodiratime,errors=remount-roext3_options = noatime,nodiratime,errors=remount-roext2_options = noatime,nodiratime,errors=remount-roxfs_options = defaultsreiserfs_options = defaultsbtrfs_options = defaultsoutput = /etc/xenextension = .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=jessieCan'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 : testaDistribution : jessieMirror : http://debian.mirrors.ovh.net/debianPartitions : swap 1024Mb (swap)/ 10Gb (ext3)Image type : sparseMemory size : 512MbBootloader : pygrubNetworking Information----------------------IP Address : DHCP [MAC: 00:16:3E:73:6F:31]Creating partition image: /home/vmimages/domains/testa/swap.imgDoneCreating swap on /home/vmimages/domains/testa/swap.imgDoneCreating partition image: /home/vmimages/domains/testa/disk.imgDoneCreating ext3 filesystem on /home/vmimages/domains/testa/disk.imgDoneInstallation method: debootstrapDoneRunning hooksDoneNo role scripts were specified. SkippingCreating Xen configuration fileDoneNo role scripts were specified. SkippingSetting up root passwordEnter new UNIX password:Retype new UNIX password:passwd: password updated successfullyAll doneLogfile produced at:/var/log/xen-tools/testa.logInstallation Summary---------------------Hostname : testaDistribution : jessieMAC Address : 00:16:3E:73:6F:31IP Address(es) : dynamicRSA Fingerprint : 21:12:bb:c3:ad:34:67:6f:cd:a1:7d:d3:ef:ae:95:f5Root 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 --versiongrub-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.36root (hd0,0)kernel /xen-4.0.gz dom0_mem=1024M,max:4048M loglvl=all guest_loglvl=allmodule /vmlinuz-2.6.32.36 ro root=/dev/sda2 console=hvc0 earlyprintk=xen nomodesetmodule /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.rawFormatting '/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.imgCommand (m for help): nPartition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions)Select (default p): pPartition number (1-4, default 1): 1First 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): aSelected partition 1The bootable flag on partition 1 is enabled now.Command (m for help): pDisk /home/vmimages/domains/test/disk.img: 4 GiB, 4294967296 bytes, 8388608 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x05824062Device Boot Start End Sectors Size Id Type/home/vmimages/domains/test/disk.img1 * 2048 8388607 8386560 4G 83 LinuxCommand (m for help): wThe 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:
- https://wiki.debian.org/Xen
- http://wiki.xen.org/wiki/Debian_Guest_Installation_Using_Debian_Installer
- http://wiki.xen.org/wiki/PvGrub
- http://xenbits.xen.org/docs/4.4-testing/man/xl.cfg.5.html
- https://wiki.debian.org/Xen
- https://wiki.debian.org/PvGrub
- http://ftp.debian.org/debian/dists/stable/main/installer-amd64/current/images/cdrom/xen/debian.cfg
- http://wiki.xenproject.org/wiki/Xen_Project_Beginners_Guide
- http://wiki.xenproject.org/wiki/Xen_Common_Problems
- http://wiki.xenproject.org/wiki/Tuning_Xen_for_Performance
- http://wiki.xenproject.org/wiki/Xen_Project_4.4_Man_Pages
- https://help.ubuntu.com/community/Xen
- https://tipstricks.itmatrix.eu/category/linux-tips/xen-tips/
- https://screenzone.eu/howto-xen-4-4-debian-hetzner/
- http://www.virtuatopia.com/index.php/Building_a_Xen_Virtual_Guest_Filesystem_on_a_Disk_Image_(Cloning_Host_System)
- http://backdrift.org/xen-pvgrub-howto
- http://xmodulo.com/install-xen-hypervisor.html
- http://wiki.libvirt.org/page/Networking
- https://jamielinux.com/docs/libvirt-networking-handbook/bridged-network.html
- http://man.cx/xen-create-image
- https://wiki.archlinux.org/index.php/xen
- https://blog.filippo.io/converting-a-partition-image-to-a-bootable-disk-image/
- http://askubuntu.com/questions/196444/how-do-i-auto-start-xen-guests-on-boot
- https://andreas.scherbaum.la/blog/archives/871-Autostart-XEN-Domains.html
- http://www.systutorials.com/816/port-forwarding-using-iptables/
No comments:
Post a Comment