一、KVM创建虚拟机
创建磁盘文件及虚拟机镜像
[root@ckcest04 images]# ll /var/lib/libvirt/images/ #kvm存放虚拟机的路径
[root@kvmaster2 images]# qemu-img create ckcest07.img 30G
Formatting 'ckcest07.img', fmt=raw size=32212254729
[root@kvmaster2 images]# virt-install --name=ckcest07 --ram 2048 --vcpus=2 --disk path=/var/lib/libvirt/images/ckcest07.img,size=150 \
--cdrom /opt/Centos-6.4-x86_64-bin-DVD1.iso --network bridge=br0 --force --autostart
#virt-install 安装虚拟机命令,更多请--help
#--name 虚拟机名字
#--ram 内存
#--vcpus cpu核数
#--disk path 虚拟机路径,大小
#--cdrom iso路径
#--network bridge 桥接到网桥的名字,教程一里的网桥名
#--autostart 自动开启
#然后就是图形界面的安装操作系统步骤,这里不再赘述。
配置操作系统
[root@ckcest07 opt]# sed -i '/^HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0[root@ckcest07 opt]# sed -i '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0[root@ckcest07 opt]# echo -n > /etc/udev/rules.d/70-persistent-net.rules#上面三条命令是为了用此系统做镜像,再建立虚拟机的时候,可以获得新的mac地址,否则到时候还要重新配置
[root@kvmaster2 images]# virsh list #虚拟机运行正常
Id 名称 状态
----------------------------------------------------
1 ckcest07 running
将raw格式转换成qcow2格式
[root@kvmaster2 images]# qemu-img info ckcest07.img
image: ckcest07.img
file format: raw
virtual size: 30G (32212254720 bytes)
disk size: 2.9G
[root@kvmaster2 images]# qemu-img convert -O qcow2 ckcest07.img ckcest07.img.qcow2
[root@kvmaster2 images]# qemu-img info ckcest07.img.qcow2
image: ckcest07.img.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 2.3G
cluster_size: 65536
#虚拟机各种磁盘格式介绍:
http://blog.chinaunix.net/uid-14735472-id-4220089.html
根据创建的"基类"镜像,生成新的虚拟机
[root@kvmaster2 images]# qemu-img create -f qcow2 ckcest08.qcow2 -o backing_file=ckcest07.img 150G \
Formatting 'ckcest08.qcow2', fmt=qcow2size=161061273600 backing_file='ckcest07.img' encryption=off cluster_size=65536
#基类镜像是30G大小、派生出来的镜像可以额外使用150G空间。
[root@kvmaster2 images]# qemu-img info ckcest08.qcow2
image: ckcest08.qcow2
file format: qcow2
virtual size: 150G (161061273600 bytes)
disk size: 196K
cluster_size: 65536
backing file: ckcest07.img
挂载额外的磁盘空间
[root@ckcest08 ~]# df -h #根目录只有20G大小
文件系统 容量 已用 可用 已用%% 挂载点
/dev/mapper/vg_ckcest07-LogVol03
20G 2.0G 17G 11% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 97M 33M 60M 35% /boot
/dev/mapper/vg_ckcest07-LogVol00
9.9G 151M 9.2G 2% /home
/dev/mapper/vg_ckcest07-LogVol02
97M 5.6M 87M 7% /tmp
[root@ckcest08 ~]# fdisk -l #额外的磁盘空间,后面将他挂载到根目录下
Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e1aeb
Device Boot Start End Blocks Id System
/dev/vda1 * 3 206 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 206 62416 31353856 8e Linux LVM
Partition 2 does not end on cylinder boundary.
#因为磁盘是用了LVM做了逻辑卷,所以先把磁盘放到VG,不了解lvm的朋友可以看下http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html
[root@ckcest08 ~]# fdisk /dev/vda #fdisk来创建分区
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): p #显示当前磁盘信息
Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e1aeb
Device Boot Start End Blocks Id System
/dev/vda1 * 3 206 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 206 62416 31353856 8e Linux LVM
Partition 2 does not end on cylinder boundary.
Command (m for help): n #创建一个新的分区
Command action
e extended
p primary partition (1-4)
p #选择新建分区
Partition number (1-4): 3 #之前有了2个,所以这里是3,回车。
First cylinder (1-312076, default 1): 62417
Last cylinder, +cylinders or +size{K,M,G} (62417-312076, default 312076):
Using default value 312076
Command (m for help): p
Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e1aeb
Device Boot Start End Blocks Id System
/dev/vda1 * 3 206 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 206 62416 31353856 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3 62417 312076 125828640 83 Linux #新加了个分区,所有剩余空间都搞这了
Command (m for help): t #修改分区类型
Partition number (1-4): 3 #选择修改的分区号
Hex code (type L to list codes): 8e #8e为lvm的分区类型
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e1aeb
Device Boot Start End Blocks Id System
/dev/vda1 * 3 206 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 206 62416 31353856 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3 62417 312076 125828640 8e Linux LVM #修改成功
Command (m for help): w #w为写入分区并保存。
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@ckcest08 ~]# partprobe #让内核加载新的分区表
[root@ckcest08 ~]# reboot #最好重启,若是新分区标加载成功可以不重启。
[root@ckcest08 ~]# pvscan #目前pv只有一个vda2在
PV /dev/vda2 VG vg_ckcest07 lvm2 [29.90 GiB / 0 free]
Total: 1 [29.90 GiB] / in use: 1 [29.90 GiB] / in no VG: 0 [0 ]
[root@ckcest08 ~]# pvcreate /dev/vda3 #将vda3也创建成pv,
Physical volume "/dev/vda3" successfully created
[root@ckcest08 ~]# pvscan
PV /dev/vda2 VG vg_ckcest07 lvm2 [29.90 GiB / 0 free]
PV /dev/vda3 lvm2 [120.00 GiB] #vda3成功加入
Total: 2 [149.90 GiB] / in use: 1 [29.90 GiB] / in no VG: 1 [120.00 GiB]
[root@ckcest08 ~]# vgdisplay #vg目前只有30G
--- Volume group ---
VG Name vg_ckcest07
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 4
Max PV 0
Cur PV 1
Act PV 1
VG Size 29.90 GiB
PE Size 4.00 MiB
Total PE 7654
Alloc PE / Size 7654 / 29.90 GiB
Free PE / Size 0 / 0
VG UUID rjJTHk-dOjL-mWQ8-I4FK-ukqW-OQvr-58Ro6Y
[root@ckcest08 ~]# vgextend vg_ckcest07 /dev/vda3 #将vda3分区的容量加入vg
Volume group "vg_ckcest07" successfully extended
[root@ckcest08 ~]# vgdisplay #变成150G了。
--- Volume group ---
VG Name vg_ckcest07
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 4
Max PV 0
Cur PV 2
Act PV 2
VG Size 149.89 GiB
PE Size 4.00 MiB
Total PE 38373
Alloc PE / Size 7654 / 29.90 GiB
Free PE / Size 30719 / 120.00 GiB
VG UUID rjJTHk-dOjL-mWQ8-I4FK-ukqW-OQvr-58Ro6Y
[root@ckcest08 ~]# lvdisplay #因为做基类镜像时就都做了lvm所以有这么多,这里只需要将vg的剩余容量挂载到根目录下即可
--- Logical volume ---
LV Path /dev/vg_ckcest07/LogVol01
LV Name LogVol01
VG Name vg_ckcest07
LV UUID QUPJNF-mUkd-9DNx-H02e-UvvN-pz4X-oga4Tt
LV Write Access read/write
LV Creation host, time ckcest07, 2016-08-04 17:11:32 +0800
LV Status available
# open 1
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/vg_ckcest07/LogVol00
LV Name LogVol00 #这个lv是挂载到/home下
VG Name vg_ckcest07
LV UUID PGkJ9t-BdF3-GG0r-P2FM-TSbX-J2ND-K3T8iB
LV Write Access read/write
LV Creation host, time ckcest07, 2016-08-04 17:11:32 +0800
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
--- Logical volume ---
LV Path /dev/vg_ckcest07/LogVol03
LV Name LogVol03
VG Name vg_ckcest07
LV UUID fOFqir-sIhU-0tBP-9a4C-tbiz-fufG-rQzrgF
LV Write Access read/write
LV Creation host, time ckcest07, 2016-08-04 17:11:35 +0800
LV Status available
# open 1
LV Size 19.70 GiB #这个lv挂载到根目录下
Current LE 5044
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
--- Logical volume ---
LV Path /dev/vg_ckcest07/LogVol02
LV Name LogVol02 #这个lv挂载到/tmp下
VG Name vg_ckcest07
LV UUID TDEt0k-HW0p-mhZZ-UQew-t3hn-ysLs-oRykxd
LV Write Access read/write
LV Creation host, time ckcest07, 2016-08-04 17:11:38 +0800
LV Status available
# open 1
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
[root@ckcest08 ~]# lvresize -l +100%FREE /dev/vg_ckcest07/LogVol03 #将vg剩余空间挂载到根目录下
Extending logical volume LogVol03 to 139.70 GiB
Logical volume LogVol03 successfully resized
[root@ckcest08 ~]# resize2fs /dev/vg_ckcest07/LogVol03 #重新加载分区大小..等待他加载完成。
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_ckcest07/LogVol03 is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 9
Performing an on-line resize of /dev/vg_ckcest07/LogVol03 to 36621312 (4k) blocks.
The filesystem on /dev/vg_ckcest07/LogVol03 is now 36621312 blocks long.
[root@ckcest08 ~]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/mapper/vg_ckcest07-LogVol03
138G 2.0G 129G 2% / #磁盘空间都挂载来了,done
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 97M 33M 60M 35% /boot
/dev/mapper/vg_ckcest07-LogVol00
9.9G 151M 9.2G 2% /home
/dev/mapper/vg_ckcest07-LogVol02
97M 5.6M 87M 7% /tmp