- LFCS: Partitioning Storage Devices, Formatting Filesystems and Configuring Swap Partition – Part 4
- Partitioning Storage Devices
- Managing MBR Partitions with fdisk
- Managing GPT Partitions with gdisk
- Formatting Filesystems
- Creating and Using Swap Partitions
- Conclusion
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Landoflinux
- Creating filesystems on a Linux System
- Partitioning
- Disk and Linux
- Disk naming conventions
- IDE Devices
- SCSI Devices
- Paravirtual Devices
- Primary Partitions
- Extended Partitions
- Logical Partitions
- Example of a Partition Layout
- Partitioning a new disk with fdisk
- Creating filesystems on partitions
- Mount Points
LFCS: Partitioning Storage Devices, Formatting Filesystems and Configuring Swap Partition – Part 4
Last August, the Linux Foundation launched the LFCS certification (Linux Foundation Certified Sysadmin), a shiny chance for system administrators to show, through a performance-based exam, that they can perform overall operational support of Linux systems: system support, first-level diagnosing and monitoring, plus issue escalation – if needed – to other support teams.
Linux Foundation Certified Sysadmin – Part 4
Please aware that Linux Foundation certifications are precise, totally based on performance and available through an online portal anytime, anywhere. Thus, you no longer have to travel to a examination center to get the certifications you need to establish your skills and expertise.
Please watch the below video that explains The Linux Foundation Certification Program.
This post is Part 4 of a 10-tutorial series, here in this part, we will cover the Partitioning storage devices, Formatting filesystems and Configuring swap partition, that are required for the LFCS certification exam.
Partitioning Storage Devices
Partitioning is a means to divide a single hard drive into one or more parts or “slices” called partitions. A partition is a section on a drive that is treated as an independent disk and which contains a single type of file system, whereas a partition table is an index that relates those physical sections of the hard drive to partition identifications.
In Linux, the traditional tool for managing MBR partitions (up to
2009) in IBM PC compatible systems is fdisk. For GPT partitions (
2010 and later) we will use gdisk. Each of these tools can be invoked by typing its name followed by a device name (such as /dev/sdb).
Managing MBR Partitions with fdisk
We will cover fdisk first.
A prompt appears asking for the next operation. If you are unsure, you can press the ‘m‘ key to display the help contents.
fdisk Help Menu
In the above image, the most frequently used options are highlighted. At any moment, you can press ‘p‘ to display the current partition table.
Show Partition Table
The Id column shows the partition type (or partition id) that has been assigned by fdisk to the partition. A partition type serves as an indicator of the file system, the partition contains or, in simple words, the way data will be accessed in that partition.
Please note that a comprehensive study of each partition type is out of the scope of this tutorial – as this series is focused on the LFCS exam, which is performance-based.
Some of the options used by fdisk as follows:
You can list all the partition types that can be managed by fdisk by pressing the ‘l‘ option (lowercase l).
Press ‘d‘ to delete an existing partition. If more than one partition is found in the drive, you will be asked which one should be deleted.
Enter the corresponding number, and then press ‘w‘ (write modifications to partition table) to apply changes.
In the following example, we will delete /dev/sdb2, and then print (p) the partition table to verify the modifications.
fdisk Command Options
Press ‘n‘ to create a new partition, then ‘p‘ to indicate it will be a primary partition. Finally, you can accept all the default values (in which case the partition will occupy all the available space), or specify a size as follows.
Create New Partition
If the partition Id that fdisk chose is not the right one for our setup, we can press ‘t‘ to change it.
Change Partition Name
When you’re done setting up the partitions, press ‘w‘ to commit the changes to disk.
Save Partition Changes
Managing GPT Partitions with gdisk
In the following example, we will use /dev/sdb.
We must note that gdisk can be used either to create MBR or GPT partitions.
Create GPT Partitions
The advantage of using GPT partitioning is that we can create up to 128 partitions in the same disk whose size can be up to the order of petabytes, whereas the maximum size for MBR partitions is 2 TB.
Note that most of the options in fdisk are the same in gdisk. For that reason, we will not go into detail about them, but here’s a screenshot of the process.
gdisk Command Options
Formatting Filesystems
Once we have created all the necessary partitions, we must create filesystems. To find out the list of filesystems supported in your system, run.
Check Filesystems Type
The type of filesystem that you should choose depends on your requirements. You should consider the pros and cons of each filesystem and its own set of features. Two important attributes to look for in a filesystem are.
- Journaling support, which allows for faster data recovery in the event of a system crash.
- Security Enhanced Linux (SELinux) support, as per the project wiki, “a security enhancement to Linux which allows users and administrators more control over access control”.
In our next example, we will create an ext4 filesystem (supports both journaling and SELinux) labeled Tecmint on /dev/sdb1, using mkfs, whose basic syntax is.
Create ext4 Filesystems
Creating and Using Swap Partitions
Swap partitions are necessary if we need our Linux system to have access to virtual memory, which is a section of the hard disk designated for use as memory, when the main system memory (RAM) is all in use. For that reason, a swap partition may not be needed on systems with enough RAM to meet all its requirements; however, even in that case it’s up to the system administrator to decide whether to use a swap partition or not.
A simple rule of thumb to decide the size of a swap partition is as follows.
Swap should usually equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.
M = Amount of RAM in GB, and S = Amount of swap in GB, then
Remember this is just a formula and that only you, as a sysadmin, have the final word as to the use and size of a swap partition.
To configure a swap partition, create a regular partition as demonstrated earlier with the desired size. Next, we need to add the following entry to the /etc/fstab file (X can be either b or c).
Finally, let’s format and enable the swap partition.
To display a snapshot of the swap partition(s).
To disable the swap partition.
For the next example, we’ll use /dev/sdc1 (=512 MB, for a system with 256 MB of RAM) to set up a partition with fdisk that we will use as swap, following the steps detailed above. Note that we will specify a fixed size in this case.
Create Swap Partition
Enable Swap Partition
Conclusion
Creating partitions (including swap) and formatting filesystems are crucial in your road to Sysadminship. I hope that the tips given in this article will guide you to achieve your goals. Feel free to add your own tips & ideas in the comments section below, for the benefit of the community.
Reference Links
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Landoflinux
Creating filesystems on a Linux System
A filesystem is an organization of data and metadata on a storage device. Basically filesystems contain files, directories and sub directories. Linux has a tree like structure with the «/» as its root directory. Each partition that you create on a block device or disk will have a filesystem added to it. The tree view structure is created when you mount a filesystem on a device at a point known as a «mount point«. This will become clearer in the examples that follow.
The general layout of directories within this tree like structure are covered in our section File Hierarchy Standard.
Partitioning
Partitioning is the name given to the slicing up of a hard disk into slices or partitions. Each partition is created to a given size, no partition may overlap. Any space that is not used on a disk is classed as free space. Linux supports many different partition formats, however the most common type on a Linux PC is the MS-DOS format. This format supports up to four primary partitions. One of these primary partitions can be replaced with an extended partition which can contain up to twelve logical partitions. This gives us fifteen usable partitions as we do not count the extended partition as it is not usable for normal data. In the examples that follow we will be using the above format, however, a format does exist for larger drives that will allow for more partitions to be used. This format is known as the «GUID Partition Table» or «GPT«. This format can support up to 128 partitions by default.
Disk and Linux
The Linux operating system can support many different types of disk. Linux can handle IDE, SCSI, Paravirtual devices, CDs, DVDs and removable media such as USB memory sticks and external drives. The most common type of disks are IDE and the newer SATA disks. The original IDE standard could support two disks on one connection. These connections were known as «Master» and «Slave». Many PCs also had a second IDE connection giving access to two more «Secondary» devices. Another not so popular type of disk used by PCs was «SCSI». Although these were often much faster, they also had a much higher price. In the last few years a newer standard has emerged called «SATA». SATA is a modern version of IDE. However, under Linux they will have the same naming standard used by SCSI disks (SCSI emulation layer).
Disk naming conventions
IDE Devices
/dev/hda
Primary Master IDE
/dev/hdb
Primary Slave IDE
/dev/hdc
Secondary Master IDE
/dev/hdd
Secondary Slave IDE
SCSI Devices
/dev/sda
First SCSI Drive
/dev/sdb
Second SCSI Drive
/dev/sdc
Third SCSI Drive
Paravirtual Devices
Paravirtual devices are how disks are presented on Virtual systems. When running a virtual machine, the virtual environment has to present devices to the guest OS. Examples of these type of disks can be seen below in the example of creating filesystems.
/dev/vda
/dev/vdb
Primary Partitions
As mentioned earlier there can be up to four primary partitions on a disk, these are normally numbered as:
/dev/hda1
/dev/hda2
/dev/hda3
/dev/hda4
Extended Partitions
An extended partition takes one of the allocations that a primary partition could take. However, the extended partition can not contain usable data. Instead it contains references to logical partitions. You can have a maximum of 12 logical partitions. Only one extended partition is allowed on a physical disk.
Logical Partitions
A logical partition exists within an extended partition. Logical partitions are numbered from five through to sixteen (12 in total).
Example of a Partition Layout
To display your current partition layout you can issue the command «fdisk -l» as «root«. The example below shows a very simple layout utilising one physical disk:
From the above we can see we have a virtual machine running with a single 20GB disk.
Partitioning a new disk with fdisk
fdisk is one of the most popular partitioning tools that is bundled with most Linux distributions. To work with fdisk you will need to be running with escalated privileges or as root. «fdisk» is started by typing fdisk followed by the device you are going to be working with.
!! WARNING — fdisk is a very powerful command and can completely destroy a valid working partition. This command should only be used if you are 100% sure of what you are doing.
Example of fdisk’s main menu screen
In the following examples I will be using a test server running under «QEMU/KVM«. You can also use «VirtualBox«. For testing I have created a 10Gb disk which we will divide into smaller chunks.
First let us take a look at our current configuration. To do this we issue the «fdisk -l» command as «root»:
From the above we can see that there are two devices on our CentOS 8 system. «/dev/vda» and «/dev/vdb». The first device «/dev/vda» is a 20GB disk which contains two partitions «vda1 and vda2». The second device «/dev/vdb» is 10GB in size and has no partition table. This is the disk we are going to partition.
In the following example, we are going to create five partitions on our 10GB disk.
I will be creating 3 Primary partitions and one extended partition. As we have identified our 10GB disk as: /dev/vdb. This is the name we specify when we launch «fdisk
Command issued: fdisk /dev/vdb
Notice in the above, we used the «P» command to print the partition table, and we used the «W» command to write the changes to disk.
Creating filesystems on partitions
The next step to making our new partitions usable is to format these with a filesystem of our choice. The command for creating a filesystem is «mkfs«.
The basic syntax is: «mkfs [options] [-t type fs-options] device [size]«.
Now we must create our filesystems on our new partitions. Only the primary and logical partitions require a filesystem. We do not need to use the «mkfs» command on the Extended partition.
We have used the command «mkfs.ext4 /dev/vdb1» to create a filesystem type of Ext4 on partition vdb1. We have repeated this process with each of our partitions (vdb2,vdb3,vdb5 and vdb6). Once we have created our filesystems we will need to create a mount point.
Mount Points
A mount point is basically an empty directory that is attached to the current filesystem. Once created we can attach our new filesystems to this point.
When we mount a filesystem we are basically indicating to the system which partition/device we want to mount at a given mount point. The basic syntax for mounting is:
mount /dev/vdb1 /test1.
Here «/dev/vdb1» is our device and «/test1» is an empty directory we shall use for our mount point.
Before we can issue our mount command, we need to create some directories that we can use for our mount point:
Command used for creating our mount points: mkdir test
Next we issue the mount commands to mount our filesystems:
From the above output, we can now see that our filesystems have been successfully mounted. We used the command «df -hT» to verify this.
And finally we need to add our handy work into a location «/etc/fstab». This will then automatically mount our filesystems when the system gets rebooted.
Add the following into /etc/fstab for auto mounting:
For more information on how to add our devices into «/etc/fstab«, click on the following link: Mounting Filesystems.
As a test, we can unmount our filesystems and then re-mount them using the entries we have added into the mount table «/etc/fstab«.
Command issued to unmount: umount /test1 /test2 /test3 /test4 /test5«
You can verify your filesystems are no longer mounted by issuing the command: df -h | grep test
Now to remount, we can use the command: «mount -a«. This command is very useful as it allows you to mount any filesystems that are not currently mounted from entries within the «/etc/fstab» mount table.
Источник