Mdadm: Difference between revisions

From Leo's Notes
This page was last edited on 15 June 2023, at 17:02.
No edit summary
Changed quick usage to table format
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Creating a Raid 1 Mirror ==
mdadm is a Linux utility to manage software RAID.


mdadm --zero-superblock /dev/sdb
== Quick Usage ==
mdadm --zero-superblock /dev/sdc
{| class="wikitable"
!Task
!Commands
|-
!Initialize disks
|Initialize the disks in your array:{{highlight|lang=terminal|code=
# mdadm --zero-superblock /dev/sdb
# mdadm --zero-superblock /dev/sdc
}}
|-
!Creating a Raid 1 Mirror
|
{{highlight|lang=terminal|code=
# mdadm --create /dev/md0 --chunk=4 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
}}
|-
!Creating a Raid 5
|{{highlight|lang=terminal|code=
# mdadm --create /dev/md0 --chunk=128 --level=5 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde
}}


Create the array:
You can specify hot spares with {{code|1=--spare-devices=N /dev/sdn}}
mdadm --create /dev/md0 --chunk=4 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
|-
!Saving mdadm config
|To save your settings, run the following. Note that Ubuntu may have the file under <code>/etc/mdadm/mdadm.conf</code>.{{highlight
| lang = terminal
| code = # mdadm --detail --scan >> /etc/mdadm.conf
# cat /etc/mdadm.conf
ARRAY /dev/md/0 metadata=1.2 name=data:0 UUID=5f486d36:37899fcb:99ad8485:088bc180
}}The config will also need to be updated in the initrd file. Rebuild the initrd filesystem with:
{{Highlight
| code = # update-initramfs -u
| lang = terminal
}}
|-
!Disassemble / stop an array
|{{highlight|lang=terminal|code=
# mdadm --stop /dev/md0
}}
|-
!Reassemble an array
|{{highlight|lang=terminal|code=
# mdadm --assemble --scan
}}


That's it! Create the file system and you're done. Using 4k chunk size.
You can assemble an array with specific devices using:
{{highlight|lang=terminal|code=
# mdadm --assemble /dev/md0 /dev/sdb /dev/sdc
}}
|-
!Recovering an Array
|If you want to recover data from an array of a failed system, image the hard drives in case something goes terribly wrong. Then, attempt to reassemble the array with the {{code|-o}} or {{code|--readonly}} option for read only. This will prevent mdadm from attempting to resync, recover, or reshape the array.{{highlight|lang=terminal|code=
# mdadm --readonly --verbose --assemble /dev/md0 /dev/sdb /dev/sdc
## or
# mdadm --readonly --verbose --assemble --scan
}}


To save your settings, run:


# echo "DEVICES /dev/sdb /dev/sdc" > /etc/mdadm.conf
|-
# mdadm --detail --scan >> /etc/mdadm.conf
!Check on the array status:
# cat /etc/mdadm.conf
|{{highlight|lang=terminal|code=
DEVICES /dev/sdb /dev/sdc
# cat /proc/mdstat
ARRAY /dev/md/0 metadata=1.2 name=data:0 UUID=5f486d36:37899fcb:99ad8485:088bc180
}}
|}


== Creating a Raid 5 ==
== Issues ==
=== Device or resource busy ===
If you're unable to reassemble an array with this error:
{{highlight|lang=terminal|code=
# mdadm --assemble /dev/md0 /dev/sdb /dev/sdc
mdadm: cannot open device /dev/sdb: Device or resource busy
mdadm: /dev/sdb has no superblock - assembly aborted
}}


Create the array:
You probably forgot to define your {{code|/etc/mdadm.conf}} settings before rebooting and {{code|mdadm}} probably tried to do something with one of your disks to create {{code|/dev/md127}} (hence the reason the device is busy). To free {{code|/dev/sdb}} in the above example, just stop {{code|/dev/md127}} by running:
mdadm --create /dev/md0 --chunk=128 --level=5 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde
{{highlight|lang=terminal|code=
# mdadm --stop /dev/md127
}}


You can specify hot spares with <code>--spare-devices=N /dev/sdn</code>
And then retry reassembling the array again.


== Disassemble / stop an array ==


=== /dev/sdx is apparently in use by the system ===
An unmounted disk that has just been repartitioned cannot be formatted:
{{highlight|lang=terminal|code=
root@velocity:~# mkfs.ext4  /dev/sdb1
mke2fs 1.43.3 (04-Sep-2016)
/dev/sdb1 is apparently in use by the system; will not make a filesystem here!
}}


mdadm --stop /dev/md0
If the disk was part of a RAID, Linux might helpfully make a raid device out of the disk and call it {{code|/dev/md127}}.


== Reassemble an array ==
{{highlight|lang=terminal|code=
mdadm --assemble --scan
root@velocity:~# ls -al /dev/md127
brw-rw----. 1 root disk 9, 127 Jan  1 12:12 /dev/md127
}}


You can assemble an array like:
Stop it, then retry making the filesystem again.
mdadm --assemble /dev/md0 /dev/sdb /dev/sdc


{{highlight|lang=terminal|code=
root@velocity:~# mdadm -S /dev/md127
mdadm: stopped /dev/md127
root@velocity:~# mkfs.ext4  /dev/sdb1
mke2fs 1.43.3 (04-Sep-2016)
Creating filesystem with 488378390 4k blocks and 122101760 inodes
...
}}


== Issues ==


# mdadm -A /dev/md0 /dev/sdb /dev/sdc
mdadm: cannot open device /dev/sdb: Device or resource busy
mdadm: /dev/sdb has no superblock - assembly aborted


If you forgot to define your mdadm.conf settings and rebooted, mdadm probably tried to do something with one of your disks to create <code>/dev/md127</code>. To free <code>/dev/sdb</code> in the above example, just stop <code>/dev/md127</code> by running:
mdadm --stop /dev/md127


And then retry reassembling the array again.




[[Category:Linux]]
{{Navbox Linux}}[[Category:Linux]]
[[Category:LinuxUtilities]]
[[Category:LinuxUtilities]]
{{Navbox Linux}}

Latest revision as of 17:02, 15 June 2023

mdadm is a Linux utility to manage software RAID.

Quick Usage

Task Commands
Initialize disks Initialize the disks in your array:
# mdadm --zero-superblock /dev/sdb
# mdadm --zero-superblock /dev/sdc
Creating a Raid 1 Mirror
# mdadm --create /dev/md0 --chunk=4 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
Creating a Raid 5
# mdadm --create /dev/md0 --chunk=128 --level=5 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde

You can specify hot spares with --spare-devices=N /dev/sdn

Saving mdadm config To save your settings, run the following. Note that Ubuntu may have the file under /etc/mdadm/mdadm.conf.
# mdadm --detail --scan >> /etc/mdadm.conf
# cat /etc/mdadm.conf
ARRAY /dev/md/0 metadata=1.2 name=data:0 UUID=5f486d36:37899fcb:99ad8485:088bc180
The config will also need to be updated in the initrd file. Rebuild the initrd filesystem with:
# update-initramfs -u
Disassemble / stop an array
# mdadm --stop /dev/md0
Reassemble an array
# mdadm --assemble --scan

You can assemble an array with specific devices using:

# mdadm --assemble /dev/md0 /dev/sdb /dev/sdc
Recovering an Array If you want to recover data from an array of a failed system, image the hard drives in case something goes terribly wrong. Then, attempt to reassemble the array with the -o or --readonly option for read only. This will prevent mdadm from attempting to resync, recover, or reshape the array.
# mdadm --readonly --verbose --assemble /dev/md0 /dev/sdb /dev/sdc
## or
# mdadm --readonly --verbose --assemble --scan


Check on the array status:
# cat /proc/mdstat

Issues

Device or resource busy

If you're unable to reassemble an array with this error:

# mdadm --assemble /dev/md0 /dev/sdb /dev/sdc
mdadm: cannot open device /dev/sdb: Device or resource busy
mdadm: /dev/sdb has no superblock - assembly aborted

You probably forgot to define your /etc/mdadm.conf settings before rebooting and mdadm probably tried to do something with one of your disks to create /dev/md127 (hence the reason the device is busy). To free /dev/sdb in the above example, just stop /dev/md127 by running:

# mdadm --stop /dev/md127

And then retry reassembling the array again.


/dev/sdx is apparently in use by the system

An unmounted disk that has just been repartitioned cannot be formatted:

root@velocity:~# mkfs.ext4  /dev/sdb1
mke2fs 1.43.3 (04-Sep-2016)
/dev/sdb1 is apparently in use by the system; will not make a filesystem here!

If the disk was part of a RAID, Linux might helpfully make a raid device out of the disk and call it /dev/md127.

root@velocity:~# ls -al /dev/md127
brw-rw----. 1 root disk 9, 127 Jan  1 12:12 /dev/md127

Stop it, then retry making the filesystem again.

root@velocity:~# mdadm -S /dev/md127
mdadm: stopped /dev/md127

root@velocity:~# mkfs.ext4  /dev/sdb1
mke2fs 1.43.3 (04-Sep-2016)
Creating filesystem with 488378390 4k blocks and 122101760 inodes
...