In this lesson, we're going to learn
about partitions and their use
in the Linux operating system.
Aside from managing user access to your Linux systems,
one of the most foundational tasks
is the management of data storage.
There are many different ways to divide, format,
or otherwise organize the data
that's going to be stored on your systems.
And Linux gives you many different tools for doing this.
Using these tools, you'll ultimately make it easier
for authorized users to work with data.
There are many different types of storage devices
that are supported by the Linux operating system.
Common types include hard disk drives,
they use magnetic storage technology to store your data.
Solid-state drives,
they use non-mechanical, solid-state technology
to store your data.
USB thumb drives, they use flash memory technology
to store your data.
And external storage drives,
they use several different types of technologies,
and usually connect to the computer
through a peripheral interface
rather than being internally connected.
In Linux, devices are referred to
as either block or character devices.
Block devices are things like storage devices,
these are things like hard drives,
solid-state drives, and USB drives.
And all of these can be read from
or written to in blocks of data.
Character devices on the other hand are devices
like keyboards, mice, serial ports,
and other things like that that can read from
or write to streams of data one character at a time.
A file system is a data structure
that's going to be used by an operating system
to store, retrieve, organize, and manage files
and directories on a given storage device.
It also maintains information about the data
such as the date of creation
and the modification of individual files,
the size of the files on that storage device,
the type of files,
and the permissions associated with those files.
But the operating system does not interpret
the data contained in the files itself
because this task is handled by specific applications.
The file systems vary depending on several different things,
such as the purpose of the file systems,
the information they store about the individual files,
and the way they store data
as well as the data security mechanisms
that are going to be implemented.
Linux systems support several different types of systems,
including FAT, ext2, ext3, ext4, XFS, and BTRFS.
These file systems can be used for varying situations
depending on your specific user needs.
FAT or the File Allocation Table
is an older file system that is compatible
with many different operating systems,
including all versions of Unix, Windows, and Mac OS X.
It does not provide the same capabilities
as more modern file systems,
and it's typically going to be used
when you need to maintain compatibility
between all these different operating systems.
Ext2 on the other hand is a native Linux file system,
and it's using some older releases
and it's still supported in your current Linux releases.
Ext3 is a more modern version of a native Linux file system.
Ext3 is much faster in recovering data,
and it does a better job of ensuring data integrity
in the case of abrupt system shutdowns than ext2 did.
Another default file system in Linux distributions is ext4,
and this supports really large volumes,
up to one exabytes of data,
and it also supports file sizes
of up to 16 terabytes in one file.
XFS is a 64-bit, high-performance journaling file system
that provides fast recovery
and can handle really large files efficiently as well.
The last type of file system we have is known as BTRFS.
BTRFS is a modern, copy-on-right file system for Linux
that's designed with advanced features
in order to provide fault tolerance, repair,
and easy administration of your file system.
BTRFS supports volumes of up to 16 exabytes in size,
files of up to 16 exabytes in size,
and it supports a maximum
of 18 quintillion files on each volume.
Some file systems function as network protocols as well,
and this enables the sharing of data over a network.
For example, the SMB protocol,
also known as Server Message Block,
is going to allow users to share access to files
and other resources over a local area network
which is known as a LAN.
SMB clients make requests for resources to SMB servers,
and these servers will respond
and provide the appropriate level of access.
In addition to SMB, we also have something known as CIFS.
CIFS is the Common Internet File System
and it's a specific implementation
of SMB that is rarely in use today.
Instead, Microsoft designed it
as a successor to SMB version 1,
but SMB version two and three have long since replaced
version one and CIFS.
However, some Linux distributions will still use
the name CIFS in some of the tools
when they're referring to tools that support SMB,
so it is something you need to be aware of.
Another protocol you need to be aware of is NFS,
NFS stands for the Network File System.
The Network File System offers similar functionality to SMB,
but NFS and SMB are not compatible with each other.
If you're going to be using all Linux clients
and servers in an environment,
then you'll probably want to use NFS
because it actually has some better features than SMB.
But if you're going to use a mixture
of Windows and Linux systems,
you want to rely on SMB
because SMB is the default inside of Windows
and is supported right out the bat,
where NFS is not supported by Windows by default.
Another thing we need to talk about
is something known as an index node,
this is also abbreviate as an Inode.
An index node is an object that stores metadata
about a file or directory on a given file system.
This metadata can include time-based values
like when the file was created and last modified,
it can have permission and ownership information.
It can have the block locations
of the files data on a storage device
and other pieces of miscellaneous information.
In instances where there is an unexpected interruption,
the file system can actually quickly recover itself
through journaling because it records changes
to those files that have not been made
to the file system yet inside of this Inode.
The journaling process involves describing
all the changes that must be made to the drive,
the background processing,
performing pending changes when rebooting,
and discarding incomplete entries.
Another type of file system you need to be aware of
is known as a Virtual File System or VFS.
A Virtual File System is going to be created
as a common software interface that sits
between the kernel and the real file system.
In effect, the VFS is going to translate
a real file systems details over to the kernel.
That way, the file system appears to be identical
to any other file system.
With VFS, you can mount many different types
of file systems on the same Linux installation,
and they're all going to appear uniform to the user
and all the applications on that system.
File system labels are going to be assigned
to those file systems for easy identification by VFS.
These labels may be up to 16 characters long,
and they can be displayed or changed
using the e2label command for ext-based file systems,
and the xfs_admin command for XFS-based file systems.
The first task in managing storage
is to partition a storage device
and format the partition with one of these file systems.
This will make the section of the device
available for reading and writing of the data
once you've formatted it.
When I use the term, partition,
this simply means it's a section of the storage drive
that is logically acting as a separate drive.
It enables you to convert a large drive
into smaller, more manageable chunks
leading to a better organization of your data.
There are three types of partitions you can use
and each one has specific features.
We have primary partitions,
extended partitions, and logical partitions.
Primary partitions contain one file system or logical drive,
and it's sometimes referred to as a volume.
The swap file system and the boot partition
are normally created in the primary partition.
Linux likes to push some of its unused files from RAM
to the swap space to free up actual physical memory,
this goes to what we call the swap space.
This swap space is simply a partition on the storage device
that's going to be used when the system runs out
of physical RAM or physical memory.
An extended partition can contain
several file systems as well,
these are referred to as logical drives.
This partition type does not contain any data,
and it has a separate partition table.
The logical partition is a part of the physical drive
that's been partitioned off
and allocated as an independent unit
and functions as a separate drive.
There's also going to be times
when you need to create a new partition table
or modify existing entries on the partition table,
to do this, you're going to use the fdisk utility.
This is a text-based, menu-driven program
that's used to create, modify,
or delete partitions on a given storage drive.
Fdisk will also enable you to specify
the size of those partitions.
The fdisk utility supports a number of command line options
including things like option -b.
If you use option -b and then the sector size,
you're going to be able to specify the number of drive sectors.
If you use option -H, heads,
this will be used to specify the number of drive heads.
If you use option -S, sectors,
this is going to be used to specify
the number of sectors per track.
If you use option -s, with a lowercase s,
and then the partition, this will be used
to print out the partition size in blocks.
The option -l is going to be used to list
all the partition tables for a given device.
Aside from supplying different command line options,
you can also choose various options
when you're working in the fdisk menu itself
including the options of N, D, P, W, and Q.
Option N is going to be used to create new partitions.
Option D is going to be used
to delete or remove a partition.
Option P is used to list out the existing partitions,
almost like a print-to-screen function.
Option W is going to be used to write the changes
to the drive and exit the utility.
Option Q is going to be used to cancel the changes made
and exit the utility as well.
Now, in addition to fdisk,
there's another utility we can use to manage our partitions,
this is known as the GNU Parted command.
The GNU Parted command is going to be particularly useful
when you're creating partitions on new storage drives.
This tool can be used to create,
destroy, and resize partitions.
The Parted command runs the GNU Parted utility,
and just like fdisk,
Parted includes a menu-driven interactive mode
where you can input various options.
The option, select, is going to be used
to choose which device or partition you want to modify.
The option, mkpart, is going to be used to create a partition
or make a partition with a specific file system type.
Option, print, is going to be used
to list out the partition table to the screen.
Option, resizepart, is going to be used to resize
or modify the end position of a given partition.
Option, rm, is going to be used to delete a partition.
And option, quit, is going to be used
to exit the GNU Parted utility.
After you create a partition,
you can't add a file system to that partition
unless the kernel can read it from the partition table.
So, instead of rebooting the system,
it's better to run the command, partprobe,
after you run fdisk or the Parted command.
If you do this, it makes the kernel aware
of your new partition configurations that you just made.
The partprobe command is going to be used
to update the kernel with the changes
that now exist within the given partition table.
The command first checks the partition table,
and if there's any changes,
it automatically updates the kernel with those changes.
Another important command to know
is the make file system command which is written as mkfs.
The mkfs command is used to build a Linux file system
on a device which is normally going to be a drive partition.
We're going to talk about some of the options
of the mkfs command here,
and give you some of their basic descriptions.
If you want to learn more, you can always type, man mkfs,
at your command prompt and learn more about it.
First, we have option -v,
this is used to produce verbose output
where the output message is going to keep updating
constantly as the program is processing.
If instead you use option -V,
this will produce verbose output as well,
but it'll also include all the file system specific commands
that are being executed.
If you use option -t and the file system type,
you're going to be specifying the type of file system
you want built on this computer.
If you use option, fs -options, it's going to be used to pass
the file system specific options to the file system builder.
If you use option -c, it's going to be used to check the device
for any bad blocks before it builds the file system.
If you use option -l and the file name,
it's going to actually read the list
of bad blocks from a specified file
that you've already run this command against.
So, when it comes down to the make file system command,
there's really two ways to run it.
The first is to type mkfs, set up your options,
and then enter your device name.
The second way is to use mkfs putting your file system type,
your options, and the device name.
Another thing we need to cover is the fstab file.
The fstab file is a configuration file
that stores information about storage devices and partitions
and where and how those partitions should be mounted.
The fstab file is going to be located in the /etc directory.
It's going to be read by the system during the boot up process
and it can be edited only by your root user.
The fstab file consists of a number of lines,
one for each file system that the system knows about.
Each line has six fields
that are going to be separated by spaces or tabs.
The first field is the Device or Partition Name,
and it specifies the name of the device
or file system that you want to mount.
The Default Mount Point indicates
where the file system is going to be mounted,
so it might be, slash,
for the root file system as an example.
The File System Type will also specify
the type of file system used by that device or partition.
The Mount Options field specifies a set
of comma-separated options that will be activated
when the file system is mounted.
The Dump Options field indicates
if the Dump utility should back up the file system.
The fsck Options field will specify the order
in which the fsck utility or the File System Checker utility
should check the file system.
Another file we need to talk about
is located in the /etc directory
and it's known as C-R-Y-P-T-T-A-B, or crypttab.
The /etc/crypttab file performs
a similar function to the fstab file,
but its purpose is to store information
about encrypted devices and partitions
that have to be unlocked and mounted on system boot.
Its format is going to be similar to the fstab file,
but it includes an optional password field
for unlocking encrypted devices.
As we've talked about all these different utilities,
the overall process of setting up
storage devices for use on Linux machines
includes just a few key things you need to do.
First, you need to partition the storage device
using a tool like fdisk or Parted.
Then, you need to format the partition using a file system,
and this will be done using something
like mkfs or the Make File System tool.
Then, you need to add the format of partition
to the fstab file so it can be configured
by the system to run at boot up.
In Linux, everything is going to be treated as a file,
even a directory or an entire disk volume or partition.
When you look at a file system, you're going to see it
written out as a directory known as /dev, for device.
A /dev directory is a special file
that contains details about all the files
and subdirectories that are housed within it.
Linux uses naming conventions
so that storage devices are easily located by the system
whenever they're attached and when the system boots up.
For storage devices, the naming convention
is typically expressed in three parts,
let's take a look at an example.
If I have a device called /dev/sda1,
this actually tells me a lot about this device.
The first part of that, the sd portion,
refers to the specific type of controller
that that device is using.
The second portion, the a,
is going to refer to the first whole drive
because, a, is the first letter of the alphabet.
Then, if we had a second drive, it would be b,
if we had a third drive, it would be c, and so on.
The third part of this is the number one,
and the number one refers
to the first partition on this drive.
If we had two partitions or three partitions,
we would have a two or a three here.
So, when I have something like /dev/sda1,
we know we have the first storage device
and the first partition on that storage device
because it is sda1.
In addition to using this naming convention,
Linux also has several different persistent naming schemes
to help us identify devices.
Controller-based naming can sometimes be problematic
in systems with more than one type of the same controller.
So, instead, we can use a persistent naming scheme
to overcome this and make it more predictable.
If I'm using a persistent naming scheme
that's based on the device's hardware serial number,
I'm going to do this by using /dev/disk/ID number.
If I'm using a persistent naming scheme
that refers to an identifier
based on the shortest physical path to the device,
I might use something like /dev/disk/path.
Finally, if I'm going to use a persistent naming scheme
based on the universally unique identifier, or UUID,
I would write this as /dev/disk/UUID.
And this would refer to that universal unique identifier
that's assigned to the device
when the file system was first created on it.
The last thing we need to discuss
is some special character devices
that exist under the /dev directory.
This includes a /dev/null, /dev/zero, and /dev/urandom.
As I mentioned earlier in this lesson,
everything in Linux is created as a file
and mounted on the file system.
Just like we can mount a hard drive
to the /dev/sda1 directory in the file system
in order to interact with it,
we can also use things like /dev/null,
and /dev/zero, and /dev/urandom
which are special character devices
that are installed by default in most Linux distributions.
And each of these are used for a specific purpose.
The /dev/null special character device
is a special type of virtual device
that allows you to discard anything
that you send to it or redirect into it.
Think of the /dev/null as a big black hole,
and any data streams or character streams
that you don't want to deal with or save to a file,
you can simply redirect those into the /dev/null
and it's going to disappear forever.
If you try to read from the /dev/null device,
you're simply going to receive an End of File message
because you've effectively reached the end of that file
because that file is always empty and contains nothing.
The /dev/zero special character device
is going to be used as a special type of virtual device
that'll return a null character anytime you read from it.
If you redirect the output to the /dev/zero device,
it is going to discard the data
just like the /dev/null device did.
But the big difference is,
when you try to read from /dev/zero,
it's going to act differently than /dev/null did.
You're not going to get that End of File back immediately.
But instead, it's going to send you back
the ASCII null character of the hexadecimal value of 0x00.
Why might you use /dev/zero if all it does is send you back
an endless stream of zeros whenever you read from it?
Well, this is actually useful
when you're trying to sanitize a hard drive
by overriding it with a bunch of zeros.
To do this, you can simply use the dd command
which stands for disk duplicator.
You enter, dd if=/dev/zero of=dev/sda1 bs=1GB count=1024.
And this command will overwrite a one-terabyte hard disk
that is mounted as sda1 with a series of zeros
across the entire drive for the entire one terabyte in size
using block sizes of one gigabyte each.
The next special character device
we need to discuss is the /dev/urandom,
/dev/urandom is a special type of virtual device
that'll return a randomized series of pseudorandom numbers.
For example, if you're creating
a program or a script on a Linux system,
and you want to generate random numbers for something
like determining which side of a rolled dice
would be returned by your program,
you can use /dev/urandom to do that.
For example, let's say I wanted to get
five random characters.
I can enter, head -c5 /dev/urandom,
and then hit Enter.
This will return five random characters
from that special character device called /dev/urandom.
As you can see, there are multiple different ways for us
to reference block and character devices on a Linux system
depending on what your use case is.