Featured image of post CentOS 7 磁盘扩容

CentOS 7 磁盘扩容

磁盘扩容,非lvm扩容,而是传统方式

CentOS 7 磁盘扩容

假设已经添加了磁盘,到了需要在系统里操作的步骤

对新增加的硬盘进行分区

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
fidsk -l
fdisk /dev/sda (根据实际情况选择)
n	(新增加一个分区)
p	(区类型选择为主分区)
回车	(分区号)
回车 (起始扇区)
回车 (结束扇区)
t
上面的分区号
8e 将分区“Linux”的类型更改为“Linux LVM”
w 保存
reboot

对新增加的硬盘格式化

1
mkfs.ext4 /dev/sda3

添加新LVM到已有的LVM组,实现扩容

1
2
3
4
5
6
lvm             进入lvm管理
lvm> pvcreate /dev/sda3 这是初始化刚才的分区,必须的
lvm>vgextend centos /dev/sda3 将初始化过的分区加入到虚拟卷组vg_dc01
lvm>lvextend -L +100G /dev/mapper/centos-root  扩展已有卷的容量(注意容量大小)
lvm>pvdisplay               查看卷容量,这时你会看到一个很大的卷了
lvm>quit 

以上只是卷扩容了,下面是文件系统的真正扩容,输入以下命令:

1
resize2fs /dev/mapper/centos-root

resize2fs: Bad magic number in super-block 当尝试打开 /dev/mapper/centos-root 时

报错:当尝试打开 /dev/mapper/centos-root 时 找不到有效的文件系统超级块

因为我的centos7的某些分区用的是xfs的文件系统(使用df -T查看即可知道)

resize2fs替换为xfs_growfs,重新执行一遍即可,如下:

1
 xfs_growfs /dev/mapper/centos-root

步骤记录

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[root@last ~]# 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

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ae200

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (41943040-209715199, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 80 GiB is set

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ae200

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040   209715199    83886080   83  Linux

Command (m for help): t
Partition number (1-3, default 3): 8e
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ae200

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040   209715199    83886080   8e  Linux LVM

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.
[root@last ~]#
[root@last ~]# reboot
Connection to 192.168.50.130 closed by remote host.
Connection to 192.168.50.130 closed.
➜  frelon
➜  frelon
➜  frelon ssh [email protected]
Last login: Sat Sep 19 04:08:59 2020 from 192.168.50.1
[root@last ~]# mkfs.ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
5242880 inodes, 20971520 blocks
1048576 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2168455168
640 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, 2654208,
        4096000, 7962624, 11239424, 20480000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

[root@last ~]#
[root@last ~]#
[root@last ~]# lvm
lvm> 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.
lvm>
lvm> vgextend centos /dev/sda3
  Volume group "centos" successfully extended
lvm> lvextend -L +79G /dev/mapper/centos-root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <96.00 GiB (24575 extents).
  Logical volume centos/root successfully resized.
lvm> quit
  Exiting.
[root@last ~]# xfs_
xfs_admin      xfs_db         xfs_fsr        xfs_io         xfs_metadump   xfs_quota
xfs_bmap       xfs_estimate   xfs_growfs     xfs_logprint   xfs_mkfile     xfs_repair
xfs_copy       xfs_freeze     xfs_info       xfs_mdrestore  xfs_ncheck     xfs_rtcp
[root@last ~]# xfs_
xfs_admin      xfs_db         xfs_fsr        xfs_io         xfs_metadump   xfs_quota
xfs_bmap       xfs_estimate   xfs_growfs     xfs_logprint   xfs_mkfile     xfs_repair
xfs_copy       xfs_freeze     xfs_info       xfs_mdrestore  xfs_ncheck     xfs_rtcp
[root@last ~]# xfs_growfs /dev/mapper/c
centos-root  centos-swap  control
[root@last ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 25164800
[root@last ~]#
[root@last ~]#
[root@last ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.5M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   96G  1.4G   95G   2% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0

 由于在安装centos系统的时候,如果在安装时没有分配磁盘空间,选择的是默认分配的,在安装完成后,可以发现大容量磁盘往往分配在了home下面。

如果要把home下面的磁盘空间分配到root磁盘下面。可以进行如下操作。

查看CentOS的系统版本

1
2
[root@controller ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

查看分区

df -h (centos-home和centos-root每人的名字可能不一样)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[root@controller ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        50G  8.5G   42G  17% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G  8.7M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda6        20G   33M   20G   1% /home
/dev/sda5        26G  804M   26G   4% /var
/dev/sda2       2.0G  125M  1.9G   7% /boot
/dev/loop0      4.1G  4.1G     0 100% /mnt/centos
/dev/loop1      2.7G  2.7G     0 100% /mnt/xiandi
tmpfs           378M     0  378M   0% /run/user/0

备份home分区文件

1
2
3
4
[root@controller ~]# tar cvzf /mnt/home.tar /home/
tar: Removing leading `/' from member names
/home/
/home/frelon/

卸载/home,如果无法卸载,先终止使用/home文件系统的进程

1
[root@controller ~]# umount /home (卸载)

如果卸载时,发现/home在使用中,所以先终止。

1
[root@controller ~]# fuser -km /home/(终止)

再次卸载,没有报错,表示成功。

1
[root@controller ~]# umount /home (卸载)

删除/home所在的lv

1
lvremove /dev/mapper/centos-home

扩展/root所在的lv

1
2
lvextend -L +100G /dev/mapper/centos-root

扩展/root文件系统

1
xfs_growfs /dev/mapper/centos-root

重新创建home lv (创建时计算好剩余的磁盘容量,建议比剩余小1G左右)

1
lvcreate -L 41G -n /dev/mapper/centos-home 

创建文件系统

1
mkfs.xfs /dev/mapper/centos-home

挂载home

1
mount /dev/mapper/centos-home

home文件恢复

1
tar xvf /tmp/home.tar -C /home/

再次使用df -h查看系统磁盘大小

可以看到home下面100G的磁盘容量已经转移到root下面了,至此,转移任务结束。此为在CentOS7.2系统下测试使用的,在CentOS6版本下还没测试过。

声明 : 文章来自互联网(实在是找不到原作者链接出处了),侵联删

Licensed under CC BY-NC-SA 4.0