Replacing smaller disks with larger disks in Linux

Hi all. In anticipation of the start of a new course group "Linux Administrator" we publish useful material written by our student, as well as a mentor on the courses, a technical support specialist for REG.RU corporate products - Roman Travin.

This article will consider 2 cases of replacing disks and transferring information to new larger disks with further expansion of the array and file system. The first case will concern the replacement of disks with the MBR/MBR or GPT/GPT layout of the same name, the second case will concern the replacement of disks with MBR partitions for disks larger than 2 TB, which will require GPT partitioning with a biosboot partition. In both cases, the disks to which we transfer the data are already installed on the server. The file system used for the root partition is ext4.

Case 1: Replacing smaller drives with larger drives (up to 2TB)

Problem: Replace current disks with larger disks (up to 2 TB) with information transfer. In this case, we have 2 x 240 GB SSD (RAID-1) drives with the system installed and 2 x 1 TB SATA drives to which the system needs to be transferred.

Consider the current disk layout.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sda2           8:2    0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0 931,5G  0 disk  
sdd              8:48   0 931,5G  0 disk  

Let's check the currently used file system space.

[root@localhost ~]# df -h
Файловая система     Размер Использовано  Дост Использовано% Cмонтировано в
devtmpfs                32G            0   32G            0% /dev
tmpfs                   32G            0   32G            0% /dev/shm
tmpfs                   32G         9,6M   32G            1% /run
tmpfs                   32G            0   32G            0% /sys/fs/cgroup
/dev/mapper/vg0-root   204G         1,3G  192G            1% /
/dev/md126            1007M         120M  837M           13% /boot
tmpfs                  6,3G            0  6,3G            0% /run/user/0

The size of the file system before replacing the disks is 204 GB, 2 md126 software arrays are used, which is mounted in /boot и md127, which is used as physical volume for VG group vg0.

1. Removing disk partitions from arrays

Check the state of the array

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sda1[0] sdb1[1]
      1047552 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sda2[0] sdb2[1]
      233206784 blocks super 1.2 [2/2] [UU]
      bitmap: 0/2 pages [0KB], 65536KB chunk

unused devices: <none>

The system uses 2 arrays: md126 (mount point /boot) - consists of a section /dev/sda1 и /dev/sdb1, md127 (LVM for swap and the root of the file system) - consists of /dev/sda2 и /dev/sdb2.

We mark the partitions of the first disk that are used in each array as bad.

mdadm /dev/md126 --fail /dev/sda1

mdadm /dev/md127 --fail /dev/sda2

Remove the /dev/sda block device partitions from the arrays.

mdadm /dev/md126 --remove /dev/sda1

mdadm /dev/md127 --remove /dev/sda2

After we have removed the disk from the array, the block device information will look like this.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0 931,5G  0 disk  
sdd              8:48   0 931,5G  0 disk  

The state of the arrays after removing disks.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdb1[1]
      1047552 blocks super 1.2 [2/1] [_U]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sdb2[1]
      233206784 blocks super 1.2 [2/1] [_U]
      bitmap: 1/2 pages [4KB], 65536KB chunk

unused devices: <none>

2. Copy the partition table to a new drive

You can check the used partition table on the disk with the following command.

fdisk -l /dev/sdb | grep 'Disk label type'

The output for the MBR will be:

Disk label type: dos

for GPT:

Disk label type: gpt

Copying the partition table for MBR:

sfdisk -d /dev/sdb | sfdisk /dev/sdc

In this command first disk is indicated с whom markup is copied second - where copy.

ATTENTION: For GPT first disk is indicated on which copy markup, second disk indicates disk from which copy markup. If you mix up the disks, then the initially good partitioning will be overwritten and destroyed.

Copying the markup table for GPT:

sgdisk -R /dev/sdс /dev/sdb

Next, assign a random UUID to the disk (for GPT).


sgdisk -G /dev/sdc

After the command is executed, the partitions should appear on the disk /dev/sdc.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
└─sdc2           8:34   0 222,5G  0 part  
sdd              8:48   0 931,5G  0 disk  

If after the action the partitions in the system on the disk /dev/sdc undecided, then we execute the command to reread the partition table.

sfdisk -R /dev/sdc

If current disks use the MBR table and information needs to be migrated to disks larger than 2 TB, then new disks will need to manually create a GPT partition using the biosboot partition. This case will be considered in part 2 of this article.

3. Adding new disk partitions to the array

Let's add disk partitions to the appropriate arrays.

mdadm /dev/md126 --add /dev/sdc1

mdadm /dev/md127 --add /dev/sdc2

Check that the sections have been added.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  

After that, we wait for the synchronization of the arrays.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdc1[2] sdb1[1]
      1047552 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sdc2[2] sdb2[1]
      233206784 blocks super 1.2 [2/1] [_U]
      [==>..................]  recovery = 10.6% (24859136/233206784) finish=29.3min speed=118119K/sec
      bitmap: 2/2 pages [8KB], 65536KB chunk

unused devices: <none>

You can continuously monitor the synchronization process using the utility watch.

watch -n 2 cat /proc/mdstat

Parameter -n indicates at what intervals in seconds the command must be executed to check progress.

Repeat steps 1 - 3 for the next disk to be replaced.

We mark the partitions of the second disk that are used in each array as bad.

mdadm /dev/md126 --fail /dev/sdb1

mdadm /dev/md127 --fail /dev/sdb2

Delete block device partitions /dev/sdb from arrays.

mdadm /dev/md126 --remove /dev/sdb1

mdadm /dev/md127 --remove /dev/sdb2

After we have removed the disk from the array, the block device information will look like this.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  

The state of the arrays after removing disks.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdc1[2]
      1047552 blocks super 1.2 [2/1] [U_]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sdc2[2]
      233206784 blocks super 1.2 [2/1] [U_]
      bitmap: 1/2 pages [4KB], 65536KB chunk

unused devices: <none>

Copy the MBR partition table from disk /dev/sdс to disk /dev/sdd.

sfdisk -d /dev/sdс | sfdisk /dev/sdd

After the command is executed, the partitions should appear on the disk /dev/sdd.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  
├─sdd1           8:49   0     1G  0 part  
└─sdd2           8:50   0 222,5G  0 part  

Add disk partitions to arrays.

mdadm /dev/md126 --add /dev/sdd1

mdadm /dev/md127 --add /dev/sdd2

Check that the sections have been added.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  
├─sdd1           8:49   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdd2           8:50   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

After that, we wait for the synchronization of the arrays.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdd1[3] sdc1[2]
      1047552 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sdd2[3] sdc2[2]
      233206784 blocks super 1.2 [2/1] [U_]
      [>....................]  recovery =  0.5% (1200000/233206784) finish=35.4min speed=109090K/sec
      bitmap: 2/2 pages [8KB], 65536KB chunk

unused devices: <none>

5. Installing GRUB on new drives

For CentOS:

grub2-install /dev/sdX

For Debian/Ubuntu:

grub-install /dev/sdX

where X is the letter of the block device. In this case, you need to install GRUB on /dev/sdc и /dev/sdd.

6. Extending the file system (ext4) of the root partition

On new discs /dev/sdc и /dev/sdd 931.5 GB available. Due to the fact that the partition table was copied from smaller disks, partitions /dev/sdc2 и /dev/sdd2 222.5 GB available.

sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  
├─sdd1           8:49   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdd2           8:50   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

It is necessary:

  1. Extend partition 2 on each of the drives,
  2. Expand array md127,
  3. Expand PV (physical volume),
  4. Expand LV (logical-volume) vg0-root,
  5. Expand the file system.

Using the utility parted expand the section /dev/sdc2 up to the maximum value. Execute the command parted /dev/sdc (1) and view the current partition table with the command p (2)

Replacing smaller disks with larger disks in Linux

As you can see, the end of partition 2 ends at 240 GB. Let's expand the partition with the command resizepart 2, where 2 is the section number (3). Specify the value in digital format, for example 1000 GB, or use the indication of the disk share - 100%. Check again that the partition has the new size (4).

Repeat the above steps for the disk /dev/sdd. After Partition Expansion /dev/sdc2 и /dev/sdd2 became equal to 930.5 GB.

[root@localhost ~]# lsblk                                                 
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 930,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  
├─sdd1           8:49   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdd2           8:50   0 930,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

After that, we expand the array md127 up to the maximum.

mdadm --grow /dev/md127 --size=max

We check that the array has expanded. Now its size has become 930.4 GB.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0 931,5G  0 disk  
├─sdc1           8:33   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc2           8:34   0 930,5G  0 part  
  └─md127        9:127  0 930,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0 931,5G  0 disk  
├─sdd1           8:49   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdd2           8:50   0 930,5G  0 part  
  └─md127        9:127  0 930,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

Performing an extension physical volume. Before expanding, check the current state of the PV.

[root@localhost ~]# pvscan
  PV /dev/md127   VG vg0             lvm2 [222,40 GiB / 0    free]
  Total: 1 [222,40 GiB] / in use: 1 [222,40 GiB] / in no VG: 0 [0   ]

As you can see, PV /dev/md127 uses 222.4 GB of space.

Expand PV with the following command.

pvresize /dev/md127

Checking the result of the PV expansion.

[

root@localhost ~]# pvscan
  PV /dev/md127   VG vg0             lvm2 [930,38 GiB / 707,98 GiB free]
  Total: 1 [930,38 GiB] / in use: 1 [930,38 GiB] / in no VG: 0 [0   ]

Expanding logical volume. Before expanding, check the current state of LV (1).

[root@localhost ~]# lvscan
  ACTIVE            '/dev/vg0/swap' [<16,00 GiB] inherit
  ACTIVE            '/dev/vg0/root' [<206,41 GiB] inherit

LV /dev/vg0/root uses 206.41 GB.

We expand LV with the following command (2).

lvextend -l +100%FREE /dev/mapper/vg0-root

We check the performed action (3).

[root@localhost ~]# lvscan 
  ACTIVE            '/dev/vg0/swap' [<16,00 GiB] inherit
  ACTIVE            '/dev/vg0/root' [<914,39 GiB] inherit

As you can see, after the LV expansion, the amount of disk space occupied became 914.39 GB.

Replacing smaller disks with larger disks in Linux

The LV size has increased (4), but the file system is still 204 GB (5).

1. Let's expand the file system.

resize2fs /dev/mapper/vg0-root

We check the size of the file system after the executed command.

[root@localhost ~]# df -h
Файловая система     Размер Использовано  Дост Использовано% Cмонтировано в
devtmpfs                32G            0   32G            0% /dev
tmpfs                   32G            0   32G            0% /dev/shm
tmpfs                   32G         9,5M   32G            1% /run
tmpfs                   32G            0   32G            0% /sys/fs/cgroup
/dev/mapper/vg0-root   900G         1,3G  860G            1% /
/dev/md126            1007M         120M  837M           13% /boot
tmpfs                  6,3G            0  6,3G            0% /run/user/0

The size of the root file system will increase to 900 GB. After the steps have been taken, you can remove the old disks.

Case 2: Replacing smaller disks with larger disks (more than 2TB)

The task: Replace current disks with larger disks (2 x 3TB) while preserving information. In this case, we have 2 x 240 GB SSD (RAID-1) drives with the system installed and 2 x 3 TB SATA drives to which the system needs to be transferred. The current disks use the MBR partition table. Since the new disks are larger than 2 TB, they will need to use the GPT table, since the MBR cannot handle disks larger than 2 TB.

Let's look at the current disk layout.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sda2           8:2    0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0   2,7T  0 disk  
sdd              8:48   0   2,7T  0 disk  

Check the used partition table on the disk /dev/sda.

[root@localhost ~]# fdisk -l /dev/sda | grep 'Disk label type'
Disk label type: dos

On disk /dev/sdb the same partition table is used. Let's check the used disk space in the system.

[root@localhost ~]# df -h
Файловая система     Размер Использовано  Дост Использовано% Cмонтировано в
devtmpfs                16G            0   16G            0% /dev
tmpfs                   16G            0   16G            0% /dev/shm
tmpfs                   16G         9,5M   16G            1% /run
tmpfs                   16G            0   16G            0% /sys/fs/cgroup
/dev/mapper/vg0-root   204G         1,3G  192G            1% /
/dev/md126            1007M         120M  837M           13% /boot
tmpfs                  3,2G            0  3,2G            0% /run/user/0

As you can see, the root of the file system is 204 GB. Let's check the current state of the software RAID array.

1. Install GPT partition table and disk layout

Let's check the partitioning of disks by sectors.

[root@localhost ~]# parted /dev/sda print
Модель: ATA KINGSTON SVP200S (scsi)
Диск /dev/sda: 240GB
Размер сектора (логич./физич.): 512B/512B
Таблица разделов: msdos
Disk Flags: 

Номер  Начало  Конец   Размер  Тип      Файловая система  Флаги
 1     1049kB  1076MB  1075MB  primary                    загрузочный, raid
 2     1076MB  240GB   239GB   primary                    raid

On the new 3TB disk, we will need to create 3 partitions:

  1. Section bios_grub 2MiB size for GPT BIOS compatibility,
  2. Partition for the RAID array to be mounted in /boot.
  3. A partition for a RAID array that will contain LV root и LV swap.

Installing the utility parted the team yum install -y parted (for CentOS), apt install -y parted (for Debian/Ubuntu).

Using parted run the following commands to partition the disk.

Execute the command parted /dev/sdc and go to the disk layout editing mode.

Create a GPT partition table.

(parted) mktable gpt

Create 1 section bios_grub section and set a flag for it.

(parted) mkpart primary 1MiB 3MiB
(parted) set 1 bios_grub on  

Create partition 2 and set a flag for it. The partition will be used as a block for the RAID array and mounted in /boot.

(parted) mkpart primary ext2 3MiB 1028MiB
(parted) set 2 boot on

We create a 3rd partition, which will also be used as an array block in which there will be LVM.

(parted) mkpart primary 1028MiB 100% 

In this case, it is not necessary to set the flag, but if necessary, it is possible to set it with the following command.

(parted) set 3 raid on

Check the created table.

(parted) p                                                                
Модель: ATA TOSHIBA DT01ACA3 (scsi)
Диск /dev/sdc: 3001GB
Размер сектора (логич./физич.): 512B/4096B
Таблица разделов: gpt
Disk Flags: 

Номер  Начало  Конец   Размер  Файловая система  Имя      Флаги
 1     1049kB  3146kB  2097kB                    primary  bios_grub
 2     3146kB  1077MB  1074MB                    primary  загрузочный
 3     1077MB  3001GB  3000GB                    primary

Assign a new random GUID to the disk.

sgdisk -G /dev/sdd

2. Removing partitions of the first disk from the arrays

Check the state of the array

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sda1[0] sdb1[1]
      1047552 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sda2[0] sdb2[1]
      233206784 blocks super 1.2 [2/2] [UU]
      bitmap: 0/2 pages [0KB], 65536KB chunk

unused devices: <none>

The system uses 2 arrays: md126 (mount point /boot) - consists of /dev/sda1 и /dev/sdb1, md127 (LVM for swap and the root of the file system) - consists of /dev/sda2 и /dev/sdb2.

We mark the partitions of the first disk that are used in each array as bad.

mdadm /dev/md126 --fail /dev/sda1

mdadm /dev/md127 --fail /dev/sda2

Delete block device partitions /dev/sda from arrays.

mdadm /dev/md126 --remove /dev/sda1

mdadm /dev/md127 --remove /dev/sda2

Checking the state of the array after removing the disk.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdb1[1]
      1047552 blocks super 1.2 [2/1] [_U]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sdb2[1]
      233206784 blocks super 1.2 [2/1] [_U]
      bitmap: 2/2 pages [8KB], 65536KB chunk

unused devices: <none>

3. Adding new disk partitions to the array

The next step is to add partitions of the new disk to arrays for synchronization. We look at the current state of the disk layout.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0   2,7T  0 disk  
├─sdc1           8:33   0     2M  0 part  
├─sdc2           8:34   0     1G  0 part  
└─sdc3           8:35   0   2,7T  0 part  
sdd              8:48   0   2,7T  0 disk  

Section /dev/sdc1 is bios_grub section and does not participate in the creation of arrays. Arrays will only use /dev/sdc2 и /dev/sdc3. We add these sections to the corresponding arrays.

mdadm /dev/md126 --add /dev/sdc2

mdadm /dev/md127 --add /dev/sdc3

Then we wait for the synchronization of the array.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdc2[2] sdb1[1]
      1047552 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

md127 : active raid1 sdc3[2] sdb2[1]
      233206784 blocks super 1.2 [2/1] [_U]
      [>....................]  recovery =  0.2% (619904/233206784) finish=31.2min speed=123980K/sec
      bitmap: 2/2 pages [8KB], 65536KB chunk
unused devices: <none>

Partitioning disks after adding partitions to the array.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdb2           8:18   0 222,5G  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdc              8:32   0   2,7T  0 disk  
├─sdc1           8:33   0     2M  0 part  
├─sdc2           8:34   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc3           8:35   0   2,7T  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0   2,7T  0 disk  

4. Removing partitions of the second disk from the arrays

We mark the partitions of the second disk that are used in each array as bad.

mdadm /dev/md126 --fail /dev/sdb1

mdadm /dev/md127 --fail /dev/sdb2

Delete block device partitions /dev/sda from arrays.

mdadm /dev/md126 --remove /dev/sdb1

mdadm /dev/md127 --remove /dev/sdb2

5. Copy the GPT layout table and synchronize the array

To copy the GPT markup table, use the utility sgdisk, which is included in the package for working with disk partitions and the GPT table - gdisk.

Installation gdisk for CentOS:

yum install -y gdisk

Installation gdisk for Debian/Ubuntu:

apt install -y gdisk

ATTENTION: For GPT first disk is indicated on which copy markup, second disk indicates disk from which copy markup. If you mix up the disks, then the initially good partitioning will be overwritten and destroyed.

Copy the GPT partition table.

sgdisk -R /dev/sdd /dev/sdc

Partitioning disks after transferring a table to disk /dev/sdd.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0   2,7T  0 disk  
├─sdc1           8:33   0     2M  0 part  
├─sdc2           8:34   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc3           8:35   0   2,7T  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0   2,7T  0 disk  
├─sdd1           8:49   0     2M  0 part  
├─sdd2           8:50   0     1G  0 part  
└─sdd3           8:51   0   2,7T  0 part  

Next, we add each of the partitions participating in software RAID arrays.

mdadm /dev/md126 --add /dev/sdd2

mdadm /dev/md127 --add /dev/sdd3

We are waiting for the synchronization of the array.

[root@localhost ~]# cat /proc/mdstat 
Personalities : [raid1] 
md126 : active raid1 sdd2[3] sdc2[2]
      1047552 blocks super 1.2 [2/2] [UU]
      bitmap: 1/1 pages [4KB], 65536KB chunk

md127 : active raid1 sdd3[3] sdc3[2]
      233206784 blocks super 1.2 [2/1] [U_]
      [>....................]  recovery =  0.0% (148224/233206784) finish=26.2min speed=148224K/sec
      bitmap: 2/2 pages [8KB], 65536KB chunk
unused devices: <none>

After copying the GPT partition to the second new disk, the partition will look like this.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
├─sda1           8:1    0     1G  0 part  
└─sda2           8:2    0 222,5G  0 part  
sdb              8:16   0 223,6G  0 disk  
├─sdb1           8:17   0     1G  0 part  
└─sdb2           8:18   0 222,5G  0 part  
sdc              8:32   0   2,7T  0 disk  
├─sdc1           8:33   0     2M  0 part  
├─sdc2           8:34   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdc3           8:35   0   2,7T  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0   2,7T  0 disk  
├─sdd1           8:49   0     2M  0 part  
├─sdd2           8:50   0     1G  0 part  
│ └─md126        9:126  0  1023M  0 raid1 /boot
└─sdd3           8:51   0   2,7T  0 part  
  └─md127        9:127  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

Next, install GRUB on the new drives.

Installation for CentOS:

grub2-install /dev/sdX

Installation for Debian/Ubuntu:

grub-install /dev/sdX

where X - drive letter, in our case drives /dev/sdc и /dev/sdd.

Update information about the array.

For CentOS:

mdadm --detail --scan --verbose > /etc/mdadm.conf

For Debian/Ubuntu:

echo "DEVICE partitions" > /etc/mdadm/mdadm.conf

mdadm --detail --scan --verbose | awk '/ARRAY/ {print}' >> /etc/mdadm/mdadm.conf

Updating the image initrd:
For CentOS:

dracut -f -v --regenerate-all

For Debian/Ubuntu:

update-initramfs -u -k all

Update the GRUB configuration.

For CentOS:

grub2-mkconfig -o /boot/grub2/grub.cfg

For Debian/Ubuntu:

update-grub

After the steps have been taken, the old disks can be removed.

6. Extending the file system (ext4) of the root partition

Partitioning disks to file system extension after system migration to 2 x 3TB disks (RAID-1).

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
sdb              8:16   0 223,6G  0 disk  
sdc              8:32   0   2,7T  0 disk  
├─sdc1           8:33   0     2M  0 part  
├─sdc2           8:34   0     1G  0 part  
│ └─md127        9:127  0  1023M  0 raid1 /boot
└─sdc3           8:35   0   2,7T  0 part  
  └─md126        9:126  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0   2,7T  0 disk  
├─sdd1           8:49   0     2M  0 part  
├─sdd2           8:50   0     1G  0 part  
│ └─md127        9:127  0  1023M  0 raid1 /boot
└─sdd3           8:51   0   2,7T  0 part  
  └─md126        9:126  0 222,4G  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

Now sections /dev/sdc3 и /dev/sdd3 occupy 2.7 TB. Since we created a new disk layout with the GPT table, the size of the 3rd partition was immediately set to the maximum possible disk space, in this case it is not required to expand the partition.

It is necessary:

  1. Expand array md126,
  2. Expand PV (physical volume),
  3. Expand LV (logical-volume) vg0-root,
  4. Expand the file system.

1. Expanding the array md126 up to the maximum.

mdadm --grow /dev/md126 --size=max

After expanding the array md126 the size of the occupied space has increased to 2.7 TB.

[root@localhost ~]# lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 223,6G  0 disk  
sdb              8:16   0 223,6G  0 disk  
sdc              8:32   0   2,7T  0 disk  
├─sdc1           8:33   0     2M  0 part  
├─sdc2           8:34   0     1G  0 part  
│ └─md127        9:127  0  1023M  0 raid1 /boot
└─sdc3           8:35   0   2,7T  0 part  
  └─md126        9:126  0   2,7T  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]
sdd              8:48   0   2,7T  0 disk  
├─sdd1           8:49   0     2M  0 part  
├─sdd2           8:50   0     1G  0 part  
│ └─md127        9:127  0  1023M  0 raid1 /boot
└─sdd3           8:51   0   2,7T  0 part  
  └─md126        9:126  0   2,7T  0 raid1 
    ├─vg0-root 253:0    0 206,4G  0 lvm   /
    └─vg0-swap 253:1    0    16G  0 lvm   [SWAP]

Expanding physical volume.

Before expanding, we check the current value of the occupied space PV /dev/md126.

[root@localhost ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree
  /dev/md126 vg0 lvm2 a--  222,40g    0 

We expand PV with the following command.

pvresize /dev/md126

We check the performed action.

[root@localhost ~]# pvs
  PV         VG  Fmt  Attr PSize  PFree
  /dev/md126 vg0 lvm2 a--  <2,73t 2,51t

Expanding logical volume vg0-root.

After expanding PV, check the space occupied by VG.

[root@localhost ~]# vgs
  VG  #PV #LV #SN Attr   VSize  VFree
  vg0   1   2   0 wz--n- <2,73t 2,51t

Let's check the space occupied by LV.

[root@localhost ~]# lvs
  LV   VG  Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root vg0 -wi-ao---- <206,41g                                                    
  swap vg0 -wi-ao----  <16,00g            

Volume vg0-root is 206.41 GB.

Expand LV to maximum disk space.

lvextend -l +100%FREE /dev/mapper/vg0-root 

Checking the LV space after expansion.

[root@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root vg0 -wi-ao----   2,71t                                                    
  swap vg0 -wi-ao---- <16,00g

Expanding the file system (ext4).

Let's check the current size of the file system.

[root@localhost ~]# df -h
Файловая система     Размер Использовано  Дост Использовано% Cмонтировано в
devtmpfs                16G            0   16G            0% /dev
tmpfs                   16G            0   16G            0% /dev/shm
tmpfs                   16G         9,6M   16G            1% /run
tmpfs                   16G            0   16G            0% /sys/fs/cgroup
/dev/mapper/vg0-root   204G         1,4G  192G            1% /
/dev/md127            1007M         141M  816M           15% /boot
tmpfs                  3,2G            0  3,2G            0% /run/user/0

The volume /dev/mapper/vg0-root is 204 GB after the LV expansion.

Expanding the file system.

resize2fs /dev/mapper/vg0-root 

We check the size of the file system after its expansion.

[root@localhost ~]# df -h
Файловая система     Размер Использовано  Дост Использовано% Cмонтировано в
devtmpfs                16G            0   16G            0% /dev
tmpfs                   16G            0   16G            0% /dev/shm
tmpfs                   16G         9,6M   16G            1% /run
tmpfs                   16G            0   16G            0% /sys/fs/cgroup
/dev/mapper/vg0-root   2,7T         1,4G  2,6T            1% /
/dev/md127            1007M         141M  816M           15% /boot
tmpfs                  3,2G            0  3,2G            0% /run/user/0

The size of the file system has been increased by the entire size of the volume.

Source: habr.com

Add a comment