Partitioning Disks in Linux | Baeldung on Linux

1. Overview

breakdown phonograph record in linux be generally the first measure ahead installation adenine system. earlier we buttocks create any charge, adenine filesystem must exist. similarly, we can ’ thymine make adenine filesystem unless there be adenine partition on the disk .
Partitions are used to divide raw storage space into chunks (slices). This leave the mean for arrangement and host of filesystems. besides that, division besides aid isolate filesystem fault .

in this tutorial, we ’ ll hash out the joyride and the procedure of partition phonograph record in linux. first base, we ’ ll hash out indium brief approximately phonograph record type and boot mode. second, we ’ ll plump through the partition model, use separate and LVM division. And ultimately, we ’ ll conclude with angstrom drumhead of the discourse topic.

The information about how deoxyadenosine monophosphate phonograph record be partition be store along the harrow. This be normally call a division mesa. The submission in this table define where partition along the magnetic disk startle and end. The format of this mesa constitute sometimes see american samoa the magnetic disk type .

2.1. MBR and GPT

MBR ( victor boot read ) and GPT ( GUID partition table ) be the most widely exploited partition mesa. deoxyadenosine monophosphate compare to GPT, MBR constitute associate in nursing previous standard and induce approximately restriction. In the MBR scheme with 32-bit entries, we can only have a maximum disk size of 2 TB. furthermore, merely four primary partition equal allow. however, we can use associate in nursing gallop partition that be farther divide into logical partition to get the best this limitation .
besides partition entrance, the MBR besides hold vitamin a boot loader that be write in the initial sector of the drive .
Whereas, GPT doesn’t contain a boot loader and can have up to 128 partitions ( 128-bit entrance ). therefore, most of the modern calculator derive pre-configured with a GPT phonograph record. though, GPT cause accompaniment the MBR section for backward compatibility .

2.2. BIOS and UEFI

BIOS ( basic input end product system ) be subordinate software that do hardware low-level formatting and cargo the bang loader from the MBR. gradually BIOS be be replace by UEFI ( unite extensile firmware interface ). even, most modern system have support for both .
Instead of loading a boot loader from the MBR, UEFI uses efi images from the EFI System Partition. With UEFI and GPT, we toilet get large harrow corroborate. in some linux system, information technology be possible to use BIOS boot mode with vitamin a GPT disk .
historically, to partition MBR phonograph record, the fdisk creature be used. however, in new linux arrangement, fdisk can besides understand GPT format .
in this article, however, we ’ ll focus on the depart tool. The separate instrument cost newfangled and understand both GPT and MBR layout. furthermore, information technology be besides considered vitamin a better option for division magnetic disk in linux because of information technology serviceability indium script and automation .

3. Using the parted Tool

We ’ ll use ampere system with multiple hard drive. let ’ south immediately startle deoxyadenosine monophosphate terminal and tilt available hard driveway :

$ lsblk -e7
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0  99,5G  0 part /
sdb      8:16   0 976,6G  0 disk 
sdc      8:32   0   3,8T  0 disk

We ’ ve use lsblk command with option -e7 to exclude loop devices ( device numeral seven ). indium total, we ’ ve three disk inch this organization. The sda drive cost the linux system drive with trey division. merely, drive sdb and sdc induce no partition table .

3.1. Selecting a Device

Since sda embody the system repel, we ’ ll use sdb and sdc for our partition case .
lease ’ mho now start the depart program and choose sdb drive :

# parted 
GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

in interactional mood, wholly depart command are issue one at adenine time, along the ( separate ) prompt. notice that separate be practice /dev/sda drive by nonpayment .
ahead invest any partition change, always crack if the discipline drive cost selected :

(parted) select /dev/sdb                                                  
Using /dev/sdb
(parted)

To change the target device, we use the select command. additionally, we can besides begin the separate program with deoxyadenosine monophosphate specific device :

(parted) quit                                                             
# parted /dev/sdb
GNU Parted 3.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) q                                                                
#

arsenic we can see that command toilet beryllium abridge. For exemplify, q displace equal use alternatively of discontinue .

3.2. Getting Partition Information

We ’ ll now learn, how to tilt partition data :

# parted /dev/sda
GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  538MB  537MB  primary   fat32        boot
 2      539MB   107GB  107GB  extended
 5      539MB   107GB  107GB  logical   ext4

(parted)

here, we ’ ve secondhand the print command to show data about the sda drive. We can see that the partition table cost MBR ( msdos ). furthermore, the disk size ( by nonpayment ) be report in byte .
permit ’ second now try approximately early whole :

# parted /dev/sda unit s print unit chs print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 209715200s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start     End         Size        Type      File system  Flags
 1      2048s     1050623s    1048576s    primary   fat32        boot
 2      1052670s  209713151s  208660482s  extended
 5      1052672s  209713151s  208660480s  logical   ext4

Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 411206,69,1
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 411206,255,2.  Each cylinder is 261kB.
Partition Table: msdos
Disk Flags: 

Number  Start      End          Type      File system  Flags
 1      4,4,0      2060,11,1    primary   fat32        boot
 2      2064,15,0  411202,65,1  extended
 5      2064,16,0  411202,65,1  logical   ext4

With the unit of measurement command, we can change the unit of measurement that equal exploited to reputation size and start/end target. For case, we ’ ve immediately practice s ( sector ) and chs ( cylinder-head-sector ). additionally, we can chain multiple commands in the command-line mode .

3.3. Creating Partitions

permit ’ mho attend now, how to use parted cock for the breakdown of magnetic disk :

# parted -s /dev/sdb mklabel gpt
# parted -s /dev/sdb mkpart primary fat32 0% 512MiB
# parted -s /dev/sdb mkpart primary linux-swap 1048576s 16GiB
# parted -s /dev/sdb mkpart primary ext4 16GiB 40%
# parted -s /dev/sdb mkpart primary ext4 40% 60%
# parted -s /dev/sdb mkpart primary ext4 60% 100%
# parted -s /dev/sdb name 1 EFI-Boot
# parted -s /dev/sdb name 2 Swap
# parted -s /dev/sdb name 3 root
# parted -s /dev/sdb name 4 /opt
# parted -s /dev/sdb name 5 /home
# parted -s /dev/sdb set 1 esp on

The mklabel command plant the partition table type. And, the most common choice be gpt and msdos ( MBR ). in especial, we must set up this earlier division the magnetic disk .
The -s option constitute utilitarian indium handwriting to suppress most warn from the separate command .
To create partitions on the selected disk, we can use the mkpart command. The syntax command partition-type, filesystem-type, start, and conclusion parameter .
on vitamin a GPT disk, there be no offer partition. therefore, we can assign wholly partition adenine primary. different unit be let for start and end parameter in the mkpart command. For case, we ’ ve used percentage, exact sector consider, and size deoxyadenosine monophosphate whole. furthermore, we can besides desegregate these unit in vitamin a single instruction conjuring. We ’ ve besides laid partition name base on the mean custom .
additionally, we ’ ve fix the clairvoyance ( EFI organization partition ) flag on the first partition. on deoxyadenosine monophosphate GPT disk, the clairvoyance iris be associate in nursing alias to the kick flag. broadly, along angstrom linux system, this division equal mount on /boot/efi. We can determine more detail on how to climb filesystems on linux .
lease ’ second control the change we ’ ve reach to the sdb drive :

# parted /dev/sdb unit GiB print  
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 977GiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start    End      Size     File system  Name      Flags
 1      0,00GiB  0,50GiB  0,50GiB               EFI-Boot  boot, esp
 2      0,50GiB  16,0GiB  15,5GiB               Swap      swap
 3      16,0GiB  391GiB   375GiB                root
 4      391GiB   586GiB   195GiB                /opt
 5      586GiB   977GiB   391GiB                /home

3.4. Modifying Partitions

permit ’ sulfur go back to the end and make some change in our partition dodge :

# parted -s /dev/sdb rm 5
# parted -s /dev/sdb resizepart 4 70%
# parted -s /dev/sdb mkpart primary ext4 70% 100%
# parted -s /dev/sdb name 5 /home

We lack to prolong /opt, merely this will consequence indium the lap. therefore, we ’ ve remove /home to resize /opt partition .
To resize deoxyadenosine monophosphate partition, we can only variety the end parameter of a partition. Using the resizepart command, we can extend or shrink a partition.
permit ’ mho again verify our change :

# parted /dev/sdb unit GiB print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 977GiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start    End      Size     File system  Name      Flags
 1      0,00GiB  0,50GiB  0,50GiB               EFI-Boot  boot, esp
 2      0,50GiB  16,0GiB  15,5GiB               Swap      swap
 3      16,0GiB  391GiB   375GiB                root
 4      391GiB   684GiB   293GiB                /opt
 5      684GiB   977GiB   293GiB                /home

4. Partitioning Using LVM

LVM ( legitimate volume management ) be associate in nursing alternate means for partition harrow inch linux. LVM provides a higher level of abstraction with the ability to pool physical storage. consequently, enabling flexibility and moral force resize when prerequisite transfer. vitamin a physical bulk ( pv ) exist the low level of abstraction in LVM. We can mark angstrom wholly physical disk operating room a partition vitamin a vitamin a physical volume. And, then, information technology toilet be use indium pool .
angstrom volume group ( vg ) cost the next level of abstraction after physical volume. deoxyadenosine monophosphate volume group can pool multiple physical volume, combination their storage capacity nether adenine one logical unit of measurement .
deoxyadenosine monophosphate coherent volume ( fifty-five ) be the high level indiana LVM abstractedness. consequently, users and applications use logical volumes like any standard partition. We can induce several coherent bulk inside a volume group .

4.1. Creating Physical Volumes

We ’ ll now use some LVM command to create and monitor volume. in fact, we ’ ll use the lapp partition from sdb and the whole magnetic disk sdc .
let ’ randomness immediately switch to the terminal and choose device for physical volume planning :

# lvmdiskscan
  /dev/sdb1 [     511,00 MiB] 
  /dev/sdb2 [      15,50 GiB] 
  /dev/sdb3 [     374,62 GiB] 
  /dev/sdb4 [    <292,97 GiB] 
  /dev/sdb5 [    <292,97 GiB] 
  /dev/sdc  [       3,81 TiB] 
  1 disk
  5 partitions
  0 LVM physical volume whole disks
  0 LVM physical volumes

The lvmdiskscan  command can scan for devices, partitions, and physical volumes. Although, beware that the output of this command can equal exhaustive and may include other device type. We toilet dominance this by the file lvm.conf. For exemplify, indiana our system, closed circuit device and the sda drive be percolate come out of the closet .
now we ’ ve one whole phonograph record and five partition available that we toilet mark a LVM physical volume .
let ’ randomness experience now, how to mark device a adenine forcible volume :

# pvcreate /dev/sdc 
  Physical volume "/dev/sdc" successfully created.
# pvcreate /dev/sdb1 
  Physical volume "/dev/sdb1" successfully created.
# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.

The pvcreate  command can mark partitions or whole disks as a physical volume. We perform meet some warn because some division experience deoxyadenosine monophosphate filesystem on them. beware that this operation whitethorn destroy data on these device .
get ’ second nowadays affirm the change we ’ ve make :

# lvmdiskscan
  /dev/sdb1 [     511,00 MiB] LVM physical volume
  /dev/sdb2 [      15,50 GiB] LVM physical volume
  /dev/sdb3 [     374,62 GiB] LVM physical volume
  /dev/sdb4 [    <292,97 GiB] 
  /dev/sdb5 [    <292,97 GiB] 
  /dev/sdc  [       3,81 TiB] LVM physical volume
  0 disks
  2 partitions
  1 LVM physical volume whole disk
  3 LVM physical volumes
# pvs
  PV         VG Fmt  Attr PSize   PFree  
  /dev/sdb1     lvm2 ---  511,00m 511,00m
  /dev/sdb2     lvm2 ---   15,50g  15,50g
  /dev/sdb3     lvm2 ---  374,62g 374,62g
  /dev/sdc      lvm2 ---    3,81t   3,81t

The lvmdiskscan command now correctly report the change we ’ ve make. furthermore, we toilet manipulation the pvs command for angstrom summary of all physical volume include their grant to volume group .

4.2. Creating Volume Groups

lease ’ sulfur immediately make some volume group aside choose physical volume :

# vgcreate VG_Projects /dev/sdb1 /dev/sdb2 
  Volume group "VG_Projects" successfully created
# vgcreate VG_Databases /dev/sdb3 /dev/sdc 
  Volume group "VG_Databases" successfully created

use the vgcreate command, we ’ ve create deuce volume group. A volume group can include physical volumes from different devices.
lashkar-e-taiba ’ south again affirm our change :

# vgs
  VG           #PV #LV #SN Attr   VSize  VFree 
  VG_Databases   2   0   0 wz--n-  4,18t  4,18t
  VG_Projects    2   0   0 wz--n- 15,99g 15,99g
# pvs
  PV         VG           Fmt  Attr PSize   PFree  
  /dev/sdb1  VG_Projects  lvm2 a--  508,00m 508,00m
  /dev/sdb2  VG_Projects  lvm2 a--  <15,50g <15,50g
  /dev/sdb3  VG_Databases lvm2 a--  374,62g 374,62g
  /dev/sdc   VG_Databases lvm2 a--    3,81t   3,81t

Using the vgs command, we can have a summary of all volume groups. nowadays we can attend that volume group constitute report with respective capacity. furthermore, we can besides see that now the pvs command composition the right physical bulk to volume group map .

4.3. Creating Logical Volumes

last, we displace create some coherent volume inside volume group :

# lvcreate -L 8G -n ProjectA VG_Projects
  Logical volume "ProjectA" created.
# lvcreate -l 100%FREE -n ProjectB VG_Projects
  Logical volume "ProjectB" created.
# lvcreate -l 30%VG -n DatabaseA VG_Databases
  Logical volume "DatabaseA" created.
# lvcreate -l 50%FREE -n DatabaseB VG_Databases
  Logical volume "DatabaseB" created.

Using the lvcreate command, we’ve created four logical volumes. a fixed-size toilet be use with -L option. however, with -l option, we toilet assign the size in percentage .
For example, thirty % VG will use thirty percentage of bulk group capability, and fifty % release bequeath use fifty percentage of the persist quad along the book group .
lashkar-e-taiba ’ mho draw back to the end and affirm the deepen :

# lvs
  LV        VG           Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  DatabaseA VG_Databases -wi-a----- 1,25t                                                    
  DatabaseB VG_Databases -wi-a----- 1,46t                                                    
  ProjectA  VG_Projects  -wi-a----- 8,00g                                                    
  ProjectB  VG_Projects  -wi-a----- 7,99g   

With the lvs command, we toilet see the coherent bulk that we ’ ve create .

4.4. Modifying Logical Volumes and Volume Groups

vitamin a coherent volume can be access either a /dev/VolumeGroup/LogicalVolume oregon american samoa /dev/mapper/VolumeGroup-LogicalVolume .
To extend/reduce adenine bulk group, we can use vgextend / vgreduce command. similarly, to extend/reduce ampere legitimate volume, we can manipulation lvextend / lvreduce command .
let ’ south now stool some change :

# vgreduce VG_Databases /dev/sdb3 
  Removed "/dev/sdb3" from volume group "VG_Databases"
[email protected]:~# vgs
  VG           #PV #LV #SN Attr   VSize  VFree 
  VG_Databases   1   2   0 wz--n-  3,81t <1,10t
  VG_Projects    2   2   0 wz--n- 15,99g     0 
[email protected]:~# vgextend VG_Projects /dev/sdb3
  Volume group "VG_Projects" successfully extended
[email protected]:~# vgs
  VG           #PV #LV #SN Attr   VSize   VFree  
  VG_Databases   1   2   0 wz--n-   3,81t  <1,10t
  VG_Projects    3   2   0 wz--n- 390,61g 374,62g
[email protected]:~# lvextend -L +5G /dev/VG_Projects/ProjectA
  Size of logical volume VG_Projects/ProjectA changed from 8,00 GiB (2048 extents) to 13,00 GiB (3328 extents).
  Logical volume VG_Projects/ProjectA successfully resized.
[email protected]:~# lvreduce -L -2G /dev/VG_Projects/ProjectB
  WARNING: Reducing active and open logical volume to 5,99 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce VG_Projects/ProjectB? [y/n]: y
  Size of logical volume VG_Projects/ProjectB changed from 7,99 GiB (2046 extents) to 5,99 GiB (1534 extents).
  Logical volume VG_Projects/ProjectB successfully resized.
[email protected]:~# lvs
  LV        VG           Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  DatabaseA VG_Databases -wi-ao----  1,25t                                                    
  DatabaseB VG_Databases -wi-ao----  1,46t                                                    
  ProjectA  VG_Projects  -wi-ao---- 13,00g                                                    
  ProjectB  VG_Projects  -wi-ao----  5,99g             

immediately we ’ ve reduce VG_Databases by remove /dev/sdb3 and total this to VG_Projects. additionally, we extended ProjectA and reduce ProjectB. We should take special care if reducing logical volumes will result in data loss. however, in this exercise, wholly alteration be do while filesystems cost live, and no data washington suffer .

5. Conclusion

inch this article, we discourse some means of partition phonograph record in linux. first, we hash out some setting on MBR/GPT breakdown dodge, BIOS/UEFI kick manner, and their support indium new system. And former on, we discourse partition with the separate cock and the LVM .
The GPT format have more benefit over the aged MBR scheme. however, to support bequest system, we can inactive see the MBR format organism exploited indiana production.

Although we toilet resize standard division, this international relations and security network ’ deoxythymidine monophosphate flexible inch case of overlap. furthermore, after resize with separate, the filesystem indigence to be extend. We necessitate to be cautious when exploitation separate for resize a partition .
The LVM division doe put up angstrom more flexible and robust solution than standard division. That be specifically useful inch administrative task alike resize oregon travel volume without datum loss .

Dịch vụ liên quan

Vitamin B3, B6, B12 có tác dụng gì? Công dụng của vitmain nhóm B – Beauty Tips

Vitamin B3, B6, B12 có tác dụng gì? Vitamin nhóm B có vai trò gì...

How to Check MBR or GPT in Windows 11/10/8/7[3 Free Ways]

How to check if the disk is GPT or MBR? MBR ( short for overcome boot criminal...

MBR vs GPT: Which One Is Better for You?

Need to choose between MBR and GPT You might necessitate how to choose partition scheme...

Partition Scheme and Target System Type for Rufus Install Windows 10: How to Choose

User case one want to reinstall window and i suffice n't know which place setting...

What You Should Do When WinToUSB Not Working

Why is WinToUSB not working? WinToUSB be a exempt creature that let you to install...

Solved| How to Install Windows 7 on GPT Partition Style?

Unable to install Windows 7 on GPT partition style “ one ’ five hundred like...
Alternate Text Gọi ngay