CentOS virtual machine root partition disk expansion operation

1. Shut down your virtual machine system

Find the following content: select "Edit Virtual mache settings" and
click "Expand" to
expand the space of the virtual machine to "60G" (fill in the space according to personal needs). But it is not visible under linux.

2. Use the fdisk tool under linux to partition

Log in to your linux system as the root user and check the partitions of your system

# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x00008d26
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
 
Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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
根据提示信息可以判断出此系统的磁盘接口为SCSI,对应“sda”如果上面的红色字体是“hda”,那么此系统的磁盘接口为IDE对应“hda”所以我做一下操作:
# 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): m //列出fdisk的帮助
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Command (m for help): n    //命令n用于添加新分区
Command action
   e   extended
   p   primary partition (1-4)
p  //选择创建主分区"此时
Partition number (1-4): 3  //fdisk会让你选择主分区的编号,如果已经有了主分区sda1,sda2,那么编号就选3,即要创建的该分区为sda3
First cylinder (2611-7832, default 2611): //此时,fdisk又会让你选择该分区的开始值这个就是分区的Start 值(start cylinder);这里最好直接按回车
Using default value 2611
Last cylinder, +cylinders or +size{
    
    K,M,G} (2611-7832, default 7832): //此时,fdisk又会让你选择该分区的开始值这个就是分区的End 值这里最好直接按回车
Using default value 7832
 
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.

3. Our newly created partition /dev/sda3 is not LVM. So, use fdisk to change it to LVM

# fdisk /dev/sda
Command (m for help): m
Command (m for help): t //改变分区系统id
Partition number (1-4): 3 //指定分区号
Hex code (type L to list codes): 8e //指定要改成的id号,8e代表LVM
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): w

4. After restarting the system, log in to the system. (Be sure to restart the system, otherwise the new partition cannot be expanded)

5. Format the newly added partition

# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x00008d26
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
/dev/sda3            2611        7832    41939020   8e  Linux LVM
 
Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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
你会发现多了一块分区
# mkfs -t ext3 /dev/sda3 //在硬盘分区“/dev/sda3”上创建“ext3”文件系统
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10484755 blocks
524237 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
320 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, 7962624
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

At this point we can use the new partition:

6. Expand the new partition

# lvs
 
  LV      VG       Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_root VolGroup -wi-ao---- 17.57g
  lv_swap VolGroup -wi-ao----  1.94g
# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created
# pvcreate /dev/sda3 //pvcreate指令用于将物理硬盘分区初始化为物理卷,以便被LVM使用。要创建物理卷必须首先对硬盘进行分区,并且将硬盘分区的类型设置为“8e”后,才能使用pvcreat指令将分区初始化为物理卷
  Physical volume "/dev/sda3" successfully created
# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  1.7G   15G  10% /
tmpfs                         1.9G     0  1.9G   0% /dev/shm
/dev/sda1                     485M   32M  428M   7% /boot
# vgextend /dev/mapper/VolGroup-lv_root /dev/sda3    //其中是当前需要扩充的lvm组名,可以通过df -h查看,例如我的是: /dev/mapper/VolGroup-lv_root) //vgextend指令用于动态的扩展卷组,它通过向卷组中添加物理卷来增加卷组的容量
  Volume group name VolGroup/lv_root has invalid characters
# vgextend /dev/mapper/VolGroup /dev/sda3    //原来这个组名,是-之前的字符串——这要是没有文章看着对比分析,谁知道啊?
  Volume group "VolGroup" successfully extended
#vgdisplay    //用于显示LNM卷组的元数据信息
  --- Volume group ---
  VG Name               VolGroup
  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               59.50 GiB
  PE Size               4.00 MiB
  Total PE              15232
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       10238 / 39.99 GiB
  VG UUID               Ecv3ml-86eg-LTsH-mALD-mLxW-v3Ao-GEqiFc

(Mainly check Free PE / Size 10238 / 39.99 GiB, indicating that we can have up to 39.99GB of expansion space. I generally choose less than or equal to 39.99GB)

# lvextend -L +39.99G /dev/VolGroup/lv_root /dev/sda3
  Rounding size to boundary between physical extents: 39.99 GiB
  Extending logical volume lv_root to 57.56 GiB
  Logical volume lv_root successfully resized
 
# e2fsck -f /dev/VolGroup/lv_root
e2fsck 1.41.12 (17-May-2010)
/dev/VolGroup/lv_root is mounted.
e2fsck: Cannot continue, aborting.
# resize2fs  /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/VolGroup/lv_root to 15089664 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 15089664 blocks long.
# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   57G  1.7G   53G   3% /
tmpfs                         1.9G     0  1.9G   0% /dev/shm
/dev/sda1                     485M   32M  428M   7% /boot

As you can see, this logical volume has been successfully expanded, and the root directory can be loaded with more things!

Guess you like

Origin blog.csdn.net/sinat_27674731/article/details/113740374