Work with File Systems on Oracle Linux
Introduction
Partitioning a disk is one of the first tasks when creating a file system. It divides the space on the disk but does not allow the operating system to store files on it. The ability to store files on a disk occurs when you format it with a file system. Oracle Linux supports many file systems, but we will focus on ext4 and xfs.
Objectives
In this tutorial, you'll learn how to:
- Display the current partition table
- List mounted file systems
- Partition disk devices
- Create and mount file systems on partitions
- Modify the file system table to mount the file systems on reboots
- Increase the amount of swap space
- Remove partitions and additional swap space
Prerequisites
Minimum of a single Oracle Linux system
Each system should have Oracle Linux installed and configured with:
- A non-root user account with sudo access
- Access to the Internet
- Two or more block devices attached to the system
Deploy Oracle Linux
Note: If running in your own tenancy, read the linux-virt-labs
GitHub project README.md and complete the prerequisites before deploying the lab environment.
Open a terminal on the Luna Desktop.
Clone the
linux-virt-labs
GitHub project.git clone https://github.com/oracle-devrel/linux-virt-labs.git
Change into the working directory.
cd linux-virt-labs/ol
Install the required collections.
ansible-galaxy collection install -r requirements.yml
Deploy the lab environment.
ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6" -e add_block_storage=true -e block_count=2
The free lab environment requires the extra variable
local_python_interpreter
, which setsansible_python_interpreter
for plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add
-e instance_shape="VM.Standard3.Flex"
or-e os_version="9"
to the deployment command.Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Linux is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.
Verify the Block Volumes Exist
Open a terminal and connect via SSH to the ol-node-01 instance.
ssh oracle@<ip_address_of_instance>
Verify the block volumes are available on the system.
lsblk
The command output shows three block devices:
sda
,sdb
, andsdc
.
List the Current Disk Partitions
Display the current partition table and pipe the output to
grep
to list all/dev/sd
disk devices.sudo fdisk -l | grep /dev/sd
Note: The tutorial system has three disk devices,
/dev/sda
,/dev/sdb
, and/dev/sdc
.- The device
/dev/sda
is a 46.6 GB disk containing three partitions of various types and sizes:/dev/sda1
is a 100 MB Extensible Firmware Interface (EFI partition),/dev/sda2
is a 1 GB Linux file system, with/dev/sda3
being a 45.5 GB Linux Logical Volume Manager (LVM). - Partitions
/dev/sda1
and/dev/sda2
are the primary boot devices. - Partition
/dev/sda3
is mapped to the root file system. - Disk device,
/dev/sdb
, is a 50 GB disk without any partitions. - Disk device,
/dev/sdc
, is a 50 GB disk without any partitions.
- The device
List the mounted partitions.
The devices' order and some numbers might differ.
df -h
- The hard drive's first partition,
sda1
, contains the Unified Extensible Firmware Interface (UEFI) boot loader files and is mounted on/boot/efi
. - The second partition,
sda2
, is mounted on/boot
. - The file systems mounted on
/
and/var/oled
are logical volumes.
- The hard drive's first partition,
Create an MBR Partition
Partition the
/dev/sdb
disk usingfdisk
.sudo fdisk /dev/sdb
The interactive program opens with a short statement on its use, information about the device selected, and a command prompt.
Enter m at the prompt to display the
fdisk
menu.The menu groups the available options by their function.
Enter n to add a new MBR primary partition.
- The MBR stores partition information at the beginning of a disk and is limited to four primary partitions.
- One of the four partitions can be designated as an extended partition. We can then subdivide the extended partition into multiple logical partitions.
Enter p, followed by the number 1 for the first primary partition.
Press Enter to accept the default (2048) as the start of the first sector.
Enter +500M to set the last sector using the size notation.
fdisk
returns a message telling you that the new partition is of type Linux and has a size of 500 MiB.Enter p to print the new partition.
Some of the information the print option displays is:
- The disk device name: /dev/sdb1
- The disk label type: dos, which indicates an MBR partition table
- The partition type: Linux
Enter w to save the new partition.
fdisk
notes that the partition has changed, is being re-read byioctl()
, and that the disk is synchronizing.List the partition table on
/dev/sdb
.The
fdisk -l
command uses the lowercase letter l option, not the numeral 1.sudo fdisk -l /dev/sdb
Create a GPT Partition
Get a list of the current partitions on
/dev/sdc
.sudo fdisk -l /dev/sdc
The output shows the disk does not have any partitions.
Create a GUID Partition Table (GPT) partition on
/dev/sdc
.sudo fdisk /dev/sdc
fdisk
outputs the summary as it did before about the disk- GPT partitions are ideal for disks larger than 2 TB
- In this tutorial, you use a 50 GB disk
- If this disk is larger than 2 TB, a warning displays affirming the size of the disk and indicates that it cannot use a DOS partition table
Enter g to add a new GPT partition.
The output confirms the creation of a new GPT disk label.
Enter n, followed by the number 1 for the first sector partition.
Press Enter to accept the default (2048) as the start of the first sector.
Enter Enter to set the last sector using the default size of 104857566.
fdisk
returns a message about the new partition of type Linux with a size of 50 GiB.Enter p to print the new partition.
Some of the information the print option displays is:
- The disk device name: /dev/sdc1
- The disk label type: gpt, which indicates a GPT partition table
- The partition type: Linux
Enter w to save the new partition.
fdisk
notes that the partition changed, being re-read byioctl()
, and the disks are synchronizing.
Create an ext4 File System on the MBR Partition
Make an ext4 file system on the disk and label it.
sudo mkfs -t ext4 -L Test /dev/sdb1
Display the attributes of the /dev/sdb1 block device.
sudo blkid /dev/sdb1
Create a xfs File System on the GPT Partition
Make an xfs file system on the disk and label it.
sudo mkfs -t xfs -L Dev /dev/sdc1
Display the attributes of the /dev/sdc1 block device.
sudo blkid /dev/sdc1
Mount the File Systems
Create a mount point for each file system.
sudo mkdir /Test sudo mkdir /Dev
Mount the file systems using the mount points.
sudo mount /dev/sdb1 /Test sudo mount /dev/sdc1 /Dev
Display the mounted file systems.
df -h
Show the mount details of the Test file system.
mount | grep /Test
The output shows the default mount options on
/Test
. For more information on these and other mount options, refer to the 'man mount` page.View the status of all mounted file systems.
Pipe the output to
tail
and display the last six lines.cat /proc/mounts | tail -6
The
mounts
file is part of the proc virtual file system. Unlike other files in the/proc
directory, it does not exist. It is a representation of file system status in file form.
Update the fstab File
Obtain the UUID values for /dev/sdb1 and /dev/sdc1.
sudo blkid /dev/sdb1 sudo blkid /dev/sdc1
Oracle recommends using the device's universally unique identifier (UUID) instead of /dev path names to avoid naming issues across reboots.
Add the new entries to
/etc/fstab
and reload the systemd daemon.echo "$(sudo blkid /dev/sdb1 | awk '{print $3}') /Test ext4 defaults 0 0" | sudo tee -a /etc/fstab echo "$(sudo blkid /dev/sdc1 | awk '{print $3}') /Dev xfs defaults 0 0" | sudo tee -a /etc/fstab sudo systemctl daemon-reload
Updating the
/etc/fstab
automatically mounts included file systems after the system reboots. Reload the systemd daemon so the system can see the changes to the/etc/fstab
file.Important: If you add an iSCSI remote block volume, you must include the
_netdev
mount option, or your instance will become unavailable after the next reboot.Unmount the file systems.
sudo umount /Dev /Test
Mount all file systems in the
/etc/fstab
file.sudo mount -a
Verify your new file systems are mounted.
df -h
Increase Swap Space
Get the current amount of swap space.
swapon -s
You can also use
grep -i swap /proc/meminfo
orfree -h
.Create a swap file.
sudo dd if=/dev/zero of=/.swapfile2 bs=1024 count=1048576
Change the permissions on the swap file.
sudo chmod 600 /.swapfile2
Without this change, the
mkswap
andswapon
commands will display a warning about insecure permissions, as the file defaults to permissions of 0644.Initialize the swap file.
sudo mkswap /.swapfile2
Enable the swap file.
sudo swapon /.swapfile2
Check the updated amount of swap space.
swapon -s
If you need the additional swap space after a reboot, add the new swap file to the
/etc/fstab
file.
Remove Partitions and Additional Swap Space
Remove the Test and Dev entries from
/etc/fstab
.sudo sed -i "/^$(blkid /dev/sdb1 | awk '{print $3}')/d" /etc/fstab sudo sed -i "/^$(blkid /dev/sdc1 | awk '{print $3}')/d" /etc/fstab sudo systemctl daemon-reload
Run
tail -n 5 /etc/fstab
to confirm the removal of the entries. The-n
option fortail
prints the file's last n lines.Unmount the Test and Dev file systems.
sudo umount /Dev /Test
Delete the partition on
/dev/sdb
.sudo fdisk /dev/sdb
Enter d to delete partition 1.
Enter p to print the partition table and confirm the partitions are gone.
Enter w to save the partition table and exit the
fdisk
utility.
Delete the partition on
/dev/sdc
.sudo fdisk /dev/sdc
Enter d to delete partition 1.
Enter p to print the partition table and confirm the partitions are gone.
Enter w to save the partition table and exit the
fdisk
utility.
Change the
/dev/sdc
label from gpt to msdos.sudo parted /dev/sdc
Enter mklabel msdos to create an msdos label.
Enter Yes to continue.
Enter print to the partition table to confirm the label change.
Enter quit to exit the
parted
utility.
Remove the Dev and Test mount point directories.
sudo rmdir /Dev /Test
Disable swapping to
/.swapfile2
.sudo swapoff /.swapfile2
Remove the
/.swapfile2
file.sudo rm /.swapfile2
Next Steps
You should now be able to partition a disk and then format it with a file system of your choice. Check out our other storage management content on the Oracle Linux Training Station.