linux 系统扩容 VMware Centos---VMware ESXi
用到的命令
df fdisk pvcreate pvdisplay vgdisplay vgextend lvdisplay lvextend resize2fs
01) 使用了一段时间虚拟机后发现磁盘不够用了,需要扩容。在客户端操作扩容出现磁盘已成功扩展。
您必须从客户机操作系统内部对磁盘重新进行分区和扩展文件系统。
然后进centos7
系统进行操作
02) 使用 df -h
查看磁盘空间大小
### 挂载跟目录节点的 /dev/mapper/centos-root 只有23容量 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 23G 23G 20K 100% / devtmpfs 20G 0 20G 0% /dev tmpfs 20G 4.0K 20G 1% /dev/shm tmpfs 20G 8.5M 20G 1% /run tmpfs 20G 0 20G 0% /sys/fs/cgroup /dev/sda1 497M 120M 378M 25% /boot [root@localhost ~]#
03) 使用 fdisk -l
命令查看磁盘信息
1 ### 第一行 Disk /dev/sda: 107.4 GB 与实际 df -好现实内容不符,说明增加磁盘成功 2 3 [root@localhost ~]# fdisk -l 4 5 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors ---与上面(df -h)不符,说明新增成功 6 Units = sectors of 1 * 512 = 512 bytes 7 Sector size (logical/physical): 512 bytes / 512 bytes 8 I/O size (minimum/optimal): 512 bytes / 512 bytes 9 Disk label type: dos 10 Disk identifier: 0x0004148c 11 12 Device Boot Start End Blocks Id System 13 /dev/sda1 * 2048 1026047 512000 83 Linux 14 /dev/sda2 1026048 41943039 20458496 8e Linux LVM 15 /dev/sda3 41943040 52428799 5242880 8e Linux LVM 16 17 Disk /dev/mapper/centos-root: 24.1 GB, 24121442304 bytes, 47112192 sectors 18 Units = sectors of 1 * 512 = 512 bytes 19 Sector size (logical/physical): 512 bytes / 512 bytes 20 I/O size (minimum/optimal): 512 bytes / 512 bytes 21 22 23 Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors 24 Units = sectors of 1 * 512 = 512 bytes 25 Sector size (logical/physical): 512 bytes / 512 bytes 26 I/O size (minimum/optimal): 512 bytes / 512 bytes 27 28 [root@localhost ~]#
04) 使用 fdisk /dev/sda
创建新的分区
1 ### 注意:不同操作系统的磁盘命名方式不同,有些是/dev/vda。 2 ### 具体使用方式参考第三步中fdisk -l首行显示Disk。 3 ### 如笔者在该系统中首行显示为Disk /dev/sda: 107.4 GB, 107374182400 bytes ,故使用命令fdisk /dev/sda) 4 5 6 [root@localhost ~]# fdisk /dev/sda 7 Welcome to fdisk (util-linux 2.23.2). 8 9 Changes will remain in memory only, until you decide to write them. 10 Be careful before using the write command. 11 12 13 Command (m for help): m ---------#帮助命令 14 Command action 15 a toggle a bootable flag 16 b edit bsd disklabel 17 c toggle the dos compatibility flag 18 d delete a partition 19 g create a new empty GPT partition table 20 G create an IRIX (SGI) partition table 21 l list known partition types 22 m print this menu 23 n add a new partition 24 o create a new empty DOS partition table 25 p print the partition table 26 q quit without saving changes 27 s create a new empty Sun disklabel 28 t change a partition's system id 29 u change display/entry units 30 v verify the partition table 31 w write table to disk and exit 32 x extra functionality (experts only) 33 34 Command (m for help): n -----------#创建新分区 35 Partition type: 36 p primary (3 primary, 0 extended, 1 free) 37 e extended 38 Select (default e): p -------#选择主分区 39 Selected partition 4 40 First sector (52428800-209715199, default 52428800): ---#回车使用默认 41 Using default value 52428800 42 Last sector, +sectors or +size{K,M,G} (52428800-209715199, default 209715199): ---#回车使用默认 43 Using default value 209715199 44 Partition 4 of type Linux and of size 75 GiB is set 45 46 47 Command (m for help): t --------------#修改分区类型 48 Partition number (1-4, default 4): ----------#回车使用默认值 49 Hex code (type L to list all codes): 8e -----#说明: 8e 是lvm磁盘类型 50 Changed type of partition 'Linux' to 'Linux LVM' 51 52 53 54 Command (m for help): p ------#打印分区表 55 56 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors 57 Units = sectors of 1 * 512 = 512 bytes 58 Sector size (logical/physical): 512 bytes / 512 bytes 59 I/O size (minimum/optimal): 512 bytes / 512 bytes 60 Disk label type: dos 61 Disk identifier: 0x0004148c 62 63 Device Boot Start End Blocks Id System 64 /dev/sda1 * 2048 1026047 512000 83 Linux 65 /dev/sda2 1026048 41943039 20458496 8e Linux LVM 66 /dev/sda3 41943040 52428799 5242880 8e Linux LVM 67 /dev/sda4 52428800 209715199 78643200 8e Linux LVM 68 69 Command (m for help): w -------#保存退出 70 The partition table has been altered! 71 72 Calling ioctl() to re-read partition table. 73 74 WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 75 The kernel still uses the old table. The new table will be used at 76 the next reboot or after you run partprobe(8) or kpartx(8) ---#这里提示重启系统 77 Syncing disks
05) 重启系统
[root@localhost ~]# reboot
06) 查看磁盘情况
1 ### 核对刚才所做的分区操作是否保存成功 2 3 [root@localhost ~]# fdisk -l 4 5 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors 6 Units = sectors of 1 * 512 = 512 bytes 7 Sector size (logical/physical): 512 bytes / 512 bytes 8 I/O size (minimum/optimal): 512 bytes / 512 bytes 9 Disk label type: dos 10 Disk identifier: 0x0004148c 11 12 Device Boot Start End Blocks Id System 13 /dev/sda1 * 2048 1026047 512000 83 Linux 14 /dev/sda2 1026048 41943039 20458496 8e Linux LVM 15 /dev/sda3 41943040 52428799 5242880 8e Linux LVM 16 /dev/sda4 52428800 209715199 78643200 8e Linux LVM ----#这里多出一块,说明保存成功 17 18 Disk /dev/mapper/centos-root: 24.1 GB, 24121442304 bytes, 47112192 sectors 19 Units = sectors of 1 * 512 = 512 bytes 20 Sector size (logical/physical): 512 bytes / 512 bytes 21 I/O size (minimum/optimal): 512 bytes / 512 bytes 22 23 24 Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors 25 Units = sectors of 1 * 512 = 512 bytes 26 Sector size (logical/physical): 512 bytes / 512 bytes 27 I/O size (minimum/optimal): 512 bytes / 512 bytes 28 29 [root@localhost ~]#
07) 查看当前分区类型( 这里的类型会影响最后一步调整文件系统大小命令 xfs_growfs 还是 resize2fs )
[root@localhost ~]# df -T /dev/sda1 Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sda1 xfs 508588 122080 386508 25% /boot [root@localhost ~]#
08) 使用 pvcreate /dev/sda4
创建物理卷
1 ### /dev/sda4 这里的4是上面刚才新增的分区 2 [root@localhost ~]# pvcreate /dev/sda4 3 Physical volume "/dev/sda4" successfully created 4 [root@localhost ~]#
09) 使用pvdisplay
查看新建的物理卷大小
1 [root@localhost ~]# pvdisplay 2 --- Physical volume --- 3 PV Name /dev/sda2 4 VG Name centos 5 PV Size 19.51 GiB / not usable 3.00 MiB 6 Allocatable yes (but full) 7 PE Size 4.00 MiB 8 Total PE 4994 9 Free PE 0 10 Allocated PE 4994 11 PV UUID 7q1DBZ-n5ZX-1OPX-IN9w-SdAp-qekM-BfpVbc 12 13 --- Physical volume --- 14 PV Name /dev/sda3 15 VG Name centos 16 PV Size 5.00 GiB / not usable 4.00 MiB 17 Allocatable yes 18 PE Size 4.00 MiB 19 Total PE 1279 20 Free PE 10 21 Allocated PE 1269 22 PV UUID eOLd2K-aXSx-cA1M-hwJc-zqdq-7U4Y-ZE8d2R 23 24 "/dev/sda4" is a new physical volume of "75.00 GiB" ----------#可以看到新增的75G 25 --- NEW Physical volume --- 26 PV Name /dev/sda4 27 VG Name 28 PV Size 75.00 GiB 29 Allocatable NO 30 PE Size 0 31 Total PE 0 32 Free PE 0 --------#现在这里看到的是0,在 lvextend 的时候还要重新查看 33 Allocated PE 0 34 PV UUID wyG3ZO-e1a0-3OHW-6afy-u90w-7Lc8-ddMnJL 35 36 [root@localhost ~]#
10) 使用 vgdisplay
查看 vg
状态
1 [root@localhost ~]# vgdisplay 2 --- Volume group --- 3 VG Name centos --------------#这个名字在下面会用到 4 System ID 5 Format lvm2 6 Metadata Areas 2 7 Metadata Sequence No 5 8 VG Access read/write 9 VG Status resizable 10 MAX LV 0 11 Cur LV 2 12 Open LV 2 13 Max PV 0 14 Cur PV 2 15 Act PV 2 16 VG Size 24.50 GiB 17 PE Size 4.00 MiB 18 Total PE 6273 19 Alloc PE / Size 6263 / 24.46 GiB 20 Free PE / Size 10 / 40.00 MiB 21 VG UUID q6Omkk-sEkn-Ex9Y-gfSY-0URm-4z2E-0u9Euj 22 23 [root@localhost ~]#
11) 使用 vgextend centos /dev/sda4
将添加新的物理卷加载到 centos
卷组
1 ### centos 是我的服务器vg名称,可以通过vgdisplay查看vg状态得到名称 2 ### /dev/sda4 是我刚刚新增的物理卷 3 [root@localhost ~]# vgextend centos /dev/sda4 4 Volume group "centos" successfully extended 5 [root@localhost ~]# 6 7 ###---------- 注意这里可以由于系统空间不足出现报错 ------------ 8 ###---------- 手动去删除些没有用的文件 ------------ 9 ### 报错内容 10 ### [root@localhost ~]# vgextend centos /dev/sda4 11 ### Couldn't create temporary archive name. 12 ### Volume group "centos" metadata archive failed. 13 ### Internal error: Attempt to unlock unlocked VG #orphans. 14 ### [root@localhost ~]# 15 ### 16 17 [root@localhost ~]# vgextend centos /dev/sda4 18 Couldn't create temporary archive name. 19 Volume group "centos" metadata archive failed. 20 Internal error: Attempt to unlock unlocked VG #orphans. 21 [root@localhost ~]#
12) 使用 lvdisplay
查看 lv
状态
1 [root@localhost ~]# lvdisplay 2 --- Logical volume --- 3 LV Path /dev/centos/swap 4 LV Name swap 5 VG Name centos 6 LV UUID d39rhF-bGNs-fWPE-ItwX-FNQe-QJ4h-taAm94 7 LV Write Access read/write 8 LV Creation host, time localhost, 2019-05-09 03:42:09 -0400 9 LV Status available 10 # open 2 11 LV Size 2.00 GiB 12 Current LE 512 13 Segments 1 14 Allocation inherit 15 Read ahead sectors auto 16 - currently set to 8192 17 Block device 253:1 18 19 --- Logical volume --- 20 LV Path /dev/centos/root 21 LV Name root 22 VG Name centos 23 LV UUID rvepaR-xSYs-hPOy-vPWA-Oiuh-A0Je-j29Gk1 24 LV Write Access read/write 25 LV Creation host, time localhost, 2019-05-09 03:42:10 -0400 26 LV Status available 27 # open 1 28 LV Size 22.46 GiB 29 Current LE 5751 30 Segments 2 31 Allocation inherit 32 Read ahead sectors auto 33 - currently set to 8192 34 Block device 253:0 35 36 [root@localhost ~]#
13) 使用 lvextend
增加容量
1 ## 使用 df -h 可以看到 /dev/mapper/centos-root 的名字 2 ## 使用 pvdisplay 再次查看 free 的PE数量 3 [root@localhost data]# df -h 4 Filesystem Size Used Avail Use% Mounted on 5 /dev/mapper/centos-root 23G 23G 52K 100% / -------# /dev/mapper/centos-root 这个名字要用 6 devtmpfs 20G 0 20G 0% /dev 7 tmpfs 20G 4.0K 20G 1% /dev/shm 8 tmpfs 20G 8.5M 20G 1% /run 9 tmpfs 20G 0 20G 0% /sys/fs/cgroup 10 /dev/sda1 497M 120M 378M 25% /boot 11 [root@localhost data]# 12 [root@localhost data]# pvdisplay 13 --- Physical volume --- 14 PV Name /dev/sda2 15 VG Name centos 16 PV Size 19.51 GiB / not usable 3.00 MiB 17 Allocatable yes (but full) 18 PE Size 4.00 MiB 19 Total PE 4994 20 Free PE 0 21 Allocated PE 4994 22 PV UUID 7q1DBZ-n5ZX-1OPX-IN9w-SdAp-qekM-BfpVbc 23 24 --- Physical volume --- 25 PV Name /dev/sda3 26 VG Name centos 27 PV Size 5.00 GiB / not usable 4.00 MiB 28 Allocatable yes 29 PE Size 4.00 MiB 30 Total PE 1279 31 Free PE 10 32 Allocated PE 1269 33 PV UUID eOLd2K-aXSx-cA1M-hwJc-zqdq-7U4Y-ZE8d2R 34 35 --- Physical volume --- 36 PV Name /dev/sda4 37 VG Name centos 38 PV Size 75.00 GiB / not usable 4.00 MiB 39 Allocatable yes 40 PE Size 4.00 MiB 41 Total PE 19199 42 Free PE 19199 --------#这个大小要用 43 Allocated PE 0 44 PV UUID wyG3ZO-e1a0-3OHW-6afy-u90w-7Lc8-ddMnJL 45 46 [root@localhost data]# 47 48 49 ###---------- 注意这里可以由于系统空间不足出现报错 ------------ 50 ###---------- 手动去删除些没有用的文件 ------------ 51 ### 报错内容 52 ### [root@localhost ~]# lvextend -l +19199 /dev/mapper/centos-root 53 ### Couldn't create temporary archive name. 54 ### Volume group "centos" metadata archive failed. 55 ### [root@localhost ~]# 56 ### 57 [root@localhost ~]# lvextend -l +19199 /dev/mapper/centos-root 58 Size of logical volume centos/root changed from 22.46 GiB (5751 extents) to 97.46 GiB (24950 extents). 59 Logical volume root successfully resized 60 [root@localhost ~]#
14-01)
1 ### 我的是 xfs 类型(centos7系统) 2 [root@localhost ~]# df -T /dev/sda1 3 Filesystem Type 1K-blocks Used Available Use% Mounted on 4 /dev/sda1 xfs 508588 122080 386508 25% /boot 5 [root@localhost ~]# 6 7 ###-----------或者使用 8 [root@localhost ~]# cat /etc/fstab | grep centos-root 9 /dev/mapper/centos-root / xfs defaults 0 0 10 [root@localhost ~]#
14-02) 使用 xfs_growfs
(centos7)重新调整文件系统大小
1 ### centos7中用 xfs_growfs 2 [root@localhost ~]# xfs_growfs /dev/mapper/centos-root 3 meta-data=/dev/mapper/centos-root isize=256 agcount=6, agsize=1144832 blks 4 = sectsz=512 attr=2, projid32bit=1 5 = crc=0 finobt=0 6 data = bsize=4096 blocks=5889024, imaxpct=25 7 = sunit=0 swidth=0 blks 8 naming =version 2 bsize=4096 ascii-ci=0 ftype=0 9 log =internal bsize=4096 blocks=2560, version=2 10 = sectsz=512 sunit=0 blks, lazy-count=1 11 realtime =none extsz=4096 blocks=0, rtextents=0 12 data blocks changed from 5889024 to 25548800 13 [root@localhost ~]# 14 15 ### centos6中用 resize2fs 16 [root@localhost ~]# resize2fs /dev/mapper/centos-root
15) 最后一步检查磁盘空间,确认是否新增成功
1 root@localhost ~]# df -h 2 Filesystem Size Used Avail Use% Mounted on 3 /dev/mapper/centos-root 98G 23G 75G 24% / ----#新增成功 4 devtmpfs 20G 0 20G 0% /dev 5 tmpfs 20G 4.0K 20G 1% /dev/shm 6 tmpfs 20G 8.5M 20G 1% /run 7 tmpfs 20G 0 20G 0% /sys/fs/cgroup 8 /dev/sda1 497M 120M 378M 25% /boot 9 [root@localhost ~]#
其他参考文章: