# 13.1 Mounting and Creating filesystems
A big part of the exam is the managing of disks and mounting file systems.
To have proper space for files we need to do the following things in order:
1. Find an available disk
2. Make a partition
3. Add a file system
4. Add the disk entry to fstab
See this handy diagram.
[](http://www.greateracademy.nl:8088/uploads/images/gallery/2020-08-Aug/disk_management1.png)
#### 1: Find an available disk
We can check what disks are available in several ways.
First, we can check this with the ```fdisk``` command; this command is used to control partition tables of disks on the Linux system.
```bash
fdisk -l
```
With the ```-l``` flag, this command will list all disks and or partion tables.
```bash
[root@rhcsa ~]# fdisk -l
Disk /dev/vda: 9 GiB, 9663676416 bytes, 18874368 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7f927c66
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2099199 2097152 1G 83 Linux
/dev/vda2 2099200 18874367 16775168 8G 8e Linux LVM
Disk /dev/vdc: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/cl-root: 6.1 GiB, 6543114240 bytes, 12779520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/cl-swap: 924 MiB, 968884224 bytes, 1892352 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/cl-home: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
```
You can see here that the disks:
```bash
Disk /dev/vdc: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
```
Do not seem to have partition tables.
An easier way and command is the ```lsblk``` command: this means **L**i**S**t **BL**oc**K** devices.
```bash
lsblk
```
Result should look as following:
```bash
[root@rhcsa ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 8:0 0 9G 0 disk
├─vda1 8:1 0 1G 0 part /boot
└─vda2 8:2 0 8G 0 part
├─cl-root 253:0 0 6.1G 0 lvm /
├─cl-swap 253:1 0 924M 0 lvm [SWAP]
└─cl-home 253:2 0 1G 0 lvm /home
vdb 8:16 0 10G 0 disk
vdc 8:32 0 5G 0 disk
sr0 11:0 1 1024M 0 rom
```
We can see here that:
```bash
vda 8:0 0 9G 0 disk
├─vda1 8:1 0 1G 0 part /boot
└─vda2 8:2 0 8G 0 part
├─cl-root 253:0 0 6.1G 0 lvm /
├─cl-swap 253:1 0 924M 0 lvm [SWAP]
└─cl-home 253:2 0 1G 0 lvm /home
```
```vda``` is split in 2 **partitions** ```vda1``` and ```vda2``` with a **Volume Group** called ```cl``` and the **Logical Volumes** ```root```, ```swap``` and ```home```.
On the right of this output, you can see where the partitions or logical volumes are mounted.
|disk|partition|LVM|Size|Designation|Mount point|
|:--|:--|:--|:--|:--|:--|
|vda|||9G|Disk||
||vda1||1G|Part(partition)|/boot|
||vda2||8G|Part||
|||cl-root|6.1G|lvm(logical volume management)|/|
|||cl-swap|924M|lvm|[swap]|
|||cl-home|1G|lvm|/home|
More on **Volume Group** and **Logical Volumes** in another chapter.
We can see that the disks:
```bash
vdb 8:16 0 10G 0 disk
vdc 8:32 0 5G 0 disk
```
Clearly have no partitions or anything else attached to them. They are also not mounted anywhere.
#### 2: Making an Partition
We will use fdisk to modify the second disk ```vdb``` to add an partition and make a file system for our use:
**Be warned this is the part where we can really start breaking things on our VM, so be careful and check what you do at least twice.**
We will use the ```fdisk``` command to make partions:
```bash
fdisk /dev/vdb
```
```
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xab5f1c3a.
Command (m for help):
```
Now we are in ```fdisk``` this is a TUI (Text User Interface) and we can navigate it by pressing the appropriate letters and numbers. If we press **M** we get the help function to see what we can do:
```
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
```
Let's see the status of the disk with the ```p``` option:
```
Command (m for help): p
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7af272f8
```
Let's make a new partition on the ```vdb``` with the ```n``` option and let's make the size 1G:
```
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
```
It will now ask if we want to make a ```primairy``` partition or an ```extended```.
Each disk can have up to four primary partitions or three primary partitions and an extended partition.
If you need four partitions or less, you can just create them as primary partitions.
However, let’s say you want six partitions on a single drive, you’d have to create three primary partitions as well as an extended partition.
The extended partition effectively functions as a container that allows you to create a larger amount of logical partitions. So, if you needed six partitions, you’d create three primary partitions, an extended partition, and then three logical partitions inside the extended partition. You could also just create a single primary partition, an extended partition, and five logical partitions — you just can’t have more than four primary partitions at a time.
Let's make a new **primairy** partition:
```
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
```
fdisk now asks us what partition number we want, we can press ```enter``` and fdisk will simply take the next number in this case ```1``` we can also just press ```1```:
```
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
```
Now it asks the starting sector of the partition you can just press ```enter``` and it will start the partition on the first sector available in this case sector 2048:
```
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519):
```
Now it asks for the ending sector lucky for us ```fdisk``` understands less complex inputs than the sector numbers, some examples below:
```
+100M = 100 Megabyte
+1G = 1 Gigabye
+1500M = 1500 Megabyte or 1.5 Gigabyte
```
This goes until Terrabytes and Petabytes and as low as Kilobytes.
Let's make it 1 Gigabyte (+1G):
```
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help):
```
We have now created a ```1 Gigabyte``` ```partition``` called ```vdb1```.
We can now still quit without any changes but let's save the partition table changes with the ```w``` button for write:
```
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
```
Sometimes the new partition information is not discovered by Linux and we need to send a refresh command. My advice would be to just do this always after apart fdisk command:
```bash
partprobe
```
It generates no output it simply did its job.
We can check the partition using ```lsblk```:
```bash
[root@rhcsa ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 8:0 0 9G 0 disk
├─vda1 8:1 0 1G 0 part /boot
└─vda2 8:2 0 8G 0 part
├─cl-root 253:0 0 6.1G 0 lvm /
├─cl-swap 253:1 0 924M 0 lvm [SWAP]
└─cl-home 253:2 0 1G 0 lvm /home
vdb 8:16 0 10G 0 disk
└─vdb1 8:17 0 1G 0 part
vdc 8:32 0 5G 0 disk
sr0 11:0 1 1024M 0 rom
```
There is our brand new partition called ```vdb1``` of 1G.
#### 3: Add a file system
Now, if we want to use the partition for files, we need to give it a ```filesystem```. This is the method of how files are stored, searched and indexed on the partition. There are quite a few filesystem types.
[Here a list of all filesystems](https://en.wikipedia.org/wiki/List_of_file_systems)
The most important ones for Linux are:
- ext(2,4)
- xfs
xfs has been chosen as the standard filesystem for Centos8. However, boot will still be in ext4 or ext2:
If you have a Red Hat Account, you can check this site for theoretical limits and reasoning on filesystems by Red Hat:
[Red Hat File System Solutions](https://access.redhat.com/solutions/1532)
Let's make our ```vdb1``` a xfs filesystem:
```bash
mkfs.xfs /dev/vdb1
```
```bash
[root@rhcsa ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
```
mkfs (Make Filesystem) is the command used to create filesystems. We can use it in several ways. However, the most important ones are:
```
mkfs.ext4 = make and XFS Filesystem
mkfs.xfs = make and EXT4 Filesystem
```
Now the disk has a partition and a filesystem. Now all that is left is to mount it.
#### 4: Mount and or Add the disk entry to fstab
There are 2 ways to mount an partition:
1. manual mount
2. fstab mount
Manual mounting means we mount the disk somewhere with the ```mount``` command. However, this will **NOT** persist after a reboot.
Fstab is a file located at ```/etc/fstab```. This is a list of all the filesystems together with their type, location and mount options that will be mounted on boot.
First, let's do a manual mount, in both the fstab and manual mount option. We need a directory to mount the disk on, so lets make that first:
```bash
mkdir -p /mnt/helloworld
```
This has created a directory we can mount the partition on.
To mount the partition we will do the ```mount``` command:
```bash
mount /dev/vdb1 /mnt/helloworld/
```
Let's check the result with ```lsblk```:
```bash
[root@rhcsa ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 8:0 0 9G 0 disk
├─vda1 8:1 0 1G 0 part /boot
└─vda2 8:2 0 8G 0 part
├─cl-root 253:0 0 6.1G 0 lvm /
├─cl-swap 253:1 0 924M 0 lvm [SWAP]
└─cl-home 253:2 0 1G 0 lvm /home
vdb 8:16 0 10G 0 disk
└─vdb1 8:17 0 1G 0 part /mnt/helloworld
vdc 8:32 0 5G 0 disk
sr0 11:0 1 1024M 0 rom
```
It worked! The filesystem is now mounted on ```/mnt/helloworld```.
Let's unmount it to remount it with the ```fstab``` method.
```bash
umount /mnt/helloworld
```
This should have removed the mount you can check this with ```lsblk```.
Now we need to edit the fstab to add the mount. We can approach this in several ways.
##### Easy way:
```bash
vim /etc/fstab
```
add the following line at the bottom:
```
/dev/vdb1 /mnt/helloworld xfs defaults 0 0
```
The file should now look like this:
```bash
/dev/mapper/cl-root / xfs defaults 0 0
UUID=8503adac-d001-46c0-92b5-d2c14d46c916 /boot ext4 defaults 1 2
/dev/mapper/cl-home /home xfs defaults 0 0
/dev/mapper/cl-swap swap swap defaults 0 0
/dev/vdb1 /mnt/helloworld xfs defaults 0 0
```
Write and quit the file. Now we can use the ```mount``` command to force a mount of all fstab entries. We are using the option ```a``` for all and ```v``` for verbose:
```bash
mount -va
```
```bash
[root@rhcsa ~]# mount -va
/ : ignored
/boot : already mounted
/home : already mounted
swap : ignored
mount: /mnt/helloworld does not contain SELinux labels.
You just mounted an file system that supports labels which does not
contain labels, onto an SELinux box. It is likely that confined
applications will generate AVC messages and not be allowed access to
this file system. For more details see restorecon(8) and mount(8).
/mnt/helloworld : successfully mounted
```
You can now check with ```lsblk``` if the mount was succesful.
##### Safer but more complex method:
First run umount to remove the mount.
```bash
umount /mnt/helloworld
```
We can also add it in the fstab based on filesystems block ID number, which is more static then the ```vdb1``` designation. To get a filesystem blockid, we use the ```blkid``` commmand to print the result:
```
blkid
```
```bash
[root@rhcsa ~]# blkid
/dev/mapper/cl-root: UUID="c31b56b4-19c8-4df7-9954-595e3e3943e1" TYPE="xfs"
/dev/vda2: UUID="aq53k3-yETG-pEsz-COhh-W902-2drm-vdOJPL" TYPE="LVM2_member" PARTUUID="7f927c66-02"
/dev/vda1: UUID="8503adac-d001-46c0-92b5-d2c14d46c916" TYPE="ext4" PARTUUID="7f927c66-01"
/dev/vdb1: UUID="06394480-34f3-4cd6-959e-0051a0b5815f" TYPE="xfs" PARTUUID="7af272f8-01"
/dev/mapper/cl-swap: UUID="89835ff0-dfb7-4cb2-862d-73f04f154e6e" TYPE="swap"
/dev/mapper/cl-home: UUID="a9039251-87e7-4d69-b3ce-b51e33361d92" TYPE="xfs"
```
As you can see the vdb1 has an ```uuid``` this is the id of the filesystem block.
With the following command we can safely take this section and put it in the fstab for us to use:
```bash
cp /etc/fstab /etc/fstab.bak; blkid | grep vdb1 >> /etc/fstab
```
This will make a copy to secure the fstab file. Run ```blkid``` to get the line for ```vdb1``` and append it to the bottom of the fstab file.
We can now edit the fstab file to make the last line look like below:
```
UUID=06394480-34f3-4cd6-959e-0051a0b5815f /mnt/helloworld xfs defaults 0 0
```
Write and quit the file, we can now use the ```mount``` command to force a mount of all fstab entries:
```bash
mount -va
```
```bash
[root@rhcsa ~]# mount -va
/ : ignored
/boot : already mounted
/home : already mounted
swap : ignored
mount: /mnt/helloworld does not contain SELinux labels.
You just mounted an file system that supports labels which does not
contain labels, onto an SELinux box. It is likely that confined
applications will generate AVC messages and not be allowed access to
this file system. For more details see restorecon(8) and mount(8).
/mnt/helloworld : successfully mounted
```
You can now check with ```lsblk``` if the mount was succesful.