kvm-虚拟机增加硬盘空间

来源:互联网 发布:51单片机仿真器 编辑:程序博客网 时间:2024/05/21 10:30

kvm虚拟化模式下,给虚拟机增加硬盘空间概念上有3种办法;首先执行指令:virsh resize VR.img +2G 

其中方法一,可参考http://geekpeek.net/resize-filesystem-fdisk-resize2fs/,及动态扩充非lvm磁盘空间,此处不可行的原因在于扩充需要卸载挂载点,而根节点或者boot节点不可卸载,否则会出问题;

方法一:fdisk模式下直接将新增磁盘空间给予/,及根挂载点;

方法二:fdisk模式下,将新增磁盘空间成为一个新的分区,如/dev/sda3,然后mkfs -t ext4 /dev/sda3,然后挂载到某一目录;

方法三:将新增磁盘空间成为一个新的磁盘;具体方法参照http://blog.fens.me/vps-kvm-disk/;概括如下:

1. host增加物理硬盘并分区

HOST作为KVM的宿主计算机,管理所有GUEST虚拟机。我们通过给HOST增加物理硬盘,然后分给GUEST,从而实现给虚拟机硬盘扩容的效果。

如何给计算机增加物理硬盘并分区,请参考:多硬盘分区管理fdisk 文章

查看HOST机的硬盘

~ sudo fdisk -lDisk /dev/sda: 299.4 GB, 299439751168 bytes255 heads, 63 sectors/track, 36404 cylinders, total 584843264 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000efd7c   Device Boot      Start         End      Blocks   Id  System/dev/sda1            2048    97656831    48827392   82  Linux swap / Solaris/dev/sda2        97656832   136718335    19530752   83  Linux/dev/sda3       136718336   214843335    39062500   83  Linux/dev/sda4   *   214843392   215037951       97280   83  LinuxDisk /dev/sdb: 1999.3 GB, 1999307276288 bytes255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xf919a976   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT/dev/sdb2      1952448512  3904897023   976224256    5  Extended/dev/sdb5      1952450560  2267023360   157286400+  83  Linux/dev/sdb6      2267025409  2581596160   157285376   83  Linux/dev/sdb7      2581598209  2896168960   157285376   83  Linux/dev/sdb8      2896171009  3210741760   157285376   83  Linux/dev/sdb9      3210743809  3525314560   157285376   83  Linux/dev/sdb10     3525316609  3904897023   189790207+  83  Linux~ df -hFilesystem      Size  Used Avail Use% Mounted on/dev/sda3        37G  3.4G   32G  10% /udev             24G  4.0K   24G   1% /devtmpfs           9.5G  1.1M  9.5G   1% /runnone            5.0M  8.0K  5.0M   1% /run/locknone             24G  152K   24G   1% /run/shmnone            100M   28K  100M   1% /run/usercgroup           24G     0   24G   0% /sys/fs/cgroup/dev/sda2        19G  3.6G   14G  21% /home/dev/sda4        92M   34M   54M  39% /boot/dev/sdb1       931G  100G  832G  11% /disk/sdb1/dev/sdb6       148G  188M  140G   1% /disk/sdb6

下面将进行两个测试:
通过virsh给guest增加文件硬盘:通过文件硬盘的镜像/disk/sdb6/c1d6.img
通过virsh给guest增加分区硬盘:直接使用分区硬盘/dev/sdb5

2. 通过virsh给guest增加文件硬盘

创建文件硬盘的镜像

~ cd /disk/sdb6/~ sudo qemu-img create -f raw /disk/sdb6/c1d6.img 10GFormatting '/disk/sdb6/c1d6.img', fmt=raw size=10737418240~ ls -l-rw-r--r-- 1 root root 10737418240 Jul  8 16:37 c1d6.imgdrwx------ 2 root root       16384 Jul  8 09:03 lost+found/

通过virsh管理工具加载硬盘

~ sudo virshWelcome to virsh, the virtualization interactive terminal.Type:  'help' for help with commands       'quit' to quit#查看系统内的虚拟机virsh # listId Name State----------------------------------------------------5 server3 running6 server4 running7 d2 running8 r1 running9 server2 running12 c1 running#在这里我们要对c1进行硬盘扩容virsh # edit c1#找到硬盘配置(原来的系统硬盘)<disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/disk/sdb1/c1.img'/><target dev='vda' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/></disk>#增加文件硬盘,vdb<disk type='file' device='disk'><driver name='qemu' type='raw' cache='none'/><source file='/disk/sdb6/c1d6.img'/><target dev='vdb' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></disk>#保存退出

重启c1虚拟机

#请使用destroy命令,reboot和shutdown不管用。~ virsh # destroy c1Domain c1 destroyed#list找不到c1 ~ virsh # list Id    Name                           State---------------------------------------------------- 5     server3                        running 6     server4                        running 7     d2                             running 8     r1                             running 9     server2                        running#启动虚拟机c1~ virsh # start c1Domain c1 started#进入虚拟机c1~ console c1

在c1中,进行硬盘查检并分区

~ sudo fdisk -lDisk /dev/vda: 42.9 GB, 42949672960 bytes16 heads, 63 sectors/track, 83220 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000516aa   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048      499711      248832   83  Linux/dev/vda2          501758    83884031    41691137    5  Extended/dev/vda5          501760    83884031    41691136   8e  Linux LVMDisk /dev/vdb: 10.7 GB, 10737418240 bytes16 heads, 63 sectors/track, 20805 cylinders, total 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/vdb doesn't contain a valid partition tableDisk /dev/mapper/u1210-root: 38.4 GB, 38394658816 bytes255 heads, 63 sectors/track, 4667 cylinders, total 74989568 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/u1210-root doesn't contain a valid partition tableDisk /dev/mapper/u1210-swap_1: 4294 MB, 4294967296 bytes255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/u1210-swap_1 doesn't contain a valid partition table

/dev/vdb已经被识别,接下来 分区,格式化,挂载,使用

硬盘分区

~ sudo fdisk /dev/vdbCommand (m for help): pDisk /dev/vdb: 161.1 GB, 161061274112 bytes16 heads, 63 sectors/track, 312076 cylinders, total 314572801 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x3b49c6a0   Device Boot      Start         End      Blocks   Id  SystemCommand (m for help): nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): pPartition number (1-4, default 1):Using default value 1First sector (2048-314572800, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-314572800, default 314572800):Using default value 314572800Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.#分区生效~ sudo partprobe~ sudo fdisk -lDisk /dev/vdb: 10.7 GB, 10737418240 bytes2 heads, 17 sectors/track, 616809 cylinders, total 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xf0432cd6   Device Boot      Start         End      Blocks   Id  System/dev/vdb1            2048    20971519    10484736   83  Linux

格式化

~ sudo mkfs -t ext4 /dev/vdb1mke2fs 1.42.5 (29-Jul-2012)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks9830400 inodes, 39321344 blocks1966067 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=42949672961200 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,        4096000, 7962624, 11239424, 20480000, 23887872Allocating group tables: doneWriting inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done

挂载

~ sudo mount /dev/vdb1 /home/cos/hadoopb~ df -hFilesystem              Size  Used Avail Use% Mounted on/dev/mapper/u1210-root   36G  1.1G   33G   4% /udev                    2.0G  4.0K  2.0G   1% /devtmpfs                   791M  232K  791M   1% /runnone                    5.0M     0  5.0M   0% /run/locknone                    2.0G     0  2.0G   0% /run/shmnone                    100M     0  100M   0% /run/user/dev/vda1               228M   29M  188M  14% /boot/dev/vdb1               9.9G  151M  9.2G   2% /home/cos/hadoopb

使用
/home/cos/hadoopb的目录,已经挂载到了/dev/vdb1上面,我可以在hadoopb下载做任何的操作。

3. 通过virsh给guest增加分区硬盘

直接使用HOST的分区硬盘/dev/sdb5,做个虚拟机c1的分区

virsh # edit c1#新增新硬盘vbc<disk type='block' device='disk'><driver name='qemu' type='raw' cache='none'/><source dev='/dev/sdb5'/><target dev='vbc' bus='virtio'/></disk>virsh # destroy c1Domain c1 destroyedvirsh # start c1Domain c1 startedvirsh # console c1

登陆虚拟c1,查看硬盘信息

sudo fdisk -l[sudo] password for cos:Disk /dev/vda: 42.9 GB, 42949672960 bytes16 heads, 63 sectors/track, 83220 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000516aa   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048      499711      248832   83  Linux/dev/vda2          501758    83884031    41691137    5  Extended/dev/vda5          501760    83884031    41691136   8e  Linux LVMDisk /dev/vdb: 10.7 GB, 10737418240 bytes2 heads, 17 sectors/track, 616809 cylinders, total 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xf0432cd6   Device Boot      Start         End      Blocks   Id  System/dev/vdb1            2048    20971519    10484736   83  LinuxDisk /dev/vdc: 161.1 GB, 161061274112 bytes4 heads, 4 sectors/track, 19660800 cylinders, total 314572801 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x3b49c6a0   Device Boot      Start         End      Blocks   Id  System/dev/vdc1            2048   314572800   157285376+  83  LinuxDisk /dev/mapper/u1210-root: 38.4 GB, 38394658816 bytes255 heads, 63 sectors/track, 4667 cylinders, total 74989568 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/u1210-root doesn't contain a valid partition tableDisk /dev/mapper/u1210-swap_1: 4294 MB, 4294967296 bytes255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/u1210-swap_1 doesn't contain a valid partition table

已经被正确识别
Disk /dev/vdc: 161.1 GB, 161061274112 bytes

接下来,可以参考上面类似的过程,分区,格式化,挂载,使用。

现将http://geekpeek.net/resize-filesystem-fdisk-resize2fs/方法复制如下:

There was a question in my post on “Linux partitioning with fdisk on CentOS 6“. Our reader asked if it is possible to extend an existing linux non LVM partition without loosing it’s data … here is a post on how to resizepartition and filesystem with fdisk and resize2fs.

IMPORTANT: In order to resize partition (extend), enough disk space must be available! We can not extend a partition if there are no free sectors/cylinders at the end of the partition to extend!

PLEASE BACKUP YOUR DATA BEFORE RESIZING ANY PARTITION! GeekPeek.Net is not responsible for any data loss!

Resize Partition and Filesystem with fdisk and resize2fs

Resize Partition and Filesystem with fdisk and resize2fs

 

Let’s Resize Partition and Filesystem with fdisk and resize2fs!

Our system has two disks:

  • /dev/sda – 16GB system disk with LVM partitions (root and swap)
  • /dev/sdb – 1GB clean disk for tutorial purpose

For the start of this tutorial tutorial we have created one partition on /dev/sdb disk.Partition size (/dev/sdb1) is 500MB. We created an ext4 filesystem on this partition and put some dummy data on it. We will be modifying our partitions with fdisk and filesystem with resize2fs.

For more information on how to create and modify partitions with fdisk read “Linux partitioning with fdisk on CentOS 6“.

Extend Partition and Filesystem

Before we start, let’s check the current disk configuration. Geekpeek mount point is the partition we want to extend. We can see that the partition has 494MB of available space and 401MB is already used by dummy data. We want to extend the partition to 1GB:

[root@foo1 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_foo-LogVol01                       13G  4.6G  7.7G  38% /tmpfs                 376M     0  376M   0% /dev/shm/dev/sda1             485M  105M  355M  23% /boot/dev/sdb1             494M  402M   67M  86% /geekpeek

1. Unmount the partition

[root@foo1 ~]# umount /geekpeek/[root@foo1 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_foo-LogVol01                       13G  4.6G  7.7G  38% /tmpfs                 376M     0  376M   0% /dev/shm/dev/sda1             485M  105M  355M  23% /boot

2. Delete the partition

[root@foo1 ~]# fdisk /dev/sdbWARNING: 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): dSelected partition 1Command (m for help): pDisk /dev/sdb: 1073 MB, 1073741824 bytes255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x2dbb9f13   Device Boot      Start         End      Blocks   Id  SystemCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

3. Create a new (larger) partition

[root@foo1 ~]# fdisk /dev/sdbWARNING: 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): nCommand action   e   extended   p   primary partition (1-4)pPartition number (1-4): 1First sector (2048-2097151, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): Using default value 2097151Command (m for help): pDisk /dev/sdb: 1073 MB, 1073741824 bytes255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x2dbb9f13   Device Boot      Start         End      Blocks   Id  System/dev/sdb1            2048     2097151     1047552   83  LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.

 

We are able to mount the partition at this point, but the filesystem on this partition is only 500MB large. We need to resize the filesystem using resize2fs command in the following steps.

4. Run fsck on your filesystem

[root@foo1 ~]# e2fsck -f /dev/sdb1e2fsck 1.41.12 (17-May-2010)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/sdb1: 16/130560 files (0.0% non-contiguous), 426988/522080 blocks

5. Resize your filesystem with resize2fs

[root@foo1 ~]# resize2fs /dev/sdb1 resize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/sdb1 to 1044192 (1k) blocks.The filesystem on /dev/sdb1 is now 1044192 blocks long.

6. Re-mount extended partition

[root@foo1 ~]# mount /dev/sdb1 /geekpeek/[root@foo1 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_foo-LogVol01                       13G  4.6G  7.7G  38% /tmpfs                 376M     0  376M   0% /dev/shm/dev/sda1             485M  105M  355M  23% /boot/dev/sdb1             988M  402M  536M  43% /geekpeek

 
Voila! The partition was successfully extended, as we can see all of the data survived! :) Always remember to resize the filesystem with resize2fs.

Reduce a Partition and Filesystem

Reviewing the current disk configuration: Geekpeek mount point is the partition we want to reduce. We can see that the partition has 988MB of available space and 324MB is used by dummy data. We want to reduce the partition to 400MB without loosing data:

[root@foo1 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_foo-LogVol01                       13G  4.6G  7.7G  38% /tmpfs                 376M     0  376M   0% /dev/shm/dev/sda1             485M  105M  355M  23% /boot/dev/sdb1             988M  324M  614M  35% /geekpeek

1. Unmount the partition

[root@foo1 ~]# umount /geekpeek/[root@foo1 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_foo-LogVol01                       13G  4.6G  7.7G  38% /tmpfs                 376M     0  376M   0% /dev/shm/dev/sda1             485M  105M  355M  23% /boot

2. Run fsck on your filesystem

[root@foo1 ~]# e2fsck -f /dev/sdb1e2fsck 1.41.12 (17-May-2010)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/sdb1: 15/261120 files (0.0% non-contiguous), 363953/1044192 blocks

 3. Resize the filesystem with resize2fs

[root@foo1 ~]# resize2fs /dev/sdb1 400Mresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/sdb1 to 409600 (1k) blocks.The filesystem on /dev/sdb1 is now 409600 blocks long.

4. Delete the partition

[root@foo1 ~]# fdisk /dev/sdbWARNING: 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): dSelected partition 1Command (m for help): pDisk /dev/sdb: 1073 MB, 1073741824 bytes255 heads, 63 sectors/track, 130 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x2dbb9f13   Device Boot      Start         End      Blocks   Id  SystemCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

4. Create a new (smaller) partition

[root@foo1 ~]# fdisk /dev/sdb 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): nCommand action   e   extended   p   primary partition (1-4)pPartition number (1-4): 1First cylinder (1-130, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +400M Command (m for help): pDisk /dev/sdb: 1073 MB, 1073741824 bytes255 heads, 63 sectors/track, 130 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x2dbb9f13   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1          52      417658+  83  LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

5. Mount the new partition and filesystem

[root@foo1 ~]# mount /dev/sdb1 /geekpeek/[root@foo1 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/mapper/vg_foo-LogVol01                       13G  4.6G  7.7G  38% /tmpfs                 376M     0  376M   0% /dev/shm/dev/sda1             485M  105M  355M  23% /boot/dev/sdb1             388M  323M   45M  88% /geekpeek

 
Voila! The partition was successfully reduced to 400MB, as we can see all of the data survived! :) Always remember to resize the filesystem with resize2fs.



0 0
原创粉丝点击