前言

原先使用普通挂载方式,即分区👉🏻格式化ext4👉🏻挂载

突然间想尝试一下使用`lvm+xfs`的方式挂载磁盘练练手(瞎折腾)

现状

除了系统盘,一共挂载了4块硬盘,其中3块HDD,一块SSD,其中`/dev/sdd`的硬盘存放其他盘的所有文件,打算先把其余3块盘做好,数据迁移出来,最后再做这块。

磁盘结构如下:

[root@jorelSpace ~]# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop4                       7:4    0  40.9M  1 loop /snap/snapd/20290
sda                         8:0    0 223.6G  0 disk
├─sda1                      8:1    0     1G  0 part
├─sda2                      8:2    0     2G  0 part
└─sda3                      8:3    0 220.5G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0 220.5G  0 lvm  /
sdb                         8:16   0 931.5G  0 disk
└─sdb1                      8:17   0 931.5G  0 part
sdc                         8:32   0   3.6T  0 disk
└─sdc1                      8:33   0   3.6T  0 part
sdd                         8:48   0   7.3T  0 disk
└─sdd1                      8:49   0   7.3T  0 part
nvme0n1                   259:0    0   3.6T  0 disk
└─nvme0n1p1               259:3    0   3.6T  0 part

目前只有一个pvs:

[root@jorelSpace ~]# pvs
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <220.52g    0

规划

规划说明:

  • dataBackup:配置、资料、docker等备份

  • education:学习资料

  • relaxation:娱乐

  • smbSer/document:远程存取

  • smbSer/timeMachine:MacOS远程备份

  • dockerData:docker化的各种服务、数据存储

实施

1. 依次创建lvm分区

由于已经创建过分区,所以需要先删除分区再重新创建。

说明:

  1. 首先使用`umount /dev/sdb1`卸载磁盘

  2. fdisk命令用于分区管理,使用的命令如下:

    1. p:显示当前分区

    2. d:删除分区,后面的数字即第几个分区

    3. n:创建新分区,分区信息根据需要配置,依次是分区数、起始物理扇区、结束物理扇区

    4. t:更改为`lvm`类型,输完`t`回车后再输入`L`回车,即可查看系统支持的文件系统类型,找到`Linux LVM`类型,输入前面的序号然后回车

    5. w:保存当前操作,分区完成

其余磁盘依次操作即可

[root@jorelSpace ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.37.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/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10EZEX-08W
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 90935690-4DEF-4569-98F4-049C918E1221

Device     Start        End    Sectors   Size Type
/dev/sdb1   2048 1953525134 1953523087 931.5G Linux LVM

Command (m for help): d 1
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10EZEX-08W
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 90935690-4DEF-4569-98F4-049C918E1221

Command (m for help): n
Partition number (1-128, default 1):
First sector (34-1953525134, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1953525134, default 1953525134):

Created a new partition 1 of type 'Linux filesystem' and of size 931.5 GiB.
Partition #1 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: Y

The signature will be removed by a write command.

Command (m for help): p
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10EZEX-08W
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 90935690-4DEF-4569-98F4-049C918E1221

Device     Start        End    Sectors   Size Type
/dev/sdb1   2048 1953525134 1953523087 931.5G Linux filesystem

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help): t
Selected partition 1
Partition type or alias (type L to list all): L
  1 EFI System                     C12A7328-F81F-11D2-BA4B-00A0C93EC93B
  2 MBR partition scheme           024DEE41-33E7-11D3-9D69-0008C781F39F
  3 Intel Fast Flash               D3BFE2DE-3DAF-11DF-BA40-E3A556D89593
  4 BIOS boot                      21686148-6449-6E6F-744E-656564454649
...................
...................
 25 Linux root (ARM-64)            B921B045-1DF0-41C3-AF44-4C6F280D3FAE
 26 Linux root (IA-64)             993D8D3D-F80E-4225-855A-9DAF8ED7EA97
 27 Linux reserved                 8DA63339-0007-60C0-C436-083AC8230908
 28 Linux home                     933AC7E1-2EB4-4F13-B844-0E14E2AEF915
 29 Linux RAID                     A19D880F-05FC-4D3B-A006-743F0F84911E
 30 Linux LVM                      E6D6D379-F507-44C2-A23C-238F2A3DF928
 31 Linux variable data            4D21B016-B534-45C2-A9FB-5C16E091FD2D
...................
...................
107 HiFive FSBL                    5B193300-FC78-40CD-8002-E86C45580B47
108 HiFive BBL                     2E54B353-1271-4842-806F-E436D6AF6985

Aliases:
   linux          - 0FC63DAF-8483-4772-8E79-3D69D8477DE4
   swap           - 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
   home           - 933AC7E1-2EB4-4F13-B844-0E14E2AEF915
   uefi           - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
   raid           - A19D880F-05FC-4D3B-A006-743F0F84911E
   lvm            - E6D6D379-F507-44C2-A23C-238F2A3DF928
Partition type or alias (type L to list all): 30
Changed type of partition 'Linux filesystem' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10EZEX-08W
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 90935690-4DEF-4569-98F4-049C918E1221

Device     Start        End    Sectors   Size Type
/dev/sdb1   2048 1953525134 1953523087 931.5G Linux LVM

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

完成上述操作后,命令查看磁盘分区情况(和原来并没有什么区别):

[root@jorelSpace ~]# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop4                       7:4    0  40.9M  1 loop /snap/snapd/20290
sda                         8:0    0 223.6G  0 disk
├─sda1                      8:1    0     1G  0 part
├─sda2                      8:2    0     2G  0 part
└─sda3                      8:3    0 220.5G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0 220.5G  0 lvm  /
sdb                         8:16   0 931.5G  0 disk
└─sdb1                      8:17   0 931.5G  0 part
sdc                         8:32   0   3.6T  0 disk
└─sdc1                      8:33   0   3.6T  0 part
sdd                         8:48   0   7.3T  0 disk
└─sdd1                      8:49   0   7.3T  0 part
nvme0n1                   259:0    0   3.6T  0 disk
└─nvme0n1p1               259:1    0   3.6T  0 part

2. 创建`pv、vg、lvm`

# 创建pv
[root@jorelSpace ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

# 创建vg,dataBackup为vg名称
[root@jorelSpace ~]# vgcreate dataBackup-vg /dev/sdb1
  Volume group "dataBackup" successfully created
[root@jorelSpace ~]# vgs
  VG         #PV #LV #SN Attr   VSize    VFree
  dataBackup   1   0   0 wz--n- <931.51g <931.51g
  ubuntu-vg    1   1   0 wz--n- <220.52g       0

# 创建lvm,参数:lvcreate vg名 -l 磁盘大小 lv名称
[root@jorelSpace ~]# lvcreate dataBackup  -l 100%VG -n dataBackup-lv
  Logical volume "dataBackup-lv" created.
[root@jorelSpace ~]# lvs
  LV            VG         Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  dataBackup-lv dataBackup -wi-a----- <931.51g
  ubuntu-lv     ubuntu-vg  -wi-ao---- <220.52g

# 格式化分区
[root@jorelSpace ~]# mkfs.xfs /dev/dataBackup/dataBackup-lv
meta-data=/dev/dataBackup/dataBackup-lv isize=512    agcount=4, agsize=61047296 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=244189184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=119233, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

3. 创建lv时指定使用vg容量的大小

# 50%VG容量
lvcreate nvme-vg -l 50%VG -n nvme-smb-data-lv
 
# 25%VG容量
lvcreate nvme-vg -l 25%VG -n nvme-docker-lv

# 100%VG剩余容量
lvcreate nvme-vg -l 100%free -n nvme-smb-tM-lv

4. 挂载磁盘

建议使用逻辑卷目录或者`UUID`进行挂载

# /dev/nvme0n1p1
/dev/mapper/nvme--vg-nvme--docker--lv              /dockerData                             xfs       defaults      0       0
/dev/mapper/nvme--vg-nvme--smb--tM--lv             /smbSer/timeMachine                     xfs       defaults      0       0
/dev/mapper/nvme--vg-nvme--smb--data--lv           /smbSer/document                        xfs       defaults      0       0

# /dev/sdc1
/dev/mapper/education--vg-education--lv            /education                              xfs       defaults      0       0
# /dev/sdb1
/dev/mapper/dataBackup--vg-dataBackup--lv          /dataBackup                             xfs       defaults      0       0

完成

分区完成效果如下:

[root@jorelSpace relaxation]# df -h
Filesystem                                 Size  Used Avail Use% Mounted on
tmpfs                                      1.6G  3.5M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv          216G  111G   95G  54% /
tmpfs                                      1.6G  4.0K  1.6G   1% /run/user/0
/dev/sda2                                  2.0G  130M  1.7G   8% /boot
/dev/sda1                                  1.1G  6.1M  1.1G   1% /boot/efi
/dev/mapper/nvme--vg-nvme--docker--lv      932G   39G  893G   5% /dockerData
/dev/mapper/nvme--vg-nvme--smb--tM--lv     932G  6.6G  925G   1% /smbSer/timeMachine
/dev/mapper/nvme--vg-nvme--smb--data--lv   1.9T  881G  982G  48% /smbSer/document
/dev/mapper/education--vg-education--lv    3.7T   26G  3.7T   1% /education
/dev/mapper/dataBackup--vg-dataBackup--lv  932G  6.6G  925G   1% /dataBackup
/dev/sdd1                                  7.3T  2.9T  4.1T  42% /relaxation


[root@jorelSpace relaxation]# lsblk -f
NAME                              FSTYPE      FSVER    LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
loop4                             squashfs    4.0                                                         0   100% /snap/snapd/20290
sda
├─sda1                            vfat        FAT32          ED12-A443                                   1G     1% /boot/efi
├─sda2                            ext4        1.0            95be8ffd-5d54-4902-a5e8-74a724239df0      1.7G     7% /boot
└─sda3                            LVM2_member LVM2 001       6YvVgZ-Orvz-wajD-jRI4-Vpyw-3CIR-hB8eRf
  └─ubuntu--vg-ubuntu--lv         ext4        1.0            e15a4dc3-a4c1-4629-9fab-53884e412787     94.5G    51% /
sdb
└─sdb1                            LVM2_member LVM2 001       mVZgY1-afa1-QT6z-3oha-Pgzu-dOM8-PcXvel
  └─dataBackup--vg-dataBackup--lv xfs                        a93d9c29-e954-42d3-a60c-42e147d55927    924.5G     1% /dataBackup
sdc
└─sdc1                            LVM2_member LVM2 001       UIebiB-XYyX-hYbd-Ia1R-i5Bx-mSxH-qtgC1a
  └─education--vg-education--lv   xfs                        d04ba311-f149-4429-8c61-1af2493cf179      3.6T     1% /education
sdd
└─sdd1                            ext4        1.0            962c9104-bd43-4656-b34c-ac7c7a94d66c        4T    39% /relaxation
nvme0n1
└─nvme0n1p1                       LVM2_member LVM2 001       mhllce-id1u-JHtS-4ZH7-1P5g-3xDq-bFHlXd
  ├─nvme--vg-nvme--smb--data--lv  xfs                        8a123775-120a-4967-a737-bb97ecaf7aaa    981.6G    47% /smbSer/document
  ├─nvme--vg-nvme--docker--lv     xfs                        d35fc6e1-d00a-4e61-9fad-77f7ea43faff    892.6G     4% /dockerData
  └─nvme--vg-nvme--smb--tM--lv    xfs                        d712db7b-5a34-43e2-bf31-410230face85    924.5G     1% /smbSer/timeMachin