Skip to content

Instantly share code, notes, and snippets.

@ScriptingSquirrel
Last active June 6, 2018 15:33
Show Gist options
  • Save ScriptingSquirrel/130aa842ae86802aa8aa716924b8e1be to your computer and use it in GitHub Desktop.
Save ScriptingSquirrel/130aa842ae86802aa8aa716924b8e1be to your computer and use it in GitHub Desktop.
Create Software RAID 1 array on two disks with Debian Linux

Name of the new RAID partition in this example: backup

Partition disk #1 (ata-YOURDISKID/sda in this example):

$ parted --align=optimal /dev/disk/by-id/ata-YOURDISKID

GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt

(parted) mkpart primary 0% 100%
(parted) name 1 'backup'
(parted) p                                                                
Model: ATA YOURDISKID (scsi)
Disk /dev/sda: 8002GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name    Flags
 1      1049kB  8002GB  8002GB  ext4         backup

(parted) quit 

Copy/clone/reproduce partition table from disk #1 to disk #2 (replace sdDEST and sdSOURCE):

$ sgdisk -R /dev/sdDEST /dev/sdSOURCE
$ sgdisk --randomize-guids /dev/sdDEST
# check partition table with:     parted -s /dev/sdDEST print
# check that UUIDs are different: blkid /dev/sdSOURCE /dev/sdDEST

Create new software RAID array (replace sdX1 and sdY1):

$ mdadm --create /dev/md/backup --level=1 --raid-devices=2 --name=backup /dev/sdX1 /dev/sdY1
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? yes
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md/backup started.

$ cat /proc/mdstat 
Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] 
md127 : active raid1 sdX1[1] sdY1[0]
      7813894144 blocks super 1.2 [2/2] [UU]
      [>....................]  resync =  0.1% (11127936/7813894144) finish=1106.4min speed=117536K/sec
      bitmap: 59/59 pages [236KB], 65536KB chunk

Create filesystem on newly created RAID array (note down the filesystem UUID, needed later for /etc/fstab):

$ mkfs.ext4 /dev/md/backup
mke2fs 1.43.4 (31-Jan-2017)
Creating filesystem with 1953473536 4k blocks and 244187136 inodes
Filesystem UUID: 092fa019-19b6-48a3-b429-689cf2142cc7
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848, 512000000, 550731776, 644972544, 1934917632

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

$ e2label /dev/md/backup backup

Make RAID array re-appear after reboot.

  1. copy following output:
$ mdadm -Db /dev/md/backup
ARRAY /dev/md/backup metadata=1.2 name=myserver:backup UUID=cf92189d:804e048c:24d216f4:ac76f71d
  1. Add line above to file /etc/mdadm/mdadm.conf to make the RAID array reappear after reboot:
# added by ADMIN (2018-06-05):
ARRAY /dev/md/backup metadata=1.2 name=myserver:backup UUID=cf92189d:804e048c:24d216f4:ac76f71d
  1. Finally run:
$ update-initramfs -u

Mount new filesystem on boot:

  1. Create mountpoint
$ mkdir /backup
  1. Edit /etc/fstab:
# /dev/md/backup (/dev/md127)
#    8001561821kB (size according to `parted -s /dev/sdX u kb print`)
#    raid1 with:
#      ata-YOURDISKID-part1
#      ata-YOURDISKID-part1
# from `mdadm -Db /dev/md/backup`:
#    ARRAY /dev/md/backup metadata=1.2 name=myserver:backup UUID=cf92189d:804e048c:24d216f4:ac76f71d
UUID=092fa019-19b6-48a3-b429-689cf2142cc7 /backup          ext4    defaults,acl        0       2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment