vmware下linux非LVM管理的根目录扩容经历

一、前戏

因为根目录会包含很多系统应用运行的配置等,肯定不能直接卸载,所以要采用一种既不破坏数据,又能扩容的方法。经过一下午的折腾,终于搞定,总结一下心得。
首先我们在VMware中,在虚拟机关机,并且无快照的情况下,点击如下图:

 


然后输入需要扩容的大小点击确定。

然后启动虚拟机。

二、分区管理

使用xshell登录虚拟机,按以下步骤执行:

[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        14G   13G  795M  95% /
devtmpfs        899M     0  899M   0% /dev
tmpfs           913M     0  913M   0% /dev/shm
tmpfs           913M  8.9M  904M   1% /run
tmpfs           913M     0  913M   0% /sys/fs/cgroup
/dev/sda1       297M  195M  103M  66% /boot
tmpfs           183M     0  183M   0% /run/user/0
[root@localhost ~]# fdisk -l

Disk /dev/sda: 51.5 GB, 51539607552 bytes, 100663296 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: 0x000e780b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     3762175     1572864   82  Linux swap / Solaris
/dev/sda3         3762176   100663295    48450560   83  Linux

由上图可以发现,根目录上挂载的是/dev/sda3 这个分区,而且这个系统里的分区id为83,因此不能通过LVM进行逻辑卷管理。

[root@localhost ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   48G  0 disk 
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0  1.5G  0 part [SWAP]
└─sda3   8:3    0 46.2G  0 part /
sr0     11:0    1 1024M  0 rom  

通过这个命令可以知道,sda3确实是已经加了容量了,之前的sda3只有14G的。
现在我们就要把加了的容量添加到sda3并同步到根目录的文件系统中。

[root@localhost ~]# 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): p  //这里输入p,列出分区列表,记住下面的start和end,后续操作才能保证数据不会丢失。

Disk /dev/sda: 51.5 GB, 51539607552 bytes, 100663296 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: 0x000e780b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     3762175     1572864   82  Linux swap / Solaris
/dev/sda3         3762176    31457279    13847552   83  Linux

Command (m for help): d   //这里输入d,表示删除一个分区
Partition number (1-3, default 3): 3     //这里输入3,是因为之前的分区是/dev/sda3
Partition 3 is deleted

Command (m for help): n    //删除完,输入n新建一个分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p   //选择主分区
Partition number (3,4, default 3): 3  //还是/dev/sda3
First sector (3762176-100663295, default 3762176):      //这里直接回车
Using default value 3762176    
Last sector, +sectors or +size{K,M,G} (3762176-100663295, default 100663295):    //这里明显可以看到,不仅包含了之前的sda3的start和end,而且远大于了,使用默认的将剩余的空间都给这个新建的分区。
Using default value 100663295
Partition 3 of type Linux and of size 46.2 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.
You have new mail in /var/spool/mail/root

然后我们使用一下命令查看sda3的文件格式:

[root@localhost ~]# df -T /dev/sda3
Filesystem     Type 1K-blocks     Used Available Use% Mounted on
/dev/sda3      xfs   13837312 13022968    814344  95% /

它的格式是xfs的,所以要用xfs_growfs命令进行刷新文件系统容量,如果是ext的,使用resize2fs命令。

//先重启
[root@localhost ~]# reboot
[root@localhost ~]# xfs_growfs /dev/sda3
meta-data=/dev/sda3              isize=256    agcount=4, agsize=865472 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=3461888, 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 3461888 to 12112640

然后我们再看根目录的大小:

[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        47G   13G   34G  27% /
devtmpfs        899M     0  899M   0% /dev
tmpfs           913M     0  913M   0% /dev/shm
tmpfs           913M  8.9M  904M   1% /run
tmpfs           913M     0  913M   0% /sys/fs/cgroup
/dev/sda1       297M  195M  103M  66% /boot
tmpfs           183M     0  183M   0% /run/user/0

发现从之前的14G升到了47G了,本次扩容成功。
由于我们的sda3挂载在根目录下,sda3是最后一个分区,所以刚好新增的空间跟sda3是连续的,在最后选择start和end的时候,刚好就能连接上,这也是成功的一个条件,如果不是最后一个分区挂载在根目录下,这个就比较麻烦,需要进一步研究。建议新建虚拟机的时候,选择用LVM来管理分区,这样就好好解决以上的问题了。

附:LVM进行扩容:Centos 7 利用LVM实现动态扩容_冰原狼的专栏-CSDN博客_centos7 lvm扩容


作者:_Mitch
链接:https://www.jianshu.com/p/7d23e6d1f313
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值