Linux:CentOS 6.0
ESXi:6.7
原硬盘空间为40G,在VM上增加10G空间到50G,如下硬盘扩容操作过程。
#扩容前查看硬盘使用空间为39G,已使用100%.
[root@EZ cacti]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cactiezv10-LogVol00 39G 39G 0 100% / tmpfs 1004M 0 1004M 0% /dev/shm /dev/sda1 194M 25M 160M 14% /boot
#ESXi中增加硬盘空间后,如下操作。
1,查看当前待扩容硬盘状态。 [root@EZ ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00089c87
Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 5222 41737216 8e Linux LVM
Disk /dev/dm-0: 41.7 GB, 41662021632 bytes 255 heads, 63 sectors/track, 5065 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
Disk /dev/dm-0 doesn't contain a valid partition table
Disk /dev/dm-1: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
Disk /dev/dm-1 doesn't contain a valid partition table
2,进行分区操作。
[root@EZ ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u').
Command (m for help): n (新建分区) Command action e extended p primary partition (1-4)p (主分区) Partition number (1-4): 3 (选择默认或指定分区号,默认起始位置和结束位置(全盘)) First cylinder (5222-6527, default 5222): Using default value 5222 Last cylinder, +cylinders or +size{K,M,G} (5222-6527, default 6527): Using default value 6527
Command (m for help): t (分区类型 -> 8e (LVM)) Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): wq (写入分区表,退出) 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. [root@EZ ~]#
3,查看硬盘状况,磁盘空间已增加10G,新增加的分区sda3已显示正常。
[root@EZ ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00089c87
Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 5222 41737216 8e Linux LVM/dev/sda3 5222 6527 10485087+ 8e Linux LVM
Disk /dev/dm-0: 41.7 GB, 41662021632 bytes 255 heads, 63 sectors/track, 5065 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
Disk /dev/dm-0 doesn't contain a valid partition table
Disk /dev/dm-1: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
Disk /dev/dm-1 doesn't contain a valid partition table
[root@EZ ~]#
4,用partprobe重新读取分区信息
ps:使用fdisk工具只是将分区信息写到磁盘,如果需要mkfs磁盘分区则需要重启系统,而使用partprobe则可以使kernel重新读取分区信息,从而避免重启系统,如果没有此命令,yum安装或者重新启动系统即可。
[root@EZ ~]# partprobe /dev/sda3 Physical volume "/dev/sda3" successfully created
5,创建物理卷
[root@EZ ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created [root@EZ ~]#
6,扩容逻辑卷(如果不知道卷名称,可以使用vgdisplay查看)
[root@EZ ~]# vgextend vg_cactiezv10 /dev/sda3 Volume group "vg_cactiezv10" successfully extended [root@EZ ~]#
[root@EZ ~]# vgdisplay --- Volume group --- VG Name vg_cactiezv10 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 39.80 GiB PE Size 4.00 MiB Total PE 10189 Alloc PE / Size 10189 / 39.80 GiB Free PE / Size 0 / 0 VG UUID TP4Tgy-mgCh-tJpO-ck7R-iT0a-5EZD-2G7uIF
#查看物理卷信息,可看到创建物理卷成功后剩余磁盘空间。 [root@EZ ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_cactiezv10 lvm2 a- 39.80g 0 /dev/sda3 lvm2 a- 10.00g 10.00g
7,开始正式扩容逻辑卷(第一次直接增加10G不成功,零星8G+1.8G+200M增加进去,使用lvextend命令也可以)
[root@EZ ~]# lvresize -L +10G /dev/mapper/vg_cactiezv10-LogVol00 Extending logical volume LogVol00 to 48.80 GiB Insufficient free space: 2560 extents needed, but only 2559 available [root@EZ ~]#
[root@EZ ~]# lvresize -L +8G /dev/mapper/vg_cactiezv10-LogVol00 Extending logical volume LogVol00 to 46.80 GiB Logical volume LogVol00 successfully resized [root@EZ ~]# [root@EZ ~]# lvresize -L +1.8G /dev/mapper/vg_cactiezv10-LogVol00 Rounding up size to full physical extent 1.80 GiB Extending logical volume LogVol00 to 48.60 GiB Logical volume LogVol00 successfully resized
[root@EZ ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_cactiezv10 lvm2 a- 39.80g 0 /dev/sda3 vg_cactiezv10 lvm2 a- 10.00g 200.00m
[root@EZ ~]# lvresize -L +200M /dev/mapper/vg_cactiezv10-LogVol00 Extending logical volume LogVol00 to 48.80 GiB Logical volume LogVol00 successfully resized
[root@EZ ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_cactiezv10 lvm2 a- 39.80g 0 /dev/sda3 vg_cactiezv10 lvm2 a- 10.00g 0
8,使用resizefs2命令重新加载逻辑卷的大小才能生效
[root@EZ ~]# resize2fs /dev/mapper/vg_cactiezv10-LogVol00 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/vg_cactiezv10-LogVol00 is mounted on /; on-line resizing required old desc_blocks = 3, new_desc_blocks = 4 Performing an on-line resize of /dev/mapper/vg_cactiezv10-LogVol00 to 12740608 (4k) blocks. The filesystem on /dev/mapper/vg_cactiezv10-LogVol00 is now 12740608 blocks long.
[root@EZ ~]#
9,查看磁盘空间情况,已扩容成功。 [root@EZ ~]# df -h Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg_cactiezv10-LogVol00 48G 40G 5.5G 88% / tmpfs 1004M 0 1004M 0% /dev/shm /dev/sda1 194M 25M 160M 14% /boot [root@EZ ~]#
比较resize2fs与xfs_growfs的不同与相同点
resize2fs
调整ext2\ext3\ext4文件系统的大小,它可以放大或者缩小没有挂载的文件系统的大小。如果文件系统已经挂载,它可以扩大文件系统的大小,前提是内核支持在线调整大小。
扩容/目录后,df -h查看,却没有扩容到。重启也不行,百度说umount目录重新扩容试试,但根目录不可能umout的。
具体再百度发现,(由于CentOS6和CentOS7在默认根文件系统的文件系统格式存在差异,需要判断是否为xfs,如果是xfs则应该使用xfs_growfs而不是一味的使用resize2fs。)
Linux7文件系统格式是xfs,resize2fs命令是读取不了的。
所以要用xfs_growfs来同步文件系统。
[root@csvn ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 484M 0 484M 0% /dev tmpfs 496M 0 496M 0% /dev/shm tmpfs 496M 7.1M 489M 2% /run tmpfs 496M 0 496M 0% /sys/fs/cgroup /dev/mapper/centos-root 4.9G 2.9G 2.1G 59% / /dev/sdb1 4.8G 21M 4.6G 1% /data /dev/sda1 497M 154M 344M 31% /boot tmpfs 100M 0 100M 0% /run/user/0 tmpfs 100M 0 100M 0% /run/user/1001 [root@csvn ~]# [root@csvn ~]# [root@csvn ~]# [root@csvn ~]# xfs_info /dev/centos/root meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=318976 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 spinodes=0 data = bsize=4096 blocks=1275904, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@csvn ~]#
用xfs_growfs来同步文件系统
[root@csvn ~]# xfs_growfs /dev/centos/root meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=318976 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 spinodes=0 data = bsize=4096 blocks=1275904, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 1275904 to 2324480 [root@csvn ~]# [root@csvn ~]# [root@csvn ~]# [root@csvn ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 484M 0 484M 0% /dev tmpfs 496M 0 496M 0% /dev/shm tmpfs 496M 7.1M 489M 2% /run tmpfs 496M 0 496M 0% /sys/fs/cgroup /dev/mapper/centos-root 8.9G 2.9G 6.1G 33% / /dev/sdb1 4.8G 21M 4.6G 1% /data /dev/sda1 497M 154M 344M 31% /boot tmpfs 100M 0 100M 0% /run/user/0 tmpfs 100M 0 100M 0% /run/user/1001 [root@csvn ~]# 可看到已成功扩容根目录。
附录:
xfs_admin: 调整 xfs 文件系统的各种参数
xfs_copy: 拷贝 xfs 文件系统的内容到一个或多个目标系统(并行方式)
xfs_db: 调试或检测 xfs 文件系统(查看文件系统碎片等)
xfs_check: 检测 xfs 文件系统的完整性
xfs_bmap: 查看一个文件的块映射
xfs_repair: 尝试修复受损的 xfs 文件系统
xfs_fsr: 碎片整理
xfs_quota: 管理 xfs 文件系统的磁盘配额
xfs_metadump: 将 xfs 文件系统的元数据 (metadata) 拷贝到一个文件中
xfs_mdrestore: 从一个文件中将元数据 (metadata) 恢复到 xfs 文件系统
xfs_growfs: 调整一个 xfs 文件系统大小(只能扩展)
xfs_freeze 暂停(-f)和恢复(-u)xfs 文件系统
xfs_logprint: 打印xfs文件系统的日志
xfs_mkfile: 创建xfs文件系统
xfs_info: 查询文件系统详细信息
xfs_ncheck: generate pathnames from i-numbers for XFS
xfs_rtcp: XFS实时拷贝命令
xfs_io: 调试xfs I/O路径