0% found this document useful (0 votes)
12 views

How To Resize and Move Partitions in Linux

How to Resize and Move Partitions in Linux - Como cambiar tamaño y mover particiones en Linux

Uploaded by

multiversolinux
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

How To Resize and Move Partitions in Linux

How to Resize and Move Partitions in Linux - Como cambiar tamaño y mover particiones en Linux

Uploaded by

multiversolinux
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

(/linux/) (https://www.baeldung.com/linux/)

How to Resize and Move


Partitions in Linux

Last updated: August 19, 2022

Written by: Francesco Galgani


(https://www.baeldung.com/linux/author/francescogalgani)

Administration
(https://www.baeldung.com/linux/category/administration)
Filesystems
(https://www.baeldung.com/linux/category/filesystems)

https://www.baeldung.com/linux/resize-partitions 1/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

1. Overview
In this tutorial, we’ll consider some of the actively maintained open-
source partitioning tools
(https://en.wikipedia.org/wiki/List_of_disk_partitioning_software)
currently available on Linux. We’ll compare them using the same initial
and target partitioning (/linux/rsync-clone-file-system-hierarchy#disk-
partitions-and-partitioning-tools).

2. Precautions
Although not always strictly necessary, let’s proceed to resize partitions
from a live distribution
(https://en.wikipedia.org/wiki/Linux_distribution#Installation-
free_distributions_(live_CD/USB)), ensuring that all partitions are
unmounted (/linux/mount-unmount-filesystems#umount), and that
swap (/linux/swap-space) is disabled. Before starting the live system,
let’s shut down the machine without suspending or hibernating.
For extra safety, let’s check the integrity of all file systems with fsck
(/linux/recover-lost-deleted-data#filesystem-analysis-and-
recovery) and make sure that we have a full backup.
If we have a server, not all VPS providers allow us to start a live
distribution and use its GUI via VNC
(https://en.wikipedia.org/wiki/Virtual_Network_Computing). Choosing a
provider with these capabilities will significantly help us when we need
to resize partitions or perform backups for disaster recovery
(/linux/system-image-create-restore). In this regard, Clonezilla
(/linux/system-image-create-restore#clonezilla) is convenient in a
server environment because it can use remote target folders for our
backups via ssh (/linux/secure-shell-ssh#accessing-host).
Optionally, we can also look at the disks’ health (/linux/storage-device-
check-health).

https://www.baeldung.com/linux/resize-partitions 2/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

3. How We Test the Partitioning Tools


Our test configuration is an installation of Linux Mint 20.3 on a 20GB
VirtualBox disk. In this case, the partition table is MBR type
(/linux/partitioning-disks#1-mbr-and-gpt), and UEFI
(/linux/partitioning-disks#2-bios-and-uefi) is not used. During the
installation, we chose this partitioning:

(/wp-content/uploads/sites/2/2022/07/LinuxMint-Partitioning-During-
Installation.png)Our goal is to enlarge /dev/sda5 by about 3.7 GiB
(https://en.wikipedia.org/wiki/Gigabyte#Base_2_.28binary.29) (4.0 GB
(https://en.wikipedia.org/wiki/Gigabyte#Base_10_.28decimal.29)) and,
as a side effect, to move /dev/sda6 and /dev/sda7 to the right. We
maintain a certain tolerance in partition sizes, which means that slight
variations of a few hundred MB don’t matter:

(/wp-content/uploads/sites/2/2022/07/Target-Partitions.png)The
partitions’ UUIDs must remain the same for our Linux installation to
continue booting. Or, as a workaround in case of UUID changes, we can
manually update /etc/fstab (/linux/mount-unmount-filesystems#6-
the-etcfstab-file). If in doubt, we can see the list of all UUIDs with blkid
(https://linux.die.net/man/8/blkid).
Before trying the following partition tools, these are the preparatory
commands to be executed from a live distribution in our test machine to
disable swap and check file systems integrity:
https://www.baeldung.com/linux/resize-partitions 3/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

# swapoff -a # disables swapping on all known swap devices


# partitions="sda1 sda5 sda6" # we exclude sda2 (extended part)
and sda7 (swap part)
# for part in $partitions; do umount /dev/$part; fsck
/dev/$part; done
umount: /dev/sda1: not mounted.
fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
/dev/sda1: clean, 311/124672 files, 35593/124672 blocks
umount: /dev/sda5: not mounted.
fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
/dev/sda5: clean, 290856/625856 files, 1751000/2499840 blocks
umount: /dev/sda6: not mounted.
fsck from util-linux 2.34
e2fsck 1.45.5 (07-Jan-2020)
/dev/sda6: clean, 240/305216 files, 40749/1220352 blocks

4. GUI Tools
We strongly suggest the following GUI tools because they help us
avoid mistakes. In addition, they hide the complexity of resizing and
moving partitions.

4.1. GParted
GParted (https://gparted.org/) is one of the most popular and well-
known tools for resizing and moving partitions. It offers an intuitive and
convenient GUI. The important thing is the correct order of operations:
first, we need to enlarge the extended partition
then we move /dev/sda7 and /dev/sda6 to the right
finally we enlarge /dev/sda5
When we apply the requested changes, GParted performs them in a
fully automated manner:

https://www.baeldung.com/linux/resize-partitions 4/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

GParted example of usage


(/wp-content/uploads/sites/2/2022/07/GParted-example-of-
usage.gif)To get a clear idea of what actions are possible for each type
of file system, we can also consult the “File System Support” table. Its
legend distinguishes between online operations on mounted or
otherwise active partitions and offline on unmounted or inactive ones.
We note that for ext3/4, it is possible to enlarge a partition online, but
moving it is only possible offline:

(/wp-content/uploads/sites/2/2022/07/GParted-File-System-
Support.png)GParted can also show device and partitions information. It
allows us to check the integrity of a file system and format it. Moreover,
the menu that appears by right-clicking on a partition provides an option
to change the UUID.

4.2. KDE Partition Manager

https://www.baeldung.com/linux/resize-partitions 5/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

KDE Partition Manager (https://github.com/KDE/partitionmanager)


is as intuitive as GParted. In addition, it has a partition backup and
restoring feature. Let’s see it at work, doing the same operations we
performed with GParted:
KDE Partition Manager - Example of usage
(/wp-content/uploads/sites/2/2022/07/KDE-Partition-Manager-
Example-of-usage-1.gif)Similar to GParted, it can show a table indicating
the type of support for each file system. Let’s note that this support
slightly differs from that of GParted. For example, while GParted allows
us to grow and shrink FAT32 partitions, KDE Partition Manager does not:

(/wp-content/uploads/sites/2/2022/07/KDE-Partition-Manager-File-
System-Support.png)Finally, it has a shredding feature to overwrite a
partition with random data or zeros. In this regard, we suggest looking at
various solutions to remove confidential data (/linux/wipe-free-space)
from a disk’s free space.

https://www.baeldung.com/linux/resize-partitions 6/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

5. Command-Line Tools
GParted and KDE Partition Manager use libparted
(https://en.wikipedia.org/wiki/GNU_Parted) for some tasks but
implement much other stuff themselves. Moreover, they use other
command-line tools under the hood, listed in their previously seen “File
System Support” tables. That’s why there isn’t an easy-to-use and
comprehensive command-line equivalent of GParted or KDE Partition
Manager.
Several well-known tools, such as parted
(https://www.gnu.org/software/parted/), fdisk (/linux/extend-logical-
extended-partitions-fdisk), cfdisk (https://linux.die.net/man/8/cfdisk),
and sfdisk (/linux/find-all-storage-devices#sfdisk), aren’t intuitive or
straightforward. Moreover, these essential command-line tools often
need to be used together with other dangerous utilities like dd
(/linux/dd-command) or resize2fs
(https://linux.die.net/man/8/resize2fs).
So, resizing and moving partitions from the command line is difficult
and error-prone. Let’s be cautious and do it only if it’s strictly
necessary.
Let’s remember to disable swap with swapoff -a. Then, let’s see below
how to do step-by-step the same tasks done with the graphical tools.
We’ll show how to perform them without explaining each command’s
option, for which we refer to the manual pages. Also, since the output
is particularly verbose, we’ll report only the essential information.

5.1. Verify Partition Table Consistency and List


Partitions
Let’s check the initial state of the partitions. We’ll run sfdisk several times
with the -V option because it verifies the integrity of the partitions:

https://www.baeldung.com/linux/resize-partitions 7/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

# sfdisk -Vl /dev/sda


Disk /dev/sda: 19.71 GiB, 21142175744 bytes, 41293312 sectors
[...]
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 32765951 31764482 15.2G 5 Extended
/dev/sda5 1001472 21000191 19998720 9.5G 83 Linux
/dev/sda6 21002240 30765055 9762816 4.7G 83 Linux
/dev/sda7 30767104 32765951 1998848 976M 82 Linux swap
/ Solaris
Remaining 8533501 unallocated 512-byte sectors.

In particular, we must pay attention to the size of the partitions. We will


re-run the same command at the end for comparison.

5.2. List Partitions UUIDs


The UUIDs of the partitions must remain the same to keep the system
bootable:

# blkid /dev/sda*
/dev/sda: PTUUID="015d4fac" PTTYPE="dos"
/dev/sda1: UUID="8d24bfcf-e4e5-44a9-8d5b-5ffb69f94900"
TYPE="ext4" PARTUUID="015d4fac-01"
/dev/sda2: PTTYPE="dos" PARTUUID="015d4fac-02"
/dev/sda5: UUID="dae073ff-1825-4a92-94f0-2b00bbf3b101"
TYPE="ext4" PARTUUID="015d4fac-05"
/dev/sda6: UUID="37b42f4f-6aac-43a0-955d-3929a86388ec"
TYPE="ext4" PARTUUID="015d4fac-06"
/dev/sda7: UUID="463c1ec2-b3bf-4d7f-b173-12def5a0d8d3"
TYPE="swap" PARTUUID="015d4fac-07"

In the end, we will re-execute the same command for comparison.

5.3. Remove Swap Partition /dev/sda7


When we need to move a swap partition, the most reasonable action is
to delete it, then recreate it in the new location:

https://www.baeldung.com/linux/resize-partitions 8/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

# fdisk /dev/sda
[...]
Command (m for help): d
Partition number (1,2,5-7, default 7): 7

Partition 7 has been deleted.

Command (m for help): w


The partition table has been altered.
[...]
# sfdisk -V /dev/sda

The last command is for a partition integrity check. It’s a good practice to
do this check after every change in the partition table.

5.4. Grow Extended Partition /dev/sda2 to Fill Disk


growpart (https://manpages.org/growpart) is used to extend a partition
(usually the last) to fill the available space:

# growpart /dev/sda 2
CHANGED: partition=2 start=1001470 old: size=31764482
end=32765952 new: size=40291809 end=41293279
# sfdisk -V /dev/sda

In this case, we made sure to enlarge the extended partition by


occupying the free 4GiB. Free space at the end of the disk typically
occurs when we enlarge a virtual disk.

5.5. Move Partition /dev/sda6 to the Right by 4 GiB


It’s a good idea to check the file system integrity before and after the
move. Let’s pay attention to the sfdisk options because, in this case, they
serve to move a partition without changing its size:

https://www.baeldung.com/linux/resize-partitions 9/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

# e2fsck -f -y -v /dev/sda6
[...]
231 files
# echo '+4G,' | sfdisk --move-data /dev/sda -N 6
# e2fsck -f -y -v /dev/sda6
[...]
231 files
# sfdisk -Vl /dev/sda
[...]
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 41293278 40291809 19.2G 5 Extended
/dev/sda5 1001472 21000191 19998720 9.5G 83 Linux
/dev/sda6 29390848 39153663 9762816 4.7G 83 Linux

Compared to the beginning, we can see that sda2 is larger, while sda6
has the same size but a different starting point.

5.6. Grow Partition /dev/sda5 to Fill the New Free


Space (4 GiB)
We should pay attention to sfdisk‘s options, which in this case are
different from those previously used. This time we use sfdisk to enlarge
a partition without changing its starting point:

https://www.baeldung.com/linux/resize-partitions 10/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

# e2fsck -f -y -v /dev/sda5
[...]
290850 files
# echo ',+4G' | sfdisk /dev/sda -N 5
[...]
New situation:
[...]
/dev/sda5 1001472 29388799 28387328 13.5G 83 Linux
[...]
# e2fsck -f -y -v /dev/sda5
[...]
290850 files
# resize2fs /dev/sda5
[...]
Resizing the filesystem on /dev/sda5 to 3548416 (4k) blocks.
[...]
# e2fsck -f -y -v /dev/sda5
[...]
290850 files
# sfdisk -Vl /dev/sda
[...]
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 41293278 40291809 19.2G 5 Extended
/dev/sda5 1001472 29388799 28387328 13.5G 83 Linux
/dev/sda6 29390848 39153663 9762816 4.7G 83 Linux
Remaining 2143742 unallocated 512-byte sectors.

We performed various checks with e2fsck and sfdisk, as in previous


cases. The only new entry is resize2fs
(https://manpages.org/resize2fs/8), which serves to resize an ext2/3/4
file system to fit the new partition size.

5.7. Re-Create the Swap Partition


Now it’s time to recreate the swap partition we deleted at the beginning,
taking care to assign it the same UUID:

https://www.baeldung.com/linux/resize-partitions 11/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

# fdisk /dev/sda
[...]
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 7
First sector (39155712-41293278, default 39155712):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (39155712-
41293278, default 41293278):

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

Command (m for help): w


The partition table has been altered.
[...]
# mkswap --uuid "463c1ec2-b3bf-4d7f-b173-12def5a0d8d3"
/dev/sda7
Setting up swapspace version 1, size = 1 GiB (1094426624 bytes)
no label, UUID=463c1ec2-b3bf-4d7f-b173-12def5a0d8d3

We needed mkswap (/linux/swap-space#2-create-a-swap-partition) to


set up the Linux swap area in the just-created partition.

5.8. Verify Partition Table Consistency and List the


New Partitions
Let’s repeat the command used at the beginning and compare the new
partition size:

# sfdisk -Vl /dev/sda


[...]
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 41293278 40291809 19.2G 5 Extended
/dev/sda5 1001472 29388799 28387328 13.5G 83 Linux
/dev/sda6 29390848 39153663 9762816 4.7G 83 Linux
/dev/sda7 39155712 41293278 2137567 1G 83 Linux
Remaining 6174 unallocated 512-byte sectors.

Everything is exactly as we wanted.

https://www.baeldung.com/linux/resize-partitions 12/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

5.9. Check if Partitions’ UUIDs Are the Same


Let’s verify that the UUIDs have not changed:

# blkid /dev/sda*
/dev/sda: PTUUID="015d4fac" PTTYPE="dos"
/dev/sda1: UUID="8d24bfcf-e4e5-44a9-8d5b-5ffb69f94900"
TYPE="ext4" PARTUUID="015d4fac-01"
/dev/sda2: PTTYPE="dos" PARTUUID="015d4fac-02"
/dev/sda5: UUID="dae073ff-1825-4a92-94f0-2b00bbf3b101"
TYPE="ext4" PARTUUID="015d4fac-05"
/dev/sda6: UUID="37b42f4f-6aac-43a0-955d-3929a86388ec"
TYPE="ext4" PARTUUID="015d4fac-06"
/dev/sda7: UUID="463c1ec2-b3bf-4d7f-b173-12def5a0d8d3"
TYPE="swap" PARTUUID="015d4fac-07"

Finally, we reboot the system to verify that all the partitions and the
swap are working correctly.

6. Conclusion
In this article, we saw that the easiest way to resize and move partitions
is to use GParted or KDE Partition Manager from a live distribution.
However, GUI is not always available. We then explored how to perform
the same operations using only command-line tools.
In any case, the probability of making a small mistake that can make
our disk unusable is very high when we use the terminal. For this
reason, it’s always essential to have a complete backup to make possible
disaster recovery. For instance, just moving a comma in the sfdisk‘s
parameters is enough to get two completely different results.

Comments are closed on this article!

https://www.baeldung.com/linux/resize-partitions 13/14
6/9/23, 7:20 How to Resize and Move Partitions in Linux | Baeldung on Linux

CATEGORIES
ADMINISTRATION (/LINUX/CATEGORY/ADMINISTRATION)
FILES (/LINUX/CATEGORY/FILES)
FILESYSTEMS (/LINUX/CATEGORY/FILESYSTEMS)
INSTALLATION (/LINUX/CATEGORY/INSTALLATION)
NETWORKING (/LINUX/CATEGORY/NETWORKING)
PROCESSES (/LINUX/CATEGORY/PROCESSES)
SCRIPTING (/LINUX/CATEGORY/SCRIPTING)
SEARCH (/LINUX/CATEGORY/SEARCH)
SECURITY (/LINUX/CATEGORY/SECURITY)
WEB (/LINUX/CATEGORY/WEB)

SERIES
LINUX ADMINISTRATION (/LINUX/LINUX-ADMINISTRATION-SERIES)
LINUX FILES (/LINUX/LINUX-FILES-SERIES)
LINUX PROCESSES (/LINUX/LINUX-PROCESSES-GUIDE)

ABOUT
ABOUT BAELDUNG (/ABOUT)
THE FULL ARCHIVE (HTTPS://WWW.BAELDUNG.COM/LINUX/FULL_ARCHIVE)
EDITORS (HTTPS://WWW.BAELDUNG.COM/EDITORS)

TERMS OF SERVICE (HTTPS://WWW.BAELDUNG.COM/TERMS-OF-SERVICE)


PRIVACY POLICY (HTTPS://WWW.BAELDUNG.COM/PRIVACY-POLICY)
COMPANY INFO (HTTPS://WWW.BAELDUNG.COM/BAELDUNG-COMPANY-INFO)
CONTACT (/CONTACT)

The
Baeldu
Logo

https://www.baeldung.com/linux/resize-partitions 14/14

You might also like