vmware中给centos扩展磁盘大小

起因

一开始安装虚拟机centos的时候给虚拟机分配了100G的磁盘空间,但是呢,安装的时候我自己给每一个目录单独分配了空间。其中一个目录 “/” 我分配了20G,但是后面有一次打开虚拟机的时候发现这个 “/dev/mapper/centos-root” 路径提升磁盘利用率99%,建议扩展空间。

[hi@hi~]$ df -hl
文件系统                 容量  已用  可用 已用% 挂载点
...
/dev/mapper/centos-root   20G   20G   0G  99% /
...

当时觉得不可思议,明明一开始给虚拟机分配了100G,为啥提示还不够,后面才知道,这些好像是我自定义分配的大小。既然这样,只能加大空间了。

谜题破解了:是由于笔者用了docker,但是没有对docker的日志大小进行限制,导致容器的日志十几个G

# cd /var/lib/docker/containers
# echo "" > xxx.log

进入到这个目录上,查看各个文件夹的大小,确定是否是由于docker的日志导致的,是的话把日志清除即可。此外还要对docker-compose.yml或者docker启动命令加上日志大小限制的命令

方法一:docker-compose.yml
---------------------------
nginx: 
  image: nginx:1.12.1 
  restart: always 
  logging: 
    driver: "json-file"
    options: 
      max-size: "5g"
max-file: "1"
--------------------------- 方法二: docker run增加如下参数,限制生成的json.log单个文件大小和保留文件个数:--log-opt max-size=100m --log-opt max-file=3

解决

1、运行 “fdisk -l” 查看一下目前的分区情况,如下,可以看到目前的最大分区是 “/dev/sda2”,一般情况下,新创建的分区默认是 “/dev/sda3”,此外,可以用的磁盘空间是 “/dev/sda”,共107.4GB

[hi@hi ~]# fdisk -l

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    46565375    23076864   8e  Linux LVM

磁盘 /dev/mapper/centos-root:20.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

2、运行 “fdisk /dev/sda”,如下,接着输入 “m” 查看菜单,"n" 创建新的分区,“p” 主分区(然后设置分区的起始,笔者这里创建一个20G的分区),“w” 保存

[hi@hi ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 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 获取帮助):n
  Partition type:
  p primary (2 primary, 0 extended, 2 free)
  e extended
  Select (default p): p
  分区号 (3,4,默认 3):回车
  起始 扇区 (46565376-209715199,默认为 46565376):回车
  将使用默认值 46565376
  Last 扇区, +扇区 or +size{K,M,G} (46565376-209715199,默认为 209715199):+20G
  分区 3 已设置为 Linux 类型,大小设为 20 GiB

  命令(输入 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)
  正在同步磁盘。

3、查看分区情况,可以看到我们刚刚创建的 sda3

[hi@hi ~]# fdisk -l

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    46565375    23076864   8e  Linux LVM
/dev/sda3        46565376    88508415    20971520   83  Linux

磁盘 /dev/mapper/centos-root:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

4、重启下

[hi@hi ~]# reboot

5、创建逻辑卷

[hi@hi ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

6、vgs查看vg组

[hi@hi ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   3   0 wz--n- 40.00g 4.00m

7、扩展vg

[hi@hi ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

8、vg已经扩展了,查看lv情况

[hi@hi ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   2   3   0 wz--n- 60.00g 20.00g
[hi@hi ~]# lvs LV VG Attr LSize Pool Origin Data
% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 10.00g root centos -wi-ao---- 20.00g swap centos -wi-ao---- 10.00g

9、扩展lv

[hi@hi ~]# lvextend -L +20G /dev/mapper/centos-root
  Size of logical volume centos/root changed from 20.00 GiB (5120 extents) to 40.00 GiB (10240 extents).
  Logical volume centos/root successfully resized.

10、lv已经扩展,查看磁盘情况

[hi@hi ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao---- 10.00g                                                    
  root centos -wi-ao---- 40.00g                                                    
  swap centos -wi-ao---- 10.00g       
[hi@hi ~]#
df -hl 文件系统 容量 已用 可用 已用% 挂载点 ... /dev/mapper/centos-root 20G 20G 0G 99% / ...

11、由于磁盘大小还每刷新,这里刷新下,再次查看,扩展成功

[hi@hi ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5242880, 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 5242880 to 10485760

[hi@hi ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 ... /dev/mapper/centos-root 40G 20.0G 20G 50% / ... [hi@hi ~]#
原文地址:https://www.cnblogs.com/lyc94620/p/13172748.html