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 have a single disk that I want to create a Mirror of, Lets call this disk sda I have just bought another identicle size disk, which we can call sdb. sda and sdb have one partition called sda1 and sdb1 respectively.

When creating a raid I dont want to wipe my sda clean and start again, I just want it to start mirroring with sdb. My train of thought was to do:

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=1 /dev/sda1

..to create the array without sdb disk, then run something like (Im thinking the following command out loud, because I am not sure how to achieve this step)

mdadm /dev/md0 --add /dev/sdb1

Note sdb1 is assumed to be formated similar to sda1

Is this posible?

share|improve this question
    
Yes, you can. It's not as straightforward as --add, and you need to be careful, but it's not very complicated. See How to set up disk mirroring in Ubuntu? (I can't propose that question as a duplicate because of the new rules.) –  Gilles Feb 6 '13 at 22:47
add comment

3 Answers

up vote 2 down vote accepted

The simple answer to the question in the title is "Yes". But what you really want to do is the next step, which is getting the existing data mirrored.

It's possible to convert the existing disk, but it's risky, as mentioned, due the the metadata location. Much better to create an empty (broken) mirror with the new disk and copy the exiting data onto it. Then, if it doesn't work, you just boot back to the un-mirrored original.

First, initialize /dev/sdb1 as the new /dev/md0 with a missing drive and initialize the filesystem (I'm assuming ext3, but the choice is yours)

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 missing
mkfs -text3 /dev/md0

Now, /dev/sda1 is most likely your root file system (/) so for safety you should do the next step from a live CD, rescue disk or other bootable system which can access both /dev/sda1 and /dev/md0 although I have successfully done this by dropping to single user mode.

Copy the entire contents of the filesystem on /dev/sda1 to /dev/md0. For example:

mount /dev/sda1 /mnt/a       # only do this if /dev/sda1 isn't mounted as root
mount /dev/md0 /mnt/b
cd /mnt/a                    # or "cd /" if it's the root filesystem
cp -dpRxv . /mnt/b

Edit /etc/fstab or otherwise ensure that on the next boot, /dev/md0 is mounted instead of /dev/sda1. Your system is probably set to boot from /dev/sda1 and the boot parameters probably specify this as the root device, so when rebooting you should manually change this so that the root is /dev/md0 (assuming /dev/sda1 was root). After reboot, check that/dev/md0 is now mounted (df) and that it is running as a degraded mirror (cat /proc/mdstat). Add /dev/sda1 to the array:

mdadm /dev/md0 --add /dev/sda1

Since the rebuild will overwrite /dev/sda1, which metadata version you use is irrelevant. As always when making major changes, take a full backup (if possible) or at least ensure that anything which can't be recreated is safe.

You will need to regenerate you boot config to use /dev/md0 as root (if /dev/sda1 was root) and probably need to regenerate mdadm.conf to ensure /dev/md0 is alwasy started.

share|improve this answer
    
Perfect exactly what I need :D –  whoami Feb 6 '13 at 13:21
    
Don't use cp to copy filesystems, you'll miss some things like hard links, extended attributes... Use tar or rsync with the proper options. When booting a Live CD, beware that uid-user mapping are different (see the --numeric-owner options of tar/rsync). See also clone2fs for cloning extx file systems. –  Stephane Chazelas Feb 6 '13 at 13:42
    
The options to cp handle preserving links (-d), mode, ownership and timestamps (-p), doing it recursively (-R) and staying on one filesystem (-x), but I think you may be right about hard links and extended attributes. –  StarNamer Feb 6 '13 at 22:46
add comment

Sure, you may create it specifying that second disk is currently missing:

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 missing /dev/sda1
share|improve this answer
    
Sweet thanks, Im probablly being OCD here, but is there a reason why you added the missing before the /dev/sda1, is the order important? –  whoami Feb 6 '13 at 11:14
    
I think the order does not matter. –  eppesuig Feb 6 '13 at 11:31
2  
Careful with that! with --metadata=1.2 (the new default), RAID metadata (1MB) will be inserted at the start (overriding the content there). And the start of the md array will start 1MB within /dev/sda1 (/dev/md0 will be 1MB shorter than /dev/sda1). With metadata=0.9 (the old default), the metadata (64kB) will be inserted at the end overriding what's there. So use --metadata=0.9 and resize the FS first. Whatever you do, take a backup beforehand. –  Stephane Chazelas Feb 6 '13 at 11:31
add comment

You can do that. You need to be a bit careful, but this is not dangerous¹ if you are very careful not to mistype anything and it doesn't leave any gotchas in the setup.

I highly recommend not doing any of the manipulations on a live system. It's possible in some cases but requires extra care. Boot from a liveCD/liveUSB such as Parted or SystemRescueCD.

First, you need to shrink the volume a bit, to make room for mdraid metadata (the superblock). There are several metadata formats, you must use one that puts the metadata at the end of the disk. (In some setups, you may have enough space to put the superblock at the beginning, but that's more complicated and risk-prone so I go into that.)

You must ensure that the last 128kB from the block device are unused, to make room for the superblock. So you'll need to shrink the filesystem on /dev/sda1. If this is an ext2/ext3/ext4 filesystem, obtain the current filesystem size with tune2fs /dev/sda1, then run resize2fs /dev/sda1 NNN where NNN is that size minus 128kB. You can do this with Parted instead. If you need to shrink an extN filesystem, you'll need to unmount it first; a btrfs filesystem can be shrunk live.

Once you have ensured that the last 128kB of the block device are free, call mdadm --create to create a RAID-1 volume. This doesn't touch any part of the volume aside from the superblock. Initially, the volume will have a single component: all the others are set as failed. You must pass --level=1 (or equivalently -n 1) (this approach only works for RAID-1) and --metadata=0.9 or --metadata=1.0 (the default superblock format 1.2 puts the superblock near the beginning of the device, which may overwrite data). The argument to --raid-devices (-n) is the number of components (included missing ones) in the RAID volume.

mdadm --create /dev/md0 --level=1 --raid-devices=2 --metadata=1.0 /dev/sda1 missing

You can now activate the array and add other components.

mdadm --add /dev/md0 /dev/sdb1

A note on bootloaders: Grub2 understands Linux RAID-1 and can boot from it. Bootloaders such as Grub1 that don't understand RAID read transparently from mirror volumes, but your system won't boot if the drive the bootloader is reading from fails. If the RAID volume is on a partition, be sure to install Grub's boot sector on both drives.

¹ Be sure to have backups. “Not dangerous” means “you probably won't need them”, not “gamble your data”.

Reposted and slightly adapted from How to set up disk mirroring in Ubuntu?

share|improve this answer
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.