10

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

4 Answers4

16

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.

  • 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. – Sergey Mar 02 '12 at 00:57
  • 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 – Highway of Life Mar 02 '12 at 02:36
  • 2
    @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. – Gilles 'SO- stop being evil' Mar 02 '12 at 09:51
  • 3
    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 – Sergey Mar 02 '12 at 21:16
  • 2
    @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. – Gilles 'SO- stop being evil' Mar 02 '12 at 21:23
4

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.

Sergey
  • 43,665
  • Although both answers are excellent. I choose this as the accepted solution since essentially #1 is what we ended up with. (IT fixed) – Highway of Life Mar 02 '12 at 17:34
  • I've tried the last option but lost the access to the machine because I didn't configured ssh in that other partition – testing_22 Jun 13 '22 at 15:28
1

If you're running a cloud machine (in my case it runs a custom Kali image with just 16GB of the disk), and the only access to it is through ssh, then this answer is for you.

So without needing to remount the partition, following this answer can make it work. In the case you need to remove some intermediate partitions, do so before proceeding to the following step.

  1. Increase disk size from console first
  2. SSH inside VM : sudo growpart /dev/sda 1
  3. Resize your file system : sudo resize2fs /dev/sda1
  4. Verify : df -h
0

Excuse me! I just read your question, let me help you!

Size(VM usable memory) == Size(main partitions) + Size(extended partitions);

Size(extended partition) == Size(logical partition)s;

Size(VM disk memory) == Size(VM usable memory) + Size(free space);

So, if you want to use the free space on your VM's disk space, you need to expand the /dev/sda1, and there are no other partitions between the main partition /dev/sda1 and the free space.

Just use sudo fdisk -l to check your partition's usage, I guess your output just liking below:

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 62914560 sectors
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 Sectors Size Id Type /dev/sda1 * 512M 5.2G 4902880 4.7G 83 Linux /dev/sda2 5.2G 6.2G 580000 1G 5 Extended /dev/sda5 5.2G 6.2G 580000 1G 82 Linux swap / Solaris

And after your main partition or usable partition up to 20GB, your new sudo fdisk -l:

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 62914560 sectors
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 Sectors Size Id Type /dev/sda1 * 512M 20.5G 42162880 20G 83 Linux /dev/sda2 20.5G 21.5G 580000 1G 5 Extended /dev/sda5 20.5G 21.5G 580000 1G 82 Linux swap / Solaris

Solution:

0.Just log in your VM's terminal;

1.turn off your swap permission: sudo swapoff /dev/sda5;

And use command free -h to check whether swapoff do matter or not, if you see the free or total space percent of swap partition is 0B, that means successfully turn off the swap.

Others, reboot, or restart will automatically turn on the swap, using command sudo swapoff swap_partition_directory just temporarily turn off swap.

2.Remove extended partition /dev/sda2 and logical/swap partition /dev/sda5:

First remove logical/swap partition /dev/sda5, then extended partition /dev/sda2;

Just use cfdisk to do the upstairs job, using sudo cfdisk to enter the power of cfdisk.

Using up and down direction button to choose the partition, using right and left direction button to choose the operation, uisng enter button to make sure.

Choose Delete button to remove logical/swap partition /dev/sda5 and extended partition /dev/sda2.

3.Resize the main partition:

Also, using cfdisk, just use Resize button to do that, change resized size from default 21GB into 20GB;

4.Re-create extended partition /dev/sda2 and logical/swap partition /dev/sda5:

Also, using cfdisk, use New button to create extended partition /dev/sda2 on the free space, then create logical partition /dev/sda5, both of them use default size 1 GB;

Now, select Quit button to quit cfdisk.

5.Set your logical partition /dev/sda5's file type: sudo mkswap /dev/sda5;

Update your new logical/swap partition /dev/sda5's UUID, use lsblk -f | grep "swap" to check swap partition's UUID, copy that, and change the UUID of swap in /etc/fstab via sudo vim /etc/fstab, using i button to edit, copy your new swap UUID instead of the old UUID of the former swap.

6.Set all the file types of main partition /dev/sda1: sudo resize2fs /dev/sda1.

7.Restart or reboot your virtual machine: sudo reboot.

Have fun!!!:)

OVER!