Skip to content


把xen虚拟机映像文件转化成基于lvm分区的

把xen虚拟机映像文件转化成基于lvm分区的

1.查看当前有两块硬盘
fdisk -l

Disk /dev/sda: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 25 200781 83 Linux
/dev/sda2 26 17849 143171280 8e Linux LVM

Disk /dev/sdb: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 17849 143372061 8e Linux LVM

2.查看lmv组
vgscan

Reading all physical volumes. This may take a while…
Found volume group “VolGroup00” using metadata type lvm2

只有一个组

3.查看lv分区
lvscan

ACTIVE ‘/dev/VolGroup00/LogVol02’ [30.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol03’ [92.53 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [10.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00’ [4.00 GB] inherit

4.查看pv情况
pvscan

PV /dev/sda2 VG VolGroup00 lvm2 [136.53 GB / 0 free]
Total: 1 [136.53 GB] / in use: 1 [136.53 GB] / in no VG: 0 [0 ]

硬盘/dev/sda2 分配给VolGroup00组,第二块硬盘还未分配

5.给第二块硬盘创建pv
pvcreate /dev/sdb1

Physical volume “/dev/sdb1” successfully created

6.再次查看pv,已分配
pvscan

PV /dev/sda2 VG VolGroup00 lvm2 [136.53 GB / 0 free]
PV /dev/sdb1 lvm2 [136.73 GB]
Total: 2 [273.26 GB] / in use: 1 [136.53 GB] / in no VG: 1 [136.73 GB]

7.创建新卷组
vgcreate VolGroup01 /dev/sdb1

Volume group “VolGroup01” successfully created

8.激活新卷组
vgchange -a y VolGroup01

0 logical volume(s) in volume group “VolGroup01” now active

9.查看vm1虚拟机映像文件大小
ls -lh /opt/vm1/vm1.img

-rw-r–r– 1 root root 15G Apr 12 15:32 /opt/vm1/vm1.img

10.创建一个和vm1虚拟机映像文件一样大小的lvm分区
lvcreate -L15G -n Vol01 VolGroup01

Logical volume “Vol01” created

11.关闭虚拟机
xm shutdown vm1

12.复制虚拟机映像文件和配置文件
dd if=/opt/vm1/vm1.img of=/dev/VolGroup01/Vol01

30722048+0 records in
30722048+0 records out
15729688576 bytes (16 GB) copied, 646.077 seconds, 24.3 MB/s

\cp /etc/xen/vm1 /etc/xen/vm3

13.修改虚拟机配置文件
vi /etc/xen/vm3


name = “vm1”
uuid = “85386e79-9f79-e243-9b62-3c9da736ae9f”
maxmem = 2048
memory = 2048
vcpus = 2
bootloader = “/usr/bin/pygrub”
on_poweroff = “destroy”
on_reboot = “restart”
on_crash = “restart”
disk = [ “tap:aio:/opt/vm1/vm1.img,xvda,w” ]
vif = [ “mac=00:16:36:47:a2:89,bridge=virbr0,script=vif-bridge” ]

修改name,uuid,mac保持唯一,通常将最后一位加1,修改disk路径.
修改后的配置

name = “vm3”
uuid = “85386e79-9f79-e243-9b62-3c9da736ae1f”
maxmem = 2048
memory = 2048
vcpus = 2
bootloader = “/usr/bin/pygrub”
on_poweroff = “destroy”
on_reboot = “restart”
on_crash = “restart”
disk = [ “phy:/dev/VolGroup01/Vol01,xvda,w” ]
vif = [ “mac=00:16:36:47:a2:81,bridge=virbr0,script=vif-bridge” ]

14.启动并进入虚拟机
vm create vm3 -c

15.修改主机名
15.1 hostname vm3

15.2 vi /etc/sysconfig/network

15.3 vi /etc/hosts

16.修改网卡ip和mac,和xen配置对应
vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.122.255
HWADDR=00:16:36:47:A2:81
IPADDR=192.168.122.13
NETMASK=255.255.255.0
NETWORK=192.168.122.0
ONBOOT=yes

17.重启网络,完成修改
/etc/init.d/network restart

18.测试
18.1 ctrl+]退出虚拟机
18.2启动”vm1″虚拟机
18.3测试vm1网络
ping 192.168.122.11
18.4测试vm3网络
ping 192.168.122.13
18.5测试虚拟机之间的网络
vm console vm3
ping 192.168.122.11

ps:随宿主启动及服务相关iptables不要忘记

参考:http://blog.c1gstudio.com/archives/1215

Posted in 技术, 虚拟化/xen.

Tagged with , .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.