天天看點

CentOS7.1 KVM虛拟化之虛拟機根分區擴容(8)一、擴充虛拟機磁盤二、擴充虛拟機根分區三、重新開機虛拟機,完成根分區擴容四、驗證

注:适用于未使用KVM管理的分區,要求根分區為最後一個分區

一、擴充虛拟機磁盤

1.關閉虛拟機

virsh shutdown vm1-clone
           

2.檢視虛拟機磁盤目前大小

[[email protected] ~]# qemu-img info /vm-images/vm1-clone.img 
image: /vm-images/vm1-clone.img
file format: qcow2
virtual size: 10G (11206127360 bytes)
disk size: 1.7G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
           

3.擴充磁盤大小

qemu-img resize /vm-images/vm1-clone.img +5G
           

4.再次檢視虛拟機磁盤目前大小

[[email protected] ~]# qemu-img info /vm-images/vm1-clone.img 
image: /vm-images/vm1-clone.img
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 1.7G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
           

二、擴充虛拟機根分區

1.檢視目前分區狀況,可以看到磁盤/dev/vda大小變化

[[email protected] ~]# fdisk -l

Disk /dev/vda: 16.1 GB, 16106127360 bytes

255 heads, 63 sectors/track, 1958 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *           1          25      200781   83  Linux

/dev/vda2              26         156     1052257+  82  Linux swap / Solaris

/dev/vda3             157        1305     9229342+  83  Linux      #注意這裡起始cylinder 是157,起始cylinder 絕對不可以改,否則會破壞原分區的資料。

2.擴充根分區

[[email protected] ~]# fdisk /dev/vda

The number of cylinders for this disk is set to 1958.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/vda: 16.1 GB, 16106127360 bytes

255 heads, 63 sectors/track, 1958 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *           1          25      200781   83  Linux

/dev/vda2              26         156     1052257+  82  Linux swap / Solaris

/dev/vda3             157        1305     9229342+  83  Linux

Command (m for help): d

Partition number (1-4): 3                    #根/分區是多少就輸入幾,這裡是/dev/vda3

Command (m for help): p

Disk /dev/vda: 16.1 GB, 16106127360 bytes

255 heads, 63 sectors/track, 1958 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/vda1   *           1          25      200781   83  Linux

/dev/vda2              26         156     1052257+  82  Linux swap / Solaris

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 3                  #之前是多少,這裡還輸入多少

First cylinder (157-1958, default 157):

Using default value 157

Last cylinder or +size or +sizeM or +sizeK (157-1958, default 1958):                      #由之前1305,擴為1958

Using default value 1958

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

The kernel still uses the old table.

The new table will be used at the next reboot.

Syncing disks.

三、重新開機虛拟機,完成根分區擴容

執行如下指令,重新開機系統

[[email protected] ~]#reboot

重新開機後執行如下指令,完成根分區擴充

[[email protected] ~]#resize2fs /dev/vda3

resize2fs 1.39 (29-May-2006)

Filesystem at /dev/vda3 is mounted on /; on-line resizing required

Performing an on-line resize of /dev/vda3 to 3618641 (4k) blocks.

The filesystem on /dev/vda3 is now 3618641 blocks long.

四、驗證

檢視目前磁盤狀态

[[email protected] ~]# df -h

檔案系統              容量  已用 可用 已用% 挂載點

/dev/vda3              14G  1.3G   12G  11% /

/dev/vda1             190M   12M  169M   7% /boot

tmpfs                 379M     0  379M   0% /dev/shm

如上,完成根分區擴容

繼續閱讀