LVM-逻辑卷管理

屏蔽底层磁盘布局,便于动态管理磁盘容量

创建新的swap分区

准备一个新的分区,这里我选择/dev/sdb1,并在建立sdb1分区的时候打标签82,最后让内核重新识别新建的分区partprobe

[root@server_1 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 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 label type: dos
Disk identifier: 0xdd460f12

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   82  Linux swap / Solaris

[root@server_1 ~]# lsblk /dev/sdb1

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb1   8:17   0   2G  0 part

格式化为交换分区

[root@server_1 ~]# mkswap /dev/sdb1

mkswap: /dev/sdb1: warning: wiping old swap signature.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=3f1479f0-b83b-4631-b6b4-109f0f482f34

未启用前查看

[root@server_1 ~]# swapon -s

Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2047996 0       -1

[root@server_1 ~]# free -m

              total        used        free      shared  buff/cache   available
Mem:           1824         324        1122           9         378        1313
Swap:          1999           0        1999

启用交换分区

[root@server_1 ~]# swapon /dev/sdb1

启用后查看

[root@server_1 ~]# swapon -s

Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2047996 0       -1
/dev/sdb1                               partition       2097148 0       -2

[root@server_1 ~]# free -m

              total        used        free      shared  buff/cache   available
Mem:           1824         325        1120           9         378        1312
Swap:          4047           0        4047

开机自动挂载

[root@server_1 ~]# vim /etc/fstab

/dev/sdb1   swap swap defaults 0 0

删除swap分区

  1. 关闭swap分区
    [root@server_1 ~]# swapoff /dev/sdb1
  2. 将/dev/sdb1 标签改回83
    [root@server_1 ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 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 label type: dos
Disk identifier: 0xdd460f12

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
  1. 删除/etc/fstab增加的内容
    [root@server_1 ~]# vim /etc/fstab
/dev/sdb1   swap swap defaults 0 0

LVM-逻辑卷管理

给分区打标签,物理卷_PV

使用/dev/sdb1 /dev/sdb2 打标签8e
[root@server_1 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 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 label type: dos
Disk identifier: 0xdd460f12

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM

创建PV
[root@server_1 ~]# pvcreate /dev/sdb1 /dev/sdb2

  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

[root@server_1 ~]# pvdisplay

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               test
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               I5YIQU-fS4b-4Cur-7fkK-PfxI-R80x-r3LIIk

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               test
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               CeJ8Ye-Lnva-X3Nz-Ffa8-7PJl-626O-15sw2j

将打标签的分区组成卷组_VG

[root@server_1 ~]# vgcreate test /dev/sdb1 /dev/sdb2

Volume group "test" successfully created

[root@server_1 ~]# vgdisplay

  --- Volume group ---
  VG Name               test
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               3.99 GiB
  PE Size               4.00 MiB
  Total PE              1022
  Alloc PE / Size       0 / 0
  Free  PE / Size       1022 / 3.99 GiB
  VG UUID               6evqe0-rXfQ-oaxd-wAcl-37fA-E3xV-PcteLP

从卷组分隔空间组成逻辑卷_LV

从卷组test中划分大小为100个PE(PE默认大小4MB)命名为testlv1
[root@server_1 ~]# lvcreate -n testlv1 -l 100 /dev/test

Logical volume "testlv1" created.

从卷组test中划分大小为1G 命名为testlv2
[root@server_1 ~]# lvcreate -n testlv2 -L 1G /dev/test

Logical volume "testlv2" created.

[root@server_1 ~]# lvdisplay

  --- Logical volume ---
  LV Path                /dev/test/testlv1
  LV Name                testlv1
  VG Name                test
  LV UUID                RslhqC-IjYT-xSum-zRXn-sCdN-l5YK-CcKS9Y
  LV Write Access        read/write
  LV Creation host, time server_1, 2019-05-11 16:34:50 +0800
  LV Status              available
  # open                 0
  LV Size                400.00 MiB
  Current LE             100
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/test/testlv2
  LV Name                testlv2
  VG Name                test
  LV UUID                B8klrN-Kvvj-UWdD-MIdy-hnPo-ewcT-o1QZ3L
  LV Write Access        read/write
  LV Creation host, time server_1, 2019-05-11 16:35:49 +0800
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

格式化并挂载

[root@server_1 ~]# mkfs.ext4 /dev/test/testlv1

mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

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

[root@server_1 ~]# mkdir /testlv1
[root@server_1 ~]# mount /dev/test/testlv1 /testlv1/
[root@server_1 ~]# df -Th

Filesystem               Type      Size  Used Avail Use% Mounted on
/dev/mapper/test-testlv1 ext4      380M  2.3M  354M   1% /testlv1

扩展lv

加100个PE
[root@server_1 ~]# lvextend -l +100 /dev/test/testlv1

  Size of logical volume test/testlv1 changed from 400.00 MiB (100 extents) to 800.00 MiB (200 extents).
  Logical volume testlv1 successfully resized.

加200M
[root@server_1 ~]# lvextend -L +200M /dev/test/testlv1

  Size of logical volume test/testlv1 changed from 800.00 MiB (200 extents) to 1000.00 MiB (250 extents).
  Logical volume testlv1 successfully resized.

指定大小2G
[root@server_1 ~]# lvextend -L 2G /dev/test/testlv1

  Size of logical volume test/testlv1 changed from 1000.00 MiB (250 extents) to 2.00 GiB (512 extents).
  Logical volume testlv1 successfully resized.

重新读取
[root@server_1 ~]# resize2fs -p /dev/test/testlv1

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/test/testlv1 is mounted on /testlv1; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 16
The filesystem on /dev/test/testlv1 is now 2097152 blocks long.

[root@server_1 ~]# df -Th

Filesystem               Type      Size  Used Avail Use% Mounted on
/dev/mapper/test-testlv1 ext4      2.0G  2.9M  1.9G   1% /testlv1

缩减lv

减小逻辑卷可能造成数据损坏
[root@server_1 ~]# umount /testlv1/
[root@server_1 ~]# e2fsck -f /dev/test/testlv1

e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/test/testlv1: 11/524288 files (0.0% non-contiguous), 77387/2097152 blocks

[root@server_1 ~]# resize2fs -p /dev/test/testlv1 1G

resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/test/testlv1 to 1048576 (1k) blocks.
Begin pass 3 (max = 256)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/test/testlv1 is now 1048576 blocks long.

[root@server_1 ~]# lvreduce -L 1G /dev/test/testlv1

  WARNING: Reducing active logical volume to 1.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testlv1? [y/n]: y
  Size of logical volume test/testlv1 changed from 2.00 GiB (512 extents) to 1.00 GiB (256 extents).
  Logical volume testlv1 successfully resized.

[root@server_1 ~]# mount /dev/test/testlv1 /testlv1/
[root@server_1 ~]# df -Th

Filesystem               Type      Size  Used Avail Use% Mounted on
/dev/mapper/test-testlv1 ext4      984M  2.8M  934M   1% /testlv1

移除逻辑卷

[root@server_1 ~]# umount /testlv1/
[root@server_1 ~]# lvremove /dev/test/testlv1

Do you really want to remove active logical volume testlv1? [y/n]: y
  Logical volume "testlv1" successfully removed

[root@server_1 ~]# vgremove test

Do you really want to remove volume group "test" containing 1 logical volumes? [y/n]: y
Do you really want to remove active logical volume testlv2? [y/n]: y
  Logical volume "testlv2" successfully removed
  Volume group "test" successfully removed

[root@server_1 ~]# pvremove /dev/sdb1 /dev/sdb2

  Labels on physical volume "/dev/sdb1" successfully wiped
  Labels on physical volume "/dev/sdb2" successfully wiped

将dev/sdb1 /dev/sdb2改回标签83
[root@server_1 ~]# fdisk -l

Disk /dev/sdb: 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 label type: dos
Disk identifier: 0xdd460f12

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   83  Linux

你可能感兴趣的:(RHCE)