368

How can I increase the size of the root partition of a system at runtime?

I have a partition that is not allocated after the root partition (which is also ext4), how can I add that unallocated space to the space allocated to the root partition without having to shutdown the server?

Braiam
  • 67,791
  • 32
  • 179
  • 269
BonboBingo
  • 3,782
  • 4
    I hate to sound like a stick in the mud, but this entails a fair bit of risk? Why does this need to happen? Is uptime the main constraint? – Cloud Sep 30 '13 at 17:28
  • 1
    You can't resize a partition to the left, because that would actually be a move. – Zaz May 17 '15 at 15:16
  • 8
    Increasing the size of ext4 parititions online is easy. The difficult part would be shrinking (your headline is about "resizing"). For people interested by ANY manipulation on a root partition (move, shrink, change filesystem, device) at runtime should consult my answer: http://askubuntu.com/a/728141/21888 – vaab Feb 01 '16 at 02:28
  • 5

10 Answers10

316

GUI (Ubuntu 14.04 and later): GParted v0.17 and later provide a nice GUI for this. (Older versions will refuse to resize a mounted partition).

Command line (any Ubuntu version): There are three steps to this.

Step 1. The partition must first be resized. If you're using LVM, it's easy, and you presumably know how to proceed. If you're using classic partitions, it's a bit more complicated, and may require a reboot (though you never have to boot another system or live CD).

This is how I do it: Use fdisk to first delete the partition (the idea is that the data on disk will be preserved), then carefully recreate it with a larger size at the same position.

Example:

$ sudo fdisk /dev/sda

Command (m for help): p

Device Boot Start End Blocks Id System /dev/sda1 * 2048 9437183 4717568 83 Linux

Command (m for help): d Selected partition 1

Command (m for help): p

Device Boot Start End Blocks Id System

Command (m for help): n Command action e extended p primary partition (1-4) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-10485759, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): Using default value 10485759

Created a new partition 1 of type 'Linux' and of size 10 GiB. Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Device Boot Start End Blocks Id System /dev/sda1 2048 10485759 5241856 83 Linux

Command (m for help): w The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.

Again, it is critical that the new partition starts at the same block as the old. The Id should also match (83 for Linux systems). Be prepared to lose all your data at the slightest typo.

To be on the safe side, you may also restore the boot flag (which according to Wikipedia is still required on some computers) by pressing a.

See the comment section for what to do if your swap partition is in the way.

By now it should be apparent why people recommend using a live CD. ;-)

Step 2. As fdisk helpfully reminds you, you must reload the partition table before proceeding. The safest way is to simply reboot; but you can also use partprobe or kpartx (more information).

Step 3. Once the partition is resized and the partition table reloaded, it's a simple matter of running resize2fs on the file system, and you can do this even when it's mounted as the root partition.

Example:

$ sudo resize2fs /dev/sda1
MewX
  • 105
  • 3
  • 21
    This worked perfectly for me. However I did additionally ensure that the boot flag kept is original state. – Augustus Kling Aug 10 '12 at 20:56
  • Very useful article on the same topic: http://underscore.at/underscore/newsshow.xml?id=69 cfdisk has helped me a lot. – Maxime Pacary May 27 '13 at 14:53
  • The question asked how to do this without shutting down. You will need a program that can update the kernel entries for a partition table of a disk w/ mounted partition(s). I've read that running partprobe [<device>] should work, but I don't currently have a system where I can test it. – jbo5112 Dec 02 '13 at 18:22
  • 4
    @jbo5112: As fdisk says, partprobe or kpartx may work instead of a reboot; see also this question. Even if you reboot, the solution is still preferable to using a live CD when it comes to downtime, where a simple reboot can be less than 10 s for a virtual machine. It's also faster in operator time, which is why I usually use this approach myself. :) – Søren Løvborg Dec 03 '13 at 18:07
  • What to do if I have a /dev/sda2 (swap) that prevents me from using a higher sector/cylinder value? – Raymond Jul 07 '15 at 16:59
  • 1
    @Raymond: If memory pressure allows (see free -h), disable the swap (swapoff /dev/sda2), change the partition table (including deleting and recreating the swap partition) and either 1) reboot or 2) reload the partition table and swapon again. (If memory's too tight to disable swap temporarily, you can still create and enable a new swap partition (/dev/sda3), then swapoff sda2; but then you'll have to update /etc/fstab with the new swap device name.) – Søren Løvborg Jul 08 '15 at 21:15
  • 0.18 does not allow it... – inf3rno Aug 26 '15 at 14:20
  • @inf3rno: Maybe your version wasn't compiled with the feature enabled, but the changelog says 0.17. Anyway, I can confirm that Ubuntu 15.04 (gparted 0.19 + libparted 3.2) works. YMMV. – Søren Løvborg Aug 26 '15 at 15:30
  • @SørenLøvborg I don't know, I got it from synaptic. I solved the problem with a pendrive. Does not matter. – inf3rno Aug 26 '15 at 15:35
  • 1
    If you are using vmware and have extended the size of the disk, you will have to run

    sudo lshw -C disk

    to rescan the filesystems so the vm recognises the bigger drive. Then follow the instructions above.

    – Guy Apr 26 '16 at 08:23
  • Damn, I've just crashed my Vmware box. Oh well... – Jovan Perovic Apr 18 '17 at 23:25
  • "Use fdisk to first delete the partition (the idea is that the data on disk will be preserved), then carefully recreate it with a larger size at the same position" ---> "By now it should be apparent why people recommend using a live CD. ;-)" Very much so... I salute the brave folks who did this with their production machines! – Milind R Jun 25 '17 at 07:57
  • God that sounds scary. – Daniel F Sep 21 '17 at 16:12
  • This process worked for me (I was resizing the default filesystem on a debian install on an SD card). The output from fdisk was different for me, however, and after creating a new partition it prompted me about removing an existing ext4 signature. I said "yes". Otherwise, everything worked for me. I should also note some of the text was in "red", which was a bit frightening when I first saw it, but I believe expected. – jdg Jan 20 '18 at 19:43
  • Would a snapshot be capable of restoring the lost data..? – Mitchell van Zuylen May 12 '18 at 09:26
  • 4
    What about shrinking? – Aaron Franke Jul 28 '18 at 07:09
  • Gparted 0.30.0 still does not allow resizing a mounted partition. – DYZ Aug 19 '18 at 02:03
  • For anyone who needs to resize their root partition with other partitions on the same disk (and having another disk available, in this example mounted under /bak): cp -a /mountpoint /bak/mountpointBak before running the fdisk command. After everything is done, cp -a /bak/mountpointBak/. /mounpoint

    This might be obvious to many, but as a windows user, it took some time/googling to figure out.

    – CWagner Feb 15 '19 at 23:07
  • I have GParted 0.25 on Linux Mint 18.3 (Ubuntu 16.04) and I can't resize the partitions. Is there any guide for that? – Royi Apr 17 '19 at 10:20
  • If you encounter a case where the Start sector is smaller than 2048 (I had 62), fdisk will not let you specify that as the start location. Refer to this answer for a solution that worked for me: https://superuser.com/a/1328109/105142. tl;dr is to create the partition at the default 2048, then go to Expert mode (x), and Move the partition (b) to your desired starting sector. Then just write (w). – Martin Melka Aug 21 '19 at 10:14
  • partprobe doesn't seem to be necessary on recent kernels. – Robin Green Apr 19 '20 at 06:48
  • be careful about previous sector size. also if you delete a partition and create new on and repeat it again, it will be failed! – S.M.Mousavi May 10 '20 at 10:28
  • As the highest-voted answer, you should make it clear with a bold font that this guide is about growing partitions, not resizing in general. I followed it until the step fdisk asks, after creating a shrunk partition, whether I want to erase the existing ext4 signature. This made me to back up. You should warn people more. – GolDDranks Feb 09 '21 at 17:49
  • 1
    With Ubuntu 20, after re-creating the partition, you will get a warning Partition #1 contains a ext4 signature. This is expected - you are creating a partition over existing data, after all, so it makes sense there would be the signature of the filesystem there. You do not want to delete it. Answer No to the question to remove it. After this simply write the changes to the partition table and run resize2fs. The ext4 signature you did not delete will get reused without problems. I have just now done this and everything works. As always, have backups. – Martin Melka Nov 04 '21 at 17:22
  • What a horrifically manual and dangerous process! growpart should be the highest-rated answer – Clonkex Nov 03 '22 at 23:58
  • Works with the Ubuntu 22.04 Hypervisor image created on my Windows 10 machine. This solution helped me in resizing my rootfs /dev/sda1 from 13 to 80gb. – Kwuite Feb 01 '24 at 00:52
192

An easier solution - use growpart <device> <partition>:

growpart /dev/xvda 1  # Grows the partition; note the space
resize2fs /dev/xvda1  # Grows the filesystem

As always, back up your partition table (sfdisk -d /dev/xvda > partition_bak.dmp) just in case.

growpart is part of cloud-utils. In case you don't have it, you can install with:

sudo apt-get install cloud-utils 
Artur Meinild
  • 26,018
STRML
  • 2,023
  • 8
    What about shrinking? – Aaron Franke Jul 28 '18 at 07:09
  • 1
    Was hoping to use this method, but got sfdisk: /dev/nvme0n1p5: does not contain a recognized partition table (using dual boot with windows) – monkut Jul 28 '18 at 23:26
  • Shrinking is not possible to do on-line. I recommend using gparted for it. – STRML Jul 29 '18 at 15:42
  • 3
    This was perfect for resizing the root partition and filesystem of my AWS VM. Cheers. – MetaFight Aug 10 '18 at 00:00
  • 17
    growpart is part of cloud-utils. In case you don't have installed, you can install with apt-get install cloud-utils – klor Aug 21 '18 at 19:50
  • 2
    @klor : from cloud-guest-utils package – Pierre-Damien Sep 27 '18 at 11:39
  • 1
    @monkut Late to the party, but your disk is likely /dev/nvme0n1. p5 is partition 5 on this disk. Compare with traditional BSD disk slices, they use a similar numbering scheme. – Per Lundberg Nov 13 '18 at 19:59
  • Just tried it on Ubuntu 18.04 running on Hyper-V and worked great (after extending the virtual disk on the kyper-v disk manager tool) – SamGamgee Jan 23 '19 at 13:27
  • 2
    This gives an error of resize2fs: Device or resource busy while trying to open /dev/sda – Chris Stryczynski Apr 08 '19 at 19:33
  • The manpage implies that growpart is designed for cloud hosts. Is this appropriate to use on a non-virtualized host? – Nick ODell Mar 27 '20 at 17:17
  • 3
    @NickODell - I would say that the main reason for the "designed for the cloud" part is that it would be... atypical to see HDDs changing size when using physical hosts and physical HDDs (with no abstraction layer on top, like LVM). But I see no reason why it would not work on a non-virtualized machine. – Bogd Jan 19 '21 at 09:26
  • 2
    This is awesome! – Annahri Jan 23 '21 at 22:37
  • 2
    growpart is great, safe and quick, worked for me! – Jay Mar 16 '21 at 06:20
  • If you deal with ZFS in a pool resize2fs won't work. You can allocate new space to the pool using zpool online -e rpool /dev/sda3 and it will affect all devices in the pool. – Martin Braun Aug 01 '22 at 21:36
  • growpart is really cool! I copied a virtual machine to a hard drive, so I have a 120G hard drive, and a 5G partition.

    However, I wouldn't mind growing it slowly, and theoretically using the rest of the space for a physical volume later. I wish I could just double the size of the partition instead of blindly growing the whole thing, and I'm less comfortable about deleting a partition and recreating it while the system is running.

    I guess I don't have any good options.

    – isaaclw Mar 01 '23 at 22:08
  • Easier and safer. – MariusPontmercy Mar 15 '23 at 13:31
  • Will growpart automatically handle the situation where the swap partition is in the way? – daveyost Jun 16 '23 at 19:01
137

It is possible to do a on-line resize of a ext4 filesystem, even if it's your root partition. Use the resize2fs command.

sudo resize2fs /dev/sda1

EDIT: On-line shrinking is not allowed:

root@brunojcm-htpc:/home# resize2fs /dev/sda5 2654693
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda5 is mounted on /; on-line resizing required
resize2fs: On-line shrinking not supported
CodeAddict
  • 1,507
  • 62
    From man resize2fs: The resize2fs program does not manipulate the size of partitions. If you wish to enlarge a filesystem, you must make sure you can expand the size of the underlying partition first. This can be done using fdisk(8) by deleting the partition and recreating it with a larger size or using lvextend(8),if you're using the logical volume manager lvm(8). This question is about resizing the partition, not the filesystem. The distinction is subtle but very important. – Eliah Kagan Jun 03 '12 at 07:07
  • 9
    You can use fdisk to delete the root partion and then recreate it at the same starting block. fdisk will write out the change, but it won't take effect till after a reboot. after the reboot you can use the resize2fs program to send the disk to fill the partion. – James Becwar Jun 14 '12 at 15:15
  • 3
    I have just resized an ext4 root partition online. Therefore I can confirm it's possible. But instead of passing /dev/sda* as parameter to resize2fs, you need to pass the logical volume name. – CDR Dec 04 '12 at 22:37
  • 10
    I find the first paragraph of the resize2fs manpage most interesting for the initial question: The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing. (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and ext4.). – mo' Dec 23 '12 at 14:33
  • ok, works, but anyone knows how reliable is this? – Nick Jan 27 '16 at 18:00
  • You can bypass resize2fs inability to shrink mounted partition. This consist of using pivot_root. More on my answer: http://askubuntu.com/a/728141/21888 – vaab Feb 01 '16 at 02:16
  • 11
    Please don't muck with fdisk when growpart will do this very easily for you. – STRML Sep 16 '17 at 13:45
  • This question is about resizing a partition, not a filesystem. For me growpart is the best solution. – guettli Aug 13 '19 at 11:05
17

Yes, you can shrink/move/grow an online root partition without any reboots (nor livecd, nor usbkey): consult this answer. It's very well written and easy to follow, although quite long and a little risky. So if you only want to grow your ext4 partition, you can stick to the conventional working resize2fs solutions.

The general solution I've lnked will work on any type of dedicated or VPS solution for instance.

TLDR; this solution implies to pivot_root to tmpfs so you can umount safely your root partition live and fiddle with it. Once done, you'll pivot_root back on your new root partition.

This allows pretty much any manipulation on the root file system (move it, change filesystem, changing it's physical device...).

No reboot are required in the process, and this allows to bypass limitation of resize2fs not being able to shrink ext4 partitions.

I have personally used this, and it works very well on debian system also, so it should work on Ubuntu. I'm very surprised not to see this in-depth solution a little more linked to the many question in stackexchange web sites that deals with the same issue.

Note: Of course if you want to grow your partition, a simple resize2fs will be enough as stated in numerous places and in other answers here.

vaab
  • 1,084
  • 6
    I think to most people, once you've stopped all programs and services accessing the root partition, you might as well have rebooted the machine. For shrinking/moving, that's may still be faster that using a live CD, but for growing (by far the most common task, and what OP asked about), there are ways that don't involve a temporary shutdown of most of the system. – Søren Løvborg Mar 08 '16 at 18:35
  • 3
    @SørenLøvborg: You can restart the core services that needs continuous production while doing the full procedure. There are many configurations where you can't put LiveCD (VPS instances, dedicated servers... ) or circumstances where you want to avoid any reboots for specific reasons. The original question's title mentions "resizing", which attracts people looking for shrinking partitions online. No other solution allows shrinking ext4 online.This solution is risky, complex, but the most powerful of all and it fills the shortcomings of the others. – vaab Mar 09 '16 at 05:41
  • 1
    Please do not post answers depending on external links. Put the relevant part into your answer or post the link as a comment to the question. See [answer] for details. – Melebius Oct 12 '18 at 11:44
13

You could also just make use of GParted - as long as the partition you are resizing is not the one you booted from - else I suggest the live CD option is somewhat easier for newbies.

GParted basically does all of the steps - just based on a GUI fronted.

  • I booted my system with Ubuntu 12.04 Live CD and i resized ext4 partition with GParted. Worked good for me. Anyway, before this operation I backed up all my important data. – StandDuPp May 28 '13 at 20:16
  • i think, gparted needs partition unmounted. but i can be wrong. – Nick Jan 27 '16 at 18:02
  • The question is obviously about the partition they booted from, and booting a live CD requires restarting the machine. -1 – wjandrea May 21 '17 at 02:28
11

I would like to make an extension on the answer of @Søren Løvborg: extending the partition with a swap partition present.

First the layout of the disk after extending it:

$sudo parted /dev/sda 'unit s print' free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 14336000s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags 63s 2047s 1985s Free Space 1 2048s 10485759s 10483712s primary ext4 boot 10485760s 10487805s 2046s Free Space 2 10487806s 12580863s 2093058s extended 5 10487808s 12580863s 2093056s logical linux-swap(v1) 12580864s 14335999s 1755136s Free Space

So sda1 needs to be extended with the free space at the end of the disk, but the swap partition is in between them. This is how you can do it:

First we need to disable swap. Check how much it is used and if you can turn it off.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           992M         52M        464M        3.2M        475M        784M
Swap:          1.0G          0B        1.0G

swap is unused here so we can turn it off

$sudo swapoff /dev/sda5

Now we will change the partition table:

$sudo fdisk /dev/sda

(note: if you happen to have the first partition start at sector 63 instead of 2048, you need to add the option -c=dos)

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p Disk /dev/sda: 6.9 GiB, 7340032000 bytes, 14336000 sectors 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: 0x9e11c6df

Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 10485759 10483712 5G 83 Linux /dev/sda2 10487806 12580863 2093058 1022M 5 Extended /dev/sda5 10487808 12580863 2093056 1022M 82 Linux swap / Solaris

Command (m for help): d Partition number (1,2,5, default 5): 2

Partition 2 has been deleted.

Command (m for help): d Selected partition 1 Partition 1 has been deleted.

Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-14335999, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-14335999, default 14335999): 12242941

Created a new partition 1 of type 'Linux' and of size 5.9 GiB.

Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): 2 First sector (12242942-14335999, default 12242944): Last sector, +sectors or +size{K,M,G,T,P} (12242944-14335999, default 14335999):

Created a new partition 2 of type 'Linux' and of size 1022 MiB.

Command (m for help): a Partition number (1,2, default 2): 1

The bootable flag on partition 1 is enabled now.

Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

Note 1: the size of sda1 is the total amount of sectors minus the sector size of your swap partition: 14335999-2093058=12242941

Note 2: the command a is used to set the boot flag to a partition. Check the output of the first parted command. Usually this is the first partition.

As fdisk mentions: the kernel is still using the old partition table so we need to reload it.

$ sudo partprobe

Now we need to run resize2fs on sda1 (do not forget this!)

$ sudo resize2fs /dev/sda1
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 10
The filesystem on /dev/sda1 is now 38833617 (4k) blocks long.

Now, things are not over yet. As you've probably noticed sda2 is partitioned as type Linux (Ext4). For some reason, there is no way in fdisk to choose the type. So we have to alternate it using cfdisk

$ sudo cfdisk

Choose sda2 and change type to 82 Linux swap / Solarisand make sure you write it (type yes to confirm)

Now we can re-activate the swap

$ sudo mkswap /dev/sda2
/dev/sda2
UUID=d58bf1cb-d27a-487d-b337-056767fd5ad6 none swap sw 0 0

And finally turn it on:

$ swapon /dev/sda2

The only thing we need to do is to update fstab to mount the swap partition automatically upon booting

$ sudo nano /etc/fstab

And change the UUID of the swap partition to the output above:

# swap was on /dev/sda5 during installation
UUID=d58bf1cb-d27a-487d-b337-056767fd5ad6 none            swap    sw              0       0

Now all is well and you can reboot without problems.

wouter205
  • 1,161
  • as for alternating the swap partition type, it can be done in fdisk buy selecting t then 5 (partition 5) then 82 (type Linux swap / Solaris) – Oz Edri Nov 06 '18 at 14:16
  • shouldn't partition 2 be extended? (and then in fdisk we should choose e instead of p) Also, the shouldn't the mkswap and swapon commands should be run on /dev/sda5 instead of sda2? – Oz Edri Nov 06 '18 at 14:18
  • 1
    For the swap partition type: I found in other instructions that you can indeed select the type in fdisk but for some reason it didn't work in my version. Anyway, it will always work with cfdisk – wouter205 Nov 06 '18 at 17:00
  • 1
    For the swap partition: you can use an extended partition but then you need to add two partitions: an extended and a logical one (see my initial partition layout). In my example, I end up with 2 primary partitions: sda1 - ext4 - root partition & sda2 - swap partition. So the mkswap & swaponcommands need to be run on sda2. I did make a mistake in the beginning of my post: swapoff needs to be executed on sda5. Comments are always welcome, it was a difficult on to note down. – wouter205 Nov 06 '18 at 17:04
  • 1
    When swap partition is encrypted one has to first stop cryptdisks with cryptdisks_stop, then update /etc/crypttab instead of the /etc/fstab and restart cryptdisks with cryptdisks_start. – Dima Chubarov May 26 '19 at 09:06
  • 1
    Great instruction! Instead of fdisk, I'd like to recommend cfdisk with a pseudo-graphical interface. Just type cfdisk /dev/sda insted of fdisk /dev/sda. – Vladislav Mar 06 '23 at 14:29
  • Does this solution work with a mounted boot partition (/dev/sda1). When I do the sudo fdisk /dev/sda step, fdisk reports fdisk: cannot open /dev/sda: Input/output error and exits with exit status 1. – Abdull Jul 31 '23 at 11:28
  • It should work normally, as the root partition is also mounted in my example. Did you check your disk/boot partition with fsck as your output does mention input/output errors – wouter205 Aug 08 '23 at 07:33
5

Just finished resizing an ext4 root partition on a live system while the root was mount.

[root@habib i686]# resize2fs /dev/vg_habib/lv_root
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/vg_habib/lv_root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 10
Performing an on-line resize of /dev/vg_habib/lv_root to 38427648 (4k) blocks.
The filesystem on /dev/vg_habib/lv_root is now 38427648 blocks long.

[root@habib i686]# 
CDR
  • 159
2

I just did this successfully without umount, pivot_root, or temporary removal of the main partition, using parted 3.2 on Ubuntu 16.04, 4.4.0 kernel. To be cautious I did everything from a virtual console with networking disabled, and took a snapshot beforehand just in case, but the snapshot wasn't needed, so I could have just as well done this via SSH and without changing runlevels.

Determine partition size: parted /dev/sda1 print | egrep "Disk.*GB"

Optionally switch to multi-user mode without networking (must be done from a console, not SSH):

runlevel     # remember the original runlevel
init 2

Optionally take a VM snapshot to be cautious.

Resize partition:

parted
p
resizepart NUMBER SIZE
# answer "Yes" when asked about resizing a live partition.
q

Resize filesystem: resize2fs /dev/sda1

If anything goes wrong, you could restore your snapshot here. If all went fine, return to normal runlevel (obtained above) - normally 5: init 5. It may be better to do a full reboot at this point to make sure everything comes back properly (I had a date/ntp issue afterward).

Roger Dueck
  • 237
  • 2
  • 6
1

Follow these steps.

  1. open terminal as superuser su
  2. run parted
  3. type p to see the available partitions
  4. identify your root partition number (ex: 'sda 3' means number 3) and delete an adjacent partition by using rm PARTITION NUMBERto create free space.
  5. now increase the root size by typing resizepart ROOT NUMBER and reboot system if needed
  6. exit parted by typing exit and in terminal type partprobe and hit enter (this can be done even after rebooting)
  7. finally run resize2fs /dev/sda PARTITION NUMBER and enjoy spacious root partition.
-1

As stated before:

  • expanding live from a root system is possible.(no difficulties, as the boot section ain't to be moved)

  • shrinking a live root partition needs to be done from external boot device (boot from live system cd/usb-stick), as if there’s any fault, mismatch ..whatever..your system hangs, needs to be rebooted and will eventually not be able to boot correctly.

Any sort of "but I did it and it works" is pure luck.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
ascervera
  • 29
  • 1