前言: 在實(shí)際生產(chǎn)或者實(shí)驗(yàn)環(huán)境下苏遥,我們經(jīng)常會(huì)碰到磁盤使用完了策治,不夠用泡一,需要增加硬盤或者直接擴(kuò)展容量,又或者整個(gè)系統(tǒng)全部刪除然后進(jìn)行增加硬盤進(jìn)行重裝系統(tǒng),但是刪掉系統(tǒng)重新進(jìn)行安裝付出的時(shí)間或者其他成本都太大了二拐,所以我推薦如果是虛擬化平臺(tái)直接增加一塊硬盤或者擴(kuò)大容量服鹅,本篇文章根據(jù)直接擴(kuò)大硬盤容量
一、 實(shí)驗(yàn)環(huán)境說明
- 虛擬化平臺(tái)是VMware workstation 12 pro
- Centos 7.1 最小化安裝
- 初始硬盤為 50G
二百新、開始實(shí)驗(yàn)
- 系統(tǒng)信息
[root@Allenwu ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.1.1503 (Core)
Release: 7.1.1503
Codename: Core
- 硬盤信息
[root@Allenwu ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009c3d0
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
/dev/sda3 41943040 104857599 31457280 83 Linux
Disk /dev/mapper/centos-root: 49.9 GB, 18756927488 bytes, 36634624 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
- 進(jìn)行關(guān)機(jī)擴(kuò)大硬盤企软,然后開機(jī)進(jìn)入系統(tǒng)
shutdown -h now
- 確認(rèn)硬盤已經(jīng)進(jìn)行擴(kuò)容,可以看到已經(jīng)擴(kuò)容饭望,但還沒進(jìn)行使用
[root@Allenwu ~]# fdisk -l
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: 0x0009c3d0
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
/dev/sda3 41943040 104857599 31457280 83 Linux
Disk /dev/mapper/centos-root: 49.9 GB, 49895440384 bytes, 97452032 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
- 使用 fdisk 進(jìn)行分區(qū)仗哨,創(chuàng)建一個(gè)sda4主分區(qū)
[root@Allenwu ~]# 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 (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
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 4 of type Linux and of size 50 GiB is set
Command (m for help): t
Partition number (1-4, default 4): 4
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): 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.
- 看到提示需要重啟系統(tǒng),執(zhí)行下面命令進(jìn)行重啟
shutdown -r now
- 等待啟動(dòng)啟動(dòng)完成杰妓,使用
df -Th
查看剛剛分區(qū)存在 sda4 這個(gè)分區(qū)藻治,并且可以看出根目錄/
使用的文件系統(tǒng)是 xfs 文件類型的,所以使用mkfs.xfs
命令進(jìn)行格式化巷挥,如果你系統(tǒng)使用的是 ext3或ext4 文件系統(tǒng)桩卵,需要根據(jù)自己情況進(jìn)行調(diào)整
[root@Allenwu ~]# mkfs.xfs /dev/sda4
meta-data=/dev/sda4 isize=256 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=l
= crc=0 finobt=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=l
realtime =none extsz=4096 blocks=0, rtextents=0
- 將物理硬盤分區(qū)初始化為物理卷,以便被LVM使用倍宾,輸入指令
[root@Allenwu ~]# pvcreate /dev/sda4
WARNING: xfs signature detected on /dev/sda4 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sda4 .
Physical volume "/dev/sda4" successfully created
- 向卷組中添加物理卷來增加卷組的容量
#先查看一下卷組的名字雏节,VG Name : centos
[root@Allenwu ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
.......
##進(jìn)行增加容量操作
[root@Allenwu ~]# vgextend centos /dev/sda4
Volume group "centos" successfully extended
- 再進(jìn)行查看vg組信息,有一塊空間是 Free
[root@Allenwu ~]# vgex
vgexport vgextend
[root@Allenwu ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 99.50 GiB
PE Size 4.00 MiB
Total PE 25472
Alloc PE / Size 12408 / 48.47 GiB
Free PE / Size 13064 / 51.03 GiB
VG UUID cUaGz1-pvaD-Bymp-t7ot-NC87-Eyb6-e4S73r
- 執(zhí)行擴(kuò)展命令 lvextend -L+49G /dev/mapper/centos-root /dev/sda4
[root@Allenwu ~]# lvextend -L+49G /dev/mapper/centos-root /dev/sda4
Size of logical volume centos/root changed from 46.47 GiB (11896 extents) to 95.47 GiB (24440 extents)
Logical volume root successfully resized
- 做了那么多步驟高职,使用命令 e2fsck 進(jìn)行檢查一下文件系統(tǒng)
[root@Allenwu ~]# e2fsck -a /dev/mapper/centos-root
- 前面的步驟都弄好無誤后钩乍,現(xiàn)在需要進(jìn)行增大硬盤,使用命令 xfs_growfs 命令可以來增大或者收縮未加載的
xfs
文件系統(tǒng)大小
[root@Allenwu ~]# xfs_growfs /dev/mapper/centos-root
注意:如果你系統(tǒng)使用文件系統(tǒng)是ext2怔锌、ext3寥粹、ext4,需要把 xfs_growfs 換成 resize2fs
- 查看現(xiàn)在硬盤大小埃元,發(fā)現(xiàn)已經(jīng)成功增加了硬盤
[root@Allenwu ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 96G 918M 95G 1% /
devtmpfs devtmpfs 481M 0 481M 0% /dev
tmpfs tmpfs 490M 0 490M 0% /dev/shm
tmpfs tmpfs 490M 6.6M 484M 2% /run
tmpfs tmpfs 490M 0 490M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 120M 378M 25% /boot
- 以上所有環(huán)節(jié)弄完以后涝涤,代表你已經(jīng)成功擴(kuò)大了根分區(qū)硬盤容量,你需要檢查的是 /etc/fstab 掛載硬盤配置正不正確等問題岛杀,如果服務(wù)器沒在跑業(yè)務(wù)阔拳,建議進(jìn)行重啟確保重啟后能夠正常進(jìn)行運(yùn)行