Category:

Software RAID array in Ubuntu Server 18.04

Raid 1 img

In this post I will tell you how to create a software RAID array in Ubuntu Server 18.04 using the mdadm package. Let's perform the basic configuration of the array. In the next post we will mount the RAID in the system.

We have 3 hard drive partitions. One system and the other two (for 10 GB) to create a RAID.

To view information about disks, use the fdisk utility:

sudo fdisk -l
Disk /dev/sda: 20.1 GiB, 21611151360 bytes, 42209280 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
Disklabel type: gpt
Disk identifier: BB3F706B-0CF3-45EF-B598-435CE60370FA
Device     Start      End  Sectors  Size Type
/dev/sda1   2048     4095     2048    1M BIOS boot
/dev/sda2   4096 42207231 42203136 20.1G Linux filesystem
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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 /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 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

Prepare the disks, create partitions:

sudo fdisk /dev/sdb

To create a new section, press (n).

Make it primary (p).

The partition number by default (1).

First and last sector (default).

Select the file system type (t - to enter the menu, L - to view the available file systems):

fd - Linux RAID Autodetect .

Record of changes - w.

To view information about the disk, select the disk and press p:

fdisk /dev/sdb

Do the same with the second disc (/dev/sdс).

After that, we get the created partitions (sdb1 and sdc1):

adrian@ubuntu-server:~$ sudo fdisk -l
Disk /dev/loop0: 86.9 MiB, 91099136 bytes, 177928 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 /dev/sda: 20.1 GiB, 21611151360 bytes, 42209280 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
Disklabel type: gpt
Disk identifier: BB3F706B-0CF3-45EF-B598-435CE60370FA
Device     Start      End  Sectors  Size Type
/dev/sda1   2048     4095     2048    1M BIOS boot
/dev/sda2   4096 42207231 42203136 20.1G Linux filesystem
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0x12042d1e
Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 20971519 20969472  10G fd Linux raid autodetect
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0x7ff9b8cd
Device     Boot Start      End  Sectors Size Id Type
/dev/sdc1        2048 20971519 20969472  10G fd Linux raid autodetect

To create a RAID, you must install the mdadm package. 

sudo apt install mdadm

Merging two partitions into a RAID:

sudo mdadm --create --verbose /dev/md0 --level=1 --raid-device=2 /dev/sdb1 /dev/sdc1

(--create (create RAID) --verbose (show process) /dev/md0 (RAID disk name) --level=1 (RAID level) --raid-device=2 (number of disks in RAID) /dev/sdb1 /dev/sdc1 (partitions that are merged)).

The output in the console:

adrian@ubuntu-server:~$ sudo mdadm --create --verbose /dev/md0 --level=1 --raid-device=2 /dev/sdb1 /dev/sdc1 
[sudo] password for adrian: 
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
mdadm: size set to 10475520K
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

To view the status of RAID:

sudo /proc/mdstat
adrian@ubuntu-server:~$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 sdc1[1] sdb1[0]
      10475520 blocks super 1.2 [2/2] [UU]
      
unused devices: 

To monitor the status of RAID you can use the command:

sudo watch /proc/mdstat

After a reboot the RAID did not know where to collect the disks. You should see the file /etc/mdadm/mdadm.conf:

cat /etc/mdadm/mdadm.conf

To make up-to-date information, you must run the following command:

sudo mdadm --detail --scan --verbose

(--detail (detailed info) --scan (scan) --verbose (show)).

The output in the console:

ARRAY /dev/md0 level=raid1 num-devices=2 metadata=1.2 name=ubuntu-server:0 UUID=d988e415:1b7f30f9:3d246a5d:70ed725e
   devices=/dev/sdb1,/dev/sdc1

The command will rewrite the config file (must be run as root):

sudo -s
echo "DEVICE partitions" > /etc/mdadm/mdadm.conf

After that, only this line will fit into the file. Again, take the information:

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

(from the received information on the pipeline, transfer the information to the awk utility)

Check:

cat /etc/mdadm/mdadm.conf

The output in the console:

root@ubuntu-server:~# cat /etc/mdadm/mdadm.conf 
DEVICE partitions
ARRAY /dev/md0 level=raid1 num-devices=2 metadata=1.2 name=ubuntu-server:0 UUID=d988e415:1b7f30f9:3d246a5d:70ed725e

If you have a mail server installed on your server, you can add a line with your email address to the end of this file. If the RAID will be in trouble you will receive a letter with notification.

MAILADDR my@mail.com

Check:

sudo fdisk -l

(see that there was a separate device for 10 GB)

After created the RAID need to create a file system:

sudo fdisk /dev/md0

Performed all of the steps are the same as for the previous partitions:

Install the file system under the number 83 (Linux).

After:

root@ubuntu-server:~# fdisk /dev/md0 
Welcome to fdisk (util-linux 2.31.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 0x38ff5a9e.
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20951039, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20951039, default 20951039): 
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L
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 or  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 ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 83
Changed type of partition 'Linux' to 'Linux'.
Command (m for help): p
Disk /dev/md0: 10 GiB, 10726932480 bytes, 20951040 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
Disklabel type: dos
Disk identifier: 0x38ff5a9e
Device     Boot Start      End  Sectors Size Id Type
/dev/md0p1       2048 20951039 20948992  10G 83 Linux
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks
sudo fdisk -l

You can see a new partition (md0p1), formatted it:

sudo mkfs.ext4 /dev/md0p1

The output in the console:

root@ubuntu-server:~# mkfs.ext4 /dev/md0p1 
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 2618624 4k blocks and 655360 inodes
Filesystem UUID: 63f8a296-1fc4-4eb3-8636-05673dfa072f
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Summary:

Created the RAID array - 1 level (mirror) with ext4 file system.


Posted: 2018-08-26

Comments