1. 程式人生 > >centos7擴充套件磁碟空間

centos7擴充套件磁碟空間

準備

vmware安裝centos7,在lvm下和非lvm下分別進行測試

虛擬機器擴充套件硬碟容量

這裡寫圖片描述
選擇擴充套件將原來的20g擴充套件到100g

非lvm下如何擴充套件磁碟

fdisk -l發現只有一個盤/dev/sda,分成了3個區,sda1,sda2,sda3

Disk /dev/sda: 20.4 GB
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bbf20

   Device Boot      Start         End
Blocks Id System /dev/sda1 * 2048 616447 307200 83 Linux /dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris /dev/sda3 4810752 41943039 18566144 83 Linux

開始分割槽

[[email protected] ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in
memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m Command action 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,新建主分割槽;
輸入分割槽編號4(已經有1,2,3了,所以新建的是4);
之後一直按enter鍵,設定新建分割槽預設大小;
最後輸入w 儲存。

同步分割槽

partprobe
如果沒有這一步,後面的格式化會提示下面錯誤

/dev/sda4: No such file or directory

格式化

mkfs.xfs /dev/sda4

掛載目錄

[[email protected] home]# mkdir /sda4
[[email protected] home]# mount /dev/sda4 /sda4

新建立的分割槽不能開機自動掛載,每次重啟機器都要手動掛載。
設定開機自動掛載需要修改/etc/fstab檔案
cat /etc/fstab

UUID=6ead5f9b-3451-499c-947a-08e2dc947304 /                       xfs     defaults        0 0
UUID=d5ba0def-f729-4180-b7ff-584396e36bc0 /boot                   xfs     defaults        0 0
UUID=6f708491-e4b1-44ea-bd4b-d33876fa650c swap                    swap    defaults        0 0

檢視分割槽uuid

[root@promote ~]# blkid /dev/sda4
/dev/sda4: UUID="818f6627-a91a-459c-aacb-220929178ba5" TYPE="xfs" 

在檔案的最後增加一行
UUID=818f6627-a91a-459c-aacb-220929178ba5 /sda4 xfs defaults 0 0

驗證

[root@promote ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda3      xfs        18G  8.0G  9.8G  45% /
devtmpfs       devtmpfs  970M     0  970M   0% /dev
tmpfs          tmpfs     984M  160K  984M   1% /dev/shm
tmpfs          tmpfs     984M  9.0M  975M   1% /run
tmpfs          tmpfs     984M     0  984M   0% /sys/fs/cgroup
/dev/sda4      xfs        80G   33M   80G   1% /sda4
/dev/sda1      xfs       297M  152M  146M  51% /boot
tmpfs          tmpfs     197M  8.0K  197M   1% /run/user/42
tmpfs          tmpfs     197M  8.0K  197M   1% /run/user/0

看到sda4為80g,恭喜自己成功了。

lvm下如何擴充套件磁碟

開始分割槽

注意型別是8e的

[[email protected] qiu]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n        新建
Partition type:            
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p          主分割槽
Partition number (3,4, default 3): 3   分割槽編碼
First sector (41943040-209715199, default 41943040):  回車鍵預設扇區大小
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-209715199, default 209715199): 回車鍵預設扇區大小
Using default value 209715199
Partition 3 of type Linux and of size 80 GiB is set

Command (m for help): t   修改分割槽型別
Partition number (1-3, default 3): 3  修改指定分割槽
Hex code (type L to list all codes): 8e  
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w

同步分割槽
partprobe
格式化
mkfs.xfs /dev/sda3

建立pv

pvcreate /dev/sda3

加入vg

vgextend cl /dev/sda3

擴充套件邏輯卷大小

[[email protected] qiu]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                LRdTFN-ALQF-YZ2D-Ayj1-Mati-bY5H-meOPcv
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-06-30 07:00:29 -0700
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                u2AL50-gvWQ-OLsx-Lpc6-Z0g0-gPLL-VT6Jxm
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-06-30 07:00:30 -0700
  LV Status              available
  # open                 1
  LV Size                80.00 GiB
  Current LE             20480
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

lvextend -L 80g /dev/cl/root 擴充套件到80g

同步檔案系統

xfs_growfs /dev/cl/root

驗證

[root@promote qiu]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs        80G  4.7G   76G   6% /
devtmpfs            devtmpfs  969M     0  969M   0% /dev
tmpfs               tmpfs     984M  144K  984M   1% /dev/shm
tmpfs               tmpfs     984M  9.0M  975M   1% /run
tmpfs               tmpfs     984M     0  984M   0% /sys/fs/cgroup
/dev/sda1           xfs      1014M  173M  842M  18% /boot
tmpfs               tmpfs     197M   16K  197M   1% /run/user/0

看到root邏輯卷大小為80g,恭喜自己成功了。

其他

刪除分割槽

[[email protected] qiu]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted

Command (m for help): w

檢視分割槽

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c58a8

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