yingfeng
The heart is as bright as a mirror. If you don't, you won't be welcomed

Linux Replace Hard Disk in Volume Group

When using the Linxu system, if there is no RAID, in some cases, a hard disk needs to be replaced when a bad track occurs, a hard disk with larger capacity needs to be replaced, or a solid state needs to be replaced.
In fact, in general, you can simply and roughly mount the hard disk, create a partition, directly mount it to the system, copy files, and then unplug the old hard disk. Even if some services want to ensure that the path of the file remains unchanged after replacing the hard disk, they just need to re attach to the original path. There is no need to do so many operations in this article.
However, this article discusses a special case. Suppose your server creates a volume group on the hard disk, and then creates logical volumes in it, and mounts these logical volumes for use. Now, it also encounters the case that the hard disk has a bad track or needs to be replaced with a larger capacity hard disk, or needs to be replaced with a solid state.
It is also required to ensure that the hard disk is used in this way, that the volume group name and volume name are consistent with the original, and that the file path remains unchanged after replacing the hard disk (Not only, but also hhhhh) So this article provides a way of thinking to solve this problem.

The following are the specific operation steps:

1. Insert a new hard disk

First of all, of course, insert the new hard disk into the server.
After inserting the new hard disk into the server, you can use the "fdisk - l" command to check whether the system recognizes our new hard disk.

 root@UNAS-test :~# fdisk -l ... omit other hard disk information Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Disk model: ST1000DM003-1ER1 Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes ... omit other hard disk information

As shown above, "/dev/sdb" here is my newly inserted hard disk.

2. Initialize the hard disk

After recognizing the new hard disk, we need to initialize the hard disk (that is, initialize it as a physical volume (PV)).
You can use the "pvcreate" command to initialize the hard disk. The parameter after the command is the path to the hard disk, which is "/dev/sdb" here.

be careful:

Initializing the hard disk will result in the loss of all data in the hard disk. Please repeatedly confirm that the data in the hard disk has been backed up or the data is useless.

The hard disk behind the command must not be mistaken, or the data of the corresponding hard disk will be lost

 root@UNAS-test :~# pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created.

3. Create a temporary volume group

Use the "vgcreate" command to create a temporary volume group. Here, we call this temporary volume group "temp_vg". This name has no specific requirements and does not duplicate the name of an existing volume group.

 root@UNAS-test :~# vgcreate temp_vg /dev/sdb Volume group "temp_vg" successfully created

After creating the volume group, we use the "vgdisplay" command to view the volume group information, as shown below:

 root@UNAS-test :~# vgdisplay temp_vg --- Volume group --- VG Name               temp_vg System ID Format                lvm2 Metadata Areas        1 Metadata Sequence No  6 VG Access             read/write VG Status             resizable MAX LV                0 Cur LV                1 Open LV               0 Max PV                0 Cur PV                1 Act PV                1 VG Size               931.51 GiB PE Size               4.00 MiB Total PE              238467 Alloc PE / Size       25600 / 100.00 GiB Free  PE / Size       212867 / 831.51 GiB VG UUID               K31Hcf-blow-yVK3-Y2iN-Cn7F-A9il-DE1H4F

Where "temp_vg" is the name of the volume group we created.

4. Create logical volume

In this temporary volume group, use the "lvcreate" command to create logical volumes with the same name as those in the volume group on the old hard disk to be replaced one by one. The volume size can be arbitrary, as long as it is larger than the file size stored in the corresponding logical volume.
Assuming that there is only one logical volume name under the original volume group, and the volume name is "disk1", the volume we are creating now directly uses the entire hard disk space, then the operation is as follows:

 root@UNAS-test :~# lvcreate -l 100%FREE -n disk1 temp_vg Logical volume "disk1" created.

Among them, "- l 100% free" means to use the space of the whole hard disk to create this logical volume, and "- n disk1 temp_vg" means that the created volume is named "disk1" and belongs to the "temp_vg" volume group.

If you need to create multiple volumes, you can replace "- l 100% FREE" with "- L volume size" (the volume size here can use K, M, G, T and other units), and the other parameters remain unchanged.
Suppose we want to create a volume named "disk1", which is 100GB in size and belongs to the "temp_vg" volume group. The operation is as follows:

 root@UNAS-test :~# lvcreate -L 100G -n disk1 temp_vg Logical volume "disk1" created.

After creating the volume, we can use the "lvdisplay" command to view the volume information, as shown below:

 root@UNAS-test :~# lvdisplay /dev/temp_vg/disk1 --- Logical volume --- LV Path                /dev/temp_vg/disk1 LV Name                disk1 VG Name                temp_vg LV UUID                1Ahabk-gJbW-XNZV-IscQ-4OIO-YcIp-NuHHqT LV Write Access        read/write LV Creation host, time UNAS-27X39, 2023-12-16 23:19:16 +0800 LV Status              available # open                 0 LV Size                100.00 GiB Current LE             25600 Segments               1 Allocation             inherit Read ahead sectors     auto - currently set to     256 Block device           253:0

Where "/dev/temp_vg/disk1" is the path of the volume. The volume groups created in the linxu system will be in the "/dev/" path, and the volumes in the volume group will naturally be in the path of the corresponding volume, so the path of the volume "disk1" we created is "/dev/temp_vg/disk1".

5. Format the volume

After creating a volume, we need to format it before using it. You can use the "mkfs" command to format the volume.
Suppose we want to format the previously created "disk1" volume as an xfs file system, then the operation is as follows:

 root@UNAS-test :~# mkfs.xfs /dev/temp_vg/disk1 meta-data=/dev/temp_vg/disk1     isize=512    agcount=4, agsize=6553600 blks =                        sectsz=4096  attr=2, projid32bit=1 =                        crc=1        finobt=1, sparse=1, rmapbt=0 =                       reflink=0 data     =                       bsize=4096   blocks=26214400, imaxpct=25 =                        sunit=0      swidth=0 blks naming   =version 2              bsize=4096   ascii-ci=0, ftype=1 log      =internal log           bsize=4096   blocks=12800, version=2 =                        sectsz=4096  sunit=1 blks, lazy-count=1 realtime =none                   extsz=4096   blocks=0, rtextents=0

The ". xfs" after the "mkfs" indicates that the file system is formatted as an xfs file system. The file system here can be ext4, xfs, ntfs, etc., which can be entered according to the actual needs, while the "/dev/temp_vg/disk1" after the "mkfs" indicates the path of the volume, which is also entered according to the actual path.
Another thing to note is that there is no space between "mkfs" and ". file system".

be careful:

Formatting will result in all data in the volume. Please repeatedly confirm whether the path is correct

6. Mount the created volume

After the volumes are created, we need to mount them before using them. Usually, we mount them in the "/mnt" directory.
Before attaching the volume, we need to create a folder to mount it. Suppose we want to mount it in the directory "/mnt/temp_vg/disk1", then the operation is as follows:

 root@UNAS-test :/# mkdir /mnt/temp_vg/ root@UNAS-test :/# root@UNAS-test :/# mkdir /mnt/temp_vg/disk1/ root@UNAS-test :/#

Then mount the volume to this folder. We use the "mount" command to mount it, as follows:

 root@UNAS-test :/# mount /dev/temp_vg/disk1 /mnt/temp_vg/disk1/

At this point, we have successfully mounted the volume "/dev/temp_vg/disk1" to the directory "/mnt/temp_vg/disk1/".

7. Stop services related to the old hard disk

Before copying files, we need to stop the service related to the old hard disk. Usually, we can stop the service by using the "service service name stop".
However, the specific operations in this step need to be based on the specific service conditions. This article will not introduce them specifically.

8. Copying documents

Copy the files in each volume of the hard disk to the corresponding volume of the new hard disk.
Assuming that the volume in the original hard disk is mounted in the directory "/mnt/nas_vg/disk1", and the volume corresponding to the new hard disk is mounted in the directory "/mnt/temp_vg/disk1/", the operations for copying files are as follows:

 root@UNAS-test :~# cp -r /mnt/nas_vg/disk1/ /mnt/temp_vg/disk1/

9. Unmount the volume

Use the "umount" command to unmount all the volumes in the old and new hard disks.
Let's assume that the volume in the old hard disk is mounted in the "/mnt/nas_vg/disk1" directory, and the volume corresponding to the new hard disk is mounted in the "/mnt/temp_vg/disk1/" directory. Then the operations are as follows:

 root@UNAS-test :~# umount /mnt/nas_vg/disk1 root@UNAS-test :~# umount /mnt/temp_vg/disk1

10. Modify the volume group name

The next step is to modify the volume group name of the old hard disk and the new hard disk. We can use the "vgrename" command to modify the volume group name.
Here we still assume that the volume group name of the old hard disk is "nas_vg" and the volume group name of the new hard disk is "temp_vg". The operations are as follows:

 root@UNAS-test :~# vgrename nas_vg nas_vg_old Volume group "nas_vg" successfully renamed to "nas_vg_old" root@UNAS-test :~# vgrename temp_vg nas_vg Volume group "temp_vg" successfully renamed to "nas_vg"

Here we change the volume group name of the original hard disk "nas_vg" to "nas_vg_old", and the volume group name of the new hard disk "temp_vg" to "nas_vg".

11. Mount a new hard disk

After the volume group name is modified, we also need to mount the volume of the new hard disk to the directory where the old hard disk was mounted.
Here we still assume that the volume in the original hard disk is mounted in the directory "/mnt/nas_vg/disk1", and the new hard disk is a volume created according to the above operation and the path is modified, so the path of the new hard disk is "/dev/nas_vg/disk1"
The operation is as follows:

 mount /dev/nas_vg/disk1 /mnt/nas_vg/disk1/

12. Check the file and start the service

After all the files have been copied to the new hard disk, we can start the service related to this hard disk.
However, the specific operations in this step need to be based on the specific service conditions. This article also does not make specific introduction.

13. Remove the old hard disk

After ensuring that the service is normal, the old hard disk can be removed.
It is not recommended to format or destroy the old hard disk immediately. It is recommended to wait for the new hard disk to run for a period of time to ensure that there is no data processing before processing the old hard disk.

This article is published at: yingfeng Blog >> Linux Replace Hard Disk in Volume Group , please indicate the source for reprinting.

comment Grab the sofa

  • Nickname (required)
  • Email (required)
  • website