Linux Basic Partitioning: Difference between revisions

From WikiMLT
Spas (talk | contribs)
mNo edit summary
Spas (talk | contribs)
m Text replacement - "mlw-continue" to "code-continue"
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude>
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude>
Assuming we want to create LVM and we want to occupy the entire disk space at <code>/dev/sdb</code>. You can <code>fdisk</code> or <code>gdisk</code> to create a new GPT partition table that will wipe all partitions and create new partitions you need:
In this guide we are assuming that, we want to prepare the device <code>/dev/nvme0n1</code> for Linux UEFI installation with [[LVM Basic Operations|LVM]] and GPT partition table. So the partitions that will be created are as follow:


* If the device <code>/dev/sdb</code> '''will be''' used used as '''boot device''' you can create two partitions one for <code>/boot</code> - Linux ext4 and one for the root fs <code>/</code> - Linux LVM.
* <code>/dev/nvme0n1p1</code> - EFI System (for: <code>/boot/efi</code>),
* If the device <code>/dev/sdb</code> '''won't be''' used as '''boot device''' you can create only one for LVM.
* <code>/dev/nvme0n1p2</code> - Linux filesystem (for: <code>/boot</code>),
* <code>/dev/nvme0n1p3</code> - Linux LVM (for: <code>/</code>, <code>/home</code>, etc.).


Create partition table:
We can use [https://manpages.ubuntu.com/manpages/jammy/en/man8/fdisk.8.html <code>fdisk</code>], [https://manpages.ubuntu.com/manpages/jammy/en/man8/parted.8.html <code>parted</code>] or [https://manpages.ubuntu.com/manpages/jammy/en/man8/gdisk.8.html <code>gdisk</code>] within the command line, or we can use [https://manpages.ubuntu.com/manpages/jammy/en/man8/gparted.8.html <code>gparted</code>] and other similar tools for manipulation the partition table via the graphical user interface. Note this operation will wipe all partitions and create new partitions you need.


<syntaxhighlight lang="shell" line="1" class="mlw-continue">
== Partitioning with '''Fdisk''' ==
In this guide we will use the tool <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/fdisk.8.html fdisk]</code> to accomplish this task.
 
<syntaxhighlight lang="shell" line="1" class="code-continue">
sudo fdisk /dev/nvme0n1
sudo fdisk /dev/nvme0n1
</syntaxhighlight><syntaxhighlight lang="yaml" class="mlw-continue">
</syntaxhighlight><syntaxhighlight lang="yaml" class="code-continue">
# Create a new empty GPT partition table:
# Create a new empty GPT partition table:
Command (m for help): g   
Command (m for help): g   
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Add a new partition:
# Add a new partition:
Command (m for help): n
Command (m for help): n
Line 20: Line 24:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1951473664, default 1951473664): +512M
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1951473664, default 1951473664): +512M
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.
Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Add a new partition:
# Add a new partition:
Command (m for help): n
Command (m for help): n
Line 30: Line 34:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-1951473664, default 1951473664): +488M
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-1951473664, default 1951473664): +488M
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
Created a new partition 2 of type 'Linux filesystem' and of size 488 MiB.
Created a new partition 2 of type 'Linux filesystem' and of size 488 MiB.
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Add a new partition:
# Add a new partition:
Command (m for help): n
Command (m for help): n
Line 40: Line 44:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2050048-1951473664, default 1951473664): [Press Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2050048-1951473664, default 1951473664): [Press Enter]
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
Created a new partition 3 of type 'Linux filesystem' and of size 930.54 GiB.
Created a new partition 3 of type 'Linux filesystem' and of size 930.54 GiB.
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Change a partition type:
# Change a partition type:
Command (m for help): t
Command (m for help): t
Line 49: Line 53:
Partition type (type L to list all types): 1
Partition type (type L to list all types): 1
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
Changed type of partition 'Linux filesystem' to 'EFI System'.
Changed type of partition 'Linux filesystem' to 'EFI System'.
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Change a partition type:
# Change a partition type:
Command (m for help): t
Command (m for help): t
Line 58: Line 62:
Partition type (type L to list all types): 31
Partition type (type L to list all types): 31
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Print the partition table:
# Print the partition table:
Command (m for help): p
Command (m for help): p


</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
Disk /dev/nvme0n1: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk /dev/nvme0n1: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: Samsung SSD 980 1TB                     
Disk model: Samsung SSD 980 1TB                     
Line 81: Line 85:


</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
#  Verify the partition table
#  Verify the partition table
Command (m for help): v
Command (m for help): v
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="mlw-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
No errors detected.
No errors detected.
Header version: 1.0
Header version: 1.0
Line 91: Line 95:
A total of 0 free sectors is available in 0 segments (the largest is (null)).
A total of 0 free sectors is available in 0 segments (the largest is (null)).
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="yaml" class="mlw-continue">
<syntaxhighlight lang="yaml" class="code-continue">
# Write the table to the disk and exit:
# Write the table to the disk and exit:
Command (m for help): w
Command (m for help): w
</syntaxhighlight><syntaxhighlight lang="shell-session" class="mlw-continue">
</syntaxhighlight><syntaxhighlight lang="shell-session" class="code-continue">
The partition table has been altered.
The partition table has been altered.
Calling ioctl() to re-read partition table.
Calling ioctl() to re-read partition table.
Syncing disks.
Syncing disks.
</syntaxhighlight>
</syntaxhighlight>
== Format the Partitions ==
'''1.''' Format the UEFI partition, <code>/dev/nvme0n1p1</code>, as Fat32 by <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.vfat.8.html mkfs.vfat]</code>.<syntaxhighlight lang="shell" line="1" class="code-continue">
sudo mkfs.vfat -F 32 /dev/nvme0n1p1
</syntaxhighlight>'''2.''' Format the BOOT partition, <code>/dev/nvme0n1p2</code>, as Ext2 (or Ext4) by <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.ext2.8.html mkfs.ext2]</code> (or <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.ext4.8.html mkfs.ext4]</code>).<syntaxhighlight lang="shell" line="1" class="code-continue">
sudo mkfs.ext2 /dev/nvme0n1p2
</syntaxhighlight>'''3.''' Create LVM Physical volume (PV) on the largest partition, <code>/dev/nvme0n1p2</code>, by <code>pvcreate</code>.<syntaxhighlight lang="shell" line="1" class="code-continue">
sudo pvcreate /dev/nvme0n1p3
</syntaxhighlight>Lather when the Volume groups and Logical volumes are create they could be formatted as certain file system. For more details read the article [[LVM Basic Operations]].
== Get information about a Block device by Fdisk and Parted ==
<syntaxhighlight lang="shell" line="1" class="code-continue">
sudo fdisk -l /dev/nvme0n1
</syntaxhighlight><syntaxhighlight lang="terraform">
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
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="code-continue">
sudo parted -l  # -l, --list - lists partition layout on all block device
</syntaxhighlight><syntaxhighlight lang="terraform">
...
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
</syntaxhighlight>
== References ==
* Linux for Devices: [https://www.linuxfordevices.com/tutorials/linux/detect-filesystem-of-unmounted-partition How to Detect the Filesystem of an Unmounted Partition on Linux]
* Linux for Devices: [https://www.linuxfordevices.com/tutorials/linux/top-disk-partitioning-tools Top 5 Disk Partitioning tools for Linux]
* Ubuntu Manual Pages: <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.ext2.8.html mke2fs]</code> - create an ext2/ext3/ext4 file system
* Ubuntu Manual Pages:  [https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.8.html mkfs] - build a Linux filesystem [[https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.8.html#see%20also #SEE ALSO]]
* Ubuntu Manual Pages:  [https://manpages.ubuntu.com/manpages/jammy/en/man8/mkfs.msdos.8.html mkfs.fat] - create an MS-DOS FAT filesystem
* Ask Ubuntu: [https://askubuntu.com/questions/22381/how-to-format-a-usb-flash-drive How to format a USB flash drive?]
* [[LVM Basic Operations]]
* [[Repair GPT Table]]


<noinclude>
<noinclude>
Line 105: Line 161:
  | Прндл  = Linux Server
  | Прндл  = Linux Server
  | Прндл1 = Linux Desktop
  | Прндл1 = Linux Desktop
  | Стадий = 3
  | Стадий = 6
  | Фаза  = Разработване
  | Фаза  = Утвърждаване
  | Статус = Разработван
  | Статус = Утвърден
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = {{REVISIONUSER}}
  | РзбПт = Spas
| АвтПт  = Spas
| УтвПт = {{REVISIONUSER}}
  | ИдтДт  = 12.09.2022
  | ИдтДт  = 12.09.2022
  | РзбДт  = {{Today}}
  | РзбДт = 12.09.2022
| АвтДт  = 12.09.2022
| УтвДт = {{Today}}
  | ИдтРв  = [[Special:Permalink/31448|31448]]
  | ИдтРв  = [[Special:Permalink/31448|31448]]
  | РзбРв  = {{REVISIONID}}
  | РзбРв = [[Special:Permalink/31461|31461]]
| АвтРв  = [[Special:Permalink/31463|31463]]
| УтвРв = {{REVISIONID}}
}}
}}
</div>
</div>
</noinclude>
</noinclude>

Latest revision as of 07:30, 26 September 2022

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