Marc Wäckerlin
Für eine libertäre Gesellschaft

Add or Replace Harddisk on Running System

April 2, 2017

Views: 1040

If your computer allows hot swap of harddisks, you do not even need to shutdown your computer to add a harddisk that is part of a Linux Logical Volume Manager (LVM).

I have a HP ProLiant N36L, which offers 4 slots for harddisk. I bought it with the built in 250G Harddisk and an additional 2TB drive (I strongly recommend special 7dx24h server harddisk for servers that are always up; cheaper harddisks crash after a year; don’t buy Seagate, I lost two Seagate 8TB harddisks within a week).

I set it up with a 500MB /boot partition (formatted with ext2, then I merged the remaining space /dev/sda2 of the 250GB harddisk with the full second 2TB harddisk /dev/sdb1 using the logical volume manager LVM2. Then I encrypted this huge logical volume with dmcrypt cryptsetup and formatted with xfs. I configured this in the installer of the Ubuntu alternate-, respectively server installer.

Add New Harddisk To Encrypted Logical Volume

Then I had to add a next 2TB disk. Of course, I want to do this without destroying the data; not because I have no backup, but because restoring 2TB of lasts days. My shock was, other than during installation, there’s no LVM2-GUI that prevents me from hacking into the command line. So I had to find out the commands to manually add the space of the new disk. It was quite simple.

  1. Add new Harddisk: First, shut down the system (if it does not support hotplugging), turn out the computer and add the new disk, then restart.
    Here it is the third disk, so it’s /dev/sdc. After boot, login as root and start …
  2. Use sudo parted /dev/sdc, then mklabel gpt and mkpart primary 0% 100% to create one large primary partition (alternatively: use sudo fdisk to init partition table and setup a primary partition /dev/sdc1 with id 8e)
  3. Create physical volume: sudo pvcreate /dev/sdc1
  4. Look for the name of the volume group with sudo vgdisplay; in my case, it’s named big
  5. Extend the volume group by the new harddisk: sudo vgextend big /dev/sdc1
  6. Look for the name of the logical volume within the volume group with sudo lvdisplay; in my case, it’s also named big
  7. Extend the logical volume by the new harddisk: sudo lvextend /dev/big/big /dev/sdc1
  8. Get the name of the mounted encrypted filesystem in /dev/mapper by calling mount; in my case, it’s /dev/mapper/big-big_crypt
  9. Resize the encrypted filesystem to the new dimension: sudo cryptsetup resize big-big_crypt
  10. Expand the filesystem, here it’s xfs, so I use sudo xfs_growfs, otherwise use resize2fs for ext2, ext3, ext4 or resize_reiserfs for reiserfs, or btrfs filesystem resize for btrfs.
  11. That’s it. resized a running system, no reboot necessary. df -h / now shows that my filesystem now has a bit less than 4TB. Before, it was a bit less than 2TB (yes, a «2TB» harddisk is much smaller than 2TB, that’s part of the day-to-day ceating).

Replace a Small Harddisk With a Bigger

Now that my harddisks are all full, I replace the oldest 2TB harddisk by a 8TB sized one. This will increase my overall size from 12TB to 18TB.

  1. Attach the new harddisk externally (since there is no free internal slot available) with a SATA to USB adapter. This is my drive /dev/sde.
  2. I cannot partition this huge harddisk with fdisk, so I use parted /dev/sde and within this mklabel gpt to initialize a GPT patition and mkpart primary 0% 100%. Quit with quit.
  3. Create a physical volume: sudo pvcreate /dev/sde1
  4. Look for the name of the volume group with vgdisplay; in my case, it’s named big
  5. Extend the volume group by the new harddisk: sudo vgextend big /dev/sde1
  6. This step is now different to the instructions above: At this moment move all data away from the old harddisk that you want to remove. For me, this is /dev/sd1 with partition /dev/sdb1, so I move all the data away (to the new harddisk) using the command: sudo pvmove /dev/sdb1 This process is very slow, it took two full days to finish and to move all the 2TB to the new disk.
  7. First remove /dev/sdb1 from the volume group: sudo vgreduce big /dev/sdb1
  8. Now I can savely remove /dev/sdb1 from the logical volume group: sudo pvremove /dev/sdb1 from now on, the old harddisk does no more belong to the volume group and no more contains a logical volume. It can now be safely removed.
  9. You can use the commands vgs, lvs and pvs to list all volume groups, logical volumes and physical volumes. Call sudo pvs to check that /dev/sdb1 is unused:
    $ sudo pvs
      PV         VG   Fmt  Attr PSize PFree
      /dev/sda2  big  lvm2 a--  5.46t    0 
      /dev/sdb1       lvm2 a--  1.82t 1.82t
      /dev/sdc1  big  lvm2 a--  1.82t    0 
      /dev/sdd1  big  lvm2 a--  1.82t    0 
      /dev/sde1  big  lvm2 a--  7.28t 5.46t
  10. Look for the name of the logical volume within the volume group with lvdisplay; in my case, it’s also named big
  11. Extend the logical volume by the new harddisk: sudo lvextend /dev/big/big /dev/sde1
  12. Get the name of the mounted encrypted filesystem in /dev/mapper by calling mount; in my case, it’s /dev/mapper/big-big_crypt
  13. Resize the encrypted filesystem to the new dimension: sudo cryptsetup resize big-big_crypt
  14. Expand the filesystem. Meanwhile I upgraded to btrfs, so I call sudo btrfs filesystem show / to find the device id (1), then sudo btrfs filesystem resize 1:max /. For xfs, use xfs_growfs, otherwise use resize2fs for ext2, ext3, ext4 or resize_reiserfs for reiserfs.
  15. That’s it. replaced an existing harddisk, up to now without reboot. df -h / now shows that my filesystem now has 17TB. Before, it was a bit less than 12TB.
  16. Now I shut down the computer, disassemble /dev/sdb and insert /dev/sde in the now empty slot, then restart.

comments title