centos7 在安裝后,通常我們通過df -h 來查看磁盤空間的時候率。如下:
[root@oracle01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 46G 32G 14G 71% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 17M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 125M 372M 26% /boot
tmpfs 379M 0 379M 0% /run/user/0
如上,在根目錄 / 下,已經使用了71%,快沒有空間了,這個時候,如果需要增加空間,如果你是實體機,則只能 增加磁盤,如果你是虛擬機或者云服務器,那么可以自由擴展,于是我將磁盤空間從50G增加至100G。
直接增加磁盤空間,但是,不管哪種方式,增加后,會發現,并沒有生效,df -h 能用的空間并沒有增加,而新增的空間使用 fdisk -l 則可以查看到,如下:
可以看到,雖然我的磁盤空間大小為107G ,但是下面使用了的只有50G 左右。 增加的空間并沒有被使用。
下面,就需要將沒使用到的空間,用來擴容在根目錄下,或別的掛載目錄下。 在開始之前,我們必須要對linux的磁盤管理有一定的認識:
在centos中,對于儲存和磁盤空間的管理是lvm , 我們常用的 fdisk -l 命令,顯示的就是物理分區,相當于直接作用于磁盤上的。
在物理分區中,我們可以在不同的磁盤中,劃分去分區,而這個分區必須要設置為lvm 分區格式,只有設置為lvm分區格式,才可以將這些分區,提取出來創建成物理卷,
這些在不通物理磁盤中的物理卷,就可以組成一個大的卷組,這個卷組就相當于邏輯上的磁盤,然后可以在卷組上進行分區,分為邏輯卷。而這些邏輯卷就是我們常用的 df -h 來查看的磁盤使用鎖顯示的,例如 根目錄 /home目錄等就是常用的邏輯卷。
你可能會問為什么要這樣做,因為一般的物理磁盤進行的物理分區,一旦確定,是不能修改大小的,也就無法很好的就行擴容。而且當容量大于一般磁盤的需求時,就沒有像lvm一樣可以通過組合多個物理磁盤來形成一個大的卷組使用。當然,也為了更好的管理。
好的。下面就開始對根目錄進行擴容。
[root@oracle01 ~]# fdisk /dev/sda ## /dev/sda為通過fdisk -l 查看到的物理磁盤(第一行)
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 ## n為創建一個新的分區
Partition type: ## 這里需要選擇時創建主分區還是擴展分區,都可以,這里直接選了主分區
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p ## 選擇創建一個主分區,主分區只能有4個,編號為1-4,下面的全部直接回車就好了,會自動將剩余所用空間都創建
Partition number (3,4, default 3):
First sector (104857600-209715199, default 104857600):
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 50 GiB is set
Command (m for help): t ## t為修改分區類型
Partition number (1-3, default 3): 3 ## 剛才創建的分區編號為3
Hex code (type L to list all codes): 8e ## 8e就是 lvm格式的分區
Changed type of partition 'Linux' to '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: Device or resource busy.
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.
這里,在保存后,會發現,可能會出現報錯,顯示繁忙,無法重新讀取分區信息。下面有解決辦法。可以用過重啟或者執行 partprobe or kpartx。所以,這里直接執行partprobe
[root@oracle01 ~]# partprobe
- 創建了物理分區后,就需要將物理分區創建為物理卷:
將物理分區3 創建物理卷,這里這個/dev/sda3是第一步創建的物理分區,通過fdisk -l 就可以看到
是在第一個物理磁盤/dev/sda下面的分區(一個磁盤可以有多個分區),原本已有/dev/sda1,/dev/sda2
通過第一步創建生成/dev/sda3這個分區,然后將這個分區 創建成物理卷
[root@oracle01 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
[root@oracle01 ~]# pvdisplay ## 查看物理卷
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 49.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12674
Free PE 11
Allocated PE 12663
PV UUID 8T9dpf-SrDj-maVi-IlAb-tpPb-Lzyt-WLeRHG
"/dev/sda3" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID uRRJtJ-Ds7J-dvqs-y5Lb-W95H-FXeh-C2QNtl
- 物理卷創建成功后,則需要將物理卷添加到卷組中,這樣才能在卷組中進行分配。
[root@oracle01 ~]# vgdisplay ## 查看卷組
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <49.51 GiB
PE Size 4.00 MiB
Total PE 12674
Alloc PE / Size 12663 / 49.46 GiB
Free PE / Size 11 / 44.00 MiB ## 可以看到這個卷組只剩下44M的剩余空間了,其他的都劃分為邏輯卷了,類似于已分區
VG UUID 3TMbc1-A7aP-DAMv-QtUU-VTjC-4tOf-iAxDkc
[root@oracle01 ~]# vgextend centos /dev/sda3 ## 將物理卷加入到卷組
Volume group "centos" successfully extended
[root@oracle01 ~]# vgdisplay ## 加入后,再次查看卷組
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 99.50 GiB
PE Size 4.00 MiB
Total PE 25473
Alloc PE / Size 12663 / 49.46 GiB
Free PE / Size 12810 / <50.04 GiB ## 可以看到,加入成功后,卷組的剩余空間變大了,這樣就有更多的空間可以進行分配,或新創建新的邏輯卷
VG UUID 3TMbc1-A7aP-DAMv-QtUU-VTjC-4tOf-iAxDkc
- 添加進卷組后,就是將剩余空間對邏輯卷進行擴容。
[root@oracle01 ~]# lvdisplay ## 查看邏輯卷
--- Logical volume ---
LV Path /dev/centos/root ## 邏輯卷名稱
LV Name root
VG Name centos
LV UUID RaEygR-53tg-UaJQ-5dJs-e8V4-by51-fcp1DQ
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-09-30 19:12:05 +0800
LV Status available
# open 1
LV Size 45.46 GiB ## 增加前,邏輯卷大小
Current LE 11639
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@oracle01 ~]# lvextend -l +100%FREE /dev/centos/root ## 將剩余百分百空間都添加到邏輯卷中
Size of logical volume centos/root changed from 45.46 GiB (11639 extents) to 95.50 GiB (24449 extents).
Logical volume centos/root successfully resized.
[root@oracle01 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID RaEygR-53tg-UaJQ-5dJs-e8V4-by51-fcp1DQ
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-09-30 19:12:05 +0800
LV Status available
# open 1
LV Size 95.50 GiB ## 增加后,邏輯卷大小
Current LE 24449
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
- 然后重新識別一下分區大小,就可以通過df -h看到新增的容量了
[root@oracle01 ~]# xfs_growfs /dev/centos/root ## 命令,后面跟的是邏輯卷的path
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=2979584 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=11918336, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=5819, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 11918336 to 25035776
[root@oracle01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 96G 32G 64G 34% / ## 這里可以看到,已成功擴容
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 17M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 125M 372M 26% /boot
tmpfs 379M 0 379M 0% /run/user/0