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

Customize Ec2 Partitions

The document outlines steps to create a new custom root volume from an existing AMI volume and attach it to an EC2 instance: 1. Attach the AMI root volume and a new volume to an auxiliary instance. Create a custom partition scheme on the new volume with partitions for /boot, /root, swap, /home, /usr, and /var. 2. Format the partitions, mount them, and copy over the contents from the AMI volume. Install GRUB on the new volume and update /etc/fstab with the new UUIDs. 3. Detach the volumes from the auxiliary instance and re-attach the new custom volume to the original EC2 instance launched from the AMI, making

Uploaded by

Nagendra Sahni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Customize Ec2 Partitions

The document outlines steps to create a new custom root volume from an existing AMI volume and attach it to an EC2 instance: 1. Attach the AMI root volume and a new volume to an auxiliary instance. Create a custom partition scheme on the new volume with partitions for /boot, /root, swap, /home, /usr, and /var. 2. Format the partitions, mount them, and copy over the contents from the AMI volume. Install GRUB on the new volume and update /etc/fstab with the new UUIDs. 3. Detach the volumes from the auxiliary instance and re-attach the new custom volume to the original EC2 instance launched from the AMI, making

Uploaded by

Nagendra Sahni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Create a new EBS volume

2. Attach amiroot and myroot volumes to the auxiliary instance

3. Verify the attached volumes are recognized by the auxiliary


instance
@ip-172-31-17-107:~$ lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

xvda 202:0 0 8G 0 disk

└─xvda1 202:1 0 8G 0 part /

xvdf 202:80 0 8G 0 disk

└─xvdf1 202:81 0 8G 0 part

xvdg 202:96 0 100G 0 disk

4. Create custom partition scheme on myroot volume


~# parted /dev/xvdg

GNU Parted 3.2

Using /dev/xvdg

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) mklabel gpt

(parted) mkpart bbp 1MB 2MB

(parted) set 1 bios_grub on

(parted) mkpart root xfs 2MB 20%

(parted) mkpart swap linux-swap 20% 25%

(parted) mkpart home xfs 25% 30%

(parted) mkpart usr xfs 30% 65%

(parted) mkpart var xfs 65% 100%

Once created, the partition table should look like this:


(parted) unit GiB

(parted) p

Model: Xen Virtual Block Device (xvd)

Disk /dev/xvdg: 6.00GiB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags:

Number Start End Size File system Name Flags

1 0.00GiB 0.00GiB 0.00GiB bbp bios_grub

2 0.00GiB 1.20GiB 1.20GiB root

3 1.20GiB 1.50GiB 0.30GiB linux-swap(v1) swap

4 1.50GiB 1.80GiB 0.30GiB ext4 home

5 1.80GiB 3.90GiB 2.10GiB ext4 usr

6 3.90GiB 6.00GiB 2.10GiB ext4 var

check if all partitions are optimally aligned according to the disk geometry


(parted) align-check optimal 1

1 aligned

(parted) align-check optimal 2

2 aligned

(parted) align-check optimal 3

3 aligned

(parted) align-check optimal 4

4 aligned

(parted) align-check optimal 5

5 aligned

(parted) align-check optimal 6

6 aligned

When leaving parted it is possible that you receive the following message:


(parted) quit

Information: You may need to update /etc/fstab.

~# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT


xvda 202:0 0 8G 0 disk

└─xvda1 202:1 0 8G 0 part /

xvdf 202:80 0 8G 0 disk

└─xvdf1 202:81 0 8G 0 part

xvdg 202:96 0 6G 0 disk

├─xvdg1 202:97 0 1M 0 part

├─xvdg2 202:98 0 1.2G 0 part

├─xvdg3 202:99 0 307M 0 part

├─xvdg4 202:100 0 307M 0 part

├─xvdg5 202:101 0 2.1G 0 part

└─xvdg6 202:102 0 2.1G 0 part

5. Format the new partitions


~# for I in 2 4 5 6; do mkfs.xfs /dev/xvdg${I}; done

...

...

~# mkswap /dev/xvdg3

Setting up swapspace version 1, size = 307 MiB (321908736 bytes)

no label, UUID=1aad1fa2-188e-4b7a-8a0f-33f775cdd329

6. Mount partitions
~# mkdir /mnt/amiroot

~# mkdir -p /mnt/myroot/root /mnt/myroot/home /mnt/myroot/usr /mnt/myroot/var

yum install tree -y

~# tree /mnt

/mnt

├── amiroot

└── myroot

├── home

├── root
├── usr

└── var

6 directories, 0 files

~# mount -t xfs -o nouuid /dev/xvdf1 /mnt/amiroot

~# mount /dev/xvdg2 /mnt/myroot/root

~# mount /dev/xvdg4 /mnt/myroot/home

~# mount /dev/xvdg5 /mnt/myroot/usr

~# mount /dev/xvdg6 /mnt/myroot/var

7. Synchronize amiroot contents into its corresponding partition

~# rsync -av /mnt/amiroot/home/ /mnt/myroot/home/

~# rsync -av /mnt/amiroot/usr/ /mnt/myroot/usr/

~# rsync -av /mnt/amiroot/var/ /mnt/myroot/var/

~# rsync -av --exclude=home --exclude=usr --exclude=var /mnt/amiroot/ /mnt/myroot/root/

~# mkdir /mnt/myroot/root/home;chmod 755 /mnt/myroot/root/home

~# mkdir /mnt/myroot/root/usr;chmod 755 /mnt/myroot/root/usr

~# mkdir /mnt/myroot/root/var;chmod 755 /mnt/myroot/root/var

 unmount the original amiroot volume single partition and detach it from the


auxiliary instance

~# sync && umount /mnt/amiroot

8. Install grub boot loader on new volume myroot

~# rm /mnt/myroot/root/boot/grub2/grub.cfg

~# grub2-mkconfig -o /mnt/myroot/root/boot/grub2/grub.cfg

grub2-mkconfig -o /mnt/myroot/root/etc/grub2.cfg
grub2-mkconfig –o /mnt/myroot/root/boot/efi/EFI/amzn/grub.cfg
grub2-mkconfig -o /mnt/myroot/root/etc/default/grub

Now you can install the grub boot loader on the new GB disk:

~# grub2-install --target=i386-pc --directory=/mnt/myroot/usr/lib/grub/i386-pc --recheck --boot-directory=/mnt/myroot/root/boot /dev/xvdg

grub2-install --target=i386-pc --directory=/mnt/myroot/usr/lib/grub/i386-pc --recheck --boot-


directory=/mnt/boot /dev/nvme1n1

9. Set new partitions in /etc/fstab file

First it is necessary to obtain partition UUID identifiers using the


blkid command. It is best to run as root because otherwise it
will not display swap related information.

~# blkid

/dev/xvda1: LABEL="cloudimg-rootfs" UUID="567ab888-a3b5-43d4-a92a-f594e8653924" TYPE="ext4" PARTUUID="1a7d4c6a-01"

/dev/xvdg2: UUID="286a4418-45b4-40c6-aece-6a4e1efd247a" TYPE="ext4" PARTLABEL="root" PARTUUID="56c0cfb6-20eb-4123-a766-420911980fb3"

/dev/xvdg4: UUID="6e244eed-310f-4aae-aaa1-7ef549bbdbd1" TYPE="ext4" PARTLABEL="home" PARTUUID="bdbacb79-532b-410f-853e-f5530c4f9fa7"

/dev/xvdg5: UUID="cceffcdd-1aeb-4aab-b8e0-7ac04893858c" TYPE="ext4" PARTLABEL="usr" PARTUUID="fbee2b01-9d41-4c16-bda6-a4b2abc02a04"

/dev/xvdg6: UUID="b3fe50cb-b147-4b3b-b8ce-17ee445db64c" TYPE="ext4" PARTLABEL="var" PARTUUID="b4983050-a4e6-4d8a-9284-1a269a451d35"

/dev/xvdg3: UUID="1aad1fa2-188e-4b7a-8a0f-33f775cdd329" TYPE="swap" PARTLABEL="swap" PARTUUID="24eecaf3-4942-4ead-bab4-909e359dd5ae"

/dev/xvdg1: PARTLABEL="bbp" PARTUUID="916ca11f-db90-48d1-93d2-de6a99b60f40"


Then move those identifiers to /mnt/myroot/root/etc/fstab file as follows:
UUID=286a4418-45b4-40c6-aece-6a4e1efd247a / ext4 defaults,discard,noatime,errors=remount-ro 01

UUID=6e244eed-310f-4aae-aaa1-7ef549bbdbd1 /home ext4 defaults,noatime,acl,user_xattr,nodev,nosuid 0 2

UUID=cceffcdd-1aeb-4aab-b8e0-7ac04893858c /usr ext4 defaults,noatime,nodev,errors=remount-ro 02

UUID=b3fe50cb-b147-4b3b-b8ce-17ee445db64c /var ext4 defaults,noatime,nodev,nosuid 02

UUID=1aad1fa2-188e-4b7a-8a0f-33f775cdd329 swap swap defaults 00

tmpfs /tmp tmpfs defaults,noatime,nodev,noexec,nosuid,size=256m 0 0

10. Attach the new volume as the root volume of the instance
launched in first place from the AMI

As part of the last step of this procedure, unmount all partitions


previously mounted and detach the myroot volume already
partitioned, configured and bootable from the auxiliary
instance. Then re-attach it to the final instance in which it will
be used, the AMI instance launched at first.

~# sync && umount /mnt/myroot/root /mnt/myroot/home /mnt/myroot/usr /mnt/myroot/var

[April 20, 2022, 11:46 AM] Kariuki, Peter: sudo dd if=/dev/xvda1 of=/dev/xvdf1
status=progress
[April 20, 2022, 12:04 PM] Kariuki, Peter: lsblk
[April 20, 2022, 12:05 PM] Kariuki, Peter: mount -o nouuid /dev/xvdf1 /mnt
[April 20, 2022, 12:06 PM] Kariuki, Peter: grub2-mkconfig -o /mnt/boot/grub2/grub.cfg
[April 20, 2022, 12:09 PM] Kariuki, Peter: grub2-install --target=i386-pc
--directory=/mnt/usr/lib/grub/i386-pc --recheck --boot-directory=/mnt/boot /dev/xvdf
[April 20, 2022, 1:24 PM] ‹Nagendra Sahni›: i-04609dd872588fff6
[April 20, 2022, 1:31 PM] Kariuki, Peter: A start job is running for dev-
mapp...2dlv_usr.device (36s / 1min 30s

You might also like