Wednesday, February 22, 2012

Disk-based virtual machine how to

Procedure for creating PV machines with Debian Etch amd64.  The procedure is much the same for Lenny, the differences are noted in each step.

Turn on HVM support in BIOS.  Of course, not necessary for PV, but then it’s on if you decide to do some HVM machines later.

The following assumes that you did a standard installation of Debian, using LVM with the guided partitioning scheme. Guided partitioning results in most of the disk space being allocated to the /home directory. If you do manual partitioning from the installer, skip to debootstrap.

Install xen, xen-hypervisor-3.2-1-<arch>.
Edit /boot/grub/menu.lst, adding “console=tty0 and console=hvc0″ to the end of the “module /boot/vmlinuz” line for the xen entries.  Also edit the inittab to have the following entries:

1:2345:respawn:/sbin/getty 38400 hvc0
2:23:respawn:/sbin/getty 38400 tty1 # I just commented out the original entries and changed 1 and 2 to match these.

Resize the /home partition.
umount /dev/debian0/home # substitute debian0 with the volume group name
e2fsck -f /dev/debian0/home
resize2fs /dev/debian0/home 4.5G # dirty way of ensuring you don’t stomp
lvreduce -L 5G /dev/debian0/home # the end of the file system
resize2fs /dev/debian0/home
mount -t ext3 /dev/debian0/home /home

Create a new partition in the freed space.
lvcreate -L 5G -n ns0 debian0 # -n (lv_name) vg_name
lvcreate -L 256M -n ns0-swap debian0
lvscan -v # make sure it’s there
mke2fs /dev/debian0/ns0
tune2fs -j /dev/debian0/ns0
mkswap /dev/debian0/ns0-swap
mount /dev/debian0/ns0 /mnt

Use debootstrap to install a minimal system on the partition. If you have a lot of VMs to make, you could run the following line, then tar up the /mnt directory and untar it in the next partition. Another option is to use the –make-tarball FILE option for debootstrap so you have a tar file of all the .debs locally and then use –unpack-tarball FILE when you do the next VM. Your mirror might thank you.

debootstrap etch /mnt http://debian.osuosl.org/debian # pick a mirror, use lenny instead to get the current stable

On the dom0, make sure (network-script network-bridge) and (vif-script vif-bridge) are uncommented for Lenny.  For Etch, I used (network-script network-dummy).  This gives you simple networking where the physical ethernet device and all of the virtual ethernet devices belonging to the domUs are attached to a virtual bridge once everything below gets configured.

Edit /mnt/etc/network/interfaces.
auto lo
iface lo inet loopback
Edit /mnt/etc/fstab.
proc /proc proc defaults 0 0
/dev/sda1 / ext3 defaults,errors=remount-ro 0 1
/dev/sda2 none swap sw 0 0

Unmount /mnt.
cd /
umount /mnt
Create a config file for the VM (/etc/xen/ns0.cfg).
# -*- mode: python; -*-
kernel = “/boot/vmlinuz-2.6.18-4-xen-amd64″ # put the appropriate kernel entry here
ramdisk = “/boot/initrd.img-2.6.18-4-xen-amd64″
memory = 256
name = “ns0″
vif = [ 'bridge=xenbr0']
disk = ['phy:/dev/debian0/ns0,sda1,w', 'phy:/dev/debian0/ns0-swap,sda2,w']
ip = “192.168.1.10″
netmask = “255.255.255.0″
gateway = “192.168.1.1″
hostname = “ns0″
root = “/dev/sda1 ro”
extra = “console=hvc0 xencons=tty” # console to serial, xencons sends display to vga

Start the domU.
xm create ns0.cfg -c

Log in as root (no password yet!), set a password. base-config is not installed by debootstrap, you have to do the following manually.

Set the timezone.
vi /etc/default/rcS # Set how hardware clock is interpreted, UTC or local
tzconfig

Lenny uses this instead:
dpkg-reconfigure tzdata

Configure networking through the following files (on the domU.)
vi /etc/network/interfaces
auto lo
iface lo inet loopback
# for static ip
auto eth0
iface eth0 inet static
address 192.168.1.10
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1

vi /etc/resolv.conf
search some.com
nameserver 192.168.1.2
nameserver 192.168.1.3

vi /etc/hostname
myhostname

vi /etc/hosts
127.0.0.1 localhost myhostname
# the following is for IPv6 support
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts


Set the hostname.
hostname myhostname

Make sure your networking is configured correctly.
/etc/init.d/networking restart
ping localhost
ping 192.168.1.10
ping 192.168.1.9 # whatever the dom0 is
ping 192.168.1.50 # some host outside of the box

Edit apt sources.
vi /etc/apt/sources.list
deb http://debian.osuosl.org/debian/ etch main
deb-src http://debian.osuosl.org/debian/ etch main
deb http://security.debian.org/ etch/updates main
deb-src http://security.debian.org/ etch/updates main

Run aptitude update.
Install locales and udev (debootstrap doesn’t install either and you’ll get error messages on a lenny system about no /dev/pty, which are probably harmless but annoying.)

aptitude install locales
dpkg-reconfigure locales
# pick en_US UTF-8
Debootstrap installs a minimal system, run tasksel to install a more complete system.
tasksel install standard

To check out configured volume groups:
vgdisplay

Helpful commands for Xen:
^] # CTRL-] To exit the domU console
xm console domUname # To reconnect to a console
shutdown -h now # To shutdown the VM from within the VM.
xm shutdown domUname # To shutdown from dom0.
xm create /path/to/config -c # Start a virtual machine and attach to its console.
xm destroy domUname # Stop a virtual machine dirty.
xm list # List all running VMs.
xm help

References:
Xen
http://wiki.xensource.com/xenwiki/XenFaq
http://wiki.xensource.com/xenwiki/DebianDomU
http://wiki.xensource.com/xenwiki/XenOnUbuntu64
http://www.debian-administration.org/articles/396

Debian
http://www.debian.org/releases/etch/alpha/apds03.html.en#id2549076
http://www.mail-archive.com/debian-alpha@lists.debian.org/msg24209.html
http://wiki.debian.org/Xen

LVM
http://riseuplabs.org/grimoire//storage/lvm2/#reducing_size

No comments:

Post a Comment