In this post I am going to talk about partitioning and file system post OS installation on a Red Hat based system. Basically in the first part of this post, I am going to explain how to create / remove partitions and how to format them with specific file system. In a second part, I am going to explain how to secure a partition with encryption. Finally, I am going to explain how to create and enable/disable a swap partition.
Partitioning and file System
The partitioning of hard drive give you the way to create only 4 physical partitions or 3 physical partitions and one extended in which you create logical partitions.
When you plan to add a new partition in your hard drive, if you have 3 existent partitions the last one should be extended. So later on, if you need to create more partitions it will be possible 😉
So let’s create a new partition and it’s file system!
How to use fdisk
fdisk is a Command-line tool that provides disk partitioning functions. This is what we get when we ‘print’ all the partitions informations with fdisk. To list all the command press ‘m‘. To display all the partition press ‘p‘.
root@sigmoun-redhat ~] ls /dev/sd sda sda1 root@sigmoun-redhat ~] fdisk -cu /dev/sda Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition\'s system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help):p Disk /dev/sda: 1000.2 GB, 1000204886016 bytes 255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 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 identifier: 0x00010942 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1946726399 973362176 83 Linux
Because of I already create an extended partition, I will directly create the partition I need. But if you don’t have an extended partition and you want to use one, just follow the same steps.
- To create a new partition press ‘n‘.
- Then select its size
- press ‘t‘ to change the file system type and select “83” (it is the linux file system type ID)
- press ‘w‘ to apply changes
- restart the computer ( the kernel need be fully reloaded to make him able to recognize your new partition)
If you enter ‘cat /proc/partitions‘ you will be able to see that the kernel is able to see your new partition.
Now we need to install the file system to make the Operating system able to read/write into it. To do so, just do :
root@sigmoun-redhat ~] mkfs.ext4 /dev/sda3
That’s it ! Now the partition is ready to be mounted in our file system !
There are two way to mount partitions
- the mount that exist only during the session. If the computer restart the partition will not be mounted automatically
- the permanent way. It means that each time the computer boot, the partition will be mounted automatically
Non permanent mount
To mount a mounting point like a partition we need to create a directory where the partition will be mounted.
root@sigmoun-redhat ~] mkdir /mounting_directory
Then we just use the ‘mount‘ command :
root@sigmoun-redhat ~] mount /dev/sda3 /mounting_directory
To unmount the partition use the ‘umount‘ command :
root@sigmoun-redhat ~] umount /mounting_directory
That’s all ! Try to create a file into it the /mounting_directory, unmount the partition, remount it and you will find your file.
Ok, but if I want this partition permanently mounted?
Permanent mount — fstab file
root@sigmoun-redhat ~] vim /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # proc /proc proc nodev,noexec,nosuid 0 0 # / was on /dev/sda2 during installation UUID=1b9a1b16-0e98-4d28-868e-f182476f6505 / ext4 errors=remount-ro 0 1 ######################## ADD your partition here ############################################# #
Add the following at the end of you /etc/fstab:
/dev/sda3 /mounting_point ext4 defaults 0 0
Save and close the file.
Before restart your computer, you need to check if have an error on your fstab file. To do so, just run
root@sigmoun-redhat ~] mount -a
If you have an error, fix it. If you don’t check and directly restart, your computer will not be able to restart!
Maybe you want to encrypt your partition ? ok… for that we are going to use the LUKS (Linux Unified Key Setup) which is a tool that encrypt a partition, open it through a mapper under the /dev/mapper. But keep in mind that encrypting a partition will make you lost all your file system and so your data !!!
After you have created your partition with fdisk, let’s encrypt it:
root@sigmoun-redhat ~] cryptsetup luksFormat /dev/sda3
Then we need to create a mapper that let’s us use the uncrypted partition:
root@sigmoun-redhat ~] cryptsetup luksOpen /dev/sda3 mapper_name
So if you list the content of /dev/mapper you will see that there is mapper_name that has been created. This is the equivalent of /dev/sda3 but uncrypted.
Now format the partition through the mapper and install the file system:
root@sigmoun-redhat ~] mkfs.ext4 /dev/mapper/mapper_name
Create the mounting point and mount the partition:
root@sigmoun-redhat ~] mkdir /mounting_point root@sigmoun-redhat ~] mount /dev/mapper/mapper_name /mount_point
If you want to umount the partition:
root@sigmoun-redhat ~] umount /mounting_point
And close the mapper. The next time you want to mount the partition it will ask you for the password you give when you encrypted the partition.
root@sigmoun-redhat ~] cryptsetup luksClose mapper_name
Permanent mount — crypttab file
If you want to mount automatically your partition when your computer boot ( you will always enter your password ) we need to create a file that automatically create the mapper to your encrypted partition.
root@sigmoun-redhat ~] vim /etc/crypttab mapper_name /dev/sda3
Then we add the following to the /etc/fstab:
root@sigmoun-redhat ~] /dev/mapper/mapper_name /mounting_point ext4 defaults 0 0
Make sur you have no commited errors, so what the computer will not boot ^^.
If you want to add a swap partition to your system you will need to do the following.
root@sigmoun-redhat ~] fdisk -cu /dev/sda
- press ‘n‘
- select it number(4) then its size
- press ‘t’ and then enter 82 (swap ID)
- press ‘w‘ to write changes
- restart you computer
- add in your /etc/fstab the following:
root@sigmoun-redhat ~] /dev/sda4 swap swap defaults 0 0
- to enable all swap partition press
root@sigmoun-redhat ~]swapon -a
then to list all enabled swap partitions
root@sigmoun-redhat ~]swapon -l
- to disable the swap partition just remove it from the /etc/fstab
Now you should be able to create new partitions, install new file system, mount them automatically, and for more security encrypt your partition and of course format it with new file system.
Please let me know if you have any suggestion !