Ubuntu expand disk space – Command Line

Warning: Be extremely careful when making changes to partitions and disk as it can lead to broken systems and lost data. Make sure you have a backup.

This scenario is done on a basic Ubuntu install. No fancy LVM stuff going on. If you need that, refer to here

Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 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: 0x2062ec28
Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 65011711 65009664   31G 83 Linux
/dev/sda2       65013758 67106815  2093058 1022M  5 Extended
/dev/sda5       65013760 67106815  2093056 1022M 82 Linux swap / Solaris

From the above output of fdisk -l, we see that the disk has 64GiB available, but the primary partition is only 31G. To make the primary partition larger we need to

  • Run fdisk “fdisk /dev/sda”
  • Delete partitions 2 and 5,
  • Delete Partition 1
  • Create Partition 1 again on the same starting boundary
  • Put the end boundary close to the end so we end up with ~62GiB for that partition
  • Recreate sda2, the 1GiB extended partition
  • Write changes to disk
  • Run resize2fs to resize the filesystem

You may need to boot up in recovery to get this command working. Also if you boot up in recovery, you’ll need to remount the root / partition read/write. More info here.

resize2fs /dev/sda1

Helpful Links
https://access.redhat.com/articles/1190213
https://access.redhat.com/articles/1196353

Create LUKS Encrypted Thumb Drive

Find the thumb drive with lsblk, dmesg, or sudo fdisk -l. In the following examples we are using /dev/sdc1, replace as needed.

sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sdc1
sudo cryptsetup luksOpen /dev/sdc1 encrypted_usb
sudo mkfs.ext4 /dev/mapper/encrypted_usb

Now we can mount the drive. We are mounting it to /mnt change if needed.

sudo mount /dev/mapper/encrypted_usb /mnt

Or go ahead and close the channel and remove the drive

sudo cryptsetup luksClose /dev/mapper/encrypted_usb

Command Explanation

sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sdc1

Wipe /dev/sdc1 and set the password when prompted for it.

sudo cryptsetup luksOpen /dev/sdc1 encrypted_usb

Open up a secure channel to the drive, and decrypt it so we can access it

sudo mkfs.ext4 /dev/mapper/encrypted_usb

Using the channel we created in the previous command, we can now format the drive.

sudo cryptsetup luksClose /dev/mapper/encrypted_usb

We can now close the channel for the drive and remove it.

Increase Disk Size of Linux VM in VMware

This is for extending a regular Ubuntu Linux partition, if you need to resize, expand a LVM partition refer to this guide.  I am using Gparted as I ran into some issues using parted for moving the partitions around.

Shut the VM down,

sudo shutdown -h now

It is a good idea to take a snapshot of the VM before resizing the disk, so if you run into an issue you have something to revert back to.  In the vSphere Client, right click on the VM -> Snapshot -> Take Snapshot.

Change VM Disk size by right clicking on the VM and going to Edit Settings

You can now boot up the VM.  Fire up GParted and it should show some unallocated space at the end of your drive.

Now in the next two images we are moving the Extended partition, which contains the Swap Partition to the end of the drive, so the unallocated space is adjacent to our root partition.

  1. Turn off the swap space by right clicking on the swap partition and hit Swapoff.
  2. Right click on the extended partition and extend to the the end of the Drive
  3. Right click on linux-swap and move to the end of drive.
  4. You should now have something similar to this

Hit Apply and write the changes to the disk then

  1. Right click on the extended partition and shrink to the end
  2. Right click on /dev/sda1 “Root partition” and extend to extended partition.

It should now look like this

Hit apply, then right click on the linux-swap and turn Swapon.

Enjoy the extra space.