lvm + raid (logical disk + array) create delete recovery for linux

This tutorial is suitable for linux
lvm as a logical disk and raid as an array. The two technologies can be used alone or in combination.
2023.9.3 update

The first three sections are operation commands and basic knowledge, followed by practical operations.

1. Storage hardware view related commands

硬盘分区相关操作在后面用的到,可以先略过,有需要了再回来查

(1) view

1. View all storage hardware details

fdisk -l

2. Check the file system

df -h

3. Check the hardware mount status

lsblk

4. Hard disk temperature, power-on time and other information

Check out this detailed description: linux view hard disk details

(2) Create a new partition and format it

Use fdisk -l to get all connected hard disks, for example, to create a new partition on the /dev/sde disk and mount it to the /cloud directory (there was no such directory before)
新建ext4文件系统分区

fdisk /dev/sde

n->Enter->p (view partition)->w (save)->q (exit)

mkfs.ext4 /dev/sde1
mkfs -t ext4 /dev/sde1

新建目录

mkdir /cloud

将目录和硬盘关联

mount /dev/sde1 /cloud

(3) Start up and mount

For non-desktop versions of linux, if you do not set up the newly added disk, it will become invalid after restarting.
Use the uuid of the disk to mount, do not use /dev/xxx to mount, otherwise restarting in some cases may serialize the disk
using fdisk -l Get the uuid of the hard disk

vi /etc/fstab

For example, mount 5607a174-cfb3-476f-b509-a0e2e5f9654b under /cloud and add this information

UUID=5607a174-cfb3-476f-b509-a0e2e5f9654b /cloud            ext4    defaults        0       2

(4) Delete the partition

For example, to delete the /dev/sde1 partition, this partition is mounted in the /cloud directory
卸载目录

umount /cloud

删除

fdisk /dev/sde1

d->Enter->w (may need to delete multiple times)

2. RAID explanation and basic commands

For detailed operation of specific cases, see four

(1) Description

(2) Relying on software installation

apt-get install mdadm

or

dnf install mdadm

(3) Order

1. disable raid

disable /dev/md0

mdadm -S /dev/md0

Three, lvm explanation and command

If one of the LVM disks is broken, the others will not be affected. It is not that one is broken and all of them are broken.

For detailed operation of specific cases, see four

(1) Description

1. Physical hardware

A specific hard disk, since each hardware in Linux is a "file", at the system level, the shape is /dev/sda

2. Physical volume PV (physical volume)

It can be a hard disk partition or a grouped raid

3. Volume group VG (Volume Group)

One or more physical volumes form a volume group

4. Logical volume LV (logical volume)

Partitions on volume groups

(2) Relying on software installation

如果缺失这个软件,已有的lvm也无法正常运行(假如无意之中卸载了)

apt-get install lvm2

or

dnf install lvm2

(3) Order

Lvm is available by default in linux, and it is recommended to use lvm when installing the system in recent years

1. View class-related commands

查看物理卷PV

pvdisplay

查看卷组VG

vgdisplay

查看逻辑卷lv

lvdisplay

查看逻辑卷lv挂载状态

lvscan

2. Create class related commands

创建物理卷PV

pvcreate /dev/sdc1

创建卷组VG

3. Uninstall related commands

``

3. Specific application - creation

(1) raid0-parallel-pursuit of speed

1. Description

In this case, the damage of one hard disk will lead to data loss. The raid0 group with different capacities will calculate the capacity according to the smallest one. It is recommended to use hard disks with the same capacity.

2. Advantages and disadvantages

Advantages: Fast speed, equivalent to parallel
Disadvantages: If one disk is damaged, the entire data cannot be used directly

3. Practical operation

The following operation forms /dev/sda /dev/sdc into a raid0, and the combined raid is called /dev/md0

mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sda /dev/sdc

Check the creation result, if the result can be output, it can be

cat /proc/mdstat

(2) raid1-mirror-safety

1. Description

In this case, the combined raid will only have the capacity of the smallest disk, and hard disks with the same capacity are generally selected.

2. Advantages and disadvantages

Advantages: Unless two disks are damaged at the same time, the data is complete. If two disks are damaged at the same time, but they are not damaged in one place, they can be recovered and the data is still there. That is to say, unless two disks are broken in one place at the same time and cannot be recovered, and the lost data is needed, the data will be lost.
Disadvantages: The speed is slightly lower than that of one disk, and the maximum space utilization rate is 50% (two disks are the same size)

3. Practical operation

The following operation forms /dev/sda /dev/sdc into a raid1, and the combined raid is called /dev/md1

mdadm --create --verbose /dev/md1 --level=1 --raid-devices=2 /dev/sda /dev/sdc

Check the creation result, if the result can be output, it can be

cat /proc/mdstat

(3) raid10-speed and security

1. Idea

It is equivalent to forming a mirror (raid1) every 2 pieces, and then two raid1s to form raid0
(there is another kind of raid01, two disks form raid0, and then two raid0s form raid1, this method inherits the shortcomings of raid0 and raid1, Raid01 is not recommended, only raid10 is recommended)

2. Advantages and disadvantages:

Advantages: fast and safe
Disadvantages: hard disk utilization is up to 50%

3. Practical operation

Suppose 4 hard disks
先创建两个raid1
/dev/sda and /dev/sdb group /dev/mda
/dev/sdc and /dev/sdd group /dev/mdb

mdadm --create --verbose /dev/mda --level=1 --raid-devices=2 /dev/sda /dev/sdb
mdadm --create --verbose /dev/mdb --level=1 --raid-devices=2 /dev/sdc /dev/sdd

再创建一个raid0

mdadm --create --verbose /dev/md10 --level=0 --raid-devices=2 /dev/mda /dev/mdb

The last combination is /dev/md10, just partition and format on this raid (the operation method is above, see the directory to find)

(4) Flexible expansion and security (raid1+lvm)

1. Idea

There is one raid1 image for every two hard disk groups, and one lvm for multiple raid1 groups. You can only use 2 disks, and then expand the capacity later, so that in the end it can be only one partition at the system level. For important data, this method is strongly recommended

2. Advantages and disadvantages

Advantages: flexible expansion and security
Disadvantages: 50% hard disk utilization

3. Practical operation

/dev/sda and /dev/sdb group /dev/mda
/dev/sdc and /dev/sdd group /dev/mdb
and then 2 raid1 group lvm, finally formatted and mounted to the /mirror directory

先组2个raid1

mdadm --create --verbose /dev/mda --level=1 --raid-devices=2 /dev/sda /dev/sdb
mdadm --create --verbose /dev/mdb --level=1 --raid-devices=2 /dev/sdc /dev/sdd

2个raid1组lvm

Both raid1 create pv first

pvcreate /dev/mda
pvcreate /dev/mdb

Create a vg named GroupMirror

vgcreate GroupMirror /dev/mda /dev/mdb

激活

vgchange -a y GroupMirror
lvcreate -l 100%FREE -n mirror_data GroupMirror

Pay attention to uppercase and lowercase, lowercase -l can write the ratio, uppercase is the specific value

mkfs.ext4 /dev/GroupMirror/mirror_data
mkdir /mirror && mount -t ext4 /dev/GroupMirror/mirror_data /mirror 

(five) raid5

update later

(6) raid6

update later

4. Specific application - adjustment

(1) LVM volume group expansion

Lvm can use a physical disk or a raid as the basis to build lvm.
Next expand a disk /dev/md2 to a volume group named GroupMirror
创建lvm物理卷

pvcreate /dev/md2

Expand volume group

vgextend GroupMirror /dev/md2

It is still not effective at this time, modify the command according to your own name below (100% of the space of the new disk is expanded to the original)

lvextend -l +100%FREE /dev/mapper/GroupMirror-mirror_data

The above operation still does not take effect

-------If it is in ext format, use this to refresh (-p is the actual percentage)

resize2fs -p /dev/mapper/GroupMirror-mirror_data

-------If it is in xfs format, use the following

xfs_growfs /dev/mapper/GroupMirror-mirror_data

I expanded an 8T mechanical disk and spent about an hour

5. Specific application - uninstall

(1) Uninstall lvm

1. Remove the mounted partition

For example, the mounted partition is called /mirror

umount /mirror

2. Delete the logical volume LV

For example, the logical volume is /dev/GroupMirror/mirror_data

lvremove /dev/GroupMirror/mirror_data

Will ask whether to delete, enter y


Note: At this time, the vg volume group is still there, and the data is still there. Remounting the LV is the same as the previous data
. If you need to remount, see 5. (1) Recovery of accidentally deleted LV

3. Delete the volume group VG

vgremove /dev/GroupMirror

You will be prompted to select y for all

Note: At this time, the physical volume is still there, the data is still there, and it can still be restored
. If you need to remount, see 5. (2) Recovery of accidentally deleted LV and VG

4. Delete the physical volume PV

查看pv

pvs

Remove the corresponding physical volume, which may be raid

pvremove /dev/sde

Note: There is still a chance to recover at this time, it is recommended to find a professional

5. Delete the physical disk

Note that the deleted data is really gone this time!

fdisk /dev/sde

Select in turn: m, d, 3, w

6. Specific application - LVM related recovery

(1) Accidentally delete LV (logical volume)

1. Query achievement records

Need to restore through VG volume group
查看VG

vgdisplay

You can see that the VG is still there, assuming the VG name is GroupMirror

查看VG操作记录

vgcfgrestore --list GroupMirror

You can see something like the following
insert image description here

2. Restore to the specified node

vgcfgrestore -f /etc/lvm/archive/GroupMirror_00008-1277402051.vg GroupMirror

Then it will show that the recovery is successful
insert image description here
激活LV(和新建的时候操作一样)

lvchange -ay /dev/GroupMirror/mirror_data

创建挂载点并挂载,使用原来的挂载点即可
If it is mounted to a new mount point, mkdir /xxx will do

mount -t ext4 /dev/GroupMirror/mirror_data /mirror

(2) Recovery of accidentally deleted LV (logical volume) and VG (volume group)

If the LV and VG are deleted, it can still be restored, first restore the VG, and then restore the LV (same as above to restore the LV)

1. Query achievement records

vgcfgrestore --list GroupMirror

You will see a lot of files, as well as the modification time, find the file when it was accidentally deleted, y ends with .vg (usually the most recent one)

You can see a lot of operation records, such as the following
insert image description here

2. Restore volume group VG and LV

This command restores two operations at one time, of course you have to select the restored .VG file

vgcfgrestore -f /etc/lvm/archive/GroupMirror_00010-1053623108.vg GroupMirror

Will prompt when finished
insert image description here

3. Restore logical volume LV

激活LV(和新建的时候操作一样)

lvchange -ay /dev/GroupMirror/mirror_data

创建挂载点并挂载,使用原来的挂载点即可
If it is mounted to a new mount point, mkdir /xxx will do

mount -t ext4 /dev/GroupMirror/mirror_data /mirror

Guess you like

Origin blog.csdn.net/ziqibit/article/details/129966942