1

I set up a webserver a while ago and made a mistake. I do have the following:

/dev/md0 swap  (made from sda1 and sdb1)
/dev/md1 /boot (made from sda2 and sdb2)
/dev/md2 /     (made from sda3 and sdb3)
/dev/md3 /home (made from sda5 and sdb5)

(sda4 and sdb4 are the extended partitions)

I mistyped while setting up the RAID (I was using an installer script), and ended up with the following sizes:

swap   10GB
/boot  500MB
/      50GB
/home  The Rest (About    1,8TB)

Everything worked fine.. until I uploaded about 80GB of files into /var/www, and the transfer crashed half way. Everything went nuts. After a while I found out that I simply filled up the / partition which crashed the MySQL server, and immediately recognized that making / so small was dumb (or at least I should have put /var/www on a different partition). Well, the server is in use now for quite some time and I really am not interested in setting up everything again. I backed everything up, just in case, but as you all know, setting up a server is work, and setting it up to a known state while using backups is even more work. so I would like to fix the problem.

What I would like to do now is the following:

  • shrink the /home partition to about 1TB
  • add a new partition in the now free space
  • move over /var/www
  • mount the new partition in that location

So, my plan was the following:

  1. unmount /home
  2. do an e2fsck on /dev/md3
  3. resize2fs /dev/md3 to a size of about 990GB to have 10GB as a security margin
  4. mdadm resize /dev/md3 to 1TB
  5. resize2fs without naming a size to grow it to the maximum available on the new md

(so far so good, everything up to this point worked)

  1. add new partitions sda/b6 in the now free space
  2. make them a raid
  3. mount it to /mnt
  4. copy over /var/www
  5. diff /var/www and /mnt just to make sure
  6. rm -rf * in /var/www
  7. mount /dev/md4 to /var/www, and add a corresponding entry into fstab to make it permanent.

I am now stuck at no. 6: The raid is shrinked, /dev/md3 has a size of about 1TB, everything is perfectly fine, but the partitions /dev/sda5 and /dev/sdb5 are still the old size. wWhat did I miss? Shouldnt the partitions have shrinked with the md? How do I shrink them without breaking the raid?

2 Answers 2

3

Shrinking the RAID doesn't shrink the partitions - you have to do that manually.

This is not 100% trivial, as your MD raid may contain a RAID superblock at the beginning OR at the end.

If the RAID superblock is in the beginning (version 1 and later), it should be a no-brainer though: fdisk or the tool of your choice and delete (yes: I mean delete) sda5 after noting its starting block, then recreate it with the same starting block and enough space. You can then create sda6 in the remaining space. I recommend you then reboot to make sure everything works as planned, before repeating the process with sdb.

If the RAID superblock is in the end (Version 0.9) things get very messy - I recommend you use mdadm --detail, then mdadm --stop and finally mdadm --create ... --metadata=1.0 to move it to the beginning of the device and then proceed as above.

EDIT

I didn't point out (as I wrongly assumed it to be clear), that you need to mdadm --stop ... first: On exiting, fdisk instructs the kernel to reread the partition table, which marks one half of the raid as not up to date.

9
  • Just to make sure, you mean deleting/adding the partition AFTER removing it from the raid?
    – damaltor
    Commented Jun 20, 2018 at 20:06
  • (Also, the Version is 1.2, so the Block should be near the beginning)
    – damaltor
    Commented Jun 20, 2018 at 20:14
  • I definitly mean not removing it from the RAID - just changing the partition size, so that after the reboot the recreated partition with the existing RAID data is used. Superblock 1.2 should be fine. Commented Jun 20, 2018 at 21:52
  • 1
    I did so now, i deleted and made the partition new. after reboot, the device was flagged as removed from the raid, so i re-added it. syncing back up now, will update on progress in the comments here.
    – damaltor
    Commented Jun 21, 2018 at 12:35
  • 1
    @poke Not all partitioning tools support resizing (most prominently fdisk does not), but all support deleting and recreating, which creates identical results. Commented Jun 13, 2019 at 15:10
1

Unfortunately this is harder then it seems. The MD block devices use partitions on the underlying disks - and these disk need to be the right size - and contiguous blocks of space.

NOTE: Disk operations of this sort are risky. Back up first.

Because your home directory comes after your www directory, shrinking it will free up space at the end of the disk - but you need the space at the beginning of the disk. You might be able to use gparted to move the /sda3, sda5, sdb3 and sdb5 to the end of the disk, but I've not had much luck with that tool.

The way I would do it (if not starting from scratch for reasons below) would be to move /home onto /www temporarily (or onto external storage if it's already too big), using fdisk, delete sda3,5 and sdb3,5 then delete sda2 and sdb2 and recreate with the same starting block but later ending block making sure to set the partition type (to fd for RAID), then recreate sda3,5 and sdb3,5 (or just sdb3 - if you are sure you won't need more then 4 partitions) with the remaining space.

After this, reboot and expand the RAID, then recreate md3, /home and copy data back. You will need to modify fstab as well.

That said, I would seriously consider starting again using just 2 RAID partitions - a small one for boot, and a large one for everything else. I would then use LVM on top of the large RAID partition to carve it up into blocks like /www, /home - because using LVM provides a lot of flexibility, including the ability to resize partitions regardless of underlying geometry, add disks and take snapshots.

4
  • Thanks for your thoughts. Why would i need Space at the beginning of the disk? Is there any need to have the new partition at the beginning? Why wouldnt it be possible to put the partition for /var/www after /home? Also, how should i resize the partition - do i have to fail/remove the partition from the traid first, resize it, put it back in?
    – damaltor
    Commented Jun 21, 2018 at 11:37
  • "the beginning of the disk" was badly worded - ignoring abstraction layers like LVM you can only extend partitions by adding to the end of the partition because - I believe - all major file systems have something akin to an index (which lists filenames and positions ) at the beginning of the partition. If you expad a partition at the beginning that index is suddenly in the wrong place.
    – davidgo
    Commented Jun 21, 2018 at 20:04
  • The order of mounting /var/www and /home is irrelevant - my solution was designed to free space at the correct place on disk.
    – davidgo
    Commented Jun 21, 2018 at 20:06
  • well, i wanted to shrink the partition, so it is probably fine. thanks anyways, you helped me out a bunch.
    – damaltor
    Commented Jun 21, 2018 at 20:06

You must log in to answer this question.

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