Resize Disk Partition on Remote VM Server

45,993

Solution 1

That what I would try:

  1. Many VPS hosting providers have tools to manipulate partitions in their control panels. Getting access to the control panel would be the safest option.

  2. Do not resize the partition but create a second partition and move your "working" data to that partition. I'e. mount /home or wherever your data is to a separate partition

  3. If you still feel adventurous, try remounting the boot partition read-only and see if it's possible to resize it while mounted read-only

  4. Something really tricky like remove swap - create a copy of your root partition at the very end of the drive - configure grub to boot from the new partition - reboot - resize your original partition - copy the data back - modify grub again - reboot - remove the temporary root partition - create a swap partition again.

I would consider the second two options really dangerous in the sense that if anything goes wrong your system just won't boot so you'd lose any access to the system.

Solution 2

If you want to enlarge the system partition

First, turn off swap (swapoff /dev/sda5), because we'll temporarily remove the swap partition.

Use GParted or cfdisk or fdisk to:

  1. Remove the logical partition /dev/sda5 and the extended partition /dev/sda2.
  2. Enlarge /dev/sda1 to span the whole disk minus what you want to allocate for the swap partition.
  3. Re-create a partition (make it a primary partition for simplicity) for the swap.

Do not turn swap back on. Comment out the swap line in /etc/fstab, and reboot. (If you don't reboot, the kernel keeps using obsolete data about the partitioning, which can have disastrous consequences.)

After the reboot, edit /etc/fstab to change the name of the swap device and uncomment it. Format the swap area with mkswap and activate it with swapon -a.

Finally enlarge the existing filesystem to occupy all of /dev/sda1 with resize2fs /dev/sda1.

If you want to create another partition

Use GParted to enlarge the extended partition (/dev/sda2) and create a new logical partition /dev/sda6 on it. Tell GParted that you'll want a Linux filesystem on /dev/sda6. Create a filesystem with mke2fs -t ext4 /dev/sda6.

Share:
45,993

Related videos on Youtube

Marcelo Finki
Author by

Marcelo Finki

David has worked in Software Development for over 20 years, from every phase of development, build, deployment, systems engineering, operations, and Site Reliability Engineering (DevOps). David is currently the Lead Site Reliability Engineer for the SRE Product team at Starbucks Technology where the focus is on developing applications and shared platforms to enable rapid development by engineering teams across Starbucks.

Updated on September 18, 2022

Comments

  • Marcelo Finki
    Marcelo Finki over 1 year

    I'm running Ubuntu Server 10.04 on a remote server as a VM. I only have access to the VM.

    The VM is assign a disk space of 20GB, however the drive is currently only partitioned to use about 4.7GB.

    I know that you can use LiveCD to repartition a drive using gparted. How would you go about doing that on a remote VM? (Since the partition is also the startup volume)

    # fdisk -l
    
    Disk /dev/sda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x00074164
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1         618     4959232   83  Linux
    Partition 1 does not end on cylinder boundary.
    /dev/sda2             618         653      280577    5  Extended
    Partition 2 does not end on cylinder boundary.
    /dev/sda5             618         653      280576   82  Linux swap / Solaris
    
  • Sergey
    Sergey about 12 years
    but but but... you can't resize a partition which is currenly mounted - that would have really disastrous consequences. That's the whole point - if booted from a partition you can't resize it.
  • Marcelo Finki
    Marcelo Finki about 12 years
    Although our IT department was able to solve it (they have the control panel and I believe unmounted the partition and resized it), your answer, although dangerous, is a good option if I had to do it myself. +1
  • h3.
    h3. about 12 years
    @Sergey You can enlarge a mounted partition, and that's in fact pretty safe. You can even shrink or move a mounted partition, because the kernel doesn't read the updated information; what would be disastrous would be to do anything on the partition afterwards, like create a filesystem on it.
  • Marcelo Finki
    Marcelo Finki about 12 years
    Although both answers are excellent. I choose this as the accepted solution since essentially #1 is what we ended up with. (IT fixed)
  • Sergey
    Sergey about 12 years
    I have serious doubts... Do you have any links to support this? Note that we're talking about the partition we just booted from, i.e. it already have a filesystem on it. How do you ensure that no processes use the partition while you're resizing it? That would only work in a single-task OS such as MS-DOS
  • h3.
    h3. about 12 years
    @Sergey It's vitally important that no process use the partition. But that's ok: in normal operation, no process uses the partition. It's just fine if a process continues to access an already-mounted filesystem, because the filesystem driver never updates its knowledge about the partition, it keeps using the old boundaries from mount time. Source: my experience, and browsing the kernel sources.
  • Admin
    Admin almost 2 years
    I've tried the last option but lost the access to the machine because I didn't configured ssh in that other partition