HowTo: Mounting Partitions within a XEN domU disk Image

If you have been using virtualisation technologies such as XEN, you would of come across the usage of disk images to store our operating system and data on.  To create one of these we run:

dd if=/dev/zero of=mydisk.img bs=1024k count=0 seek=5000

This would have created for me an image which is approximately 5GB in size. And then I format it to an ext3 file-system with:

This command will of create a new disk image, 5GB in size called mydisk.img.  This is where Debian will be installed but firstly we need to give the new disk a file sytem type, so we run:

mkfs.ext4 mydisk.img

The problems people may face is when the disk image isn’t formatted by dom0, but instead are formatted by an installer and creates more that one partition inside the image.

usually we can mount the newly formatted disk like this:

mount -o loop mydisk.img /mnt/disk1

But since the image which gets created during the installation process of the VM, has multiple partittions, a simple mount won’t work. This is what happens when we try to mount an image which has multiple partitions:

Since the installer creates multiple partitions inside the image, the normal easy mounting within dom0 will not work:

mount -o loop mydisk.img /mnt/disk1: you must specify the filesystem type

We can examine the file further by executing:

fdisk -lu hda

Which outputs:

You must set cylinders.
You can do this from the extra functions menu.

Disk hda: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot      Start         End      Blocks   Id  System
  centos.img   *     1060290    17848214     8393962+  83  Linux
Partition 1 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1110, 254, 63)
  hda2        17848215    20964824     1558305   83  Linux
Partition 2 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 254, 63) logical=(1111, 0, 1)
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1304, 254, 63)

The ‘u’ flag inside fdisk tells us the partition table sizes in sectors rather than cylinders.  From here we will need this information so we can calculate the correct offset.

Calculate the Offset in “Bytes”

In order to mount the two partitions from the image, we need to know the starting Byte from where each partition starts. To calculate this offset, we mulitply the (start_sector * sector_byte_size). So for the Partition 1 we have (1060290 * 512) = 542868480 and for Partition 2 we have (17848215 * 512) = 9138286080.

Now we can use those “Start Byte” values to mount those partitions in two seperate locations:

mount -o loop,offset=542868480 mydisk.img /mnt/partition1
mount -o loop,offset=9138286080 mydisk.img /mnt/partition2

HowTo: Install XEN Dom0 on Debian Jessie from source

HowTo: Install XEN Dom0 on Debian Jessie from source

xen logo
This article assumes you have a clean install of Debian and have root level access to the server. Note this was performed on a minimal install with no desktop.

Prereqs

To be able to perform a successful kernel build, you will need to install the following packages:

apt-get install bc bcc bin86 gawk bridge-utils iproute libcurl3 libcurl4-openssl-dev bzip2 module-init-tools transfig texinfo texlive-latex-base gcc-multilib texlive-fonts-extra texlive-fonts-recommended pciutils-dev mercurial libncurses5-dev patch libvncserver-dev libsdl-dev gettext libaio1 libaio-dev libssl-dev iasl libbz2-dev git-core uuid-dev python python-dev python-twisted build-essential make gcc libc6-dev zlib1g-dev texlive-latex-recommended e2fslibs-dev libyajl-dev libpixman-1-dev liblzma-dev

XEN Compile and install

Now lets get the XEN tarball and make XEN

cd /usr/src

wget http://bits.xensource.com/oss-xen/release/4.8.0/xen-4.8.0.tar.gz

tar xvf xen-4.8.0.tar.gz

cd xen-4.8.0

./configure –enable-githttp –libdir=/usr/lib

make -j 5 xen

make -j 5 tools

make -j 5 stubdom

cd dist/install

cp -R * /

vi /etc/default/xendomains and comment out #XENDOMAINS_SAVE=/var/lib/xen/save

These tweeks should make XEN start at system startup and also create the bridge we need for the DomUs. Please note that rc3.d could be rc2.d depending on your runlevel, check with the command runlevel at the prompt.

ln -s /etc/init.d/xendomains /etc/rc0.d/S10xendomains

ln -s /etc/init.d/xendomains /etc/rc6.d/S10xendomains

ln -s /etc/init.d/xencommons /etc/rc3.d/S98xencommons

ln -s /etc/init.d/xendomains /etc/rc3.d/S98xendomains

ln -s /root/xendom0caps /etc/rc3.d/S98xendom0caps

ln -s /root/iptables-enable /etc/rc3.d/S99iptables-enable (not required)

vi /root/xendom0caps (add xl sched-credit -d Domain-0 -w 512)

chmod +x /root/xendom0caps

vi /etc/rc.local (add /usr/bin/setterm -powersave off -blank 0

vi /etc/xen/xend-config.sxp

find line (network-script network-bridge) and comment out

#network-script network-bridge

vi /etc/xen/xl.conf (disable autoballon)

autoballon=0

Interfaces

As newer versions of XEN can use the system bridge rather than its own script we need to edit the network configuration to invoke our new bridge. Firstly create our new bridge interface. Replace the IP values with your settings.

vi /etc/network/interfaces

auto xenbr0
iface xenbr0 inet static
bridge_ports eth0
address 172.20.45.1
netmask 255.255.0.0
network 172.20.0.0
broadcast 172.20.255.255
gateway 172.20.0.1

Once done reboot to ensure the config applies successfully.

Kernel Compile

Now XEN is complete we need to get our kernel for Dom0. Replace the kernel number with the version you want to use.

cd /usr/src

(Please note the kernel version maybe significantly newer than detailed here)

wget ftp://ftp.kernel.org/pub/linux/kernel/v4.x/linux-4.2.tar.gz

tar xvf linux-4.2.0.tar.gz

cd linux-4.2.0

make menuconfig

Go into Processor type and features

pic1

Statically enable all XEN features

pic21

Go back to the main menu and enter Device Drivers menu, then enter block devices menu

pic3

Statically enable the 2 XEN options

pic4

Go back to the Device Drivers menu and go down to XEN driver support

pic5

Statically enable all features

pic7

Go back to Device Drivers, go into Network device support and statically enable the 2 XEN options at the bottom

pic8

Exit out and save.

Note to make sure all options are selected run

cat /usr/src/linux-4.2.0/.config | grep XEN

You should see the same as

CONFIG_XEN=y
CONFIG_XEN_DOM0=y
CONFIG_XEN_PRIVILEGED_GUEST=y
CONFIG_XEN_PVHVM=y
CONFIG_XEN_MAX_DOMAIN_MEMORY=500
CONFIG_XEN_SAVE_RESTORE=y
CONFIG_XEN_DEBUG_FS=y
CONFIG_PCI_XEN=y
CONFIG_XEN_PCIDEV_FRONTEND=y
CONFIG_XEN_BLKDEV_FRONTEND=y
CONFIG_XEN_BLKDEV_BACKEND=y
CONFIG_NETXEN_NIC=m
CONFIG_XEN_NETDEV_FRONTEND=y
CONFIG_XEN_NETDEV_BACKEND=y
CONFIG_INPUT_XEN_KBDDEV_FRONTEND=y
CONFIG_HVC_XEN=y
CONFIG_HVC_XEN_FRONTEND=y
# CONFIG_XEN_WDT is not set
CONFIG_XEN_FBDEV_FRONTEND=y
CONFIG_XEN_BALLOON=y
CONFIG_XEN_BALLOON_MEMORY_HOTPLUG=y
CONFIG_XEN_SCRUB_PAGES=y
CONFIG_XEN_DEV_EVTCHN=y
CONFIG_XEN_BACKEND=y
CONFIG_XENFS=y
CONFIG_XEN_COMPAT_XENFS=y
CONFIG_XEN_SYS_HYPERVISOR=y
CONFIG_XEN_XENBUS_FRONTEND=y
CONFIG_XEN_GNTDEV=y
CONFIG_XEN_GRANT_DEV_ALLOC=y
CONFIG_SWIOTLB_XEN=y
CONFIG_XEN_PCIDEV_BACKEND=y
CONFIG_XEN_PRIVCMD=y
CONFIG_XEN_ACPI_PROCESSOR=y

If it looks good then continue otherwise please correct before hand.

make -j 5 bzImage

make -j 5 modules

make -j 5 INSTALL_MOD_STRIP=1 modules_install

cp -a arch/x86/boot/bzImage /boot/vmlinuz-4.2.0

cp -a System.map /boot/System.map-4.2.0

cp -a .config /boot/config-4.2.0

depmod –a

mkinitramfs -o /boot/initrd.img-4.2.0 4.2.0

Modify GRUB2 to boot in XEN mode

cd /etc/grub.d

mv 10_linux 50_linux

vi /etc/default/grub

add these lines under GRUB_CMDLINE_Linux””

GRUB_CMDLINE_XEN_DEFAULT=dom0_mem=1024M,max:1024M
GRUB_CMDLINE_XEN=

save and exit

now issue

update-grub

reboot once complete.

HowTo: Install XEN Dom0 on CentOS 6 from source

HowTo: Install XEN Dom0 on CentOS 6 from source

xen logo

This article assumes you have a clean install of CentOS and have root level access to the server. Note this was performed on a minimal install with no desktop.

Prereqs

To be able to perform a successful kernel build, you will need to install the following packages:

yum groupinstall “Development tools” “Compatibility libraries”

yum install transfig wget texi2html libaio-devel dev86 glibc-devel e2fsprogs-devel gitk mkinitrd iasl xz-devel bzip2-devel pciutils-libs pciutils-devel SDL-devel libX11-devel gtk2-devel bridge-utils PyXML qemu-common qemu-img mercurial texinfo libuuid-devel openssl-devel python-twisted python python-devel ncurses-devel gcc gettext yajl jajl-devel patch yajl yajl-devel lzma-devel

We also need the 32 bit version of glibc-devel.i686

yum install glibc-devel.i686

XEN Compile and install

Now lets get the XEN tarball and make XEN

cd /usr/src

wget http://bits.xensource.com/oss-xen/release/4.8.0/xen-4.8.0.tar.gz

tar xvf xen-4.8.0.tar.gz

cd xen-4.8.0

./configure -enable-githttp -libdir=/usr/lib

make xen

make tools

make stubdom

cd dist/install

cp -R * /

vi /etc/sysconfig/xendomains and comment out #XENDOMAINS_SAVE=/var/lib/xen/save

These tweeks should make XEN start at system startup and also create the bridge we need for the DomUs.

ln -s /etc/init.d/xendomains /etc/rc0.d/S10xendomains

ln -s /etc/init.d/xendomains /etc/rc6.d/S10xendomains

ln -s /etc/init.d/xencommons /etc/rc3.d/S98xencommons

ln -s /etc/init.d/xendomains /etc/rc3.d/S98xendomains

ln -s /root/xendom0caps /etc/rc3.d/S98xendom0caps

ln -s /root/iptables-enable /etc/rc3.d/S99iptables-enable (not required)

vi /root/xendom0caps (add xl sched-credit -d Domain-0 -w 512)

chmod +x /root/xendom0caps

vi /etc/rc.local (add /usr/bin/setterm -powersave off -blank 0)

vi /etc/xen/xl.conf (disable autoballon)

find line (network-script network-bridge) and comment out

#network-script network-bridge

Interfaces

As newer versions of XEN can use the system bridge rather than its own script we need to edit the network configuration to invoke our new bridge. Firstly create our new bridge interface. Replace the IP values with your settings.

cd /etc/sysconfig/network-scripts

vi ifcfg-xenbr0

DEVICE=”xenbr0″
NM_CONTROLLED=”yes”
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=none
IPADDR=172.20.1.73
PREFIX=16
GATEWAY=172.20.0.76
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=”System xenbr0″

Now we need to edit ifcfg-eth0 and comment out the IP address and add the bridge in.

vi ifcfg-eth0

DEVICE=”eth0″
BOOTPROTO=”none”
NM_CONTROLLED=”yes”
ONBOOT=”yes”
#IPADDR=172.20.1.73
#GATEWAY=172.20.0.76
#NETMASK=255.255.0.0
TYPE=”Ethernet”
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=”System eth0″
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
BRIDGE=xenbr0

Once both files are done reboot to ensure they both apply successfully.

Kernel Compile

Now XEN is complete we need to get our kernel for Dom0. Replace the kernel number with the version you want to use.

(Please note the kernel version maybe significantly newer than detailed here)

cd /usr/src

wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.2.tar.gz

tar xvf linux-4.2.tar.gz

cd linux-4.2

make menuconfig

Go into Processor type and features

pic1

Statically enable all XEN features

pic21

Go back to the main menu and enter Device Drivers menu, then enter block devices menu

pic3

Statically enable the 2 XEN options

pic4

Go back to the Device Drivers menu and go down to XEN driver support

pic5

Statically enable all features

pic7

Go back to Device Drivers, go into Network device support and statically enable the 2 XEN options at the bottom

pic8

Exit out and save.

Note to make sure all options are selected run

cat /usr/src/linux-4.2.0/.config | grep XEN

You should see the same as

CONFIG_XEN=y
CONFIG_XEN_DOM0=y
CONFIG_XEN_PRIVILEGED_GUEST=y
CONFIG_XEN_PVHVM=y
CONFIG_XEN_MAX_DOMAIN_MEMORY=500
CONFIG_XEN_SAVE_RESTORE=y
CONFIG_XEN_DEBUG_FS=y
CONFIG_PCI_XEN=y
CONFIG_XEN_PCIDEV_FRONTEND=y
CONFIG_XEN_BLKDEV_FRONTEND=y
CONFIG_XEN_BLKDEV_BACKEND=y
CONFIG_NETXEN_NIC=m
CONFIG_XEN_NETDEV_FRONTEND=y
CONFIG_XEN_NETDEV_BACKEND=y
CONFIG_INPUT_XEN_KBDDEV_FRONTEND=y
CONFIG_HVC_XEN=y
CONFIG_HVC_XEN_FRONTEND=y
# CONFIG_XEN_WDT is not set
CONFIG_XEN_FBDEV_FRONTEND=y
CONFIG_XEN_BALLOON=y
CONFIG_XEN_BALLOON_MEMORY_HOTPLUG=y
CONFIG_XEN_SCRUB_PAGES=y
CONFIG_XEN_DEV_EVTCHN=y
CONFIG_XEN_BACKEND=y
CONFIG_XENFS=y
CONFIG_XEN_COMPAT_XENFS=y
CONFIG_XEN_SYS_HYPERVISOR=y
CONFIG_XEN_XENBUS_FRONTEND=y
CONFIG_XEN_GNTDEV=y
CONFIG_XEN_GRANT_DEV_ALLOC=y
CONFIG_SWIOTLB_XEN=y
CONFIG_XEN_PCIDEV_BACKEND=y
CONFIG_XEN_PRIVCMD=y
CONFIG_XEN_ACPI_PROCESSOR=y

If it looks good then continue otherwise please correct before hand.

make bzImage

make modules

make modules_install

cp -a arch/x86/boot/bzImage /boot/vmlinuz-4.2.0

cp -a System.map /boot/System.map-4.2.0

cp -a .config /boot/config-4.2.0

depmod –a

mkinitrd -o /boot/initrd.img-4.2.0 4.2.0

Modify GRUB to boot in XEN mode

vi /boot/grub/grub.conf

scroll down to the current setting and add the following above your current boot config, you will need to edit the lines to match your root and paths to the files.

title Xen 4.5.1 / Debian GNU/Linux, kernel 4.2.0
root (hd0,0)
kernel /xen.gz
module /vmlinuz-4.2.0 root=root=UUID=03f9e700-ba18-41a1-bbe7-65a372716c73 ro console=tty0
module /initrd.img-4.2.0

save and reboot.

If your system fails to boot then you can boot back into the normal kernel and correct any issues.

Once the system is booted run this as a simple test to make sure XEN is running

xl list