Scripting

Convert Raid 1 To Plain Disk

How to convert an MDADM Managed RAID1? drive to a plain disk

Courtesy of:

1 - Stephane Chazelas answer here: https://unix.stackexchange.com/questions/57477/convert-dev-md0-to-dev-sda1

Get data via netcat from other PC:

[root@acer-dt-arch ~]# nc 192.168.200.6 5000 | dd of=/dev/sde2 bs=10M
0+16437776 records in
0+16437776 records out
107374182400 bytes (107 GB, 100 GiB) copied, 10785.6 s, 10.0 MB/s

unplug and re-plug drive:

[root@acer-dt-arch ~]# dmesg
.....
[12982.495291] sd 8:0:0:0: [sde] Attached SCSI disk

Cannot directly mount drive:

[root@acer-dt-arch ~]# mount /dev/sde2 /mnt/tmp/
mount: /mnt/tmp: unknown filesystem type 'linux_raid_member'.

Also cannot remove superblock

[root@acer-dt-arch ~]# mdadm --zero-superblock /dev/sde2
mdadm: Couldn't open /dev/sde2 for write - not zeroing

This is because it has already been detected as raid and mdadm initialised:

[root@acer-dt-arch ~]# cat /proc/mdstat
Personalities : [raid1]
md127 : inactive sde2[2](S)
      104792064 blocks super 1.2

unused devices: <none>

Get Data Offset of partition (in this case 131072 sectors).

[root@acer-dt-arch ~]# mdadm -E /dev/sde1
/dev/sde1:
   MBR Magic : aa55
[root@acer-dt-arch ~]# mdadm --zero-superblock /dev/sde2
mdadm: Couldn't open /dev/sde2 for write - not zeroing
[root@acer-dt-arch ~]# mdadm -E /dev/sde2
/dev/sde2:
          Magic : a92b4efc
        Version : 1.2
    Feature Map : 0x0
     Array UUID : eb274c42:226693ba:c781d04c:2cf395c9
           Name : archiso:0
  Creation Time : Sat Feb  1 18:21:04 2014
     Raid Level : raid1
   Raid Devices : 2

 Avail Dev Size : 209584128 (99.94 GiB 107.31 GB)
     Array Size : 104792064 (99.94 GiB 107.31 GB)
    Data Offset : 131072 sectors
   Super Offset : 8 sectors
          State : clean
    Device UUID : c9692bec:21c7f2b4:68b3760d:793b370d

    Update Time : Sat Dec 29 00:24:36 2018
  Bad Block Log : 512 entries available at offset 72 sectors
       Checksum : e32bd0fb - correct
         Events : 32355


   Device Role : Active device 0
   Array State : AA ('A' == active, '.' == missing, 'R' == replacing)

Stop array:

[root@acer-dt-arch ~]# mdadm --stop /dev/md127
mdadm: stopped /dev/md127

Now remove superblock (not sure if this is strictly necessary)

[root@acer-dt-arch ~]# mdadm --zero-superblock /dev/sde2

It still won't mount due to the data offset:

[root@acer-dt-arch ~]# mount /dev/sde2 /mnt/tmp/
mount.nilfs2: Error while mounting /dev/sde2 on /mnt/tmp: Invalid argument

[root@acer-dt-arch ~]# mount -t ext4 /dev/sde2 /mnt/tmp/
mount: /mnt/tmp: wrong fs type, bad option, bad superblock on /dev/sde2, missing codepage or helper program, or other error.

We need to move the partition along by the offset so that the underlying ext4 filesystem is at the start:

[root@acer-dt-arch ~]# fdisk -u /dev/sde

Welcome to fdisk (util-linux 2.33).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sde: 111.8 GiB, 120034123264 bytes, 234441647 sectors
Disk model: SNA-DC/U
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: 1E62424E-0B97-405A-8799-AE9A4EFFB716

Device       Start       End   Sectors  Size Type
/dev/sde1     2048   1026047   1024000  500M EFI System
/dev/sde2  1026048 210741248 209715201  100G Linux filesystem

Note the start sector of sde2, then add the data offset from above to get the new value for the partition. Then delete and add the new partition with the new start sector. Do not remove the 'signature' when prompted:

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (1026048-234441613, default 1026048): 1157120
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1157120-234441613, default 234441613): 210741248

Created a new partition 2 of type 'Linux filesystem' and of size 100 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): p

Disk /dev/sde: 111.8 GiB, 120034123264 bytes, 234441647 sectors
Disk model: SNA-DC/U
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: 1E62424E-0B97-405A-8799-AE9A4EFFB716

Device       Start       End   Sectors  Size Type
/dev/sde1     2048   1026047   1024000  500M EFI System
/dev/sde2  1157120 210741248 209584129  100G Linux filesystem

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now your device is a single drive and mountable:

[root@acer-dt-arch ~]# mount -t ext4 /dev/sde2 /mnt/tmp/

[root@acer-dt-arch ~]# ls /mnt/tmp/
bin   dead.letter  etc   lib    local       media  opt   root  sbin  sys  usr
boot  dev          home  lib64  lost+found  mnt    proc  run   srv   tmp  var