Linux(Redhat6.9) 磁盘格式化/扩容

预备知识

逻辑卷管理LVM是一个硬盘系统工具。无论在Linux或者其他类似的系统,都是非常的好用。传统分区使用固定大小分区,重新调整大小十分麻烦。但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。LVM并不需要重启就可以让内核知道分区的存在。


lvm

图中顶部,首先是实际的物理磁盘及其划分的分区和其上的物理卷(PV)。一个或多个物理卷可以用来创建卷组(VG)。然后基于卷组可以创建逻辑卷(LV)。只要在卷组中有可用空间,就可以随心所欲的创建逻辑卷。文件系统就是在逻辑卷上创建的,然后可以在操作系统挂载和访问。


pv/vg/lv

磁盘的扩容主要依据就是lvm

磁盘信息

  • lsblkdf -hT
[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 17.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk 
[root@micocube ~]# df -hT
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_micocube-lv_root
                     ext4    18G  1.9G   15G  12% /
tmpfs                tmpfs  939M     0  939M   0% /dev/shm
/dev/sda1            ext4   477M   41M  412M   9% /boot
  • 可以看到sdb的类型是disk,没有分区,也没有挂载点,就是个空白盘

将空白磁盘直接格式化,挂载到/data

  • 如何将其挂上去用呢?最简单的方法,就是将整个磁盘格式化,不分区,直接使用,但是用这种方法之后磁盘不再支持扩容
[root@micocube ~]# mkfs -t ext4 /dev/sdb 
mke2fs 1.41.12 (17-May-2010)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 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

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

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  • 临时挂载
[root@micocube ~]# mkdir /data
[root@micocube ~]# mount /dev/sdb /data/
[root@micocube ~]# df -hT
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_micocube-lv_root
                     ext4    18G  1.9G   15G  12% /
tmpfs                tmpfs  939M     0  939M   0% /dev/shm
/dev/sda1            ext4   477M   41M  412M   9% /boot
/dev/sdb             ext4   9.8G   23M  9.2G   1% /data
  • 永久挂载
[root@micocube ~]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Sun Apr 28 05:50:32 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_micocube-lv_root /                       ext4    defaults        1 1
UUID=57ad23fa-8c2f-42f8-91c1-01e5bcf4a058 /boot                   ext4    defaults        1 2
/dev/mapper/vg_micocube-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb                /data                   ext4    defaults        0 0

# reboot 
[root@micocube ~]# reboot

[root@micocube ~]# df -hT
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_micocube-lv_root
                     ext4    18G  1.9G   15G  12% /
tmpfs                tmpfs  939M     0  939M   0% /dev/shm
/dev/sda1            ext4   477M   41M  412M   9% /boot
/dev/sdb             ext4   9.8G   23M  9.2G   1% /data

## 可以看到sdb已经挂载上去了
[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 17.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk 

如果想给/(根)挂载点扩容,咋办?

[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 17.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk /data

可以看到/根挂载点的类型是lvm,这说明他可以动态扩容,为了试验,我加了一块15G的硬盘:

[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 17.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk /data
sdc                              8:32   0   15G  0 disk 
  • 接下来我会把sdc扩容到dev/vg_micocube/lv_root 即/节点
  • 给sdc分区,为了方便,这里只分一个区,注意代码8e,即Changed system type of partition 1 to 8e (Linux LVM),分区的系统类型修改为lvm,
[root@micocube ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x66a87408.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1958, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1958, default 1958): 
Using default value 1958

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdc: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x66a87408

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1958    15727603+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 接着创建pv,扩展vg,扩展lv,resize2fs
[root@micocube ~]# pvcreate /dev/sdc1 
  Physical volume "/dev/sdc1" successfully created
## 查看所有vg
[root@micocube ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  vg_micocube   1   2   0 wz--n- 19.51g    0 
## 扫描vg
[root@micocube ~]# vgscan 
  Reading all physical volumes.  This may take a while...
  Found volume group "vg_micocube" using metadata type lvm2
[root@micocube ~]# vgextend vg_micocube /dev/sdc1 
[root@micocube ~]# lvextend -L +15G /dev/mapper/vg_micocube-lv_root 
  Insufficient free space: 3840 extents needed, but only 3839 available
[root@micocube ~]# lvextend -L +14G /dev/mapper/vg_micocube-lv_root 
  Size of logical volume vg_micocube/lv_root changed from 17.51 GiB (4482 extents) to 31.51 GiB (8066 extents).
  Logical volume lv_root successfully resized.
  [root@micocube ~]# df -hlT
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_micocube-lv_root
                     ext4    18G  1.9G   15G  12% /
tmpfs                tmpfs  939M     0  939M   0% /dev/shm
/dev/sda1            ext4   477M   41M  412M   9% /boot
/dev/sdb             ext4   9.8G   23M  9.2G   1% /data
[root@micocube ~]# resize2fs -f /dev/mapper/vg_micocube-lv_root 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_micocube-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg_micocube-lv_root to 8259584 (4k) blocks.
The filesystem on /dev/mapper/vg_micocube-lv_root is now 8259584 blocks long.
[root@micocube ~]# df -hlT
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_micocube-lv_root
                     ext4    31G  1.9G   28G   7% /
tmpfs                tmpfs  939M     0  939M   0% /dev/shm
/dev/sda1            ext4   477M   41M  412M   9% /boot
/dev/sdb             ext4   9.8G   23M  9.2G   1% /data

### 可以看到已经扩容成功了
[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk /data
sdc                              8:32   0   15G  0 disk 
└─sdc1                           8:33   0   15G  0 part 
  └─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /

如果想给/data节点扩容呢?

emmm,我又重新添加了一块磁盘sdd:

[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk /data
sdc                              8:32   0   15G  0 disk 
└─sdc1                           8:33   0   15G  0 part 
  └─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
sdd                              8:48   0   20G  0 disk 
  • 先卸载sdb,umount /dev/sdb或者umount /data
## 将之前写入分区表的 /dev/sdb                /data                   ext4    defaults        0 0 这一行删除
[root@micocube ~]# vi /etc/fstab 
[root@micocube ~]# umount /dev/sdb
umount: /data: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@micocube ~]# fuser -m -v /dev/sdb
                     USER        PID ACCESS COMMAND
/data/:              root       1895 F.... bash
### 这一步可能连接会断,毕竟你用到了/data 重连就好了
  • 因为没有分区,不是lvm类型,肯定是扩容不了的,只能给磁盘重新分区
[root@micocube ~]# fdisk /dev/sdb 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xe7603f99.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
### 我想直接改变类型,行不通,只能重新创建分区
Command (m for help): t
No partition is defined yet!

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 
Using default value 1305

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe7603f99

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1305    10482381   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.
  • 在进行下一步之前,我还想再强调一下pv/vg/lv的关系
    一个硬盘分区组成一个pv(物理卷)
    一个或者多个pv组成一个vg(卷组)
    一个vg可以划分出多个lv(逻辑卷)
  • 创建pv
## 用分区/dev/sdb1创建一个pv
[root@micocube ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
## 这一阶段并没有什么变化
[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk 
└─sdb1                           8:17   0   10G  0 part 
sdc                              8:32   0   15G  0 disk 
└─sdc1                           8:33   0   15G  0 part 
  └─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
sdd                              8:48   0   20G  0 disk 
  • 如果sdb1找不到可以试着重读分区表
## 重读分区表
[root@micocube ~]# partprobe 
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sdc (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
  • 创建vg
## 用pv(/dev/sdb1)创建一个vg
[root@micocube ~]# vgcreate vg_data /dev/sdb1 
  Volume group "vg_data" successfully created
  • 查看所有vg
## 查看创建的vg
[root@micocube ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_data
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.99 GiB
  PE Size               4.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               iyB24d-X5KM-mhKR-Lxw1-kL9i-VALO-ez4EnG
   
  --- Volume group ---
  VG Name               vg_micocube
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizableF
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               34.50 GiB
  PE Size               4.00 MiB
  Total PE              8833
  Alloc PE / Size       8578 / 33.51 GiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               KBuu3K-sZ9j-NdYo-RaK3-ajJs-UAQm-3NJK53
  • 创建lv
## -L 后面是分给lv的大小,-n 新建名字为vg_data的lv,所属卷组(vg)为vg_data
[root@micocube ~]# lvcreate -L 9.99G -n vg_data vg_data
  Rounding up size to full physical extent 9.99 GiB
  Logical volume "vg_data" created.
  • 格式化lv
## dev后边第一个是vg 第二个是lv
[root@micocube ~]# mkfs.ext4 /dev/vg_data/vg_data 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2619392 blocks
130969 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 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

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

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  • 永久挂载
[root@micocube ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 28 05:50:32 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_micocube-lv_root /                       ext4    defaults        1 1
UUID=57ad23fa-8c2f-42f8-91c1-01e5bcf4a058 /boot                   ext4    defaults        1 2
/dev/mapper/vg_micocube-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/vg_data/vg_data                    /data                   ext4    defaults        0 0
  • 重新挂载
### 重新挂载,使/etc/fstab生效
[root@micocube ~]# mount -a
  • 为了测试扩容后数据是否丢失
#### 按惯例先写一些文件,看扩容后是否还存在
[root@micocube ~]# echo 111111111111 >/data/a.txt
[root@micocube ~]# echo 222222222222 >/data/b.txt
[root@micocube ~]# echo 333333333333 >/data/c.txt
[root@micocube ~]# ls /data/
a.txt  b.txt  c.txt  lost+found
[root@micocube ~]# cat /data/*.txt
111111111111
222222222222
333333333333
  • 给sdd分区
### sdd分区
[root@micocube ~]# fdisk /dev/sdd 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x7a10ae95.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 
Using default value 2610

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdd: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7a10ae95

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1        2610    20964793+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 创建pv
### 创建pv
[root@micocube ~]# pvcreate /dev/sdd1 
  Physical volume "/dev/sdd1" successfully created
[root@micocube ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree   
  vg_data       1   1   0 wz--n-  9.99g       0 
  vg_micocube   2   2   0 wz--n- 34.50g 1020.00m
  
[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk 
└─sdb1                           8:17   0   10G  0 part 
  └─vg_data-vg_data (dm-2)     253:2    0   10G  0 lvm  /data
sdc                              8:32   0   15G  0 disk 
└─sdc1                           8:33   0   15G  0 part 
  └─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
sdd                              8:48   0   20G  0 disk 
└─sdd1                           8:49   0   20G  0 part 
  • 将创建的pv扩展到vg_data卷组中
## 将创建的pv扩展到vg_data卷组中
[root@micocube ~]# vgextend vg_data /dev/sdd1 
  Volume group "vg_data" successfully extended
  • 扩展lv
## 扩展lv
[root@micocube ~]# lvextend -L +20G /dev/vg_data/vg_data 
  Insufficient free space: 5120 extents needed, but only 5118 available
[root@micocube ~]# lvextend -L +19.9G /dev/vg_data/vg_data
  Rounding size to boundary between physical extents: 19.90 GiB.
  Size of logical volume vg_data/vg_data changed from 9.99 GiB (2558 extents) to 29.89 GiB (7653 extents).
  Logical volume vg_data successfully resized.
  • resize2fs 使扩展生效
## 写入扩展
[root@micocube ~]# resize2fs -f /dev/vg_data/vg_data 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_data/vg_data is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_data/vg_data to 7836672 (4k) blocks.

The filesystem on /dev/vg_data/vg_data is now 7836672 blocks long.
### 可以看到sdb和sdd已经合并了
[root@micocube ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                             11:0    1 55.3M  0 rom  
sda                              8:0    0   20G  0 disk 
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0 19.5G  0 part 
  ├─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
  └─vg_micocube-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                              8:16   0   10G  0 disk 
└─sdb1                           8:17   0   10G  0 part 
  └─vg_data-vg_data (dm-2)     253:2    0 29.9G  0 lvm  /data
sdc                              8:32   0   15G  0 disk 
└─sdc1                           8:33   0   15G  0 part 
  └─vg_micocube-lv_root (dm-0) 253:0    0 31.5G  0 lvm  /
sdd                              8:48   0   20G  0 disk 
└─sdd1                           8:49   0   20G  0 part 
  └─vg_data-vg_data (dm-2)     253:2    0 29.9G  0 lvm  /data
  • 检查文件是否丢失
### 检查文件是否丢失
[root@micocube ~]# ls /data/
a.txt  b.txt  c.txt  lost+found
[root@micocube ~]# cat /data/*.txt
111111111111
222222222222
333333333333
### 未丢失

一次给oracle数据库盘扩容的经历

  • 接手的时候,oracle数据库文件在一个磁盘(/dev/vdb)的vdb7分区里,即类型为part,挂载点为/database/db1,也就是说,没有建lvm(逻辑卷管理),直接格式化分区,数据库文件全在里边(dbf),150G,已经用了差不多99%,另外加了一块600G的空白磁盘(/dev/vdc)很明显,是不能直接扩容的,因为是开发环境,那就有的搞了,随便停机多久都没问题啊
  • 思路
    • 将vdc分区(/dev/sdc1),分区系统类型为lvm,创建pv/vg(vg_data)/lv(vg_data_lv),挂载到/db/temp(之前不存在)文件夹
    • 将/database/db1/下的所有文件拷贝到/db/temp,cp -R /database/db1/* /db/temp/
    • vd7 已经是part类型了,那就直接创建pv,之后vgextend vg_data /dev/vdb7,扩展/dev/vdb7到vg_data卷组,lvextend -L +150G /dev/vg_data/vg_data_lv,将原本的150G分配给/dev/vg_data/vg_data_lv
    • resize2fs -f /dev/vg_data/vg_data_lv
    • 扩容成功之后将/dev/vg_data/vg_data_lv永久挂载到/database/db1
  • 意外:
    • resize2fs 报错kernel does not support online resizing:先将/dev/vg_data/vg_data_lv卸载(umount /dev/vg_data/vg_data_lv),再执行resize2fs -f /dev/vg_data/vg_data_lv,之后挂载(mount -a),参考连接:‘filesystem not support online resizing’ 错误
    • 扩容之后数据库起不来了,报错:ORA-00205: error in identifying control file, check alert log for more info,造成这个问题的原因是oracle用户访问不了/database/db1,因为扩容的时候用的是root用户创建的,所以没权限,更改用户所属组就好了,chown -R oracle:dba /database/db1 就好了.参考连接:ORACLE11g 实例启动报错 ORA-00205: error in identifying control file, check alert log for more info

总结

  • 使用空白磁盘的时候不要直接格式化整个磁盘,最少给磁盘分个区,分区系统类型设置为lvm(8e),再创建pv/vg/lv,要不然以后扩容麻烦
  • lvextend命令最好再加上-r 参数
    lvextend -r -l +100%FREE /dev/mapper/VolGroup-lv_home
    如果不加 -r 参数,需要手动使用 resize2fs /dev/mapper/VolGroup-lv_home才能生效。

lvm 的常用操作

lv/pv/vg 查看显示 创建 删除 扩容 激活 扫描查找
LV lvdisplay lvcreate lvremove lvextend lvchange lvscan
PV pvdisplay pvcreate pvremove pvchange pvscan
VG vgdisplay vgcreate vgremove vgextend vgchange vgscan

参考链接

RedHat/CentOS根目录扩容
linux分区满了,如何进行扩容
【linux】新添加一块硬盘制作LVM卷并进行分区挂载
故障处理:磁盘扩容出错:e2fsck: Bad magic number in super-block while trying to open /dev/vdb1

你可能感兴趣的:(Linux(Redhat6.9) 磁盘格式化/扩容)