Monday, January 27, 2020

AWS EC2 resize partition

While Amazon Web Services (AWS) provides allot of services and administrative capabilities thru their Console web application, you still have to do some things manually.

After creating a EC2 instance, you may find out that the Amazon Machine Image (AMI), ie install image, you used had a different OS/root partition size than the size you allocated in the AWS Console, which is where this tutorial comes into play.

If you are using an AMI which has the root partition as 8GB, and you have launched an EC2 instance with 16GB, you can resize the root partition.

Resize volume

Check the used partition size via lsblk, which lists information about all available or the specified block devices
> sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1     259:1 0 8G  0 disk
├─nvme0n1p1 259:2 0 1M  0 part
└─nvme0n1p2 259:3 0 8G  0 part /


First we will extend the partition, and then we will extend the file system

Install growpart to extend a partition in a partition table to fill the available space
> sudo yum install cloud-utils-growpart

Extend the partition, the first option is the volume ie nvme0n1, the second options is the partition number ie the 1 in p1
> sudo growpart /dev/nvme0n1 1

Verify
> sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1     259:1 0 16G 0 disk
├─nvme0n1p1 259:2 0 1M  0 part
└─nvme0n1p2 259:3 0 16G 0 part /

The 8GB partition now lists as 16GB
But the file system is still 8GB
> df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2   8G  1.9G   8G  24% /

Now extend the file system
> sudo xfs_growfs /

Verify
> df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2   16G  1.9G   16G  12% /

Your good to use your fully allocated partition now.


-End of Document-
Thanks for reading

Monday, January 13, 2020

AWS EC2 mount new volume

While Amazon Web Services (AWS) provides allot of services and administrative capabilities thru their Console web application, you still have to do some things manually.

After creating a EC2 instance, you may want to attach additional storage.  The additional storage can be used to host your application independent of the OS/root partition, allowing you to more easily migrate, backup, and manage your application and it's data.

After creating the additional storage and attaching the volume to your EC2 instance, you still need to tell the OS on the EC2 about the extra storage, which is where this tutorial comes into play.

Mount attached volume 

List partitions via lsblk, which lists information about all available or the specified block devices
> sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1     259:0 0 50G 0 disk         
<- no mount point
nvme0n1     259:1 0 16G 0 disk
├─nvme0n1p1 259:2 0 1M  0 part
└─nvme0n1p2 259:3 0 16G 0 part /

nvme0n1
Is the OS/root partition

nvme1n1   
Is the external attached volume

You can tell the partitions by the size, or the or by the index

Verify that there is no data on the partition
> sudo file -s /dev/nvme1n1

Response if no file system thus no data
/dev/nvme1n1: data

Response if the partition has already been formatted
/dev/nvme1n1: SGI XFS filesystem data

If no data, create a file system
> sudo mkfs -t xfs /dev/nvme1n1
 
Make the mount directory, which can be any name, but `data` is generic enough
> sudo mkdir /data

Mount the partition to the directory
> sudo mount /dev/nvme1n1 /data

Edit fstab to mount on boot
fstab defines your volumes and mount points at boot, so make a copy first
> sudo cp /etc/fstab /etc/fstab.orig

Find the UUID of device, which will be used in fstab to identity the volume
> sudo blkid

Edit fstab; Use your UUID; match existing entry spacing
the option nofail allows the boot sequence to continue even if the drive fails to mount
> sudo vi /etc/fstab
UUID=123ebf5a-8c9b-1234-1234-1234f6f6ff30 /data xfs defaults,nofail 0 2

To verify the fstab configuration works, without rebooting, unmount and then auto mount the volume
> sudo umount /data
> sudo mount -a

List partitions, and you will see your data directory, which you can now utilize
> sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1     259:0 0 50G 0 disk /data        
<- it worked
nvme0n1     259:1 0 16G 0 disk
├─nvme0n1p1 259:2 0 1M  0 part
└─nvme0n1p2 259:3 0 16G 0 part /

> ls -l /data


-End of Document-
Thanks for reading