在使用Xen的时候将会遇到一个非常实际的问题,就是要为虚拟机添加磁盘。通过Xen建立的磁盘镜像可能会不够用,那么就肯定要在之后调整。其实也非常简单,大致方法就是可以通过dd命令建立一个块设备文件,然后合并到虚拟机里面去。于是,这里又牵扯到了一些Linux的系统知识以及命令。简单交待一下好了:
这里主要使用的命令是dd,用于建立块设备文件。
那什么又是块设备文件呢?
一般来说设备文件有两种,一种是块设备,一种是字符设备。块设备的特点是可以随机读写(Random Access),比如内存、硬盘等。字符设备的特点是顺序读写(Sequential Access),比如鼠标,键盘,麦克风等。
另外这里又要提到一个特殊的字符设备/dev/zero,它是Linux提供的一个特殊的字符设备,它的作用就是提供无限二进制0。经常就是用它作为输入设备,当建立一个块设备文件的时候,就用它来拼命往里面填0,直到把该块设备填到指定的大小,然后一个指定大小的块设备就诞生了= =|||
一.了解dd命令:
这里我只讲一些相关的参数和说明,暂不对dd命令本身做详细解释了。
[root@KCXEN ~]# dd --help
----------------------------------------------------------------
Usage: dd [OPERAND]...
or: dd OPTION
使用方法: dd [运算对象]
或者 : dd 选项
Copy a file, converting and formatting according to the operands.
复制一个文件,并且根据运算对象来转换格式。
if=FILE read from FILE instead of stdin
指定输入文件(路径)。
of=FILE write to FILE instead of stdout
指定输出文件(路径)。
bs=BYTES force ibs=BYTES and obs=BYTES
字节数,包括了输入(读)字节数以及输出(写)字节数。
count=BLOCKS copy only BLOCKS input blocks
只复制输入的blocks块。
cbs=BYTES convert BYTES bytes at a time
一次过程中转化的字节数。
conv=CONVS convert the file as per the comma separated symbol list
指定转化后的类型。
ibs=BYTES read BYTES bytes at a time
一次过程中输入(读)的字节数。
obs=BYTES write BYTES bytes at a time
一次过程中输出(写)的字节数。
BLOCK(块)以及BYTES(字节)可以使用很多的复数单位,如:
“K”表示千
“M”表示兆
“G”表示千兆
更加高的单位还可以继续使用“T”,“P”,“E”,“Z”,“Y”。(貌似对于XEN来讲后面几个都一般用不到的吧= =)
----------------------------------------------------------------
二.建立块设备:
1.建立
我先建立一个6G的块设备文件,名字叫做disk2.img,将作为Xen的磁盘。
[root@KCXEN ~]# dd if=/dev/zero of=/opt/xen/win2003/disk2.img bs=1M count=6000 &
建立完毕后系统会提示出以下的反馈信息来确认。建立块设备文件时间将根据大小而定,像6G这种程度的也需要一段时间,所以一般就放在后台让它运行(参数&表示后台运行)。
----------------------------------------------------------------
6000+0 records in
6000+0 records out
6291456000 bytes (6.3 GB) copied, 200 seconds, 31.5 MB/s
生成的块文件大小为6.3GB,所用时间为200秒,生成速度是平均每秒31.5M字节。
----------------------------------------------------------------
对于这个命令我再作几点补充说明:
(1)if=/dev/zero,这个是输入设备的路径,这个设备的实际工作就是将建立的块设备文件“用0来填满”。
(2)of=/opt/xen/disk2.img,这个是生成块设备文件的目的路径。
(3)bs=1M,表示每一次“填充空间大小”为1M字节。
(4)count=6000,表示生成该块设备文件,要分6000次才能“填满”,每次填1M,那么6000当然就是6G咯= =|||
2.察看权限差别
虽然已经生成好了块设备文件,但是还是需要看一下该文件的权限。
[root@KCXEN ~]# ll /opt/xen/win2003
----------------------------------------------------------------
total 7354604
-rwxr-xr-x 1 root root 4294967297 Oct 26 08:46 disk1.img
这个disk1.img是通过XEN生成的。
-rw-r--r-- 1 root root 6291456000 Oct 26 09:14 disk2.img
这个disk2.img是手动建立的。
-rw-r--r-- 1 root root 641 Oct 26 08:46 win2003
----------------------------------------------------------------
3.同步权限
将新建块设备文件的权限该成和XEN默认建立的镜像文件的权限一致。
[root@KCXEN win2003]# chmod a+x /opt/xen/win2003/disk2.img
4.察看权限
[root@KCXEN win2003]# ll /opt/xen/win2003
----------------------------------------------------------------
total 7354604
-rwxr-xr-x 1 root root 4294967297 Oct 26 08:46 disk1.img
-rwxr-xr-x 1 root root 6291456000 Oct 26 09:14 disk2.img
-rw-r--r-- 1 root root 641 Oct 26 08:46 win2003
----------------------------------------------------------------
三.给Xen虚拟机安装新硬盘:
1.修改Xen虚拟机的配置文件
建立好了新的块设备文件,就可以添加到虚拟机里面做磁盘用,首先就要在虚拟机配置文件当中的disk配置项里添加该文件路径。
[root@KCXEN ~]# vi /etc/xen/win2003
---------------------------------------------------------
# Automatically generated xen config file
import os, re
arch = os.uname()[4]
if re.search('64', arch):
arch_libdir = 'lib64'
else:
arch_libdir = 'lib'
name = "win2003"
builder = "hvm"
memory = "1024"
disk = [ 'file:/opt/xen/win2003/disk1.img,hda,w', 'file:/opt/yum/en2003entsp2.iso,hdb:cdrom,r',
'file:/opt/xen/win2003/disk2.img,hdc,w' ]
注意,添加磁盘要注意以下:
(1)file路径要和实际吻合。
(2)磁盘顺序hda,hdb,hdc等要注意,不应该冲突。
(3)读写属性要设定为“w”,即可写,否则该块文件将不能被虚拟机写入。
vif = [ 'type=ioemu, mac=00:16:3e:2a:ee:46, bridge=xenbr0', ]
uuid = "becb87fa-763e-829a-8204-ee67eee377cd"
device_model = "/usr/lib/xen/bin/qemu-dm"
kernel = "/usr/lib/xen/boot/hvmloader"
vnc=1
vncunused=1
apic=0
acpi=0
pae=1
boot="c"
vcpus=1
serial = "pty" # enable serial console
on_reboot = 'restart'
on_crash = 'restart'
---------------------------------------------------------
2.启动虚拟机测试
[root@KCXEN ~]# xm create win2003
---------------------------------------------------------
Using config file "/etc/xen/win2003".
Started domain win2003
出现启动虚拟域反馈信息的话,表示至少配置文件没有出错。虚拟机已经运行起来了
---------------------------------------------------------
3.察看运行情况
[root@KCXEN ~]# xm list
---------------------------------------------------------
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 2215 4 r----- 41215.9
win2003 32 1038 1 ------ 3.6
rhel5 24 781 1 -b---- 5509.7
---------------------------------------------------------
四.进入Xen虚拟机系统内还需要对该新磁盘进行操作
这里我只说一下Windows下添加新硬盘到系统内的主要步骤
1.Run下diskmgmt.msc进入系统磁盘管理台。
2.找到新磁盘,这个磁盘上面应该会标有Uninitialized“未初始化”。
3.点击该磁盘,在弹出的快捷菜单当中选择Initialize“初始化”,该磁盘就会被初始化。
4.指定该磁盘为动态磁盘还是基本磁盘。
5.为该磁盘分区,并为每个区决定空间大小以及盘符。
6.格式化建立的分区。
7.格式化完毕的分区就能够使用了。
Linux 系统增加swap分区
[root@YJ-FlashMeetingServer ~]# cat /dev/xv
xvc0 xvda xvda1 xvdb
说明有xvdb已找到设备
[root@YJ-FlashMeetingServer ~]# fdisk /dev/xvdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): m
Command action
d delete a partition
l list known partition types
n add a new partition
p print the partition table
t change a partition's system id
v verify the partition table
w write table to disk and exit
选择参数n
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-124, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-124, default 124):
Using default value 124
选择参数p打印分区表查看,已创建分区ID为83,不是swap
Command (m for help): p
Disk /dev/xvdb: 1024 MB, 1024000000 bytes
255 heads, 63 sectors/track, 124 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvdb1 1 124 995998+ 83 Linux
选择t修改分区类型
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/xvdb: 1024 MB, 1024000000 bytes
255 heads, 63 sectors/track, 124 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvdb1 1 124 995998+ 82 Linux swap / Solaris
查看正确,选择w写入
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
不重启情况下更新分区表信息
[root@YJ-FlashMeetingServer ~]# partprobe /dev/xvdb
Swap化分区
[root@YJ-FlashMeetingServer ~]# mkswap /dev/xvdb1
Setting up swapspace version 1, size = 1019895 kB
启用swap分区
[root@YJ-FlashMeetingServer ~]# swapon /dev/xvdb1
编辑文件系统表
[root@YJ-FlashMeetingServer ~]# vi /etc/fstab
LABEL=/ / ext3 defaults 1 1
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/xvdb1 swap swap defaults 0 0
!!慎重编辑此文件,一旦有误重启后就葛屁!
reboot
检查
[root@YJ-FlashMeetingServer ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/xvdb1 partition 995988 0 -1
OK!
联系客服