playground/aws/how-to-make-multiple-parttions-on-root-device.md

216 lines
8.7 KiB
Markdown

Date: Aug 25, 2021
The volume created of Amazon Linux 2 AMI(HVM), SSD Volume Type - ami-0f511ead81ccde020(64-bit x86) contains two partitions in the GPT partition table. The first partition is the Bios Boot Partition that stores the second stage of the boot loader such as GNU grub. The second partition is the actual root partition.
```
parted /dev/xvda
GNU Parted 3.1
Using /dev/xvda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
128 1049kB 2097kB 1049kB BIOS Boot Partition bios_grub
1 2097kB 16.1GB 16.1GB xfs Linux
(parted)
```
Do the followings in the AWS console or with the AWS cli command.
- Create two EC2 instances with the same configuration. Namely IX and IY.
- Let's call the default volumes attached VX and VY respectively.
- Stop both instances.
- Create 1 extra volume off the instances. Namely, VZ, (10GB in this sample)
- Detach VY from IY and attach it to IX as /dev/sdf
- Attach VZ to IX as /dev/sdg
- Start IX and connect to it.
Do the followings as root inside IX.
- Check the available block devices with lsblk.
```
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 15G 0 disk
└─xvda1 202:1 0 15G 0 part /
xvdf 202:80 0 8G 0 disk
└─xvdf1 202:81 0 8G 0 part
xvdg 202:96 0 10G 0 disk
```
- Parition VZ (/dev/xvdg) according to your own needs. Ensure to create the BIOS Boot Partition.
```
# parted /dev/xvdg
GNU Parted 3.1
Using /dev/xvdg
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Error: /dev/xvdg: unrecognised disk label
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdg: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted) mklabel gpt
(parted) mkpart 'BIOS Boot Partition' 1MB 2MB
(parted) set 1 bios_grub on
(parted) mkpart root xfs 2MB 50%
(parted) mkpart data xfs 50% 95%
(parted) mkpart swap linux-swap 95% 100%
(parted) p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdg: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB BIOS Boot Partition bios_grub
2 2097kB 5369MB 5367MB root
3 5369MB 10.2GB 4832MB data
4 10.2GB 10.7GB 536MB swap
(parted) q
Information: You may need to update /etc/fstab.
```
- Check recognized block devices again.
```
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 15G 0 disk
└─xvda1 202:1 0 15G 0 part /
xvdf 202:80 0 8G 0 disk
└─xvdf1 202:81 0 8G 0 part
xvdg 202:96 0 10G 0 disk
├─xvdg1 202:97 0 1M 0 part
├─xvdg2 202:98 0 5G 0 part
├─xvdg3 202:99 0 4.5G 0 part
└─xvdg4 202:100 0 511M 0 part
```
- Create file systems on xvdf1 and xvdg2 in the xfs format.
```
# mkfs -t xfs -f /dev/xvdg2
meta-data=/dev/xvdg2 isize=512 agcount=4, agsize=327552 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=0
data = bsize=4096 blocks=1310208, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# mkfs -t xfs -f /dev/xvdg3
meta-data=/dev/xvdg3 isize=512 agcount=4, agsize=294912 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=0
data = bsize=4096 blocks=1179648, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
```
- Copy everything from xvdf1 to xvdg2.
```
# mount /dev/xvdf1 /mnt/vy
# mount /dev/xvdg2 /mnt/vz
# rsync -av /mnt/vy/ /mnt/vz/
# umount /mnt/vy
```
- Update /mnt/vz/etc/fstab to match the UUID of xvdg2.
```
# blkid | grep xvdg2
/dev/xvdg2: UUID="e47315e4-ff01-49b0-81f7-bac53f08a88b" TYPE="xfs" PARTLABEL="root" PARTUUID="de12b03c-53ff-4f53-b990-1d823f42a7aa"
[root@ip-172-31-18-121 ~]# cat /mnt/vz/etc/fstab
#
UUID=04b92f2f-4366-4687-868b-7c403cc59901 / xfs defaults,noatime 1 1
# sed -i 's|04b92f2f-4366-4687-868b-7c403cc59901|e47315e4-ff01-49b0-81f7-bac53f08a88b|g' /mnt/vz/etc/fstab
[root@ip-172-31-18-121 ~]# cat /mnt/vz/etc/fstab
#
UUID=e47315e4-ff01-49b0-81f7-bac53f08a88b / xfs defaults,noatime 1 1
```
- Update /mnt/vz/boot/grub2/grub.cfg
```
# mv /mnt/vz/boot/grub2/grub.cfg /mnt/vz/boot/grub2/grub.cfg.org
# sed 's|04b92f2f-4366-4687-868b-7c403cc59901|e47315e4-ff01-49b0-81f7-bac53f08a88b|g' /mnt/vz/boot/grub2/grub.cfg.org > /mnt/vz/boot/grub2/grub.cfg
```
While ```grub2-mkconfig -o /mnt/vz/boot/grub2/grub.cfg``` is a proper command, it inserts the menu entries using the current boot partition(VX). The sample above substituted the UUID of xvdg2 for the auto-produced UUIDs. If you want to skip updating fstab and grub.cfg, you may consider setting the UUID of xvdg2 to match that of xvdf1.
- Install the grub boot loader on /dev/xvdg and unmount /dev/xvdg2
```
# grub2-install --target=i386-pc --directory=/mnt/vz/usr/lib/grub/i386-pc --recheck --boot-directory=/mnt/vz/boot /dev/xvdg
Installing for i386-pc platform.
Installation finished. No error reported.
# umount /dev/xvdg2
```
Do the followings in the AWS console or with the AWS cli command.
- Stop the instance IX.
- Detach the volume VZ from IX.
- Attach VZ to IY as /dev/xvda.
- Start IY.
IY has the boot device volume with 2 normal partitions, 1 swap partition, and 1 BIOS Boot partition. The second normal partition labeled 'data' is left free for your own disposal.
```
# parted /dev/xvda
GNU Parted 3.1
Using /dev/xvda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB BIOS Boot Partition bios_grub
2 2097kB 5369MB 5367MB xfs root
3 5369MB 10.2GB 4832MB xfs data
4 10.2GB 10.7GB 536MB swap
(parted)
# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 492676 0 492676 0% /dev
tmpfs 503444 0 503444 0% /dev/shm
tmpfs 503444 500 502944 1% /run
tmpfs 503444 0 503444 0% /sys/fs/cgroup
/dev/xvda2 5230592 1565416 3665176 30% /
tmpfs 100692 0 100692 0% /run/user/1000
tmpfs 100692 0 100692 0% /run/user/0
# mount /dev/xvda3 /mnt
# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 492676 0 492676 0% /dev
tmpfs 503444 0 503444 0% /dev/shm
tmpfs 503444 500 502944 1% /run
tmpfs 503444 0 503444 0% /sys/fs/cgroup
/dev/xvda2 5230592 1565416 3665176 30% /
tmpfs 100692 0 100692 0% /run/user/1000
tmpfs 100692 0 100692 0% /run/user/0
/dev/xvda3 4183040 37200 4145840 1% /mnt
```
You may enable the swap in the swap partition. If you don't need it, you can change the partition type and format it to a normal file system. You are free to use /dev/xvda3 the way you like.
## Summary
- Copy everything from the standard volume to one of the partitions of the new volume
- Update the boot loader and fstab
## TODO
automate the entire process with the AWS CLI or other tools.