Re-sizing a virtual drive without losing data in Linux

vr_linux
If you’re like me, sometimes you create a virtual machine without planning ahead with regard to space needs. If you aren’t like me, and all 4 partitions aren’t used up on your particular VM, you could easily add an additional partition without having to enlarge an existing one. However, if you are like me, your virtual hard-drive already has 4 partitions and it’s impossible to add another one. In this case, the easiest solution is to resize the drive and reformat. But what about all the data! If reformatting isn’t feasible for you, do not fret! It’s still possible to resize your virtual Linux drive without losing your data!

  1. Resize the virtual drive in VM management software; once this is done, you’ll notice that it doesn’t yet translate into a larger linux drive — this is because although the new drive has grown, the filesystem hasn’t grown to match it.
  2. fdisk -l (see what your drives are)
  3. fdisk /dev/sda (or whatever the virtual disk’s ID is — main disk, not an individual partition)
  4. p (print the partition table and take note of the 4th partition’s starting sector)
  5. d then 4 (delete the 4th partition)
  6. n then 4 (should default to 4 — create a new partition over the old one)
  7. p (make it a primary partition and make sure the first sector matches; the last should default to the new last sector of the “drive“)
  8. w (write the new partition table)
  9. resize2fs /dev/sda4 (re-size the file system to match the partition table for the 4th partition)

And poof! You’re all done! You should now have re-sized the underlying file system to match your new virtual drive’s size.

This entry was posted in Digital Humanities. Bookmark the permalink.