Step-by-Step Tutorial: Configure software Linear RAID 0 in Linux


RAID, Linux

In this article I will share the steps to configure software raid i.e. linear raid which is pretty much similar to RAID 0 with little difference which we will go through in the below chapters. I have written another article with comparison and difference between various RAID types using figures including pros and cons of individual RAID types so that you can make an informed decision before choosing a RAID type for your system.

 

 

RAID 0 stripping vs Linear RAID

If you run multiple disks in a non-redundant configuration, and you want to be able to recover something when any of the disks fails, then, at the very least, you need to ensure that your data is not striped over all devices. When using a striped layout, parts of most files will end up on all disks, which means that when you lose any disk, you've irrevokably lost parts of nearly every file, not to mention parts of the file-system structures themselves. This means, as you seem to already surmise, that you need to use linear allocation, which essentially appends the constituent devices' extents together, and gives you some chance of recovering files that exist entirely on the surviving device(s) after a device failure.

Step-by-Step Tutorial: Configure software Linear RAID 0 in Linux

NOTE:
Each array can contain a maximum of MD_SB_DISK, as defined by the kernel. By default, that maximum is 27, but since arrays can also act as member disks, this limit is avoidable. In addition, a maximum of 256 software RAID devices are available for use.

 

Linear (Append) Mode

Linear RAID requires a minimum of two disks, but does not require that member disks be the same size or type. Since the system writes to each disk until it is full, the speed and size of individual disks is largely irrelevant, in terms of aggregate RAID performance.

 

What is Chunk Size in Linear RAID?

chunk-size would normally define the number of kilobytes to write to each member disk for arrays that support disk striping. However, when working with linear raid arrays, the chunk-size defines the rounding factor. Each component disk is sized so that it is a multiple of the rounding factor. Because of the way the RAID superblock is placed on each array member, rounding factors of less than 64 KB are effectively equal to 64 KB.

NOTE:
The chunk-size, regardless of what type of array is used, must be defined as any power of two

 

 

Create Linear RAID Array

Currently I have added two virtual disks (/dev/sdb and /dev/sdc) to my virtual machine for the demonstration of this article. There are no partitions available on these disks which we will create in the upcoming chapters.

[root@node1 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   30G  0 disk
├─sda1            8:1    0  512M  0 part /boot
└─sda2            8:2    0 27.5G  0 part
  ├─centos-root 253:0    0 25.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    4G  0 disk
sdc               8:32   0    4G  0 disk
sr0              11:0    1 1024M  0 rom

 

Important Rules of partitioning

Here are some points to remember when you are partitioning.

  • If you don’t require autodetection and plan to use whole disks as array members, you don’t need to partition member disks individually.
  • Set partitions to type Linux Raid Auto (0xFD) if you want the kernel to automatically start arrays at boot time. Otherwise, leave them as Linux (0x83).
  • RAID-0 and linear raid arrays can contain partitions that have varying sizes without losing any disk space. Remember that when the smaller disks that belong to a RAID-0 become full, only the remaining disks are striped. So you might see variable performance on a RAID-0 with member disks of differing sizes as the array fills up.
  • Using matched drives is strongly recommended when working with any nonlinear raid array.

 

Partitioning with fdisk

You don’t need to partition disks before using them in an array, but partitioning does provide a couple of advantages.

  • Partitioning is necessary if you want the kernel to automatically start arrays, because the md driver uses the partition type to identify member disks.
  • md devices don’t support partitioning directly, but in some cases, having a filesystem that spans an entire array is undesirable. Using software RAID for system partitions means that smaller partitions are necessary. After all, you don’t want /var or /boot to span a whole array.
WARNING:
If you have a lot of disks, then you might not want to go through the trouble of partitioning each disk since this process can take a lot of time if you have more than a few drives. In that case, you can simply use a whole, unpartitioned disk as an array member (/dev/sda, for example). This means that you won’t be able to autostart arrays, however, so you’ll have to include commands to start md devices in your system initialization scripts.

Next we will start creating partitions on our disks /dev/sdb and /dev/sdc using fdisk

[root@node1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0xbea244e4.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-8388607, default 2048): ⇐ We pressed Enter leaving it to the default value
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): ⇐ We pressed Enter leaving it to the default value
Using default value 8388607
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): t ⇐ Change the partition type
Selected partition 1
Hex code (type L to list all codes): l ⇐ List the available partition types

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix
Hex code (type L to list all codes): fd ⇐ Change the partition type of selected partition to fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): w ⇐ Save the changes
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Next we will repeat the same steps for /dev/sdc

[root@node1 ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0xe215a659.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-8388607, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607):
Using default value 8388607
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): p

Disk /dev/sdc: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe215a659

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     8388607     4193280   fd  Linux raid autodetect

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Update the partition table in the kernel.

[root@node1 ~]# partprobe

Now list the available partitions on your node and verify the changes. So now we have two new partitions /dev/sdb1 and /dev/sdc1 for setting up linear mode software raid.

[root@node1 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   30G  0 disk
├─sda1            8:1    0  512M  0 part /boot
└─sda2            8:2    0 27.5G  0 part
  ├─centos-root 253:0    0 25.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    4G  0 disk
└─sdb1            8:17   0    4G  0 part
sdc               8:32   0    4G  0 disk
└─sdc1            8:33   0    4G  0 part
sr0              11:0    1 1024M  0 rom

 

Create Linear Software RAID

To create a software raid using linear mode execute below command

[root@node1 ~]# mdadm -Cv -llinear -n2 /dev/md0 /dev/sd{b,c}1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

OR you can also use the long version of this command to create linear raid

# mdadm --create --verbose --level=linear --raid-devices=2 /dev/md0 /dev/sdb1 /dev/sdc1

Here,

-C, --create
Create a new array.

-v, --verbose
Be more verbose.

-l, --raid-level
Select the RAID level: linear, 0, 1, 4, or 5.

-n, --raid-disks
Set the number of member disks in the array.

mdadm automatically activate newly created linear raid arrays. Information about the array and its member disks is now available via the /proc/mdstat pseudo file.

[root@node1 ~]# cat /proc/mdstat
Personalities : [linear]
md0 : active linear sdc1[1] sdb1[0]
      8380416 blocks super 1.2 0k rounding

unused devices: <none>

 

Create filesystem

Next, create a file-system on the new software raid array. We will create ext4 filesystem on our linear raid array

[root@node1 ~]# mkfs.ext4 /dev/md0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2095104 blocks
104755 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2145386496
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

 

Create mount point

Create a mount point for accessing the software raid array

[root@node1 ~]# mkdir /linear_raid

Next mount the raid array on the created directory

[root@node1 ~]# mount /dev/md0 /linear_raid/

Check the mount status which also gives more details about mount point, available space etc details.

[root@node1 ~]# df -h /linear_raid/
Filesystem      Size  Used Avail Use% Mounted on
/dev/md0        7.8G   36M  7.3G   1% /linear_raid

Next add an entry for the array to /etc/fstab file so it will be mounted automatically when the system restarts.

[root@node1 ~]# tail -n 1 /etc/fstab
/dev/md0     /linear_raid     ext4    defaults     0 0
WARNING:
Be warned that some distributions (Red Hat, for one) halt system initialization if an /etc/fstab entry could not be properly checked and mounted. So if the kernel doesn’t automatically start your array, an entry in /etc/fstab might be preventing the system from booting successfully. It’s a good idea to place commands that will manually start arrays in your initialization scripts before filesystems are checked and mounted, even if you’re already successfully using autodetection. This will provide additional stability and, at worst, display some innocuous warnings on the console.

 

Verify the software raid changes

Post reboot verify the raid status

[root@node1 ~]# cat /proc/mdstat
Personalities : [linear]
md0 : active linear sdc1[1] sdb1[0]
      8380416 blocks super 1.2 0k rounding

unused devices: <none>

 

Lastly I hope the steps from the article to configure Software RAID using Linear RAID on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:
Managing RAID in Linux

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

2 thoughts on “Step-by-Step Tutorial: Configure software Linear RAID 0 in Linux”

Leave a Comment