There are many different tools for managing partitions in Linux. In Red Hat Enterprise Linux, the default tool is parted. parted works with both GPT and MBR partition schemes. Parted takes the disk as the first image and if followed by print (p can also be used) it will examine the disk for any partition tables and print out the information to the console. In the example below, we'll examine /dev/vda:
root@localhost:~# parted /dev/vda print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB xfs bls_boot
3 1076MB 64.4GB 63.3GB lvm
It is possible to create partitions in a one shot command, however we can use parted in an interactive prompt which may be easier:
parted /dev/sda.printed in to the parted prompt to print out the currently dected partition table.mklabel msdos for MBR type.
mklabel gpt for GPT type.
2048s (s for sector).1001MB to create a 1GB partitionquit to exit partedudevadm settle. Verify that the new partition(s) are seen by running lsblkNew partitions will need to be mounted before use.
Similarly to creating partitions, we can delete partitions in a one shot command, however here we will describe the interactive method using the parted tool.
parted /dev/sda.printed to check the partition table. This will also list out the partition numbers. This information will be needed for the next step.rm 1quit to exit the parted tool.Ensure that
/etc/fstabis updated to reflect the changes and the system does not try to mount a non-existant partition.
There are many different file systems available in Linux Kernel, and plenty more available if you want to add custom modules. Distributions have their own recommendations, for example Red Hat recommends the use of the xfs file system and, indeed, is Red Hat's default file system.
Creating a file system once the partition is created is a relatively straightforward process.
mkfs.xfs /dev/sda1 to format the /dev/sda1 partition with XFS.If you do not want to use XFS, then you can see what other options are available using mkfs.TAB,TAB to auto complete and show the other file systems which mkfs can handle.
In order to mount a file system in Linux, you will require a mount point. This can be located more or less anywhere and is created using the simple mkdir command, e.g. mkdir /mnt/newmountpoint.
Once a mount point is created, we can (temporarily) mount a file system there (it will be removed upon reboot) by running (as root) mount /dev/sda1 /mnt/newmountpoint.
The
mountcommand can also be used to verify the current mount options being used for a mount point:mount | grep sda1which will give an output similar to:
/dev/sda1 on /mnt/newmountpoint type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
For most distributions, the way to manage persistent mounts is using the /etc/fstab file, an example of which is shown below.
UUID=edcaf54b-40ba-4bcc-8708-1fd871eb04ab / xfs defaults 0 0
UUID=b694e9b6-055b-447d-9d06-15b737e7c4a9 /boot xfs defaults 0 0
UUID=20f49bf5-3827-497d-8554-bcf6fc75bc20 /home xfs defaults 0 0
UUID=4dc0c5f2-1f22-4a2c-8512-379a652c14c0 none swap defaults 0 0
The UUID field refers to the partition to be mounted. The /dev/sda1 label can be used here, but it is not recommended as the disks can enumerated in different orders during boot. The UUID persists across boots.
The next field is the mount point (/, /boot, /home, and none in the example above).
Next is the file system type (xfs, swap).
The forth field is a list of comma separated values to specify different options on how to mount the filesystem, default is usually sufficient for most cases.
Fifth field is used by the dump command in order to back up the device. Other applications do not need this field.
Sixth field is the fsck field. This determins if fsck should be run at boot time to verify the file system contents.
xfs does not require this, set to 0.ext4set to 1 for the root file system and 2 for othersTo update the /etc/fstab file we will need to find out what the UUID is of the file system we wish to mount. This can be obtained using the following command:
root@localhost:~# lsblk --fs
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
sr0
vda
├─vda1
├─vda2 xfs b694e9b6-055b-447d-9d06-15b737e7c4a9 547M 43% /boot
└─vda3 LVM2_member LVM2 001 faramp-VgRm-8Mp0-Ob4Y-4vLA-qaVZ-XT1EIk
├─rhel-root xfs edcaf54b-40ba-4bcc-8708-1fd871eb04ab 34.3G 7% /
├─rhel-swap swap 1 4dc0c5f2-1f22-4a2c-8512-379a652c14c0 [SWAP]
└─rhel-home xfs 20f49bf5-3827-497d-8554-bcf6fc75bc20 17.6G 2% /home
vdb
└─vdb1 xfs b368e814-03b9-4d5a-b0e9-3d848c6ade8a 840.6M 6% /mnt/newmountpoint
vdc