8

Background: I was given SSH access to a Ubuntu 20.04 VPS that has root partition of 5G (sda total 25G). VPS provider says Linux admin can resize the /dev/sda1 partition.

$ sudo fdisk -l && df -h
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe1f10bb0

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 10483711 10481664   5G 83 Linux
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           394M  788K  393M   1% /run
/dev/sda1       4.9G  3.0G  1.7G  64% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           394M     0  394M   0% /run/user/1002

I need to resize /dev/sda1 partition mounted to / over SSH. As far as I know, it is not possible.

Is it impossible, is it possible or has there been some miscommunications with the VPS provider? I am in contact with them through a third party.

Dave M
  • 4,514
  • 22
  • 31
  • 30
Tanel Tammik
  • 191
  • 1
  • 4

2 Answers2

14

You can use the growpart utility to resize the partition to fill the available space. growpart expects the disk device and partition number as separate arguments. So you can resize the partition /dev/sda1 by: growpart /dev/sda 1. Note that the space is required. After that is done, you can resize the filesystem.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • thanx! does `growpart` come with the `cloud-guest-utils` package? this ubuntu does not have it. `sudo apt install cloud-guest-utils` says unable to locate. – Tanel Tammik Jan 31 '21 at 17:10
  • 1
    Yes, that is the correct package. You should check to ensure that you are using official Ubuntu repositories. Some VPS providers give VPSes that do not use official repositories. – Michael Hampton Jan 31 '21 at 17:16
12

I do this all the time on VMs, using parted and resize2fs:

$ sudo parted /dev/sda
(parted) resizepart 1 100%
(parted) quit
$ sudo partprobe /dev/xvda
$ sudo resize2fs -p /dev/xvda1

Obviously you need free space on the sda disk after the first partition.

partprobe (comes with parted) is necessary to get kernel to reread partition table.

resize2fs to resize an ext* filesystem can normally be done online, but it can only grow filesystems. Other Linux filesystems will have other resizing commands; most can only grow online, not shrink.

Use print command in parted before and after resizepart if unsure.

grifferz
  • 948
  • 5
  • 13
  • 2
    Actually, online partition shrinking _is_ possible on Linux if you are using BTRFS, and it’s rock solid reliable too as long as you do not write more data to the volume than the intended size of the volume while the shrink is happening. Internally it actually uses the same logic that lets it do online defragmentation of open files. – Austin Hemmelgarn Feb 01 '21 at 12:56
  • Unfortunately all my experiences with Btrfs are either tragedies or nightmares. It's probably not worth the need to shrink an online filesystem. The only FS that supports online shrinking that is not a nightmare for me is NTFS, but that's Windows-only apparently. – iBug Feb 01 '21 at 19:02