因为linux使用了lvm进行磁盘空间的组织与管理,有些类似于raid0。
磁盘空间在使用上直接面对的是逻辑卷,而逻辑卷则又使用卷组管理物理卷,物理卷就是直接创建在物理磁盘上的空间。
所以除了系统文件所在的根目录之外,所有挂载的数据目录都是可以动态调整的,只要加入新的磁盘空间,就可以对逻辑卷进行缩放。
而系统所在的根目录,理论上只能扩容,无法进行缩容,因为磁盘空间一旦加入进去就被完全占用了,无法释放。
Step.1 物理磁盘分区
先查看系统上安装的磁盘情况。
[root@0001 server1]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 50G 46G 4.5G 92% /devtmpfs 16G 0 16G 0% /devtmpfs 16G 0 16G 0% /dev/shmtmpfs 16G 1.6G 15G 11% /runtmpfs 16G 0 16G 0% /sys/fs/cgroup/dev/vda1 1014M 169M 846M 17% /boot/dev/mapper/centos-home 42G 324M 41G 1% /hometmpfs 3.2G 0 3.2G 0% /run/user/1001tmpfs 3.2G 0 3.2G 0% /run/user/1003tmpfs 3.2G 0 3.2G 0% /run/user/0[root@0001 server1]# fdisk -lDisk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000d5b31Device Boot Start End Blocks Id System/dev/vda1 * 2048 2099199 1048576 83 Linux/dev/vda2 2099200 209715199 103808000 8e Linux LVMDisk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 8455 MB, 8455716864 bytes, 16515072 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-home: 44.1 GB, 44149243904 bytes, 86228992 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
发现有一块磁盘 /dev/vdb 没有使用。接下来对物理磁盘进行格式化与分区。
[root@0001 server1]# fdisk /dev/vdbWelcome 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.Device does not contain a recognized partition tableBuilding a new DOS disklabel with disk identifier 0xea2ef8f6.Command (m for help): nPartition type:p primary (0 primary, 0 extended, 4 free)e extendedSelect (default p):Using default response pPartition number (1-4, default 1): First sector (2048-209715199, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): Using default value 209715199Partition 1 of type Linux and of size 100 GiB is setCommand (m for help): tSelected partition 1Hex code (type L to list all codes): 8eChanged type of partition 'Linux' to 'Linux LVM'Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.
Step.2 操作LVM
[root@0001 server1]# fdisk -lDisk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000d5b31Device Boot Start End Blocks Id System/dev/vda1 * 2048 2099199 1048576 83 Linux/dev/vda2 2099200 209715199 103808000 8e Linux LVMDisk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xea2ef8f6Device Boot Start End Blocks Id System/dev/vdb1 2048 209715199 104856576 8e Linux LVMDisk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-swap: 8455 MB, 8455716864 bytes, 16515072 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/centos-home: 44.1 GB, 44149243904 bytes, 86228992 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes[root@0001 server1]# pvcreate /dev/vdb1Physical volume "/dev/vdb1" successfully created.[root@0001 server1]# pvdisplay--- Physical volume ---PV Name /dev/vda2VG Name centosPV Size <99.00 GiB / not usable 3.00 MiBAllocatable yesPE Size 4.00 MiBTotal PE 25343Free PE 1Allocated PE 25342PV UUID D3atmG-vYwp-asY9-UCMy-L5LZ-JgH6-CrYXdl"/dev/vdb1" is a new physical volume of "<100.00 GiB"--- NEW Physical volume ---PV Name /dev/vdb1VG NamePV Size <100.00 GiBAllocatable NOPE Size 0Total PE 0Free PE 0Allocated PE 0PV UUID BSpZvK-jM8r-fYDo-XsuS-huoa-NMsd-EdnSSw[root@0001 server1]# vgdisplay--- Volume group ---VG Name centosSystem IDFormat lvm2Metadata Areas 1Metadata Sequence No 4VG Access read/writeVG Status resizableMAX LV 0Cur LV 3Open LV 3Max PV 0Cur PV 1Act PV 1VG Size <99.00 GiBPE Size 4.00 MiBTotal PE 25343Alloc PE / Size 25342 / 98.99 GiBFree PE / Size 1 / 4.00 MiBVG UUID kZrifW-oQKr-6ezp-J1fz-l704-BHCs-QZhIRq[root@0001 server1]# vgextend centos /dev/vdb1Volume group "centos" successfully extended[root@0001 server1]# vgdisplay--- Volume group ---VG Name centosSystem IDFormat lvm2Metadata Areas 2Metadata Sequence No 5VG Access read/writeVG Status resizableMAX LV 0Cur LV 3Open LV 3Max PV 0Cur PV 2Act PV 2VG Size 198.99 GiBPE Size 4.00 MiBTotal PE 50942Alloc PE / Size 25342 / 98.99 GiBFree PE / Size 25600 / 100.00 GiBVG UUID kZrifW-oQKr-6ezp-J1fz-l704-BHCs-QZhIRq[root@0001 server1]# lvextend -l +100%FREE /dev/mapper/centos-root /dev/vdb1Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to <150.00 GiB (38399 extents).Logical volume centos/root successfully resized.[root@0001 server1]# xfs_growfs /dev/mapper/centos-rootmeta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=13107200, imaxpct=25= sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=6400, version=2= sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0data blocks changed from 13107200 to 39320576[root@0001 server1]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 150G 46G 105G 31% /devtmpfs 16G 0 16G 0% /devtmpfs 16G 0 16G 0% /dev/shmtmpfs 16G 1.6G 15G 11% /runtmpfs 16G 0 16G 0% /sys/fs/cgroup/dev/vda1 1014M 169M 846M 17% /boot/dev/mapper/centos-home 42G 324M 41G 1% /hometmpfs 3.2G 0 3.2G 0% /run/user/1001tmpfs 3.2G 0 3.2G 0% /run/user/1003tmpfs 3.2G 0 3.2G 0% /run/user/0
