RSS icon Home icon
  • Resize the RAID1 root partition without rebooting

    Posted on February 26th, 2023 admin No comments

    Resizing the root partition on a server 12 miles away seemed a bit risky. The root partition is on a software RAID1 mirror, no LVM. Twelve years ago I left only 2 GB for the root partition. I wanted to upgrade to Ubuntu 20.04.5 and the small size quickly became a problem. I managed to upgrade to Ubuntu 18.04.6 LTS, but ran out of space while upgrading to 20.04.5 LTS. Even after uninstalling previous versions, there was not enough space left. I had to make the root partition bigger. There was a relatively large 8 GB swap partition right after the root partition. The RAM is only 2 GB, so I was able to reduce the size of the swap. The plan was to delete the swap and extend the root partition, then create a smaller swapfile. I’ll show you how I did it. The server did not have to be restarted even once during the partition conversion. Finally, I have 5 GB free space after upgrading to Ubuntu 20.04.5 LTS.

    Step 1. Identify the swap array (command ‘lsblk’).

    ├─sda1 8:1 0 1.9G 0 part
    │└─md0 9:0 0 1.9G 0 raid1 /
    ├─sda2 8:2 0 7.5G 0 part
    │└─md1 9:1 0 7.5G 0 raid1 [SWAP]

    ├─sdb1 8:17 0 1.9G 0 part
    │└─md0 9:0  0 1.9G 0 raid1 /
    ├─sdb2 8:18 0 7.5G 0 part
    │└─md1 9:1  0 7.5G 0 raid1 [SWAP]

    In summary, I had to delete the md1 array and sda2+sdb2 partitions, then I had to increase the sda1+sdb2 partitions and the md0 array.

    Step 2. Turn off the swap and delete it from /etc/fstab

    swapoff -a

    Step 3. Remove the swap array and erase the RAID information. But before using mdadm, remove the swap entry from /etc/mdadm/mdadm.cfg. The line which starts with “ARRAY /dev/md/1” in my case. At least, I couldn’t stop the array without this.

    mdadm --stop /dev/md1
    mdadm --zero-superblock /dev/sda2 /dev/sdb2

    Step 4. Delete the swap partitions from both disks then resize the root partition. I had to repeat this process for /dev/sdb. The value of End should match the value of the deleted swap partition. In my case it was 9999MB.


    Using /dev/sda
    (parted) print

    Number Start  End    Size   Type    File system    Flags
    1      1049kB 2000MB 1999MB primary ext3           boot, raid
    2      2000MB 9999MB 8000MB primary linux-swap(v1) raid

    (parted) rm 2
    (parted) resizepart
    Partition number? 1
    End? 9999MB

    (parted) select /dev/sdb
    Using /dev/sdb
    (parted) rm 2
    (parted) resizepart
    Partition number? 1
    End? 9999MB
    (parted) quit

    Step 5. Issue the ‘partprobe’ command to inform the OS of the partition table changes.

    partprobe /dev/sda
    partprobe /dev/sdb

    Step 6. Resize the RAID array. To calculate the new size, the partition size is a bit misleading. It seems to be about 10 GB, but if you look back at the output of the lsblk command it’s just 9.4 GB (1.9 GB + 7.5 GB). I was in a hurry, I didn’t have time to calculate the exact size, so I left it at 9400 MB.

    mdadm --grow --size=9400M /dev/md0

    Step 7. Resize the filesystem to match the new array size.

    resize2fs -p /dev/md0

    Step 8. Create a new swap file on the root partition and activate it.

    dd if=/dev/zero of=/swap.img bs=2048 count=1048576
    chmod 600 /swap.img
    mkswap /swap.img
    swapon /swap.img

    Don’t forget to insert a line in /etc/fstab for the new swap image.

    /swap.img   none   swap   sw  0  0

    Step 9. Update grub and initramfs. Apt always gave an error message complaining about “no swap device available” when calling initramfs. I had a resume file in /etc/initramfs-tools/conf.d/ which is for hibernation. This file pointed to the old non-existent swap partition. I don’t use it for the server so I simply deleted it.

    rm /etc/initramfs-tools/conf.d/resume
    update-initramfs -u -k all

    That’s all about it. No need to reboot the server.


    Leave a Reply

    Your email address will not be published. Required fields are marked *