Linux 磁盘扩容
注意:针对kvm,vnware架构的云服务器,不要轻易动系统盘,如果系统盘空间不足,可以扩容数据盘,挂在到相应的系统盘目录下(做好数据备份),对于数据盘最好做成逻辑卷挂在使用,避免后期业务需求在线扩容。
一,对数据盘分区
#fdisk /dev/vdb
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-75497471, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-75497471, default 75497471): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/vdb: 38.7 GB, 38654705664 bytes, 75497472 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: 0x0d1d7a83
Device Boot Start End Blocks Id System
/dev/vdb1 2048 20973567 10485760 83 Linux
记得w保存
二,1,物理卷
pvcreate /dev/vdb1
Physical volume “/dev/vdb1” successfully created.
2,卷组
vgcreate vg01 /dev/vdb1
Volume group “vg01” successfully created
3逻辑卷
lvcreate -n lv01 -L 9.8G vg01
4、格式化文件系统
mkfs -t ext4 /dev/vg01/lv01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
643376 inodes, 2569216 blocks
128460 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
79 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
5,创建挂载点
mkdir /u01
[root@hexian ~]# mount /dev/vg01/lv01 /u01 临时挂在,永久挂载的话写到/etc/fstab
[root@hexian ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.6G 36G 5% /
devtmpfs 912M 32K 912M 1% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 320K 920M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg01-lv01 9.6G 37M 9.0G 1% /u01
当空间不足时,可以扩容数据盘,在分区创建物理卷,扩容上述卷组,再而扩容逻辑卷,满足业务需求。(不能之间,避免造成文件系统的损坏),如果有损坏,可以用fsck修复,不一定能修好,所以轻易不要在用的服务器上直接操作,测试没问题方可操作。
root@hexian ~]# fdisk /dev/vdb
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): p
Disk /dev/vdb: 38.7 GB, 38654705664 bytes, 75497472 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: 0x0d1d7a83
Device Boot Start End Blocks Id System
/dev/vdb1 2048 20973567 10485760 83 Linux
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (20973568-75497471, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-75497471, default 75497471):
Using default value 75497471
Partition 2 of type Linux and of size 26 GiB is set
Command (m for help): p
Disk /dev/vdb: 38.7 GB, 38654705664 bytes, 75497472 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: 0x0d1d7a83
Device Boot Start End Blocks Id System
/dev/vdb1 2048 20973567 10485760 83 Linux
/dev/vdb2 20973568 75497471 27261952 83 Linux
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.
三1,做物理卷
pvcreate /dev/vdb2
2,扩容卷组
#vgextend vg01 /dev/vdb2
Volume group “vg01” successfully extended
3,扩容逻辑卷
[root@hexian ~]# lvextend -L +10G /dev/mapper/vg01-lv01
Size of logical volume vg01/lv01 changed from 9.80 GiB (2509 extents) to 19.80 GiB (5069 extents).
Logical volume vg01/lv01 successfully resized.
4,同步文件系统
[root@hexian ~]# lvextend -L +10G /dev/mapper/vg01-lv01
Size of logical volume vg01/lv01 changed from 9.80 GiB (2509 extents) to 19.80 GiB (5069 extents).
Logical volume vg01/lv01 successfully resized.
[root@hexian ~]# resize2fs /dev/vg01/lv01
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg01/lv01 is mounted on /u01; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/vg01/lv01 is now 5190656 blocks long.
[root@hexian ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.6G 36G 5% /
devtmpfs 912M 32K 912M 1% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 380K 920M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/vg01-lv01 20G 44M 19G 1% /u01
/u01增加到了20G