Linux分区扩容

调整分区有风险,请在虚拟机中多次练习。
调整分区有风险,请在虚拟机中多次练习。
调整分区有风险,请在虚拟机中多次练习。

先使用 fdisk -l 或者 lsblk 查看机器的分区情况。如下

[[email protected] ~]# fdisk -l

Disk /dev/sda: 68.7 GB, 68719476736 bytes, 134217728 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: 0x0000e8f7

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

Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 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-root: 44.0 GB, 44006637568 bytes, 85950464 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

Disk /dev/mapper/centos-home: 21.5 GB, 21483225088 bytes, 41959424 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

从上面可以看到,有计算机有两块硬盘 /dev/sda 和 /dev/sdb。其中/dev/sda已经有分区了。

我们先用pvcreate命令把/dev/sdb这块硬盘初始化为物理卷。如果先分区再初始化会出现 Device /dev/sdb excluded by a filter. 这样的错误提示。

[[email protected] ~]# pvcreate /dev/sdb
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created.

我们现在可以给/dev/sdb分区。使用命令 fdisk /dev/sdb

n 为新建立分区
分区类型默认为主分区
给分区编号默认为1
开始扇区为默认2048
结束扇区为默认
分区格式选择8e Linux VLM
w 保存分区设置并退出
[[email protected] ~]# 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 0xe2f99c4e.

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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
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.

接下来我们再看看现在的分区列表,使用 lsblk命令我们将得到下面信息。

[[email protected] ~]# 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
  ├─centos-root 253:0    0   41G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0   20G  0 lvm  /home
sdb               8:16   0   40G  0 disk
└─sdb1            8:17   0   40G  0 part
sr0              11:0    1 1024M  0 rom

现在我们可以把创建好的分区增加到想要的分区中了。
先用vgdisplay命令显示LVM卷组的信息。

[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <63.00 GiB
  PE Size               4.00 MiB
  Total PE              16127
  Alloc PE / Size       16126 / 62.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               mrcO26-T2rC-l0Wb-NPyO-KxWn-mMem-FpGU5B

从上面的信息中可以看出,我们这台测试机中只有一个LVM卷组centos

接下来我们用vgextend命令动态扩展LVM卷组。

[[email protected] ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

再查看LVM卷组信息

[[email protected] ~]# 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                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               102.99 GiB
  PE Size               4.00 MiB
  Total PE              26366
  Alloc PE / Size       16126 / 62.99 GiB
  Free  PE / Size       10240 / 40.00 GiB
  VG UUID               mrcO26-T2rC-l0Wb-NPyO-KxWn-mMem-FpGU5B

卷组centos得到了40.00 GiB的可用空间,也就是上面的Free PE / Size 10240 / 40.00 GiB。但是还没被分配。其中PE是物理块的意思(默认为4 MiB)。

我们再用 df -h命令确定要给哪个分区扩容。

[[email protected] ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   41G  1.2G   40G   3% /
devtmpfs                 908M     0  908M   0% /dev
tmpfs                    920M     0  920M   0% /dev/shm
tmpfs                    920M  8.6M  911M   1% /run
tmpfs                    920M     0  920M   0% /sys/fs/cgroup
/dev/mapper/centos-home   20G   33M   20G   1% /home
/dev/sda1               1014M  189M  826M  19% /boot
tmpfs                    184M     0  184M   0% /run/user/0

要扩容可以使用这个命令 lvresize ,其中+10240意思是分区卷增加10240个块。当然也可以用百分比或者比特来表示增加的数量,具体使用可以看手册 man lvresize 。

[[email protected] ~]# lvresize -l +10240 /dev/mapper/centos-home
  Size of logical volume centos/home changed from <20.01 GiB (5122 extents) to <60.01 GiB (15362 extents).
  Logical volume centos/home successfully resized.

用lsblk可以看到/home目录已经增加到了60G

[[email protected] ~]# 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
  ├─centos-root 253:0    0   41G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0   60G  0 lvm  /home
sdb               8:16   0   40G  0 disk
└─sdb1            8:17   0   40G  0 part
  └─centos-home 253:2    0   60G  0 lvm  /home
sr0              11:0    1 1024M  0 rom

如果后悔加错空间了,我们可以用lvreduce命令重新收缩/home空间大小。当然如果你已经向扩容的分区写入数据,将会丢失。

[[email protected] ~]# lvreduce -l -10240 /dev/mapper/centos-home
  WARNING: Reducing active and open logical volume to <20.01 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from <60.01 GiB (15362 extents) to <20.01 GiB (5122 extents).
  Logical volume centos/home successfully resized.

我们重新给/根目录增加空间。

[[email protected] ~]# lvresize -l +10240 /dev/mapper/centos-root
  Size of logical volume centos/root changed from 40.98 GiB (10492 extents) to 80.98 GiB (20732 extents).
  Logical volume centos/root successfully resized.
[[email protected] ~]# 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
  ├─centos-root 253:0    0   81G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0   20G  0 lvm  /home
sdb               8:16   0   40G  0 disk
└─sdb1            8:17   0   40G  0 part
  └─centos-root 253:0    0   81G  0 lvm  /
sr0              11:0    1 1024M  0 rom

从上面信息可以看出,/根目录块已经扩容了。系统容量还没有扩容。
用 df -lhT 命令可以查看到自己分区是ext3或者是xfs。
如果是ext3 用 resize2fs /dev/mapper/centos-root
如果是xfs 用 xfs_growfs /dev/mapper/centos-root

下面是一些有用的文档

https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/recognize-expanded-volume-linux.html
https://wiki.archlinux.org/index.php/LVM_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
https://www.ibm.com/developerworks/cn/linux/l-lvm2/