Creating a partitian, filesystem and how to mount
On this page, I will explain the basics of a linux partition, what a file system is as well as how to create one and how to mount the partition after creation.
So lets begin…..
What is a partition?
– A partition is a part of a hard drive. A hard disk can be divided into several partitions. Each partition functions as if it were a separate hard disk. The idea is that if you have one hard disk, and want to have, say, two operating systems on it, you can divide the disk into two partitions. Each operating system uses its partition as it wishes and doesn’t touch the other ones. This way the two operating systems can co-exist peacefully on the same hard disk. Without partitions one would have to buy a hard disk for each operating system.
The MBR, boot sectors and partition table
The information about how a hard disk has been partitioned is stored in its first sector (that is, the first sector of the first track on the first disk surface). The first sector is the master boot record (MBR) of the disk; this is the sector that the BIOS reads in and starts when the machine is first booted. The master boot record contains a small program that reads the partition table, checks which partition is active (that is, marked bootable), and reads the first sector of that partition, the partition’s boot sector (the MBR is also a boot sector, but it has a special status and therefore a special name). This boot sector contains another small program that reads the first part of the operating system stored on that partition (assuming it is bootable), and then starts it.
Extended and logical partitions
The original partitioning scheme for PC hard disks allowed only four partitions. This quickly turned out to be too little in real life, partly because some people want more than four operating systems but primarily because sometimes it is a good idea to have several partitions for one operating system. For example, swap space is usually best put in its own partition for Linux instead of in the main Linux partition for reasons of speed.
To overcome this design problem, extended partitions were invented. This trick allows partitioning a primary partition into sub-partitions. The primary partition thus subdivided is the extended partition; the sub-partitions are logical partitions. They behave like primary partitions, but are created differently. There is no speed difference between them. By using an extended partition you can now have up to 15 partitions per disk
What are filesystems?
A filesystem is the methods and data structures that an operating system uses to keep track of files on a disk or partition; that is, the way the files are organized on the disk. The word is also used to refer to a partition or disk that is used to store the files or the type of the filesystem. Thus, one might say “I have two filesystems” meaning one has two partitions on which one stores files, or that one is using the “extended filesystem”, meaning the type of the filesystem.
The difference between a disk or partition and the filesystem it contains is important. A few programs (including, reasonably enough, programs that create filesystems) operate directly on the raw sectors of a disk or partition; if there is an existing file system there it will be destroyed or seriously corrupted. Most programs operate on a filesystem, and therefore won’t work on a partition that doesn’t contain one.
Different types of Filesystems:
Linux supports several types of filesystems. As of this writing the most important ones are:
- The oldest, presumed to be the most reliable, but quite limited in features (some time stamps are missing, at most 30 character filenames) and restricted in capabilities (at most 64 MB per filesystem).
- A modified version of the minix filesystem that lifts the limits on the filenames and filesystem sizes, but does not otherwise introduce new features. It is not very popular, but is reported to work very well.
- The ext3 filesystem has all the features of the ext2 filesystem. The difference is, journaling has been added. This improves performance and recovery time in case of a system crash. This has become more popular than ext2.
- The most featureful of the native Linux filesystems. It is designed to be easily upwards compatible, so that new versions of the filesystem code do not require re-making the existing filesystems.
- An older version of ext2 that wasn’t upwards compatible. It is hardly ever used in new installations any more, and most people have converted to ext2.
- A more robust filesystem. Journaling is used which makes data loss less likely. Journaling is a mechanism whereby a record is kept of transaction which are to be performed, or which have been performed. This allows the filesystem to reconstruct itself fairly easily after damage caused by, for example, improper shutdowns.
- JFS is a journaled filesystem designed by IBM to to work in high performance environments>
- XFS was originally designed by Silicon Graphics to work as a 64-bit journaled filesystem. XFS was also designed to maintain high performance with large files and filesystems.
In addition, support for several foreign filesystems exists, to make it easier to exchange files with other operating systems. These foreign filesystems work just like native ones, except that they may be lacking in some usual UNIX features, or have curious limitations, or other oddities.
- Compatibility with MS-DOS (and OS/2 and Windows NT) FAT filesystems.
- Extends the msdos filesystem driver under Linux to get long filenames, owners, permissions, links, and device files. This allows a normal msdos filesystem to be used as if it were a Linux one, thus removing the need for a separate partition for Linux.
- This is an extension of the FAT filesystem known as FAT32. It supports larger disk sizes than FAT. Most MS Windows disks are vfat.
- The standard CD-ROM filesystem; the popular Rock Ridge extension to the CD-ROM standard that allows longer file names is supported automatically.
- A networked filesystem that allows sharing a filesystem between many computers to allow easy access to the files from all of them.
- A networks filesystem which allows sharing of a filesystem with an MS Windows computer. It is compatible with the Windows file sharing protocols.
- The OS/2 filesystem.
- SystemV/386, Coherent, and Xenix filesystems.
- The most advanced Microsoft journaled filesystem providing faster file access and stability over previous Microsoft filesystems.
Before one can use a filesystem, it has to be mounted. The operating system then does various bookkeeping things to make sure that everything works. Since all files in UNIX are in a single directory tree, the mount operation will make it look like the contents of the new filesystem are the contents of an existing subdirectory in some already mounted filesystem.
The mount command takes two arguments. The first one is the device file corresponding to the disk or partition containing the filesystem. The second one is the directory below which it will be mounted. After these commands the contents of the two filesystems look just like the contents of the /home and /usr directories, respectively. One would then say that /dev/hda2 is mounted on /home”, and similarly for /usr. To look at either filesystem, one would look at the contents of the directory on which it has been mounted, just as if it were any other directory. Note the difference between the device file, /dev/hda2, and the mounted-on directory, /home. The device file gives access to the raw contents of the disk, the mounted-on directory gives access to the files on the disk. The mounted-on directory is called the mount point.
But why do we have to mount the device?
Basically, and to put it easily, the operating system needs to know how to access the files on that device.
mount is not only “giving you access to the files”, it’s telling the OS the filesystem the drive has, if it’s read only or read/write access, etc.
/dev/cdrom is a low-level device, the operating system functions wouldn’t know how to access them… imagine you put a weirdly formatted cdrom in it (even an audio cd), how would
ls tell which files (if any) are there on the cd-rom without “mounting” it first?
OK!!!! So now that we know what patitioning, filesystems and mounting does, let quickly do a little practical.
- Lets SSH into an instance of ours.
- First, lets run the following command to view the current block devices on your instance:
$ lsblk (This command is used to list information about all available block devices)
There are seven columns namely:
NAME : This is the device name.
MAJ:MIN : This column shows the major and minor device number.
RM : This column shows whether the device is removable or not. Note in this example the device sdb and sr0 have their RM values equals to 1 indicating they are removable.
SIZE : This is column give information on the size of the device. For example 298.1G indicate the device is 298.1GB and 1K indicate the device size is 1KB.
RO : This indicates whether a device is read-only. In this case all devices have a RO=0, indicating they are not read only.
TYPE :This column shows information whether the block device is a disk or a partition(part) within a disk. In this example sda and sdb are disks while sr0 is a read only memory (rom).
MOUNTPOINT : This column indicates mount point on which the device is mounted.
Moving on, I’m using AWS so I’ve added an additional volume to my instance that I will use for this exercise. After adding the volume, I ran the command init 6 to reboot my instance for the change to show on my side.
Ok, so now I’m going to run the command lsblk again to see if my new volume is showing:
As you can see from the above image, I added a new device called xvdg which is an 8GiB volume.
Next, we are going to partition this volume to be used as a swap volume or anything you wanna use it for really. So to create a partition, we are going to use the command called “$fdisk” followed by the name of the device we will be partitioning. I will be using xvdg as this is the device being used for the exercise. As soon as you type in the command and hit enter, you will be presented with the following page below:
Press “m” and hit enter:
As you can see, we are presented with many options to choose from. As we are creating a new partition, we will be typing in the letter “n”.
Lets make this a primary partition, so lets enter “p” and hit enter. No lets select 1 as the partition number and hit enter:
We going to leave this option blank as we are going to use the default value, so hit enter again:
And hit enter again as we will be using the default value for the last sector:
Oooops, forgot one steps, selecting the partition type. Very important….. haha. Okay so press “l” and select the partition type of your choice. I’m going to type in number 82 as I will be using this as a swap partition.
Press “w” to confirm and Booooyaaaa!!!
You have successfully created a partition. Well done.
If we run “lsblk” again, you should see a new entry:
Now lets move on to creating the filesystem we will be using.
As we explained earlier on this page, there are many filesystems to choose from but we will be using the most command filesystem which is ext3 as it has journaling enabled. Really important in case something goes wrong.
So the syntax we will be using is as follows “ mkfs -t (type) ext3 (type of filesystem) /dev/xvdg (where we will be creating the filesystem on/partition)
And there you go, you’ve just created your very own filesystem.
Now to mount the filesystem using the mount command as follows:
$sudo mount (device you are mounting) (mount point) – You can mount it anywhere you want to, but for the practical I will select /mnt
$sudo mount /dev/xvdg /mnt
After mounting, we are going to use vi to edit the fstab file so that the partition is mounted on startup as shown above. We run the command sudo vi /etc/fstab and hit enter
No we select “i” for insert and add an entry for the partition to automount it at startup. The format it as follows:
file_system mount_point type options dump pass
brief explanation of the above format:
1.file_system = your device id.
/dev/sdax ( you should check it with sudo fdisk -l)
it may be /dev/sdbx or /dev/sdcx if you have more than one disks connected.
2. mount_point =where you want to mount your partition.
here user is your user name, label is “software”, “movies” or whatever label your partiton have.
3. type=fat32,ntfs, ntfs-3g,ext2,ext4 or whatever your partition type is.
4. options =mount options for the partition(explained later).
5. dump=Enable or disable backing up of the device/partition .usually set to 0, which disables it.
6. pass =Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking.
so for auto mounting case the above format reduces to:
/dev/sdax /media/user/label type options 0 0
(you can check the type with sudo fdisk -l)
So now that we know what should be added into each option, lets go ahead and add the parition. I have added mine below as an example:
After adding the parition, hit “esc” then shift colon and then wq to save and exit the vi text editor.
We have created a partition, created a filesystem and now mounted the parition, and edited the /etc/fstab file to make sure it automounts the partition on startup.
Lastly, we need to confirm if we mounted the parition successfully. I generally use to commands to do this:
As you can see from the output above, my parition has been mounted successfully and ours should be to.
And thats all to it. Hope this was helpful.