docker容器跑起来默认占用/var/lib目录的空间,我知道的有两种解决办法,欢迎大家补充。
一是迁移工作目录,二是扩根。
反正都是要挂盘,我直接扩根了,省点事。

一、扩容

查看磁盘空间
df -h 
lsblk
fdisk  -l

系统盘真的很小,全靠外挂数据盘

centos 磁盘分区修复 centos7修复根分区_centos 磁盘分区修复

lsblk

这样看得更清晰些

centos 磁盘分区修复 centos7修复根分区_linux_02


我有一块500G的盘,分个249G出来扩根吧!!!

磁盘分区
fdisk /dev/sdb
[root@123123 ~]# fdisk /dev/sdb 
	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.
	
	Device does not contain a recognized partition table
	Building a new DOS disklabel with disk identifier 0xcffa3732.
	
	Command (m for help): m            	             #打印帮助文档
	Command action
	   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							#n表示创建1个新分区
	   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					#w表示保存并退出
	   x   extra functionality (experts only)
	
	Command (m for help): n							#输入 n,我们开始新建一个分区
	Partition type:
	   p   primary (0 primary, 0 extended, 4 free)
	   e   extended
	Select (default p): p							#输入 p,我们将分区创建为主分区
	Partition number (1-4, default 1): 1			#输入1,我们将分区定义为第1个分区,即sdb1
	First sector (2048-1048575999, default 2048):  	#好像是开始字节数
	Using default value 2048
	Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): 249.9G
																				#新分区的大小,要规范输入
	Value out of range.
	Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): +249G
	Partition 1 of type Linux and of size 249 GiB is set
	
	Command (m for help): t							#输入t,我们来改变一下分区的类型
	Selected partition 1
	Hex code (type L to list all codes): 8e			#输入8e,8e就是LVM
	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.
	Syncing disks.
	
	[root@nanhavezhi~]# lsblk 
	NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
	sda                 8:0    0   64G  0 disk 
	├─sda1              8:1    0    1G  0 part /boot
	└─sda2              8:2    0   63G  0 part 
	  ├─rootvg-lvroot 253:0    0   26G  0 lvm  /
	  ├─rootvg-lvswap 253:1    0   10G  0 lvm  [SWAP]
	  ├─rootvg-lvvar  253:2    0   16G  0 lvm  /var
	  ├─rootvg-lvopt  253:3    0   10G  0 lvm  /opt
	  └─rootvg-lvhome 253:4    0    1G  0 lvm  /home
	sdb                 8:16   0  500G  0 disk 
	└─sdb1              8:17   0  249G  0 part 
	sr0                11:0    1 1024M  0 rom
格式化sdb1
mkfs.xfs /dev/sdb1

centos 磁盘分区修复 centos7修复根分区_centos 磁盘分区修复_03

查看卷组信息

找到根分区的卷组信息

lvdisplay

centos 磁盘分区修复 centos7修复根分区_运维_04

开始扩容
[root@123123~]# pvcreate /dev/sdb1  #创建pv
	WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
	  Wiping xfs signature on /dev/sdb1.
	  Physical volume "/dev/sdb1" successfully created.  
	[root@123123 ~]# vgextend rootvg  /dev/sdb1
	#将/dev/sdb1卷加入根目录所在的卷组名rootvg,rootvg就是查看卷组信息时看到的vg的名字(这一步俗称  扩容 vg )
	  Volume group "rootvg" successfully extended
	[root@123123~]# vgdisplay 
	  --- Volume group ---
	  VG Name               rootvg
	  System ID             
	  Format                lvm2
	  Metadata Areas        2
	  Metadata Sequence No  7
	  VG Access             read/write
	  VG Status             resizable
	  MAX LV                0
	  Cur LV                5
	  Open LV               5
	  Max PV                0
	  Cur PV                2
	  Act PV                2
	  VG Size               311.99 GiB
	  PE Size               4.00 MiB
	  Total PE              79870
	  Alloc PE / Size       16127 / <63.00 GiB
	  Free  PE / Size       63743 / <249.00 GiB			##关注这个可用Free PE
	  VG UUID               bCPgVl-rpYe-lqlT-ooCs-gA3j-E2AP-wENic3
	  
	[root@123123 ~]# lvextend -l +63743  /dev/rootvg/lvroot
	#把可用空间全部加给根分区,2559就是上面的Free PE值,/dev/cl/root就是上面查看卷组信息的LV Path值(扩容LV)
	  Size of logical volume rootvg/lvroot changed from 26.00 GiB (6656 extents) to <275.00 GiB (70399 extents).
	  Logical volume rootvg/lvroot successfully resized.
	 #以上只是完成了卷的扩容,文件系统还没扩容,执行最后一步:
	[root@123123~]# xfs_growfs /dev/rootvg/lvroot
	meta-data=/dev/mapper/rootvg-lvroot isize=512    agcount=4, agsize=1703936 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0 spinodes=0
	data     =                       bsize=4096   blocks=6815744, imaxpct=25
	         =                       sunit=0      swidth=0 blks
	naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
	log      =internal               bsize=4096   blocks=3328, version=2
	         =                       sectsz=512   sunit=0 blks, lazy-count=1
	realtime =none                   extsz=4096   blocks=0, rtextents=0
	data blocks changed from 6815744 to 72088576

二、重大失误

到这一步我出现了,严重的错误。
这台机器是我申请来了,不是我自己装的,/var/是独立挂载出来的,不是在/根下的,扩根没啥用。
看了看缩容的文档,找到一个老哥写的
https://blog.51cto.com/u_15070093/2812023 用 lvreduce 可以缩容,收回来的空间可以重新划分,我现在的需求是把/var扩容,正好在一个vg里面,把收回来的空间扩容给/var就好了。

三、缩容

[root@123123~]# lvs
	  LV     VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
	  lvhome rootvg -wi-ao----    1.00g                                                    
	  lvopt  rootvg -wi-ao----   10.00g                                                    
	  lvroot rootvg -wi-ao---- <273.00g                                                    
	  lvswap rootvg -wi-ao----  <10.00g                                                    
	  lvvar  rootvg -wi-ao----   18.00g            
	                                          
	[root@123123 ~]# df -h
	Filesystem                 Size  Used Avail Use% Mounted on
	devtmpfs                    32G     0   32G   0% /dev
	tmpfs                       32G     0   32G   0% /dev/shm
	tmpfs                       32G   12M   32G   1% /run
	tmpfs                       32G     0   32G   0% /sys/fs/cgroup
	/dev/mapper/rootvg-lvroot  275G   14G  262G   5% /
	/dev/sda1                 1014M  172M  843M  17% /boot
	/dev/mapper/rootvg-lvopt    10G   33M   10G   1% /opt
	/dev/mapper/rootvg-lvhome 1014M   37M  978M   4% /home
	/dev/mapper/rootvg-lvvar    18G  2.6G   16G  15% /var
	tmpfs                      6.3G   12K  6.3G   1% /run/user/42
	tmpfs                      6.3G     0  6.3G   0% /run/user/1000
	
	[root@123123~]# lsblk 
	NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
	sda                 8:0    0   64G  0 disk 
	├─sda1              8:1    0    1G  0 part /boot
	└─sda2              8:2    0   63G  0 part 
	  ├─rootvg-lvroot 253:0    0  273G  0 lvm  /
	  ├─rootvg-lvswap 253:1    0   10G  0 lvm  [SWAP]
	  ├─rootvg-lvvar  253:2    0   18G  0 lvm  /var
	  ├─rootvg-lvopt  253:3    0   10G  0 lvm  /opt
	  └─rootvg-lvhome 253:4    0    1G  0 lvm  /home
	sdb                 8:16   0  500G  0 disk 
	├─sdb1              8:17   0  249G  0 part 
	│ ├─rootvg-lvroot 253:0    0  273G  0 lvm  /
	│ └─rootvg-lvvar  253:2    0   18G  0 lvm  /var
	└─sdb2              8:18   0  251G  0 part 
	sr0                11:0    1 1024M  0 rom  

	#以上是磁盘调整前的状况
	
	[root@123123 ~]# lvreduce -L 240G  /dev/rootvg/lv     
	#这条命令的意思是给它剩240g,我一开始弄错了!!
	/dev/rootvg/lvhome  /dev/rootvg/lvopt   /dev/rootvg/lvroot  /dev/rootvg/lvswap  /dev/rootvg/lvvar   
	[root@123123 ~]# lvreduce -L 240G  /dev/rootvg/lvroot
	  WARNING: Reducing active and open logical volume to 240.00 GiB.
	  THIS MAY DESTROY YOUR DATA (filesystem etc.)
	Do you really want to reduce rootvg/lvroot? [y/n]: y
	  Size of logical volume rootvg/lvroot changed from <273.00 GiB (69887 extents) to 240.00 GiB (61440 extents).
	  Logical volume rootvg/lvroot successfully resized.

	[root@123123123 ~]# vgdisplay 
	  --- Volume group ---
	  VG Name               rootvg
	  System ID             
	  Format                lvm2
	  Metadata Areas        2
	  Metadata Sequence No  11
	  VG Access             read/write
	  VG Status             resizable
	  MAX LV                0
	  Cur LV                5
	  Open LV               5
	  Max PV                0
	  Cur PV                2
	  Act PV                2
	  VG Size               311.99 GiB
	  PE Size               4.00 MiB
	  Total PE              79870
	  Alloc PE / Size       71423 / <279.00 GiB
	  Free  PE / Size       8447 / <33.00 GiB        #记住这个 Free  PE值
	  VG UUID               bCPgVl-rpYe-lqlT-ooCs-gA3j-E2AP-wENic3
	   
	[root@123123 ~]# lvextend -l  +8447 /dev/rootvg/lvvar
	  Size of logical volume rootvg/lvvar changed from 18.00 GiB (4608 extents) to <51.00 GiB (13055 extents).
	  Logical volume rootvg/lvvar successfully resized.

	[root@123123 ~]# xfs_growfs /dev/rootvg/lvvar			#这次只扩容了33g
	meta-data=/dev/mapper/rootvg-lvvar isize=512    agcount=5, agsize=1048576 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0 spinodes=0
	data     =                       bsize=4096   blocks=4718592, 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 4718592 to 13368320
	[root@e7tops ~]# df  -h
	Filesystem                 Size  Used Avail Use% Mounted on
	devtmpfs                    32G     0   32G   0% /dev
	tmpfs                       32G     0   32G   0% /dev/shm
	tmpfs                       32G   12M   32G   1% /run
	tmpfs                       32G     0   32G   0% /sys/fs/cgroup
	/dev/mapper/rootvg-lvroot  275G   14G  262G   5% /
	/dev/sda1                 1014M  172M  843M  17% /boot
	/dev/mapper/rootvg-lvopt    10G   33M   10G   1% /opt
	/dev/mapper/rootvg-lvhome 1014M   37M  978M   4% /home
	/dev/mapper/rootvg-lvvar    51G  2.6G   49G   5% /var
	tmpfs                      6.3G   12K  6.3G   1% /run/user/42
	tmpfs                      6.3G     0  6.3G   0% /run/user/1000
	
	[root@123123 ~]# lvreduce -L 100G  /dev/rootvg/lvroot
	  WARNING: Reducing active and open logical volume to 100.00 GiB.
	  THIS MAY DESTROY YOUR DATA (filesystem etc.)
	Do you really want to reduce rootvg/lvroot? [y/n]: y
	  Size of logical volume rootvg/lvroot changed from 240.00 GiB (61440 extents) to 100.00 GiB (25600 extents).
	  Logical volume rootvg/lvroot successfully resized.
	[root@123123 ~]# vgdisplay 
	  --- Volume group ---
	  VG Name               rootvg
	  System ID             
	  Format                lvm2
	  Metadata Areas        2
	  Metadata Sequence No  13
	  VG Access             read/write
	  VG Status             resizable
	  MAX LV                0
	  Cur LV                5
	  Open LV               5
	  Max PV                0
	  Cur PV                2
	  Act PV                2
	  VG Size               311.99 GiB
	  PE Size               4.00 MiB
	  Total PE              79870
	  Alloc PE / Size       44030 / 171.99 GiB
	  Free  PE / Size       35840 / 140.00 GiB
	  VG UUID               bCPgVl-rpYe-lqlT-ooCs-gA3j-E2AP-wENic3
	   
	[root@123123 ~]# lvextend -l  +35840 /dev/rootvg/lvvar
	  Size of logical volume rootvg/lvvar changed from <51.00 GiB (13055 extents) to <191.00 GiB (48895 extents).
	  Logical volume rootvg/lvvar successfully resized.
	  
	[root@213213 ~]# xfs_growfs /dev/rootvg/lvvar
	meta-data=/dev/mapper/rootvg-lvvar isize=512    agcount=13, agsize=1048576 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0 spinodes=0
	data     =                       bsize=4096   blocks=13368320, 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 13368320 to 50068480
	
经过我的一番操作,磁盘空间调整后是这样的

[root@123123 ~]# df -h
	Filesystem                 Size  Used Avail Use% Mounted on
	devtmpfs                    32G     0   32G   0% /dev
	tmpfs                       32G     0   32G   0% /dev/shm
	tmpfs                       32G   12M   32G   1% /run
	tmpfs                       32G     0   32G   0% /sys/fs/cgroup
	/dev/mapper/rootvg-lvroot  275G   14G  262G   5% /
	/dev/sda1                 1014M  172M  843M  17% /boot
	/dev/mapper/rootvg-lvopt    10G   33M   10G   1% /opt
	/dev/mapper/rootvg-lvhome 1014M   37M  978M   4% /home
	/dev/mapper/rootvg-lvvar   261G  2.6G  259G   1% /var
	tmpfs                      6.3G   12K  6.3G   1% /run/user/42
	tmpfs                      6.3G     0  6.3G   0% /run/user/1000
	
	[root@123123 ~]# lsblk 
	NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
	sda                 8:0    0   64G  0 disk 
	├─sda1              8:1    0    1G  0 part /boot
	└─sda2              8:2    0   63G  0 part 
	  ├─rootvg-lvroot 253:0    0   42G  0 lvm  /
	  ├─rootvg-lvswap 253:1    0   10G  0 lvm  [SWAP]
	  ├─rootvg-lvvar  253:2    0  249G  0 lvm  /var
	  ├─rootvg-lvopt  253:3    0   10G  0 lvm  /opt
	  └─rootvg-lvhome 253:4    0    1G  0 lvm  /home
	sdb                 8:16   0  500G  0 disk 
	├─sdb1              8:17   0  249G  0 part 
	│ ├─rootvg-lvroot 253:0    0   42G  0 lvm  /
	│ └─rootvg-lvvar  253:2    0  249G  0 lvm  /var
	└─sdb2              8:18   0  251G  0 part 
	sr0                11:0    1 1024M  0 rom