A Quick Look at File Sys­tem Par­ti­tion­ing and Log­i­cal Vol­ume Man­age­ment

OpenSource For You - - Contents - By: Kshi­tij Upad­hyay The au­thor is RHCSA and RHCE cer­ti­fied, and loves to write about new tech­nolo­gies. He can be reached at upad­hyayk04@gmail.com.

Disk par­ti­tion­ing al­lows a hard drive to be di­vided into mul­ti­ple log­i­cal stor­age units that are re­ferred to as par­ti­tions. By di­vid­ing the disk into par­ti­tions, sys­tems ad­min­is­tra­tors can use dif­fer­ent par­ti­tions to per­form dif­fer­ent func­tions.

This scheme sup­ports a max­i­mum of four pri­mary par­ti­tions. On Linux sys­tems, with the use of ex­tended and log­i­cal par­ti­tions, ad­min­is­tra­tors can cre­ate a max­i­mum of 15 par­ti­tions. Since the data in the par­ti­tions is stored as 32-bit val­ues, disks par­ti­tioned with the MBR (Mas­ter Boot Record) scheme have a max­i­mum disk and par­ti­tion size limit of 2TiB.

Cre­at­ing MBR disk par­ti­tions

For disks with the MBR par­ti­tion­ing scheme, the fdisk par­ti­tion ed­i­tor can be used to cre­ate disk par­ti­tions. Cre­at­ing an MBR style disk par­ti­tion in­volves the fol­low­ing steps.

Spec­ify the disk de­vice to cre­ate the par­ti­tion on

As the root user, ex­e­cute the fdisk com­mand and spec­ify the disk de­vice’s name as an ar­gu­ment. This will start the fdisk com­mand in in­ter­ac­tive mode, and will present a com­mand prompt:

# fdisk /dev/vdb Re­quest a new pri­mary or ex­tended par­ti­tion

En­ter n to re­quest a new par­ti­tion, and spec­ify whether the par­ti­tion should be cre­ated as a pri­mary or ex­tended par­ti­tion. The de­fault se­lec­tion is the pri­mary type.

Spec­ify the par­ti­tion num­ber

This serves as the iden­ti­fi­ca­tion num­ber of the new par­ti­tion on the disk for use in fu­ture par­ti­tion op­er­a­tions. The de­fault value is the low­est un­used par­ti­tion num­ber.

Spec­ify the first sec­tor on the disk that the new par­ti­tion will start on

The de­fault value is the first sec­tor avail­able on the disk.

Spec­ify the last sec­tor on the disk that the new par­ti­tion will end on

The de­fault value is the last of the avail­able, un­al­lo­cated sec­tors con­tigu­ous to the new par­ti­tion’s first sec­tor.

In ad­di­tion to the end­ing sec­tor num­ber, fdisk also ac­cepts a num­ber rep­re­sent­ing the de­sired size of the par­ti­tion ex­pressed in

sec­tors. We can also ex­press size in units of KiB, MiB or GiB.

Once the par­ti­tion bound­ary is en­tered, fdisk will dis­play a con­fir­ma­tion that the par­ti­tion has been cre­ated.

De­fine the par­ti­tion type

If the newly cre­ated par­ti­tion should have a type other than Linux, en­ter the t com­mand to change the type of the par­ti­tion. En­ter the hex code for the new par­ti­tion type. If needed, a ta­ble of hex codes for all par­ti­tion types can be dis­played with the L com­mand. Set­ting this par­ti­tion type cor­rectly is cru­cial, since some tools rely on it to func­tion prop­erly.

Save the par­ti­tion ta­ble changes

Is­sue the w com­mand to fi­nalise the par­ti­tion-cre­ation re­quest by writ­ing the changes to the disk par­ti­tions ta­ble and ex­it­ing the fdisk pro­gram.

Ini­ti­ate a ker­nel read of the new par­ti­tion ta­ble

Run the part­probe com­mand with the disk de­vice’s name as an ar­gu­ment to force a re-read of its par­ti­tion ta­ble.

Cre­at­ing a file sys­tem

Af­ter a block de­vice has been cre­ated, the next step is to ap­ply a file sys­tem for­mat to it. A file sys­tem ap­plies a struc­ture to the block de­vice so that data can be stored and re­trieved from it. Red Hat Linux sup­ports many file sys­tems, two of the most com­mon be­ing xfs and ext4.

The mkfs com­mand can be used to ap­ply a file sys­tem to a block de­vice. If no type is spec­i­fied and ex­tended, the type two (ext2) file sys­tem will be used, which is not de­sir­able in many cases. To spec­ify the file sys­tem type, the com­mand -t should be used. # mkfs -t ext4 /dev/vdb1

Mount­ing file sys­tems

Once the file sys­tem for­mat has been ap­plied, the last step to adding a new file sys­tem is to at­tach a file sys­tem into a new direc­tory struc­ture. When the file sys­tem is at­tached into the direc­tory’s hi­er­ar­chy, user space util­i­ties can ac­cess or write files on the de­vice.

Man­u­ally mount­ing file sys­tems

Sys­tems ad­min­is­tra­tors can use the mount com­mand to man­u­ally at­tach the de­vice into the direc­tory’s lo­ca­tion, or mount point, by spec­i­fy­ing the de­vice and the mount point, as well as any op­tions that may be de­sired, to cus­tomise the be­hav­iour of the de­vice. We can see the mounted file sys­tem by us­ing the com­mand df -h.

# mount /dev/vdb1 /share

Direc­tory (any name) should be cre­ated man­u­ally be­fore run­ning the mount com­mand. The name of the cre­ated direc­tory will be spec­i­fied with mount com­mand. In Fig­ure 3, a direc­tory with the name/share has been cre­ated. The mount com­mand can also be used to view cur­rently mounted file sys­tems, the mount point and the op­tions.

Man­u­ally mount­ing a file sys­tem is an ex­cel­lent way to ver­ify that a for­mat­ted de­vice is ac­ces­si­ble or work­ing in the way de­sired. How­ever, once the sys­tem is re­booted, the file sys­tem (while it still ex­ists and has in­tact data) will not be mounted into the direc­tory tree again. If an ad­min­is­tra­tor wants the file sys­tem to be per­ma­nently mounted, a list­ing of the file sys­tem needs to be added to /etc/fstab.

Per­sis­tently mount­ing file sys­tems

By adding a list­ing for a de­vice into the /etc/fstab file, the ad­min­is­tra­tor can con­fig­ure a de­vice to be mounted to a mount point at sys­tem boot.

/etc/fstab has six fields per line. The first field spec­i­fies the de­vice to be used. The UUID can be used to spec­ify the de­vice or, al­ter­na­tively, the de­vice file could be used. The UUID is stored in the file sys­tem su­perblock and cre­ated when the file sys­tem is cre­ated.

The sec­ond field is the mount point where the de­vice should be at­tached into the direc­tory’s hi­er­ar­chy. The mount point should al­ready ex­ist, and if it does not, it can be cre­ated by us­ing the mkdir com­mand.

The third field con­tains the file sys­tem’s type that has been

ap­plied to the block de­vice.

The fourth field is the list of op­tions that should be ap­plied to the de­vice when mounted to cus­tomise the be­hav­iour.

This field is re­quired, and there is a set of com­monly used op­tions called de­faults.

The last two fields are the dump flag and fsck or­der. The dump flag is used with the dump com­mand to take a backup of the con­tents of the de­vice. The fsck or­der field de­ter­mines if the fsck should be run at the boot time, in the event that the file sys­tem was not un­mounted cleanly. The value of the fsck or­der in­di­cates the or­der in which file sys­tems should have fsck run on them if mul­ti­ple file sys­tems are re­quired to be checked.

Hav­ing an in­cor­rect en­try in /etc/fstab may ren­der the ma­chine un­bootable. To avoid that sit­u­a­tion, an ad­min­is­tra­tor should ver­ify that an en­try is valid by un­mount­ing the file sys­tem back into place. If the mount –a com­mand re­turns an er­ror, it should be cor­rected be­fore re­boot­ing the ma­chine.

Man­ag­ing swap space

A swap space is an area of the disk that can be used with the Linux ker­nel’s mem­ory man­age­ment sub-sys­tem. Swap sys­tems are used to sup­ple­ment RAM by hold­ing in­ac­tive pages of mem­ory. The com­bined RAM plus swap spa­ces is called vir­tual mem­ory.

When the mem­ory us­age on a sys­tem ex­ceeds a de­fined limit, the ker­nel will comb through the RAM look­ing for idle mem­ory pages as­signed to pro­cesses. The ker­nel writes the idle pages to the swap area, and will re­as­sign the RAM page to be used by an­other process. If a pro­gram re­quires ac­cess to a page that has been writ­ten to disk, the ker­nel will lo­cate an­other idle page of mem­ory, write it to disk, and then re­call the needed page from the swap area.

Since the swap ar­eas re­side on disk, swap is in­cred­i­bly slow com­pared to RAM. When it is used to aug­ment sys­tem RAM, us­age of swap space should be kept to a min­i­mum, when­ever pos­si­ble.

Cre­at­ing a swap space

To cre­ate a swap space, an ad­min needs to do the fol­low­ing three things:

Cre­ate a par­ti­tion Set the type of the par­ti­tion as 82 Linux Swap For­mat a swap sig­na­ture on the de­vice

The cre­ation of a par­ti­tion and set­ting the type of the par­ti­tion is shown in Fig­ure 5.

For­mat­ting the de­vice

The mk­swap com­mand ap­plies a swap sig­na­ture to the de­vice. Un­like other for­mat­ting util­i­ties, mk­swap writes a sin­gle block at the be­gin­ning of the de­vice, leav­ing the rest of the de­vice un­for­mat­ted so that it can be used for stor­ing mem­ory pages.

# mk­swap /dev/vdb2

Ac­ti­vat­ing a swap space

An ad­min can use the swapon com­mand to ac­ti­vate a for­mat­ted swap space. swapon can be called on a de­vice, or swapon -a will ac­ti­vate all swap spa­ces avail­able.

Log­i­cal vol­ume man­age­ment con­cepts

Log­i­cal vol­ume and its ef­fec­tive man­age­ment make it eas­ier to han­dle disk space. If an LVM hosted file sys­tem needs space, it can be al­lo­cated to its log­i­cal vol­ume from the free space in its vol­ume group and the file sys­tem can be re­sized. If a disk starts to fail, a re­place­ment disk can be reg­is­tered as a phys­i­cal vol­ume with the vol­ume group, and the log­i­cal vol­ume ex­tents can be mi­grated to the new disk.

LVM def­i­ni­tions

Phys­i­cal stor­age de­vices pre­serve data in log­i­cal vol­ume. These block de­vices can be ei­ther disk par­ti­tions or whole disks. They must be ini­tialised as LVM de­vices in or­der to be used as an LVM.

Phys­i­cal vol­ume: These are used for un­der­ly­ing phys­i­cal de­vices in vol­ume groups. LVM au­to­mat­i­cally con­verts phys­i­cal vol­umes into phys­i­cal ex­tents. Phys­i­cal ex­tents are the small­est chunks of data — the small­est stor­age block on a phys­i­cal vol­ume.

Vol­ume group: This con­sists of one or more phys­i­cal vol­umes. A par­tic­u­lar phys­i­cal vol­ume can be al­lo­cated to only one vol­ume group. A vol­ume group can con­sist of un­used space and it can have more than one LVM.

Log­i­cal vol­umes: Log­i­cal vol­umes are cre­ated from free phys­i­cal ex­tents in vol­ume group, and pro­vide the stor­age de­vice that can be used by users and op­er­at­ing sys­tems. Log­i­cal vol­umes are a col­lec­tion of log­i­cal ex­tents that map to phys­i­cal ex­tents (a phys­i­cal ex­tent is the small­est stor­age chunk of a phys­i­cal vol­ume). By de­fault, each log­i­cal ex­tent maps to one PE. But this map­ping can be changed by us­ing spe­cific op­tions.

Im­ple­ment­ing LVM stor­age

Pre­par­ing the phys­i­cal de­vice

We have to cre­ate a par­ti­tion of any size but re­mem­ber to change the type of par­ti­tion to the Linux LVM by us­ing code 8e.

Cre­at­ing a phys­i­cal vol­ume

Phys­i­cal vol­ume can be cre­ated by the pvcre­ate com­mand as shown in Fig­ure 7. pvdis­play is used to ver­ify the cre­ated phys­i­cal vol­ume.

Cre­at­ing a vol­ume group

A vol­ume group can be cre­ated by vgcre­ate and then by pro­vid­ing the name of the vol­ume group. vgdis­play is used to ver­ify vol­ume group.

Cre­at­ing a log­i­cal vol­ume

An LVM can be cre­ated by us­ing the lvcre­ate com­mand, whereby we spec­ify the size of the LVM us­ing the -L op­tion and the name of the LVM with the -n op­tion, af­ter which the vol­ume group’s name needs to be pro­vided.

The log­i­cal vol­ume size can be ex­tended, pro­vided space is avail­able in the as­so­ci­ated vol­ume group. If so, then the lvre­size com­mand can be used to ex­tend the LVM. You can ver­ify the size of the ex­tended LVM by us­ing the lvdis­play com­mand.

To con­clude, here are some rea­sons why disk par­ti­tion­ing is nec­es­sary or ben­e­fi­cial:

Al­lows multi-boot­ing of dif­fer­ent op­er­at­ing sys­tems from the same disk.

Sep­a­rates the op­er­at­ing sys­tem and pro­gram files from the user files.

Cre­ates sep­a­rate ar­eas for OS vir­tual mem­ory swap­ping. Lim­its disk us­age to im­prove per­for­mance of di­ag­nos­tic tools and backup imag­ing.

Disk par­ti­tion­ing fa­cil­i­tates bet­ter util­i­sa­tion and man­age­ment of disk space. This ar­ti­cle goes into the ba­sics of disk man­age­ment. It also is a guide to log­i­cal vol­ume man­age­ment.

Fig­ure 1: The cre­ation of a par­ti­tion of 300MiB

Fig­ure 2: File sys­tem of the par­ti­tion has been cre­ated

Fig­ure 3: The file sys­tem has been mounted to the /share direc­tory

Fig­ure 5: The cre­ation of a swap par­ti­tion

Fig­ure 4: The en­try of the fstab file for per­ma­nent mount­ing of a file us­ing UUID

Fig­ure 10: Log­i­cal vol­ume size has been ex­tended to 500MiB

Fig­ure 7: The cre­ation of a phys­i­cal vol­ume

Fig­ure 9: The cre­ation of an LVM (of size 200MiB), named mylvm from the vol­ume group myvol

Fig­ure 6: The cre­ation of a phys­i­cal disk /dev/vdb1 of size 1GiB

Fig­ure 8: The cre­ation of a vol­ume group named myvol

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.