Sie sind auf Seite 1von 15

Chapter 4 Disk partitioning and mounting

Introduction
We already know how to manage users and software packages. But where is all the related
information stored? Data is stored on internal devices like HDDs, SSDs, but it also possible to
use network accessible devices like Network Attached Storage (NAS) or Storage Area
Network (SAN). All those devices are using an interface to run the transactions requested by
the OS and the responsible kernel modules. Examples of interfaces are:

PATA (Parallel Advanced Technology Attachment) old standard, found on legacy


devices or embedded controllers
SATA (Serial ATA) a standard that improved over time and is very used
SCSI (Small Computer System Interface) is a high performance interface used that
was the base for newer technologies
SAS (Serial Attached SCSI) mainly used in servers. Can support very high rotational
speed for drives
iSCSI (Internet SCSI) used to access remote storage over IP. Used for accessing
high performance storage devices over network

Why is important to know about those interfaces? Well, one of the most important things
when you work with a drive is to identify the file from /dev folder associated with it. There is
a well-known quote: In Linux, everything is a file. Therefore, you will see that storage
devices and partitions are represented as a file. For example, /dev/hda is the first PATA drive
of the system, while /dev/sdb is the second disk attached to the Serial interface of the
machine. When working with Virtual Machines, you will see /dev/vd* files associated with
VM's HDD. This happens when the VirtIO driver is used inside the VM to enable
paravirtualization.

Virtualization is an interesting topic, but far from our purposes new. In the following pages
we will discuss about storage devices, partitions, file systems and how to use them in Linux.
The bonus will be using quotas: how can an administrator limit the storage space used per
user or per group.

Disks and partitions


Everything stored on a Linux system must be finally written to a physical drive, whether it is
installed in the local machine or it is accessible over network. Storage devices ca be fixed or
removable and this defines how they are connected to computers.

On desktop computers you will usually see SATA and USB HDDs. As both of them are
attached to serial interfaces, you will identify the as /dev/sd* entries.

On servers, things look a bit different. Here you can find SATA/SAS drives (/dev/sd*), but
you can also face RAID controllers. RAID is used to combine more drives in an array of disks
that looks like a single storage device. For example, on my IBM server, the two 72 GB drives
are combined in a RAID 1 (mirroring) array of 72 GB. The nice thing is that the hardware
controller will show only one single /dev/sda disk to the running OS, so it masks all the RAID
technology behind a single logic entry. But be aware that not all controllers do this, so you
should read the documentation provided machine's vendor.

To make things simple, in a Linux box you will see one entry per drive, a virtual file in /dev
folder.

Here we can see two entries associated with disk drives: /dev/vda (VirtIO disk) and /dev/sda
(SATA drive). If I add another SATA disk I get:
See the /dev/sdb entry? That is associated with the second serial drive attached to the
computer. But what about the /dev/vda1 and /dev/vda2 entries? Well, these entries are
associated with the first and second partitions created on /dev/vda disk.
First of all, remember that partitions represent divisions of storage space. And also remember
that MBR is the first sector of the drive (512 bytes), where partitions are defined. In fact, the
partition table has only 64 bytes and can define at most 4 partitions. The partitions fully
defined here are considered primary partitions. However, as 4 partitions proved to be
insufficient, it is possible to transform the fourth entry in a so-called extended partition. An
extended partition is just a container for other usable partitions: the logical partitions.

Let's check the output of fdisk -l command used to list the partitions from a drive:

First of all, let's explain the units used in Start and End columns: cylinders. The cylinders
are an ordered internal division of the hard drive and have the size described on the third line
of output. Now look at the first 3 entries: nothing special, the partitions do not overlap and all
of them are standard primary Linux partitions. What about the fourth one? Check the limits:
starts from cylinder 43 and ends on cyclinder 130 the last cylinder available (check second
line of output). This one is the extended partitions. The /dev/sdb5 and /dev/sdb6 are logical
partitions that can be actually used for formatting and storing data.

So, the command used to display the partition table from a drive is fdisk, with -l flag:

fdisk -l <drive_file>

With fdisk we can list partitions, but we can use the same tool for creating partitions.
However, is it enough to just create partitions to use the storage space? Well, no. Unless you
perform raw data storage (far away from the purpose of this chapter), you need to format the
partitions. Formatting refers to the process of writing a file system on a partition. During the
format process, the target partition is inspected and data structures are written on a dedicated
area. The file system itself is a logical layer that manages the storage space available and
resolves kernel requested operations.

Few general ideas about the relationship between partitions and file systems:

partitions cannot be mounted if they are unformatted, so you can't read/write data
the kernel must support the file system used (if you kernel does not support a file
system, search for available kernel modules)
if you modify partition parameters (partition resize, move, etc.) you should always
update the file system from the partition

If you want to see the partition used and check the free storage space, use :

df -h

Without the -h flag, the units used in the command output is 1 Kbyte block
Now that we know basic details about partitions, let's move to file systems.

File systems
Partitions cannot be mounted and used before being formatted with a file system supported by
the operating system. During the install process (Disk Druid is the application responsible for
disk layout), we used ext4 (4th extended file system) for our partitions and it is time to know
more about our options. We will describe briefly some file systems that can be used in a
Linux system: the extended family, ReiserFS, XFS and JFS. Linux also supports mounting
FAT and NTFS partitions (used on Windows-powered computers), as well as network file
systems like SMB/CIFS (Windows Share) and NFS (Network File System).

You will hear often about journals and journalization when talking about file systems. A file
system that uses jurnalization behaves like this: when value A should be written on block X, a
message like Write A to X is stored before actually performing the action in a dedicated
area on the partition called journal. Only after that, the OS will attempt to write data. If
anything goes wrong (power or drive failures, etc.) the system will be able to either complete
the transaction or fully revoke them. Once an entry from the journal is completed, it can be
overwritten.

Let's check some details about the major file systems that can be found in Unix/Linux.

Ext 2
does not have journalization (reduces drive write access, which is good for storing
devices that have a limited number of write cycles, like SD cards)
you must manually fix file system errors using fsck
introduced the general file system structure used by the extended file system family

Ext 3
basically, it is ext2 with added jurnalization
relies on the ext2 design, considered stable and tested
resource efficient
default file system for RHEL/CentOS 5 partitions

Ext 4
latest ext file system
maximum file size of 16TB
uses delayed allocation and pre-allocation
aggressive caching strategy for improved performance
used by Android
default file system for RHEL/CentOS 6 partitions

XFS
high performance file system
used for lots of parallel I/O operations
can perform resize on a mounted partition
maximum file size: 9 ExaB (64 bit machine)

JFS
developed by IBM for their own systems
works only on 64 bits machines
maximum file size: 4 PB (peta B)

ReiserFS
works very well with lots of small files
low fragemtation
however, the file system recovery is difficult in case of failures
can perform resize on a mounted partition

Extended file system family


The design of ext file system is simple and considered tested and reliable. It focuses on few
data structures that will be described bellow.

Inode
The inode has a central role in the EXT architecture. It is associated with a content: a set of
data blocks. But it is not associated with a path or a file name. The inode also contains the
following informations, which are associated with the content:

ownership information
permissions
access, modification and change time

Use stat command to extract information from an inode associated with a file:

Dentry objects
Dentry object associated the following details:

one file
a directory
the inode associated with that file

Superblocks
The superblocks represent the information required by the OS to identify the file system type
and to mount the partition correctly. The data from the superblock is first loaded when the
driver mounts an ext partition and supports identifying the other relevant data.

A basic scheme of the architecture is presented bellow:


Partitioning and formatting in Linux
Partition management is very simple in Linux and can be performed using many tools.
However, for simplicity we will use the simple fdisk tools for creating partitions and mkfs for
writing file systems on partitions.

It is crucial to correctly identify the /dev entry associated with your storage device. Also
please pay attention to what are you doing, because any mistake can compromise important
data stored on your hard drive. Note that fdisk will actually flush changes on the disk only
when you will issue the write command. However, mkfs will format the partition without
allowing any recovery.

You already know that using fdisk -l will list the partition table from that device. If the -l
switch is removed, fdisk will start in an interactive mode with single character commands.
See the help output:
So, to create a new partition, just press the 'n' key, answer the following questions and don't
forget to 'w' (write) data to disk. Without writing data to disk, fdisk will not perform any
changes in the partition table.
Now we have the /dev/sda1 entry associated with the new partition. Let's format it with ext4
file system.
There are other tools that can perform the roles of fdisk and mkfs, but the outcome is the
same. However, you should take a look at gparted or the GUI tool available under System
Administration menu, when the full General Purpose Desktop group is installed.

Mounting partitions
Now that we have created a partition we should be able to use it. But, before writing data to it,
we have to mount the partition in the file system structure. For example, I will mount the
/dev/sda1 partition to /opt folder, write some data, than unmount it.

To mount a partition use:

mount <partition> <mountpoint>

To unmount a partition:

umount <partition> or umount <mountpoint>

Using mount without any argument or option will list the mounted partitions

Using mount from the CLI will temporary mount your partition. However, after a reboot or a
shutdown, the partition will not be re-mounted by the system, unless you configure it to do so.
To do this, you will have to edit the /etc/fstab file.

An entry (one row) from this file has the following fields:

partition identifier (can be the /dev entry, UUID or LABEL)


mountpoint
file system type
mount options
0 or 1 flag to identify if the partition content should be backed-up by dump (1 do
backup, 0 don't backup partition contents)
file system check order

The partition identifier used here is the UUID. This is a string generated when the partition is
formatted and uniquely identifies it in the system. To get the UUID of a partition, use blkid
command.

blkid <partition>

It is also possible to assign a label to a partition and identify it by this string. Use e2label for
this:

e2label <partition> <label>

If the <label> argument is missing, the command will display the existing label, if set.
Check the way that I used findfs to verify if the system will correctly identify my partition if I
supply the LABEL=myopt identifier in the fstab command. You can use findfs UUID=... to
check UUID identifiers also.

The fstab entry for persistent mount should look like:


To test it, reboot your machine. It is also possible to use mount -a. This will re-read the fstab
and will mount the partitions listed here that are not already mounted.

Quotas
Quotas should be used to limit the storage space used by users or groups. It requires the
existence of the quota package and requires two additional mount options to be active. The
following paragraphs will walk you through the process of setting up quotas.

Install quota package with yum:


yum install quota

Add to fstab usrquota,grpquota options for each partition:

UUID=12917a66-24c2-47bc-8aa6-9b7d36a5614d /home ext4 defaults,usrquota,grpquota

Remount partition
mount -o remount /home

Create quota files. This command checks the files on the partition. It might pe necessary to
add the m option to force checking.

quotacheck -cug /home

Generate usage tables. The m switch might be useful for forcing the check.

quotacheck -avug

Configure quota for one user. Note that for quota, block size unit is 1 Kb.

edquota usr1

Print quota state

quotaon -p -a

Activate quota

quotaon -ugv /home/

Report quota state to check that previous command was configured successfully.

repquota -a

Optional: You can schedule with cron the following command to maintain the quota tables
up-to-date:

quotacheck -avug
NOTE: Due to bug 703871 reported to RedHat for RHEL 6, when configuring quotas for
partitions mounted in other folders than /home it might be necessary to change the SeLinux
context before updating/creating quota files:

[root@localhost tmp]# chcon --reference=/var /opt/

This lab concludes chapter 4 and the basic storage concepts. After following this document
you should be able to create and format partitions, perform temporary and persistent mounts
and configure quotas.

Das könnte Ihnen auch gefallen