Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've a typical small office server with two hard disks configured for RAID-1 (mirroring). Each disk has several partitions: one for swap, the others paired in several /dev/mdX arrays.

Every couple of years one of the disks dies and is replaced. The replacement typically goes something like this:

# copy partition table from the remaining good disk to the empty replacement disk
# (instead of /dev/good_disk and /dev/new_disk I use /dev/sda and /dev/sdb, as appropriate)
sfdisk -d /dev/good_disk | sfdisk /dev/new_disk

# install boot loader
grub-install /dev/new_disk

# create swap partition reusing the same UUID, so I don't need to edit /etc/fstab
mkswap /dev/new_disk1 -U xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# hot-add the new partitions to my RAID arrays
mdadm /dev/md0 -a /dev/new_disk2
mdadm /dev/md1 -a /dev/new_disk5
mdadm /dev/md2 -a /dev/new_disk6
mdadm /dev/md3 -a /dev/new_disk7
mdadm /dev/md4 -a /dev/new_disk8

The disks were originally partitioned with cfdisk back in 2009, and so the partition table is aligned traditionally to cylinder boundaries (255 heads * 63 sectors). This is not the optimum configuration for new 4K-sector drives.

My question is: how can I create a set of partitions for the new disk and ensure they're properly aligned, and have correct sizes for my RAID arrays (rounding up is acceptable, I suppose, but rounding down is definitely not)?

share|improve this question

3 Answers 3

As you said, you can allign it to multiples of 8 and you can do it error prone with GNU parted. I will assume /dev/sda is the old good disk and /dev/sdb will be the new one with 4k sector size.

sudo parted /dev/sda unit s print

Write down partition sizes. And round them up to 8. We'll use rounded numbers here.

sudo parted --align minimal /dev/sdb
unit s
print
mktable msdos
mkpart primary 64s <64+sda1-1>s
align-check minimal 1
mkpart primary <64+sda1>s <64+sda1+sda2-1>s
align-check minimal 2
mkpart primary <64+sda1+sda2>s <64+sda1+sda2+sda3-1>s
align-check minimal 3

and so on

If you have more than 4 partitions, you will have to set the fourth partition as "extended" and the remaining partitions define as "logical".

Mark you raid partitions with label raid

toggle 1 raid
toggle 2 raid

and so on

print
quit
share|improve this answer

I just had to replace the mirror in one of my arrays today, and I opted to just fdisk the replacement instead of using sfdisk. Since I know the partition sizes (its just one, in my case) and I went with a bigger drive for the replacement.

Original is RAID1 of a pair of ST31500341AS, and replacement disk is an ST2000DL003.

So, what I did was:

  1. mdadm /dev/md6 --fail /dev/sde1
  2. mdadm /dev/md6 --remove /dev/sde1
  3. replace /dev/sde
  4. fdisk /dev/sde

    1. c
    2. u
    3. n
    4. p
    5. 1
    6. enter (for 2048)
    7. 3907028736
    8. t
    9. fd
    10. w

    This results in new partition with start 2048 and end 3907029167 - for 1953513560 blocks (or 244189195 in 4k blocks) 3907028904 - for 1953513428 blocks (244189178 in 4k blocks). The old partition was start 63 and end 2930272064 with 1465136001 blocks. I think lvm takes care of being aligned with the space, so it should all be good.

    Forgot that I can't go all the way to the end of the disk, otherwise mdadm will switch to using the whole disk rather than the partition on reboot. LVM seems to tolerate this shift, but putting a filesystem directly onto the MD didn't. Recommendation was to shorten the partition by at least 256 sectors, so I did (3907029167-256)/8*8 in bc to get 3907028904. Resulting partition is mod 8 == 0, and short by 263 sectors. This problem got me when I did this for another array :(

  5. mdadm /dev/md6 --add /dev/sde1

  6. watch the array rebuild. Seems to be rebuilding at a pretty good speed, seems faster than it was with the old drive.

  7. and then remove and readd the other side, so that the superblocks will match.

share|improve this answer
    
Apparently, mdadm is smart and sees that the blocksize of the new disk is 4k, and sets the superblock for this. Resulting in a problem when I rebooted this morning. Had to stop what it had tried to bring up, assemble it with just the new disk and repartition the 'good' old disk for 4k alignment and add it. Now it seems to be happy. Though I do have another ST2000DL003 that'll be available soon. –  TheDreamer Sep 26 '12 at 1:51

I'll post my solution, with which I'm profoundly unhappy:

  1. sfdisk -d /dev/good_disk > /root/part.txt

  2. vi /root/part.txt

  3. Increase partition start numbers until they're all multiples of 8 (8 sectors of 512 bytes = 4K).

  4. Increase the size of the extended partition until it covers the entire new disk (which is 1 TB, while the old disk was 500 GB -- when the other disk dies and is replaced, I'll be able to create a new RAID array in the leftover free space.)

  5. sfdisk /dev/new_disk < /root/part.txt -L (the -L makes it accept partitions not aligned to cylinder boundaries, otherwise sfdisk aborts without changing anything).

I don't like it because it's error-prone and tedious.

share|improve this answer

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.