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 / Solaris
and 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.