1

I found no way to remove a mdraid from a server with one command.

I can stop it via mdadm --stop /dev/md0

But the superblock is still in the devices.

mdadm --zero-superblock DEVICE needs every device (like /dev/sdb1)

I know that I can do mdadm --detail /dev/md0 and then I see the devices.

I could write a fragile script to fetch the /dev/sd... strings from the output of mdadm --detail /dev/md0, but I would like to avoid this.

Is there a one-liner to remove the superblock from all devices of a mdraid?

I would like to avoid to parse the output of mdadm --detail, since this feels fragile.

2
  • I don't think so. I typically use the --zero-superblock on the partitions or drive by hand. Or depending on the requirement, I do pv /dev/zero > /dev/sdXXX to simply erase the entire drive. And then I just know which drives they were. Do you need to do this so often that a one-time manual operation does not suffice?
    – Halfgaar
    Sep 12 at 15:41
  • @Halfgaar yes, I need to do it often, so I would like to automate it. But reading the output of mdadm --details ... feels a bit fragile.
    – guettli
    Sep 12 at 16:11

3 Answers 3

3
+100

If you know the array component devices, you can simply issue something as

mdadm --zero-superblock /dev/sd[abcd]

to remove the superblock from multiple devices with a single command.

Please note that mdadm checks for superblock before zeroing anything, so specifying sd* in the command above should only touch component disks, without writing anything to the others. However, I strongly suggest against doing that: any issues in the superblock detection routing, or using --force, are going to overwrite good data on non-component disks (causing data loss). From the man page:

If the device contains a valid md superblock, the block is overwritten with zeros. With --force the block where the superblock would be is overwritten even if it doesn't appear to be valid.

For the reason above, if you don't know the array component devices I suggest listing them via

blkid -t TYPE="linux_raid_member" -o device

and then zeroing the specific disks with mdadm --zero-superblock. This can be scripted with a single-line bash command:

for dev in `blkid -t TYPE="linux_raid_member" -o device`; do echo "zeroing $dev"; mdadm --zero-superblock $dev; done

As always, triple-check any such commands to avoid data loss.

EDIT: to discover the array component devices without resorting to parsing of mdadm --detail output, you can use lsblk. For example, on a test array created via loop devices:

# lsblk -l -n -s /dev/md127
md127   9:127  0  255M  0 raid1
loop5   7:5    0  256M  0 loop
loop6   7:6    0  256M  0 loop
2
  • Thank you. Image I have two raids md0 and md1. I want to know which devices are part of md0. Up to now I found no clean way to get the devices. Parsing the output of mdadm --detail feels too fragile to me.
    – guettli
    Sep 20 at 7:37
  • 1
    If you don't want to parse mdadm --detail, you can try with lsblk -l -n -s /dev/md0
    – shodanshok
    Sep 20 at 11:11
1

There isn't a single command (mdadm …) that will remove the superblock from all devices of a mdraid. However, it's fairly straightforward to write a script that then becomes a one-liner:

#!/bin/bash
md=$1
[ ! -b "$md" ] && { echo "ERROR: Not a block device: $md" >&2; exit 1; }

# Collect the slices
slices=($(mdadm --detail "$md" 2>/dev/null | awk '/active/{print $NF}'))
[ ${#slices} -eq 0 ] && { echo "ERROR: No slices found for $md" >&2; exit 1; }

# Sanity check
if [ -t 2 ]
then
    echo "Active MD RAID $md has slices: ${slices[*]}" >&2
    if [ -t 0 ]
    then
        read -p 'Stop and erase (Y/n)? ' yn
        [ ! "$yn" = 'Y' ] && exit 1
    fi
fi

# Stop the array
echo "Stopping: $md" >&2
if ! mdadm --stop "$md"
then
    echo "ERROR: Cannot stop: $md" >&2
    exit 1
fi

# Erase the slices
for slice in "${slices[@]}"
do
    echo "Zero: $slice" >&2
    mdadm --zero-superblock "$slice"
done

# All done
exit 0

Example usage, assuming the script is called mdzero and it's in the PATH, etc. and the unwanted RAID device is /dev/md1:

mdzero /dev/md1

Active MD RAID /dev/md1 has slices: /dev/dm-2 /dev/dm-3 /dev/dm-4
Stop and erase (Y/n)? Y
Stopping: /dev/md1
mdadm: stopped /dev/md1
Zero: /dev/dm-2
Zero: /dev/dm-3
Zero: /dev/dm-4

Unless you enter a (capital) "Y" in response to the "Stop and erase?" question - or the command is being run non-interactively - the tool will quit at that point. Essentially, you've got 20+ lines of sanity checking to ensure that the active mdadm --zero-superblock command is only run if and where absolutely intended

2
  • thank you for the shell script. Nevertheless I would prefer a solution which gets the data in a more precise way (without awk from mdadm --detail).
    – guettli
    Sep 19 at 7:35
  • @guettli I don't believe there is a better or more precise way (mdadm doesn't provide machine parseable output, so we have to extract the data heuristically). That's why there is an "Are you sure?" in the middle that cannot be overridden by a command-line flag. You can test the code by commenting out the mdadm --zero-superblock Sep 19 at 8:17
0

You can use blkid to do this with a little bit of manipulation.

First, get the raid array you care for.

mdadm --detail /dev/md0
...
UUID : 8c9c9aa9:0c574535:e9080607:7ac4c6d8

Convert the UUID Into a a proper UUID format and you can use blkid to pull out all disk members.

blkid -t UUID=8c9c9aa9-0c57-4535-e908-06077ac4c6d8 --output device
/dev/nvme1n1p3
/dev/nvme0n1p3

If you just want to blast all mdadm raid arrays you dont need to collect any other identifier:

blkid -t TYPE="linux_raid_member" --output device
/dev/nvme1n1p2
/dev/nvme1n1p3
/dev/nvme0n1p2
/dev/nvme0n1p3

To blast everything using this method you can even use wipefs. That could look something like..

blkid -t TYPE="linux_raid_member" --output device | xargs wipefs -n

Of course, this will zero all raid arrays of all disks without caring about the particular raid array in question. I added a -n to prevent people being careless with their copy/paste!

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .