VirtualBox扩容CentOS磁盘

159 阅读6分钟

首先在virtualBox 介质中增加容量

image.png

登录虚拟机终端

/dev/sda上创建新分区
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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)

命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (33554432-52428799,默认为 33554432):
将使用默认值 33554432
Last 扇区, +扇区 or +size{K,M,G} (33554432-52428799,默认为 52428799):
将使用默认值 52428799
分区 3 已设置为 Linux 类型,大小设为 9 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sda:26.8 GB, 26843545600 字节,52428800 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00062e5f

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM
/dev/sda3        33554432    52428799     9437184   83  Linux

命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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)

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@localhost ~]# ll /dev/sda
brw-rw---- 1 root disk 8, 0 9月  14 12:56 /dev/sda
[root@localhost ~]# ll /dev/sda*
brw-rw---- 1 root disk 8, 0 9月  14 12:56 /dev/sda
brw-rw---- 1 root disk 8, 1 9月  14 12:56 /dev/sda1
brw-rw---- 1 root disk 8, 2 9月  14 12:56 /dev/sda2
[root@localhost ~]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@localhost ~]# ll /dev/sda*
brw-rw---- 1 root disk 8, 0 9月  14 12:57 /dev/sda
brw-rw---- 1 root disk 8, 1 9月  14 12:57 /dev/sda1
brw-rw---- 1 root disk 8, 2 9月  14 12:57 /dev/sda2
brw-rw---- 1 root disk 8, 3 9月  14 12:57 /dev/sda3
格式化新分区 mkfs.ext4 /dev/sda3
[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   25G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0   15G  0 part 
│ ├─centos-root 253:0    0 13.4G  0 lvm  /
│ └─centos-swap 253:1    0  1.6G  0 lvm  [SWAP]
└─sda3            8:3    0    9G  0 part 
sr0              11:0    1 58.3M  0 rom  
[root@localhost ~]# mkfs.ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
589824 inodes, 2359296 blocks
117964 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2151677952
72 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

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 
创建物理卷并添加到卷组。

首先,使用以下命令创建物理卷

[root@localhost ~]# 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@localhost ~]# vgex
vgexport  vgextend  
[root@localhost ~]# vgextend /dev/sda3
  No command with matching syntax recognised.  Run 'vgextend --help' for more information.
  Correct command syntax is:
  vgextend VG PV ...

然后,将新创建的物理卷添加到CentOS卷组(假设卷组名为centos)

[root@localhost ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
查看卷组属性,确认新分区已经添加到卷组
[root@localhost ~]# 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               23.99 GiB
  PE Size               4.00 MiB
  Total PE              6142
  Alloc PE / Size       3839 / <15.00 GiB
  Free  PE / Size       2303 / <9.00 GiB
  VG UUID               02SdB1-ICbN-vibe-6RsA-gQWW-kmnn-1qDVs7
将空闲的空间分配给root文件系统。假设我们要将所有空闲空间分配给root文件系统
[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root 
  Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 22.39 GiB (5732 extents).
  Logical volume centos/root successfully resized.
更新文件系统以使用新增的空间。对于ext4文件系统
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root 
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=877824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3511296, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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 3511296 to 5869568
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G   16K  1.9G    1% /dev/shm
tmpfs                    1.9G  8.7M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/mapper/centos-root   23G   13G   11G   55% /
/dev/sda1               1014M  194M  821M   20% /boot
tmpfs                    379M     0  379M    0% /run/user/1002
mine                     276G   68G  209G   25% /media/sf_mine
wwwgit                   276G   68G  209G   25% /media/sf_wwwgit
wwwroot                  276G   68G  209G   25% /media/sf_wwwroot
tmpfs                    379M     0  379M    0% /run/user/0