Linux虛擬機擴展磁盤


Linux虛擬機擴展磁盤

1.虛擬機關機,Vmware中擴展磁盤

image-20210726093124228

2.虛擬機開機,查看磁盤大小

[root@hadoop6 ~]# df -h
文件系統                 容量  已用  可用 已用% 掛載點
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G  891M  6.9G   12% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   17G   17G   20K  100% /
/dev/sda1               1014M  150M  865M   15% /boot
cm_processes             7.8G     0  7.8G    0% /run/cloudera-scm-agent/process
tmpfs                    1.6G     0  1.6G    0% /run/user/0
[root@hadoop6 ~]# fdisk -l

磁盤 /dev/sda:536.9 GB, 536870912000 字節,1048576000 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x000ee5d4

   設備 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

磁盤 /dev/mapper/centos-root:18.2 GB, 18249416704 字節,35643392 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節


磁盤 /dev/mapper/centos-swap:2147 MB, 2147483648 字節,4194304 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節

這里的擴展只是增加了操作系統的磁盤空間,並沒有與系統內部的文件目錄掛載,所以,磁盤占有量還是不會變化,下一步就是要把擴展的容量掛載到文件目錄上去。

3.對新硬盤分區並修改系統類型

[root@hadoop6 ~]# fdisk /dev/sda
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。


命令(輸入 m 獲取幫助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

依次輸入:

  • n --新建分區
  • p --默認創建主分區
  • 3 --默認分區號
  • 回車 --默認大小不浪費空間
  • 回車 --默認大小不浪費空間
命令(輸入 m 獲取幫助):n --新建分區
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p --默認創建主分區
分區號 (3,4,默認 3):3 --默認
起始 扇區 (41943040-1048575999,默認為 41943040):    --回車
將使用默認值 41943040
Last 扇區, +扇區 or +size{K,M,G} (41943040-1048575999,默認為 1048575999):    --回車
將使用默認值 1048575999
分區 3 已設置為 Linux 類型,大小設為 480 GiB

輸入:w --保持修改

命令(輸入 m 獲取幫助):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)
正在同步磁盤。

將system 類型改成Linux LVM

[root@hadoop6 ~]# fdisk -l 

磁盤 /dev/sda:536.9 GB, 536870912000 字節,1048576000 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x000ee5d4

   設備 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040  1048575999   503316480   83  Linux

磁盤 /dev/mapper/centos-root:18.2 GB, 18249416704 字節,35643392 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節


磁盤 /dev/mapper/centos-swap:2147 MB, 2147483648 字節,4194304 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節

我們的新建分區/dev/sda3,的Id為83不是LVM的。所以,接下來使用fdisk將其改成Id為8e的LVM。

[root@hadoop6 ~]# fdisk /dev/sda
歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。


命令(輸入 m 獲取幫助):m  
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
命令(輸入 m 獲取幫助):t  --修改分區系統Id
分區號 (1-3,默認 3):3  --指定分區號
Hex 代碼(輸入 L 列出所有代碼):8e  --指定要改成的id號,8e代表LVM
已將分區“Linux”的類型更改為“Linux LVM”

命令(輸入 m 獲取幫助):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)
正在同步磁盤。

fdisk -l 再查看一下是否改成8e 和Linux LVM

[root@hadoop6 ~]# fdisk -l

磁盤 /dev/sda:536.9 GB, 536870912000 字節,1048576000 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x000ee5d4

   設備 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040  1048575999   503316480   8e  Linux LVM

磁盤 /dev/mapper/centos-root:18.2 GB, 18249416704 字節,35643392 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節


磁盤 /dev/mapper/centos-swap:2147 MB, 2147483648 字節,4194304 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節

重啟系統

[root@hadoop6 ~]# reboot

4.擴充新分區

格式化新添加的分區,改為ext4類型

[root@hadoop6 ~]# mkfs.ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
文件系統標簽=
OS type: Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
31457280 inodes, 125829120 blocks
6291456 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=2273312768
3840 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000

Allocating group tables: 完成                            
正在寫入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成     

添加新LVM到已有的LVM組,實現擴容

創建sda3:

[root@hadoop6 ~]# pvcreate /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created.

查看創建結果:

[root@hadoop6 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               2UuDxd-kchc-YxC2-VJYX-yK9r-qGMG-7H0fvi
   
  "/dev/sda3" is a new physical volume of "480.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               480.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               i60nWa-40d3-jFCg-F0Su-24FO-QQCh-Ky0QA7

**這里操作要根據上面中VG Name來定義用vgextend誰,我這里是centos那么我就用vgextend centos /dev/sda3 **

[root@hadoop6 ~]# vgextend centos /dev/sda3 
  Volume group "centos" successfully extended
[root@hadoop6 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               2UuDxd-kchc-YxC2-VJYX-yK9r-qGMG-7H0fvi
   
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               centos
  PV Size               480.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              122879
  Free PE               122879
  Allocated PE          0
  PV UUID               i60nWa-40d3-jFCg-F0Su-24FO-QQCh-Ky0QA7

執行命令:lvextend -L +480G /dev/mapper/centos-root 進行擴容,容量大小,自己根據情況定義

[root@hadoop6 ~]# lvextend -L +480G /dev/mapper/centos-root
  Insufficient free space: 122880 extents needed, but only 122879 available

直接按最大容量擴充會報錯

執行 pvdisplay 可以看到 /dev/sda3 可用的 PE 總數量是 122879,而每個 PE 大小是 4.00MiB,所以這個卷組實際的可用空間不是480G,122879*4/1024=479.99609375 而是479.99G

[root@hadoop6 ~]# lvextend -L +479.99G /dev/mapper/centos-root
  Rounding size to boundary between physical extents: 479.99 GiB.
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <496.99 GiB (127229 extents).
  Logical volume centos/root successfully resized.

執行命令:lvs 進行查看是否成功

[root@hadoop6 ~]# lvs
  LV   VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <496.99g                                                    
  swap centos -wi-ao----    2.00g 

此時df -h查看容量未變化

[root@hadoop6 ~]# df -h
文件系統                 容量  已用  可用 已用% 掛載點
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G   12M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   17G   17G  2.0M  100% /
/dev/sda1               1014M  150M  865M   15% /boot
cm_processes             7.8G     0  7.8G    0% /run/cloudera-scm-agent/process
tmpfs                    1.6G     0  1.6G    0% /run/user/0

查看磁盤空間采用的文件系統

[root@hadoop6 ~]# df -T
文件系統                類型        1K-塊     已用    可用 已用% 掛載點
devtmpfs                devtmpfs  8111764        0 8111764    0% /dev
tmpfs                   tmpfs     8123804        0 8123804    0% /dev/shm
tmpfs                   tmpfs     8123804    11932 8111872    1% /run
tmpfs                   tmpfs     8123804        0 8123804    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs      17811456 17809512    1944  100% /
/dev/sda1               xfs       1038336   153464  884872   15% /boot
cm_processes            tmpfs     8123804        0 8123804    0% /run/cloudera-scm-agent/process
tmpfs                   tmpfs     1624764        0 1624764    0% /run/user/0

需要修改ext4文件系統為xfs文件系統

執行命令:xfs_growfs /dev/mapper/centos-root

[root@hadoop6 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4455424 to 130282496

再次查看

[root@hadoop6 ~]# df -h
文件系統                 容量  已用  可用 已用% 掛載點
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G   12M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root  497G   17G  480G    4% /
/dev/sda1               1014M  150M  865M   15% /boot
cm_processes             7.8G     0  7.8G    0% /run/cloudera-scm-agent/process
tmpfs                    1.6G     0  1.6G    0% /run/user/0

到此,容量已經擴大,磁盤擴展成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM