AMP AMP

How to create, add and remove block devices to the Raid device on Ubuntu 21.04

To Create, add and remove block devices to the Raid device on Ubuntu 21.04

Introduction:

The RAID device is a virtual device created from two or more real block devices that allows multiple devices to be combined into one and hold a single file system. A Linux Software RAID device is implemented using the MD device driver (Multiple Devices).

Step 1: Check the installed OS version by using the following command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Install mdadm package by using the following command

root@linuxhelp:~# apt install mdadm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libllvm11 net-tools
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  finalrd
Suggested packages:
  default-mta | mail-transport-agent dracut-core
The following NEW packages will be installed:
  finalrd mdadm
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 430 kB of archives.
After this operation, 1,299 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 finalrd all 7 [6,860 B]
Get:2 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 mdadm amd64 4.1-10ubuntu3 [423 kB]
Fetched 430 kB in 1s (833 kB/s)
Preconfiguring packages ...
Selecting previously unselected package finalrd.
(Reading database ... 209649 files and directories currently installed.)
Preparing to unpack .../apt/archives/finalrd_7_all.deb ...
Unpacking finalrd (7) ...
Selecting previously unselected package mdadm.
Preparing to unpack .../mdadm_4.1-10ubuntu3_amd64.deb ...
Unpacking mdadm (4.1-10ubuntu3) ...
Setting up finalrd (7) ...
Created symlink /etc/systemd/system/sysinit.target.wants/finalrd.service → /lib/systemd/system/finalrd.service.
Setting up mdadm (4.1-10ubuntu3) ...
Generating mdadm.conf... done.
update-initramfs: deferring update (trigger activated)
Sourcing file `/etc/default/grub'
Created symlink /etc/systemd/system/mdmonitor.service.wants/mdcheck_continue.timer → /lib/systemd/system/mdcheck_continu
e.timer.
Created symlink /etc/systemd/system/mdmonitor.service.wants/mdcheck_start.timer → /lib/systemd/system/mdcheck_start.time
r.
Created symlink /etc/systemd/system/mdmonitor.service.wants/mdmonitor-oneshot.timer → /lib/systemd/system/mdmonitor-ones
hot.timer.
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for initramfs-tools (0.139ubuntu3) ...
update-initramfs: Generating /boot/initrd.img-5.11.0-40-generic

Step 3: Open fdisk to create partions on /dev/sdb disk

root@linuxhelp:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xec47ae17.



Creating New Partition

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.

Using default partition number and using entire disk space partition by pressing enter for all
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): 

Created a new partition 1 of type 'Linux' and of size 20 GiB.


Changing the partition type to Linux raid autodetect 
Command (m for help): t

Selected partition 1
Hex code or alias (type L to list all): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'.



Saving the all changes made to the disks
Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Step 4: Open fdisk to create partions on /dev/sdc disk

root@linuxhelp:~# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa183b98d.

Creating New Partition
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.


Using default partition number and using entire disk space partition by pressing enter for all
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): 

Created a new partition 1 of type 'Linux' and of size 20 GiB.




Change the partition type to Linux LVM 
Command (m for help): t

Selected partition 1
Hex code or alias (type L to list all): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'.


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

Step 5: Create Raid device in Raid level 1 with two block devices

root@linuxhelp:~# mdadm -C /dev/md0 -l 1 -n 2 /dev/sd[b-c]1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array? 
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Step 6: View the details of Raid device

root@linuxhelp:~# mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Nov 29 19:19:08 2021
        Raid Level : raid1
        Array Size : 20953088 (19.98 GiB 21.46 GB)
     Used Dev Size : 20953088 (19.98 GiB 21.46 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent

       Update Time : Mon Nov 29 19:19:27 2021
             State : clean, resyncing 
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : resync

     Resync Status : 20% complete

              Name : linuxhelp:0  (local to host linuxhelp)
              UUID : f925a91a:86f15757:d4721be4:5f6ccd45
            Events : 3

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1

Step 7: Format Raid partition to ext4 file system

root@linuxhelp:~# mkfs.ext4 /dev/md0
mke2fs 1.45.7 (28-Jan-2021)
Creating filesystem with 5238272 4k blocks and 1310720 inodes
Filesystem UUID: 34ab81ad-6dd4-40fc-aba5-9c93acc37cb9
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

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

Step 8: Create directory to mount Raid partition

root@linuxhelp:~# mkdir /raid1

Step 9: Mount Raid partition on /raid1 directory

root@linuxhelp:~# mount /dev/md0 /raid1

Step 10: Create a file in Raid partition

root@linuxhelp:~# vi /raid1/test

Linuxhelp.com

Step 11: Make fstab entry for Raid partition

root@linuxhelp:~# vi /etc/fstab
/dev/md0                /raid1              ext4    defaults        0 0

Step 12: Make partition permanent

root@linuxhelp:~# mount -a

Step 13: Make one block device fault out of two then only able to remove it

root@linuxhelp:~# mdadm -f /dev/md0 /dev/sdc1
mdadm: set /dev/sdc1 faulty in /dev/md0

Step 14: Remove one block device out of two

root@linuxhelp:~# mdadm --remove /dev/md0 /dev/sdc1
mdadm: hot removed /dev/sdc1 from /dev/md0

Step 15: View the devices details

root@linuxhelp:~# mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Nov 29 19:19:08 2021
        Raid Level : raid1
        Array Size : 20953088 (19.98 GiB 21.46 GB)
     Used Dev Size : 20953088 (19.98 GiB 21.46 GB)
      Raid Devices : 2
     Total Devices : 1
       Persistence : Superblock is persistent

       Update Time : Mon Nov 29 19:23:18 2021
             State : clean, degraded 
    Active Devices : 1
   Working Devices : 1
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : resync

              Name : linuxhelp:0  (local to host linuxhelp)
              UUID : f925a91a:86f15757:d4721be4:5f6ccd45
            Events : 28

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       -       0        0        1      removed

Step 16: View the file created before removing one block device

root@linuxhelp:~# cat /raid1/test

Linuxhelp.com

By this to create, add and remove block devices to the Raid device on Ubuntu 21.04 comes to an end

FAQ
Q
How to create a RAID array?
A
To create RAID array use the command "mdadm" with option "-C to create RAID array.
Q
How to set levels on creating Raid?
A
Use command "mdadm" with option "-l or --level=" to set Raid level.
Q
Name the RAID's that supports Linux?
A
Linux supports LINEAR md devices, RAID0 , RAID1, RAID4, RAID5, RAID6, RAID10, MULTIPATH, FAULTY, and CONTAINER.
Q
What is Faulty Raid?
A
Faulty is also not true RAID, and it only involves one device.
Q
What does RAID mean?
A
RAID means Redundant Array of Independent/Inexpensive Disks