centos7根目录root或指定目录扩容操作记录

1、首先确认当前各文件系统大小,可以看到根目录对应文件系统大小是10GB,/boot对应文件系统大小是0.4GB。

[root@backup03 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   10G  9.5G  588M  95% /
devtmpfs                 471M     0  471M   0% /dev
tmpfs                    488M     0  488M   0% /dev/shm
tmpfs                    488M  8.6M  479M   2% /run
tmpfs                    488M     0  488M   0% /sys/fs/cgroup
/dev/sda1                197M  135M   62M  69% /boot
tmpfs                     98M  4.0K   98M   1% /run/user/42
tmpfs                     98M   40K   98M   1% /run/user/1000
/dev/sr0                 4.2G  4.2G     0 100% /run/media/admin/CentOS 7 x86_64
tmpfs                     98M     0   98M   0% /run/user/0
[root@backup03 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               12.00 GiB
  PE Size               4.00 MiB
  Total PE              3073
  Alloc PE / Size       3072 / 12.00 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               Qed7Il-G05u-VJwf-Z8M4-3UWK-N5Q1-Cd5UNm
   
[root@backup03 ~]# vgdisplay -v centos
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               12.00 GiB
  PE Size               4.00 MiB
  Total PE              3073
  Alloc PE / Size       3072 / 12.00 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               Qed7Il-G05u-VJwf-Z8M4-3UWK-N5Q1-Cd5UNm
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                NjK7gM-hymI-WqnN-1voq-zSfx-9XCC-Jae1AQ
  LV Write Access        read/write
  LV Creation host, time www.ywl.com, 2019-05-04 18:03:38 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                e4bOKq-cwBJ-J6Xr-NwpX-ISA8-WWAz-TfYXLR
  LV Write Access        read/write
  LV Creation host, time www.ywl.com, 2019-05-04 18:03:38 +0800
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               vuDSnI-BoRc-JICJ-SJ0l-1jAE-PSbu-OfS0U8
  PV Status             allocatable
  Total PE / Free PE    3073 / 1

2、确认当前磁盘容量信息,/dev/sda大小为53.7GB,划分了2个分区,已划分的总容量不到20GB。还有将近30GB可用空间

[root@backup03 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000bc6ac

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    25593855    12591104   8e  Linux LVM

Disk /dev/mapper/centos-root: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

[root@backup03 ~]# 

 

3、在未用空间创建新的分区sda3。

[root@backup03 ~]# 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): 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
   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
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 
First sector (25593856-104857599, default 25593856): 
Using default value 25593856
Last sector, +sectors or +size{K,M,G} (25593856-104857599, default 104857599): 
Using default value 104857599
Partition 3 of type Linux and of size 37.8 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.

 

4、根据上面步骤的提示,需要重启或者运行命令partprobe重新同步磁盘分区信息,分区重新识别后可以看到新分区sda3。 

[root@backup03 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

查看磁盘信息  fdisk -l 

[root@backup03 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000bc6ac

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    25593855    12591104   8e  Linux LVM
/dev/sda3        25593856   104857599    39631872   83  Linux

Disk /dev/mapper/centos-root: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

 

5、使用lvm命令pvcreate格式化sda3分区。

[root@backup03 ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

5.1查看磁盘分区情况

[root@backup03 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000bc6ac

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    25593855    12591104   8e  Linux LVM
/dev/sda3        25593856   104857599    39631872   83  Linux

Disk /dev/mapper/centos-root: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

[root@backup03 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <12.01 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3073
  Free PE               1
  Allocated PE          3072
  PV UUID               vuDSnI-BoRc-JICJ-SJ0l-1jAE-PSbu-OfS0U8
   
  "/dev/sda3" is a new physical volume of "<37.80 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               <37.80 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               S2kmTF-YDWR-CzTt-7Krv-wUWk-elwu-xwQdBf

 

6、将分区sda3加入root所在的vg(centos)里  。 

[root@backup03 ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
[root@backup03 ~]# vgdisplay -v centos
  --- 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               <49.80 GiB
  PE Size               4.00 MiB
  Total PE              12748
  Alloc PE / Size       3072 / 12.00 GiB
  Free  PE / Size       9676 / <37.80 GiB
  VG UUID               Qed7Il-G05u-VJwf-Z8M4-3UWK-N5Q1-Cd5UNm
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                NjK7gM-hymI-WqnN-1voq-zSfx-9XCC-Jae1AQ
  LV Write Access        read/write
  LV Creation host, time www.ywl.com, 2019-05-04 18:03:38 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                e4bOKq-cwBJ-J6Xr-NwpX-ISA8-WWAz-TfYXLR
  LV Write Access        read/write
  LV Creation host, time www.ywl.com, 2019-05-04 18:03:38 +0800
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               vuDSnI-BoRc-JICJ-SJ0l-1jAE-PSbu-OfS0U8
  PV Status             allocatable
  Total PE / Free PE    3073 / 1
   
  PV Name               /dev/sda3     
  PV UUID               S2kmTF-YDWR-CzTt-7Krv-wUWk-elwu-xwQdBf
  PV Status             allocatable
  Total PE / Free PE    9675 / 9675

 

7、扩展root根分区所在的逻辑卷大小,vgdisplay可以看到root所在lv大小已经发生变化。

[root@backup03 ~]# lvextend -L +37.8G /dev/centos/root
  Rounding size to boundary between physical extents: 37.80 GiB.
  Insufficient free space: 9677 extents needed, but only 9676 available
[root@backup03 ~]# lvextend -L +30G /dev/centos/root
  Size of logical volume centos/root changed from 10.00 GiB (2560 extents) to 40.00 GiB (10240 extents).
  Logical volume centos/root successfully resized.
[root@backup03 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               <49.80 GiB
  PE Size               4.00 MiB
  Total PE              12748
  Alloc PE / Size       10752 / 42.00 GiB
  Free  PE / Size       1996 / <7.80 GiB
  VG UUID               Qed7Il-G05u-VJwf-Z8M4-3UWK-N5Q1-Cd5UNm
   
[root@backup03 ~]# 

[root@backup03 ~]#  vgdisplay -v
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               <49.80 GiB
  PE Size               4.00 MiB
  Total PE              12748
  Alloc PE / Size       10752 / 42.00 GiB
  Free  PE / Size       1996 / <7.80 GiB
  VG UUID               Qed7Il-G05u-VJwf-Z8M4-3UWK-N5Q1-Cd5UNm
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                NjK7gM-hymI-WqnN-1voq-zSfx-9XCC-Jae1AQ
  LV Write Access        read/write
  LV Creation host, time www.ywl.com, 2019-05-04 18:03:38 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                e4bOKq-cwBJ-J6Xr-NwpX-ISA8-WWAz-TfYXLR
  LV Write Access        read/write
  LV Creation host, time www.ywl.com, 2019-05-04 18:03:38 +0800
  LV Status              available
  # open                 1
  LV Size                40.00 GiB
  Current LE             10240
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               vuDSnI-BoRc-JICJ-SJ0l-1jAE-PSbu-OfS0U8
  PV Status             allocatable
  Total PE / Free PE    3073 / 0
   
  PV Name               /dev/sda3     
  PV UUID               S2kmTF-YDWR-CzTt-7Krv-wUWk-elwu-xwQdBf
  PV Status             allocatable
  Total PE / Free PE    9675 / 1996

 

8、根目录大小依然没变化,

[root@backup03 ~]# df -h 
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   10G  9.7G  394M  97% /
devtmpfs                 471M     0  471M   0% /dev
tmpfs                    488M     0  488M   0% /dev/shm
tmpfs                    488M  8.6M  479M   2% /run
tmpfs                    488M     0  488M   0% /sys/fs/cgroup
/dev/sda1                197M  135M   62M  69% /boot
tmpfs                     98M  4.0K   98M   1% /run/user/42
tmpfs                     98M   36K   98M   1% /run/user/0
/dev/sr0                 4.2G  4.2G     0 100% /run/media/root/CentOS 7 x86_64
[root@backup03 ~]# 

 

9,确认root文件系统的类型是xfs后,使用xfs_growfs命令扩展根目录大小

[root@backup03 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=481808k,nr_inodes=120452,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=13748)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)
tmpfs on /run/user/42 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=99796k,mode=700,uid=42,gid=42)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=99796k,mode=700)
gvfsd-fuse on /run/user/0/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
/dev/sr0 on /run/media/root/CentOS 7 x86_64 type iso9660 (ro,nosuid,nodev,relatime,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)
[root@backup03 ~]# 

 

10,使用xfs_growfs命令扩展根目录大小

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

 

11,最后使用df命令,确认根目录大小已经从原来的10GB扩展为40GB。

[root@backup03 ~]#  df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   40G  9.7G   31G  25% /
devtmpfs                 471M     0  471M   0% /dev
tmpfs                    488M     0  488M   0% /dev/shm
tmpfs                    488M  8.6M  479M   2% /run
tmpfs                    488M     0  488M   0% /sys/fs/cgroup
/dev/sda1                197M  135M   62M  69% /boot
tmpfs                     98M  4.0K   98M   1% /run/user/42
tmpfs                     98M   36K   98M   1% /run/user/0
/dev/sr0                 4.2G  4.2G     0 100% /run/media/root/CentOS 7 x86_64
[root@backup03 ~]# 

 

 

===================快速简化指令=============================

centos手动扩展磁盘空间指令
lsblk
vgdisplay vg_root
lvs /dev/mapper/vg_root-root
pvdisplay
pvcreate /dev/vdb
vgextend vg_root /dev/vdb
lvextend -L+50G /dev/mapper/vg_root-usr
resize2fs /dev/mapper/vg_root-usr
df -h

vgdisplay vg_root
lvextend -l+14847 /dev/mapper/vg_root-root (取上一条命令查到的 Free /size)
resize2fs /dev/mapper/vg_root-root

 

 常见错误:

如图所示遇到 Cannot archive volume group metadata for centos to read-only filesystem. 错误

可通过 如下命令解决,上面错误说明盘符只读状态了,至于怎么造成,不清楚, 留下大大疑问?

mount -o remount rw /
然后再执行   vgextend centos /dev/sda3 操作

 

 
posted @ 2022-07-01 11:36  彼扬  阅读(347)  评论(0)    收藏  举报

感谢您的阅读,如果有了您的奉献系统会更加完善