Linux file system
A linux file system is a structured collection of files on a disk drive or a partition. A partition is a
segment of memory and contains some specific data. In our machine, there can be various
partitions of the memory. Generally, every partition contains a file system.
The general-purpose computer system needs to store data systematically so that we can easily
access the files in less time. It stores the data on hard disks (hdd) or some equivalent storage
type. There may be below reasons for maintaining the file system:
o Primarily the computer saves data to the ram storage; it may lose the data if it gets turned
off. However, there is non-volatile ram (flash ram and ssd) that is available to maintain
the data after the power interruption.
o Data storage is preferred on hard drives as compared to standard ram as ram costs more
than disk space. The hard disks costs are dropping gradually comparatively the ram.
The linux
File system contains the following sections:
o The root directory (/)
o A specific data storage format (ext3, ext4, btrfs, xfs and so on)
o A partition or logical volume having a particular file system.
What is the linux file system?
Linux file system is generally a built-in layer of a linux operating system
Used to handle the data management of the storage. It helps to arrange the file on the disk
storage. It manages the file name, file size, creation date, and much more information about a
file.
301.9k
If we have an unsupported file format in our file system, we can download software to deal with
it.
Linux file system structure
Linux file system has a hierarchal file structure as it contains a root directory and its
subdirectories. All other directories can be accessed from the root directory. A partition usually
has only one file system, but it may have more than one file system.
A file system is designed in a way so that it can manage and provide space for non-volatile
storage data. All file systems required a namespace that is a naming and organizational
methodology. The namespace defines the naming process, length of the file name, or a subset of
characters that can be used for the file name. It also defines the logical structure of files on a
memory segment, such as the use of directories for organizing the specific files. Once a
namespace is described, a metadata description must be defined for that particular file.
The data structure needs to support a hierarchical directory structure; this structure is used to
describe the available and used disk space for a particular block. It also has the other details
about the files such as file size, date & time of creation, update, and last modified.
Also, it stores advanced information about the section of the disk, such as partitions and
volumes.
The advanced data and the structures that it represents contain the information about the file
system stored on the drive; it is distinct and independent of the file system metadata.
Linux file system contains two-part file system software implementation architecture. Consider
the below image:
The file system requires an api (application programming interface) to access the function calls
to interact with file system components like files and directories. Api
Facilitates tasks such as creating, deleting, and copying the files. It facilitates an algorithm that
defines the arrangement of files on a file system.
The first two parts of the given file system together called a linux virtual file system. It provides
a single set of commands for the kernel and developers to access the file system. This virtual file
system requires the specific system driver to give an interface to the file system.
Linux file system features
In linux, the file system creates a tree structure. All the files are arranged as a tree and its
branches. The topmost directory called the root (/) directory. All other directories in linux can
be accessed from the root directory.
Some key features of linux
File system are as following:
o Specifying paths: linux does not use the backslash (\) to separate the components; it uses
forward slash (/) as an alternative. For example, as in windows, the data may be stored in
c:\ my documents\ work, whereas, in linux, it would be stored in /home/ my document/
work.
o Partition, directories, and drives: linux does not use drive letters to organize the drive
as windows does. In linux, we cannot tell whether we are addressing a partition, a
network device, or an "ordinary" directory and a drive.
o Case sensitivity: linux file system is case sensitive. It distinguishes between lowercase
and uppercase file names. Such as, there is a difference between test.txt and test.txt in
linux. This rule is also applied for directories and linux commands.
o File extensions: in linux, a file may have the extension '.txt,' but it is not necessary that a
file should have a file extension. While working with shell, it creates some problems for
the beginners to differentiate between files and directories. If we use the graphical file
manager, it symbolizes the files and folders.
o Hidden files: linux distinguishes between standard files and hidden files, mostly the
configuration files are hidden in linux os. Usually, we don't need to access or read the
hidden files. The hidden files in linux are represented by a dot (.) Before the file name
(e.g., .ignore). To access the files, we need to change the view in the file manager or need
to use a specific command in the shell.
Types of linux file system
When we install the linux operating system, linux offers many file systems such as ext, ext2,
ext3, ext4, jfs, reiserfs, xfs, btrfs, and swap.
Let's understand each of these file systems in detail:
1. Ext, ext2, ext3 and ext4 file system
The file system ext stands for extended file system. It was primarily developed for minix os.
The ext file system is an older version, and is no longer used due to some limitations.
Ext2 is the first linux file system that allows managing two terabytes of data. Ext3 is developed
through ext2; it is an upgraded version of ext2 and contains backward compatibility. The major
drawback of ext3 is that it does not support servers because this file system does not support file
recovery and disk snapshot.
Ext4 file system is the faster file system among all the ext file systems. It is a very compatible
option for the ssd (solid-state drive) disks, and it is the default file system in linux distribution.
2. Jfs file system
Jfs stands for journaled file system, and it is developed by ibm for aix unix. It is an alternative
to the ext file system. It can also be used in place of ext4, where stability is needed with few
resources. It is a handy file system when cpu
Power is limited.
3. Reiserfs file system
Reiserfs is an alternative to the ext3 file system. It has improved performance and advanced
features. In the earlier time, the reiserfs was used as the default file system in suse linux, but later
it has changed some policies, so suse returned to ext3. This file system dynamically supports the
file extension, but it has some drawbacks in performance.
4. Xfs file system
Xfs file system was considered as high-speed jfs, which is developed for parallel i/o processing.
Nasa still using this file system with its high storage server (300+ terabyte server).
5. Btrfs file system
Btrfs stands for the b tree file system. It is used for fault tolerance, repair system, fun
administration, extensive storage configuration, and more. It is not a good suit for the production
system.
6. Swap file system
The swap file system is used for memory paging in linux operating system during the system
hibernation. A system that never goes in hibernate state is required to have swap space equal to
its ram
Size.
**************************************************************
The file system is divided into two segments called User Data and Metadata. In this article, I
am trying to explore how to create and convert various Linux file systems and high-level
differences amongst Ext2, Ext3, and Ext4 file systems.
Before moving further readings, let me introduce a brief about Linux file systems.
Ext2 – Second Extended File System
The ext2 file system was introduced in 1993 and Ext2 was developed by Remy Card. It
was the first default file system in several Linux distros like RedHat and Debian.
It was to overcome the limitation of the legacy Ext file system.
Maximum file size is 16GB – 2TB.
The journaling feature is not available.
It’s being used for normally Flash-based storage media like USB Flash drive, SD
Card, etc.
Ext3 – Third Extended File System
Ext3 file system was introduced in 2001 and the same was integrated with Kernel
2.4.15 with a journaling feature, which is to improve reliability and eliminates the need to
check the file system after an unclean shutdown.
Max file size 16GB – 2TB.
Provide facility to upgrade from Ext2 to Ext3 file systems without having to back up and
restore data.
Ext4 – Fourth Extended File System
Ext4, the high-anticipated Ext3 successor.
In October 2008, Ext4 as stable code was merged in Kernel 2.6.28 which contains
an Ext4 file system.
Backward compatibility.
Max file size 16GB to 16TB.
The ext4 file system has the option to Turn Off the journaling feature.
Other features like Sub Directory Scalability, Multiblock Allocation, Delayed
Allocation, Fast FSCK etc.
GRUB bootloader
GNU GRUB is a bootloader (can also be spelled boot loader) capable of
loading a variety of free and proprietary operating systems. GRUB will work
well with Linux, DOS, Windows, or BSD. GRUB stands
for GRand Unified Bootloader.
GRUB is dynamically configurable. This means that the user can make
changes during the boot time, which include altering existing boot entries,
adding new, custom entries, selecting different kernels, or modifying initrd.
GRUB also supports Logical Block Address mode. This means that if your
computer has a fairly modern BIOS that can access more than 8GB (first
1024 cylinders) of hard disk space, GRUB will automatically be able to access
all of it.
GRUB can be run from or be installed to any device (floppy disk, hard disk,
CD-ROM, USB drive, network drive) and can load operating systems from just
as many locations, including network drives. It can also decompress
operating system images before booting them. You can learn much, much
more from the official GNU GRUB Manual 0.97
How does GRUB work?
When a computer boots, the BIOS transfers control to the first boot device,
which can be a hard disk, a floppy disk, a CD-ROM, or any other BIOS-
recognized device. We'll concentrate on hard disks, for the sake of simplicity.
The first sector on a hard is called the Master Boot Record (MBR). This sector
is only 512 bytes long and contains a small piece of code (446 bytes) called
the primary boot loader and the partition table (64 bytes) describing the
primary and extended partitions.
By default, MBR code looks for the partition marked as active and once such
a partition is found, it loads its boot sector into memory and passes control
to it.
GRUB replaces the default MBR with its own code.
Furthermore, GRUB works in stages.
Stage 1 is located in the MBR and mainly points to Stage 2, since the MBR is
too small to contain all of the needed data.
Stage 2 points to its configuration file, which contains all of the complex user
interface and options we are normally familiar with when talking about
GRUB. Stage 2 can be located anywhere on the disk. If Stage 2 cannot find
its configuration table, GRUB will cease the boot sequence and present the
user with a command line for manual configuration.
Stage 1.5 also exists and might be used if the boot information is small
enough to fit in the area immediately after MBR.
The Stage architecture allows GRUB to be large (~20-30K) and therefore
fairly complex and highly configurable, compared to most bootloaders, which
are sparse and simple to fit within the limitations of the Partition Table.
GRUB notation
This section is intended to help you get familiar with GRUB, without touching
anything. The next section deals with actual files and manual configuration.
GRUB has its own notation, which is very similar yet somewhat different from
the general notation a typical Linux user might be used to. Now, here's an
example of a typical GRUB entry:
(hd0,1)
The brackets are a must; all devices listed in GRUB menu must be
enclosed in brackets.
hd stands for hard disk; alternatively, fd stands for floppy disk, cd stands
for CD-ROM etc.
The first number (integer for geeks) refers to the physical hard drive
number; in this case, the first drive, as they are counted from zero up. For
example, hd2 refers to the third physical hard drive.
The second number refers to the partition number of the selected hard
drive; again, partitions are counted from zero up. In this case, 1 stands for
the second partition.
From here, it is evident that GRUB (menu) does not discriminate between IDE
or SCSI drives or primary or logical partitions. The task of deciding which
hard drive or partition may boot is left to BIOS and Stage 1. As you see, the
notation is very simple.
Primary partitions are marked
from 0 to 3 (hd?,0), (hd?,1), (hd?,2), (hd?,3). Logical partitions in
the extended partition are counted from 4 up, regardless of the actual
number of primary partitions on the hard disk, e.g. (hd1,7).
The entries alone are not enough to boot an operating system though. GRUB
also needs to know what operating system images to load. These are
assigned as parameters to each of the called devices, including special flags
(switches). For example, Windows Safe Mode is a special flag. Here is an
example of a GRUB menu booting only Ubuntu.
Here's an example of a GRUB menu from a computer with triple boot (SUSE
10.2, Ubuntu 6.10 and Windows XP). This is the actual menu.lst on one of my
real machines.
Let's try to understand what the entries mean. Only uncommented lines
count. Comments are marked with #. The lines that have a series of small
gray xs showing are in indication that text therein belongs to the line
preceding it. In other words, there text simply dropped to another line to
visually accommodate the text editor and screen resolution limitations.
default 0
timeout 8
The first line (default 0) means that the first operating system listed will be
booted. In this case, it's SUSE 10.2. The second line (timeout 8) tells how
much time (seconds) the user has to make his choice before the default
entry is loaded. Simple, isn't it?
gfxmenu (hd0,2)/boot/message
The GRUB menu can also be graphic. The fancy stuff needed to present the
user with a colorful background and possibly some extras is located on
the first physical disk, third partition (hd0,2). This is a primary partition, as
we have seen earlier.
title openSUSE 10.2
root (hd0,2)
kernel /boot/vmlinuz-...
initrd /boot/initrd.img-...
This is the first operating system entry in the menu.
title is as simple as it sounds. It's merely a string that's meant to help the
user read the menu in human terminology.
root (hd0,2) tells GRUB where its configuration files are located. In this
instance, they can be found under (hd0,2)/boot/grub.
kernel /boot/vmlinuz-... boots the actual kernel image. There can be many
such images available. The fact there is no device specified before
the /boot/vmlinuz indicates the image is located on the same partition as
the GRUB itself. This is often the default case for your primary choice of
operating system.
initrd /boot/initrd.img-... is the temporary file system that makes system
preparations - adapts generic kernel image to specific hardware - before
the real root is loaded.
The extra switches used after the kernel indicate where the actual root is
located, what graphic mode is used and where the swap partition resides.
This article will not go into detail regarding the more advanced GRUB
configurations.
title Ubuntu, kernel ... (/dev/sda9)
kernel (hd0,8)/boot/vmlinuz-...
initrd (hd0,8)/boot/initrd.img-...
Again, title indicates a name, in this case fancily adorned with technical
details.
kernel (hd0,8)/boot/vmlinuz-... points to the 9th partition on the first hard
disk (hd0,8). Accidentally, the root flag (/dev/sda9) indicates that the root
partition is the same as the one containing the kernel image. Normally, this
is the case, and for the sake of simplicity, you will want this option during
your installations.
initrd (hd0,8)/boot/initrd.img-... nothing new here.
Note regarding kernel images and root partitions: On older computers with
BIOS that do not support access to more than the first 1024 cylinders, you
might setup a boot partition that contains the kernel image, while the root
itself is located elsewhere. But people with computers younger than the
botched Y2K crisis need not worry.
Another thing you might notice is that the Ubuntu entry is fairly detailed.
This feature is called Multi-boot Compliance; openSUSE recognizes Ubuntu
and can accurately call its images (including special switches) and mount the
partitions. However, most operating systems are only partially multi-boot
compliant. A little later on, I will show you how the Ubuntu entry could have
been treated differently, with the same results.
title Windows
rootnoverify (hd0,0)
chainloader (hd0,0)+1
rootnoverify (hd0,0) means that openSUSE cannot understand Windows
operating system, i.e. no multi-boot compliance. Therefore, the operating
system is called without any fore-knowledge of the kernel. GRUB assumes
that the relevant boot images will be found on the target partition
and mounted by the other operating system bootloader. As you can see,
Windows was installed on the first partition of the first hard disk. This is the
most convenient option.
chainloader (hd0,0)+1 feature is used for operating systems that cannot
be booted directly. Not surprisingly, Windows operating systems cannot be
booted directly. They are booted by the method of chainloading. As the
name implies, GRUB passes the control of the boot sequence to another
bootloader, located on the device to which the menu entry points. This can
be a Windows operating system, but also any other, including Linux.
Back to Ubuntu. You remember that I have told we could have booted
Ubuntu in an alternative way? Indeed, we could have simply chainloaded it,
just like Windows.
title Ubuntu
root (hd0,8)
chainloader (hd0,8)+1
This would have worked equally well. The last option in the menu allows you
to boot SUSE in the failsafe mode (sort of Safe Mode, no graphics) and does
not contribute to our knowledge, therefore we will skip it at the moment.
Let's see some more examples. The example below is actually written in
the Ubuntu menu.lst (commented, of course).
title Windows 95/98/NT/2000
root (hd0,0)
makeactive
chainloader +1
title Linux
root (hd0,1)
kernel /vmlinuz root=/dev/hda3 ro
By now, you should be able to "read" GRUB language with ease. We begin
with the title. root specifies the partition where we expect to find the
Windows kernel and mounts it (rootnoverify would leave this job to the
Windows bootloader). makeactive command sets the active partition on the
root disk (above) to GRUB root device. This means that the next
command, chainloader, is executed without the target partition specified (as
the target partition is the same, now).
The second entry is even simpler. We name a Linux, we call its partition and
we boot the kernel. In this case, we see the very interesting case where
the kernel image and root (/) partition of the operating system are NOT
located on the same partition. This would be a very likely case for older
computers - or ones with a specific boot partition. Finally, the crazy stuff: A
GRUB menu booting 100+ systems of DOS, Windows, Linux, BSD, and
Solaris.
You see, it's very simple! Once you get the hang of it, it actually becomes
fun. For more information, you must read the GNU GRUB Manual 0.97. In
particular, you need to familiar with The list of command line and menu entry
commands.
GRUB configuration
Most of the time, you will not want or need to touch GRUB. When you install
operating systems, especially in the right sequence (inconsiderate OS first -
like Windows, flexible OS later - Linux and family), the GRUB will be installed
automatically and relevant entries appended.
Nevertheless, you should know when and what to do if things go wrong.
Installation of GRUB
GRUB can be installed to a variety of devices. Most people will be interested
in setting up GRUB on their hard disk. Nevertheless, it does not hurt to know
learn about other options. Of course, you can skip forward. Before we install
anything, we need to know where our files are. If installed, GRUB menu is
located on the root partition under:
/boot/grub/menu.lst
Always, always back this file up before making any changes! The GRUB files
can be found in the image of your operating system (usually a CD), under:
/usr/lib/grub/i386-pc
You should also be aware of the basic Linux commands regarding the hard
disks and partitions. To this end, you might want to try my article Highly
useful Linux commands & configurations. Nevertheless, for those who do not
fancy reading yet another article, here's a brief version:
You can display your PC environment information either through terminal or
a text editor. Files through text editor are invoked by specifying the text
editor and a target file (with su or sudo for system files).
sudo vi /etc/fstab
OR
sudo gedit /etc/fstab
Alternatively, you can simply print the contents of the files in the terminal by
using the cat (concatenate) command.
(sudo) cat /etc/fstab
Some useful commands that one might need when dealing with boot, hard
drives and partitions: Display the partition table:
fdisk -l
Display the mounted partitions:
cat /etc/fstab
Now that we know what we need and where to find it, it's time to install
GRUB.
GRUB on a floppy disk
To create GRUB on a floppy disk, we must copy the Stage 1 and Stage 2 files
from the image directory to the first and second sector of the floppy disk.
We'll use the dd command, which copies information sector by sector. Here's
the set of commands that you need to execute (taken from the Manual):
cd /usr/lib/grub/i386-pc
dd if=stage1 of=/dev/fd0 bs=512 count=1
dd if=stage2 of=/dev/fd0 bs=512 seek=1
Just a short explanation: if stands for input file, of stands for output
file, /dev/fd0 refers to the first mounted floppy device, bs specifies the block
size (in bytes), count instructs how many blocks should be copied,
and seek tells how many blocks should be skipped before writing. Not
surprisingly, since we need to write the files to the first two sectors of the
floppy disk, we write stage1 to the first sector, and copy stage2 to
the second (skip first, then write). Now that we have taken this deep fancy
for the command line, I'll leave you with the nuances of different commands
for homework. More on dd on Wikipedia.
Regarding the GRUB on floppy, that's it. Since we have copies the stages
from OS image, all of the required information is there. Of course, you can
always make manual adjustments. But that's the next chapter.
Installing GRUB natively
Native install means placing GRUB Stage 1 in the first sector of the hard disk
(MBR or Partition Table). This means you will be able to boot without a
secondary device, like a floppy disk (which have become a rarity nowadays).
However, this also means that if you install an inconsiderate OS later on (like
Windows) or try to repair the MBR for some reason (by running fdisk /MBR
from DOS prompt), you will erase the GRUB and render all systems listed in
the menu.lst unbootable.
To install GRUB in MBR, you will need to boot from external media (floppy,
live Linux CD). Then, once you reach the GRUB prompt, execute the following
commands:
Find the GRUB root device:
find /boot/grub/stage1
GRUB will search for all available Stages 1 and present them. If you have
more than one operating system image present (e.g. SUSE, Kubuntu,
Mandriva), you will have more than one stage1 available. Example - Let's say
the computer has the following operating systems installed on different
partitions:
SUSE on (hd0,1)
Kubuntu on (hd0,2)
Mandriva on (hd0,4)
All these will be returned as potential roots for GRUB device (as each OS has
its own files). If you wish to use SUSE GRUB, then you will setup the GRUB
root device to (hd0,1):
root (hd0,1)
If you want Mandriva's GRUB, then:
root (hd0,4)
If you know in advance what you want to do, you can skip the find command.
Once you have decided on the root, you need to write the information to the
MBR:
setup (hd0)
Finally, quit the GRUB prompt:
quit
As a sequence, the commands that you need are:
find /boot/grub/stage1 (optional)
root (hdX,Y)
setup (hd0)
quit
You can also setup GRUB on another drive or partition, but then you will have
to chainload GRUB to another bootloader for this to work. That's it. Easy
peasy orange squeasy!
Installing GRUB with grub-install
This method is considered less safe (according to the Manual), as it guesses
the mapping. Still, for total newbs in need of dire help, this might be the
preferred method. You only need to invoke a single command - namely,
where to install the bootloader. Furthermore, this command can be written in
several ways, all equivalent:
grub-install /dev/hda
grub-install /dev/hd0
grub-install '(hd0)'
grub-install hd0
After you have installed the GRUB, your operating systems should boot.
Once booted, you can once again start playing with GRUB, manually
changing settings - adding and removing entries, chainloading other
bootloaders, or even hiding and unhiding partitions.
Setting up GRUB manually (after installation)
You can reconfigure or reinstall GRUB at any moment.
Backup!
First, before you make any changes to the GRUB configuration file, it is most
warmly recommended that you backup the existing menu. You might even
consider copying to another machine or printing the menu, in case things go
bunkerous.
cp /boot/grub/menu.lst /boot/grub/menu.lst_backup
To access GRUB, execute the following command in Linux terminal:
sudo grub
After a few moments, the GRUB command line should show up. You can
identify it by the grub> prompt.
Alternatively, you can reach the GRUB command line during bootup. When
the GRUB menu loads, press C on the keyboard.
Adding a new operating system to the GRUB menu
Let's say you have installed yet another operating system on your machine,
Sabayon. During the installation, you skip the GRUB setup. This means that
the original GRUB remains untouched - and it does not contain an entry
about Sabayon. For all practical purposes, Sabayon is not bootable.
Note: GRUB menu entries are called stanzas (probably a twist of instance?).
So, we need to add Sabayon to the list. If you know where Sabayon is
installed, you just need to add its entry to the menu.lst.
title Sabayon
root (hdX,Y)
chainloader +1
That's it. You can also do this while booting the computer, without editing the
menu. When the original GRUB comes up, press C to reach the command
line. And then:
root (hdX,Y)
chainloader +1
Optionally, you will use the find command to get around. Basically, this is the
whole of GRUB magic. As you can see, it's very very simple. But for people
who have never heard of GRUB and see long lists of strange commands, the
prospect can be daunting. Now that we have covered the basics of grubbing,
it's time for extras and some more common problems.