Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to mirror my existing root (and only) partition on an SSD to another disk. It should be a sort of RAID-1, just asymmetric*. I know there's the option mdadm --write-behind, which should do it.

But I have no idea if it is possible with preserving the context of the existing partition. I imagine it like

  • create the "slave" partition
  • setup the RAID telling it that the slave partition is not initialized
  • let it initialize it by cloning the master partition

but I'm probably too optimistic, aren't I?


* All reads should access the first disk and writes should be considered finished when the first disk is written.

share|improve this question
1  
A friend of my friend recently nuked all his data by running mdadm --create while trying to achieve the same thing. I don't know how to do it correctly but that's not the way. –  stribika Mar 10 '11 at 14:25
    
@stribika: You can run mdadm --create on an existing disk as long as you choose a superblock version that goes to the end of the disk, and shrink the filesystem accordingly. –  Gilles Mar 10 '11 at 19:33
    
@Gilles: I think he didn't do the degraded trick just added both disks to the array and hoped it would sync the right way. –  stribika Mar 10 '11 at 21:54
add comment

2 Answers

up vote 6 down vote accepted

You can create an mdraid RAID-1 array starting with an existing partition. First, you need to make room for the mdadm superblock, which means you need to shrink your filesystem a little.

At the moment, the normal superblock format is 0.9. Its location is between 128kB and 60kB from the end of the partition, it is 4kB long, and it starts on an address that is a multible of 64kB. So shrink your filesystem by 128kB, or more precisely to ((device_size mod 64kB) - 1) * 64kB.

If you want more than 2TB per stripe, you need the 1.0 superblock format, which isn't supported out-of-the-box by all distributions yet. The 1.0 superblock is at the end of the device, which I understand to mean that you only need to shrink your filesystem by 8kB.

Now that you've shrunk the filesystem, you can create the array. First create a degraded array with just the existing data. Make sure the filesystem isn't mounted at this point. For your use case the write-intent bitmap must be on a separate partition. Use -e 1.0 to use the newer version-1 superblock format.

mdadm --create /dev/md0 -e 0.9 -l 1 -n 2 \
      --write-behind=256 --bitmap=/path/to/bitmap /dev/sda1 missing

Now you can mount the filesystem in /dev/md0. Add the second disk at your leasure. The data will be copied to the new drive in the background.

mdadm --add /dev/md0 --write-mostly /dev/sdb1

I've created a mirrored array like this, but without write-behind mode. I don't think write-behind mode would invalidate the procedure.

share|improve this answer
    
Great. This would take half the time my method takes. –  stribika Mar 10 '11 at 21:51
    
Can I remove the write-intent later? –  Lionel Jan 18 '12 at 6:56
    
@Lionel: I've never tried this myself, but the mdadm manpage documents the following: * under "For Manage mode", that one can reset --write-mostly with mdadm --readwrite --re-add /dev/sdb1 * under --bitmap, that one can use --grow /dev/md0 --bitmap=none. BTW, the 1.0 format seems more 'defaultish' by now. –  Blaisorblade Dec 10 '13 at 14:56
add comment

I have an idea. I tested this with small filesystems on loop devices I recommend you do the same before trying it yourself. In this answer /dev/sda is your disk with the important data and /dev/sdb is the new emtpy disk.

export DATA_DISK='/dev/sda'
export EMPTY_DISK='/dev/sdb'
  1. Create a degraded RAID1 array from the empty disk. This is important!

    mdadm --create /dev/md0 --level=1 --raid-devices=2 "${EMPTY_DISK}" missing
    
  2. Then shrink the filesystem on the disk you want to mirror. (Hopefully it's supported.) This is needed because the RAID arrays have a header and the full filesystem won't fit on the array.

  3. Copy the data to the new degraded array.

    dd if="${DATA_DISK}" of=/dev/md0 bs=4k
    
  4. Add the original disk to the array.

    mdadm --manage /dev/md0 --add "${DATA_DISK}"
    
  5. You can watch the synchronization progress.

    watch 'cat /proc/mdstat'
    
share|improve this answer
    
This procedure can be tuned in a few ways (rsync instead of raw copy, settings to choose the syncing bandwidth, and so on). One page describing such techniques which looks good is wiki.archlinux.org/index.php/…, though I didn't try that myself. –  Blaisorblade Dec 14 '13 at 7:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.