Linux Basic Partitioning

From WikiMLT

In this guide we are as­sum­ing that, we want to pre­pare the de­vice /​​​dev/​​​nvme0n1 for Lin­ux UE­FI in­stal­la­tion with LVM and GPT par­ti­tion ta­ble. So the par­ti­tions that will be cre­at­ed are as fol­low:

  • /​​​dev/​​​nvme0n1p1 – EFI Sys­tem (for: /​​​boot/​​​efi),
  • /​​​dev/​​​nvme0n1p2 – Lin­ux filesys­tem (for: /​​​boot),
  • /​​​dev/​​​nvme0n1p3 – Lin­ux LVM (for: /, /​​​home, etc.).

We can use fdisk, part­ed or gdisk with­in the com­mand line, or we can use gpart­ed and oth­er sim­i­lar tools for ma­nip­u­la­tion the par­ti­tion ta­ble via the graph­i­cal user in­ter­face. Note this op­er­a­tion will wipe all par­ti­tions and cre­ate new par­ti­tions you need.

Par­ti­tion­ing with Fdisk

In this guide we will use the tool fdisk to ac­com­plish this task.

sudo fdisk /dev/nvme0n1
# Create a new empty GPT partition table:
Command (m for help): g
# Add a new partition:
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-1951473664, default 2048): [Press Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1951473664, default 1951473664): +512M
Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.
# Add a new partition:
Command (m for help): n
Partition number (2-128, default 1): 2
First sector (1050624-1951473664, default 1050624): [Press Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-1951473664, default 1951473664): +488M
Created a new partition 2 of type 'Linux filesystem' and of size 488 MiB.
# Add a new partition:
Command (m for help): n
Partition number (3-128, default 1): 3
First sector (2050048-1951473664, default 2050048): [Press Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2050048-1951473664, default 1951473664): [Press Enter]
Created a new partition 3 of type 'Linux filesystem' and of size 930.54 GiB.
# Change a partition type:
Command (m for help): t
Partition number (1-3, default 3): 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.
# Change a partition type:
Command (m for help): t
Partition number (1-3, default 3): 3
Partition type (type L to list all types): 31
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
# Print the partition table:
Command (m for help): p
Disk /dev/nvme0n1: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: Samsung SSD 980 1TB                     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 16384 bytes / 131072 bytes
Disklabel type: gpt
Disk identifier: E5B93AC4-141D-4174-B94D-07FCBB074D5C

Device           Start        End    Sectors   Size Type
/dev/nvme0n1p1    2048    1050623    1048576   512M EFI System
/dev/nvme0n1p2 1050624    2050047     999424   488M Linux filesystem
/dev/nvme0n1p3 2050048 1953523711 1951473664 930.5G Linux LVM
#  Verify the partition table
Command (m for help): v
No errors detected.
Header version: 1.0
Using 2 out of 128 partitions.
A total of 0 free sectors is available in 0 segments (the largest is (null)).
# Write the table to the disk and exit:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

For­mat the Par­ti­tions

1. For­mat the UE­FI par­ti­tion, /​​​dev/​​​nvme0n1p1, as Fat32 by mkfs.vfat.

sudo mkfs.vfat -F 32 /dev/nvme0n1p1

2. For­mat the BOOT par­ti­tion, /​​​dev/​​​nvme0n1p2, as Ext2 (or Ext4) by mkfs.ext2 (or mkfs.ext4).

sudo mkfs.ext2 /dev/nvme0n1p2

3. Cre­ate LVM Phys­i­cal vol­ume (PV) on the largest par­ti­tion, /​​​dev/​​​nvme0n1p2, by pvcre­ate.

sudo pvcreate /dev/nvme0n1p3

Lath­er when the Vol­ume groups and Log­i­cal vol­umes are cre­ate they could be for­mat­ted as cer­tain file sys­tem. For more de­tails read the ar­ti­cle LVM Ba­sic Op­er­a­tions.

Get in­for­ma­tion about a Block de­vice by Fdisk and Part­ed

sudo fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: Samsung SSD 980 1TB                     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 16384 bytes / 131072 bytes
Disklabel type: gpt
Disk identifier: E5B93AC4-141D-4174-B94D-07FCBB074D5C

Device           Start        End    Sectors   Size Type
/dev/nvme0n1p1    2048    1050623    1048576   512M EFI System
/dev/nvme0n1p2 1050624    2050047     999424   488M Linux filesystem
/dev/nvme0n1p3 2050048 1953523711 1951473664 930.5G Linux LVM
sudo parted -l   # -l, --list - lists partition layout on all block device
...
Model: Samsung SSD 980 1TB (nvme)
Disk /dev/nvme0n1: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size   File system  Name                  Flags
 1      1049kB  538MB   537MB  fat32        EFI System Partition  boot, esp
 2      538MB   1050MB  512MB  ext2
 3      1050MB  1000GB  999GB                                     lvm

Ref­er­ences