Linux Centos虚拟机扩容(/dev/mapper/centos-root)
场景
我们在使用linux虚拟机时会经常出现空间不够用的情况,下面主要分享下如何增加VMWare虚拟机(Linux)的根目录磁盘空间
实践
1、在虚拟机软件上,为对应的虚拟机扩充20G硬盘空间.
2、启用该虚拟机,使用Linux的fdisk分区工具给磁盘分区
[root@Agile936 ~]# 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: 0x000f0195 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 52428799 25164800 8e Linux LVM Disk /dev/mapper/centos-root: 23.6 GB, 23613931520 bytes, 46120960 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 [root@Agile936 ~]#
根据提示可以看到此系统的磁盘接口为scsi,对应‘sda’,下面进行分区.
- 输入m查看帮助信息,
- 再输入n(表示增加分区),
- 回车后输入p(创建主分区),
- 回车后partition number输入3(因为上面已经有两个分区sda1和sda2),
- 回车会提示输入分区的start值(通过fdisk -l 可以看出sda2的end值为40960),我们可以指定start值为40961,end值为默认即可(即当前最大值),
- 回车后输入W进行保存,分区划分完毕。
[root@Agile936 ~]# 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 (2 primary, 0 extended, 2 free) e extended Select (default p): p # 创建主分区 Partition number (3,4, default 3): 3 First sector (52428800-104857599, default 52428800): Using default value 52428800 Last sector, +sectors or +size{K,M,G} (52428800-104857599, default 104857599): Using default value 104857599 Partition 3 of type Linux and of size 25 GiB is set 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.
查看当前分区情况:
[root@Agile936 ~]# 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: 0x000f0195 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 52428799 25164800 8e Linux LVM /dev/sda3 52428800 104857599 26214400 83 Linux Disk /dev/mapper/centos-root: 23.6 GB, 23613931520 bytes, 46120960 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
可以看到/dev/sda3的Id号为83,我们要将其改成8e(LVM卷文件系统的Id),具体方法同上,如下图所示:
[root@Agile936 ~]# 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): t Partition number (1-3, default 3): 3 Hex code (type L to list all codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix 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.
重启后,查看最新的分区,修改成功
[root@Agile936 ~]# 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: 0x000f0195 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 52428799 25164800 8e Linux LVM /dev/sda3 52428800 104857599 26214400 8e Linux LVM Disk /dev/mapper/centos-root: 23.6 GB, 23613931520 bytes, 46120960 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
3、扩展文件系统
将该分区创建为文件系统:
[root@Agile936 ~]# mkfs.ext4 /dev/sda3 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 1638400 inodes, 6553600 blocks 327680 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2155872256 200 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
创建PV:
pvdisplay当前的物理卷
[root@Agile936 ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size <24.00 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 6143 Free PE 1 Allocated PE 6142 PV UUID WjHxHP-WmJh-MXl7-qixZ-EGJY-pFxb-MeJK88
pvcreate指令用于将物理硬盘分区初始化为物理卷,以便被LVM使用。要创建物理卷必须首先对硬盘进行分区,并且将硬盘分区的类型设置为“8e”后,才能使用pvcreat指令将分区初始化为物理卷。
[root@Agile936 ~]# pvcreate /dev/sda3 WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/sda3. Physical volume "/dev/sda3" successfully created. [root@Agile936 ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size <24.00 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 6143 Free PE 1 Allocated PE 6142 PV UUID WjHxHP-WmJh-MXl7-qixZ-EGJY-pFxb-MeJK88 "/dev/sda3" is a new physical volume of "25.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 25.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID lM84VY-OQEa-qwf3-OyO6-W4bm-UNxk-Az6wRb
扩展VG:
当前需要扩充的lvm组名,可以通过vgdisplay查看:
[root@Agile936 ~]# vgdisplay --- Volume group --- VG Name centos 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 <24.00 GiB PE Size 4.00 MiB Total PE 6143 Alloc PE / Size 6142 / 23.99 GiB Free PE / Size 1 / 4.00 MiB VG UUID FWHNlT-utQu-jqv5-ukto-POcw-YOY5-9thDL8
vgextend指令用于动态的扩展卷组,它通过向卷组中添加物理卷来增加卷组的容量,可以看出可扩展的空间为24GB,接下来进行LV扩展,扩展空间要小于VG 的Free PE的大小,即小于24GB。
[root@Agile936 ~]# vgextend centos /dev/sda3 Volume group "centos" successfully extended [root@Agile936 ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 48.99 GiB PE Size 4.00 MiB Total PE 12542 Alloc PE / Size 6142 / 23.99 GiB Free PE / Size 6400 / 25.00 GiB VG UUID FWHNlT-utQu-jqv5-ukto-POcw-YOY5-9thDL8
lvextend命令用于扩展空间,如下面将root分区扩展22GB。
[root@Agile936 ~]# lvextend -L +22G /dev/mapper/centos-root Size of logical volume centos/root changed from 21.99 GiB (5630 extents) to 43.99 GiB (11262 extents). Logical volume centos/root successfully resized.
命令使系统重新读取大小, 添加成功
[root@Agile936 ~]# xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1441280 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=5765120, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2815, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 5765120 to 11532288 [root@Agile936 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 44G 4.2G 40G 10% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 8.6M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 407M 0 407M 0% /run/user/0
关注公众号:熊猫Jay字节之旅,了解更多 AI 技巧 ~