Jay Taylor's notes
back to listing indexHow can I resize an ext root partition at runtime?
[web search]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: the size of sda1 is the total amount of sectors minus the sector size of your swap partition: 14335999-2093058=12242941
As fdisk mentions: the kernel is still using the old partition table so we need to reload it.
$partprobe
Now we need to run resize2fs on sda1 (do not forget this!)
$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 / Solaris
and make sure you write it (type yes to confirm)
Now we can re-activate the swap
$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.
mkswap
& swapon
commands 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 6 '18 at 17:04
Follow these steps.
- open terminal as superuser
su
- run
parted
- type
p
to see the available partitions - identify your root partition number (ex: 'sda 3' means number 3) and delete an adjacent partition by using
rm PARTITION NUMBER
to create free space. - now increase the root size by typing
resizepart ROOT NUMBER
and reboot system if needed - exit
parted
by typingexit
and in terminal typepartprobe
and hit enter (this can be done even after rebooting) - finally run
resize2fs /dev/sda PARTITION NUMBER
and enjoy spacious root partition.
sfdisk: /dev/nvme0n1p5: does not contain a recognized partition table
(using dual boot with windows)
– monkut
Jul 28 '18 at 23:26
gparted
for it.
– STRML
Jul 29 '18 at 15:42
apt-get install cloud-utils
– klor
Aug 21 '18 at 19:50
/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
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.
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 3 '13 at 18:07
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 8 '15 at 21:15
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.
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]#
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
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 3 '12 at 7:07
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
fdisk
when growpart
will do this very easily for you.
– STRML
Sep 16 '17 at 13:45
Your Answer
Not the answer you're looking for? Browse other questions tagged partitioning ext4 or ask your own question.
asked |
8 years, 2 months ago |
viewed |
744,957 times |
active |
Linked
Related
Hot Network Questions
-
Is a 5 watt UHF/VHF handheld considered QRP?
-
Married in secret, can marital status in passport be changed at a later date?
-
Multiple options vs single option UI
-
My bank got bought out, am I now going to have to start filing tax returns in a different state?
-
With indentation set to `0em`, when using a line break, there is still an indentation of a size of a space
-
How to open locks without disable device?
-
Dynamic Return Type
-
Where did Arya get these scars?
-
Identify story/novel: Tribe on colonized planet, not aware of this. "Taboo," altitude sickness, robot guardian (60s? Young Adult?)
-
What was Apollo 13's "Little Jolt" after MECO?
-
Is Electric Central Heating worth it if using Solar Panels?
-
AI positioning circles within an arc at equal distances and heights
-
What's parked in Mil Moscow helicopter plant?
-
Is this homebrew racial feat, Stonehide, balanced?
-
Why did Israel vote against lifting the American embargo on Cuba?
-
Map material from china not allowed to leave the country
-
Seek and ye shall find
-
How to not starve gigantic beasts
-
What is /etc/mtab in Linux?
-
As an international instructor, should I openly talk about my accent?
-
Has a Nobel Peace laureate ever been accused of war crimes?
-
Can you stand up from being prone using Skirmisher outside of your turn?
-
Could Neutrino technically as side-effect, incentivize centralization of the bitcoin network?
-
std::is_constructible on incomplete types
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.4.24.33422