Ubuntu上Xen安装虚拟机

     2014年03月18日       磊磊syh       运维笔记->xen虚拟化       xen domain0 domainU 

方法一

dd一个空的磁盘

sudo dd if=/dev/zero of=/home/vm1.img bs=1G count=8

下载Xen VM通用配置文件

sudo wget http://mirrors.aliyun.com/ubuntu/dists/precise/main/installer-amd64/current/images/netboot/xen/xm-debian.cfg \
-O /etc/xen/vm1.conf

对下载下来的配置文件进行相应的修改

memory = 256
name = "vm1"
disk   = [ 'tap2:tapdisk:aio:/home/vm1/vm1.img,xvda1,w']

执行安装命令

sudo xm create -f /etc/xen/vm1.conf  -c install=true \
install-kernel="http://mirrors.aliyun.com/ubuntu/dists/precise/main/installer-amd64/current/images/netboot/xen/vmlinuz" \
install-ramdisk="http://mirrors.aliyun.com/ubuntu/dists/precise/main/installer-amd64/current/images/netboot/xen/initrd.gz" \
install-mirror="http://mirrors.aliyun.com/ubuntu"

方法二

dd一个空的磁盘

sudo dd if=/dev/zero of=/home/vm1.img bs=1G count=8

格式化磁盘

sudo dd if=/dev/zero of=/home/vm1.img bs=1G count=8

挂载磁盘

sudo mkdir /mnt/vm1
sudo mount -o loop /home/vm1.img /mnt/vm1

利用Domain0的文件系统创建DomainU的文件系统

#!/bin/bash
cat > /tmp/exclude.list <<EOF
/proc
/tmp/
/lost+found
/sys
/mnt
/media
/dev
/tmp
/home
/var/cache/apt
/var/cache/apt-xapian-index
/var/lib/apt
EOF

#注意vmdisk变量后面的/
vmdisk='/mnt/'
rsync -arv --progress --exclude-from=/tmp/exclude.list / $vmdisk
mkdir -p ${vmdisk}/home ${vmdisk}/mnt ${vmdisk}/tmp ${vmdisk}/dev ${vmdisk}/proc ${vmdisk}/sys 

卸载虚拟机磁盘

sudo umount  /mnt/vm1

创建虚拟机配置文件
编辑vim /etc/xen/vm1.conf 如下

name       = 'vm1'
vcpus      =  1
memory     = '2048'
disk       = [ 'tap2:tapdisk:aio:/home/vm1/vm1.img,xvda,w']
vif        = [ '' ]
on_reboot  = 'restart'
on_crash   = 'restart'
kernel     = "/home/vm1/vmlinuz"
ramdisk    = "/home/vm1/initrd.img"
extra      = "ks=http://www.opstool.com/files/man/vm-ks.cfg"

启动虚拟机

sudo xm create -c /etc/xen/vm1.conf