Introduction
Welcome to the lab on configuring storage devices in Linux. Managing storage is a fundamental skill for any Linux system administrator. When you add a new hard drive or a solid-state drive (SSD) to a system, it isn't immediately available for use. You must first prepare it by creating a filesystem and then make it accessible by attaching it to the directory tree.
In this lab, you will walk through the complete lifecycle of adding a new storage device. You will learn how to:
- Identify a newly attached disk.
- Create a standard Linux filesystem (
ext4) on the disk. - Mount the disk to a directory to make it accessible.
- Verify its usage and write data to it.
- Safely unmount and detach the disk.
We will be using commands such as lsblk, mkfs.ext4, mount, df, and umount. By the end of this lab, you will have a solid understanding of how to manage basic storage devices in a Linux environment.
Attach Virtual Disk
In this lab environment, a virtual disk has been created for you through an automated setup process. This simulates adding a new storage device to a system. The setup script creates a 100MB virtual disk file and associates it with a loop device, then creates a symbolic link /dev/sdb for consistency.
Your first task is to confirm that the operating system can see this new disk. The lsblk (list block devices) command is perfect for this. It displays information about all available block devices in a tree-like format.
Run the following command in your terminal to list the block devices:
lsblk
You should see an output similar to the following. Look for a 100MB loop device (it might be named loop4, loop14, or another available loop device number) that is not mounted and has no mount points listed.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 91.4M 1 loop /snap/lxd/35819
...
loop4 7:4 0 100M 0 loop
...
vda 252:0 0 40G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 200M 0 part /boot/efi
└─vda3 252:3 0 39.8G 0 part /
This confirms the disk is recognized by the system and ready for the next step: formatting.
If you don't see the loop device, you can run the following command to create it:
## Create a 100MB file to act as our virtual disk
dd if=/dev/zero of=/tmp/disk.img bs=1M count=100 &> /dev/null
## Find the next available loop device
LOOP_DEVICE=$(sudo losetup -f)
## Associate the file with a loop device, simulating a physical disk
sudo losetup $LOOP_DEVICE /tmp/disk.img
## Create a symbolic link for consistency in the lab
sudo ln -sf $LOOP_DEVICE /dev/sdb
Then run the lsblk command again to confirm the loop device is created.
Format Disk with mkfs.ext4 /dev/sdb Command
In this step, you will format the new disk. An unformatted disk is like a blank slate; the operating system doesn't know how to store files on it. Formatting, also known as creating a filesystem, writes a data structure to the disk that allows it to organize and store files.
We will use the ext4 filesystem, which is a reliable and widely used standard for Linux systems. The command to create an ext4 filesystem is mkfs.ext4.
Warning: This command is destructive. It will erase all data on the specified device. Always double-check the device name before running it.
Now, format the /dev/sdb disk. Since this is a privileged operation that modifies a device, you must use sudo.
sudo mkfs.ext4 /dev/sdb
The command will output information about the filesystem it is creating, including the number of inodes and blocks. The output will look something like this:
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 25600 4k blocks and 25600 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
The disk /dev/sdb is now formatted with an ext4 filesystem and is ready to be mounted.
Mount Disk to /mnt/data Directory
In this step, you'll make the formatted disk accessible to the filesystem. This process is called "mounting". Mounting attaches the filesystem on a device to a specific directory, known as a "mount point". Once mounted, you can read from and write to the device by accessing the mount point directory.
First, we need to create a directory to serve as the mount point. It's a common convention to use subdirectories within /mnt for temporary mounts. Let's create a directory named /mnt/data.
sudo mkdir /mnt/data
Now, use the mount command to attach the /dev/sdb device to the /mnt/data directory. The syntax is sudo mount [device] [mount_point].
sudo mount /dev/sdb /mnt/data
The command should execute without any output if successful. To verify that the disk is mounted, you can use the lsblk command again.
lsblk
Notice in the output below that the loop device (which /dev/sdb points to) now shows /mnt/data as its MOUNTPOINT.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 91.4M 1 loop /snap/lxd/35819
...
loop4 7:4 0 100M 0 loop /mnt/data
...
vda 252:0 0 40G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 200M 0 part /boot/efi
└─vda3 252:3 0 39.8G 0 part /
The disk is now ready for use.
Check Disk Usage with df -h Command
In this step, you will verify that the mounted disk is usable and check its available space. The df (disk free) command is used to report filesystem disk space usage. Using the -h (--human-readable) flag makes the output easier to read by printing sizes in powers of 1024 (e.g., K, M, G).
Run the df -h command to see all mounted filesystems, including our new one:
df -h
Look for the line corresponding to /dev/sdb in the output. It will show the total size, used space, available space, and the mount point.
Filesystem Size Used Avail Use% Mounted on
...
/dev/sdb 98M 2.5M 89M 3% /mnt/data
...
Now that the disk is mounted, the /mnt/data directory acts as the root of the new disk. Let's write a file to it to confirm it's working. We'll use tee with sudo because the mount point is owned by root.
echo "Hello LabEx" | sudo tee /mnt/data/test.txt
You can verify the file was created by listing the contents of the directory:
ls /mnt/data
The output should show your new file:
test.txt
You have successfully formatted, mounted, and written data to your new storage device.
Unmount and Detach Disk
In this final step, you will learn how to safely unmount and detach the storage device. It is critical to unmount a filesystem before the underlying device is removed from the system. This ensures that all pending data is written to the disk, preventing data loss or corruption.
The command to unmount a filesystem is umount. You can specify either the device name or the mount point. We will use the mount point.
sudo umount /mnt/data
To confirm the disk has been unmounted, run lsblk one more time. You will see that the loop device no longer has a mount point associated with it.
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 91.4M 1 loop /snap/lxd/35819
...
loop4 7:4 0 100M 0 loop
...
vda 252:0 0 40G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 200M 0 part /boot/efi
└─vda3 252:3 0 39.8G 0 part /
Now that the filesystem is unmounted, we can "detach" the virtual disk. In our simulated environment, this means dissociating the loop device. Since /dev/sdb is a symbolic link to the actual loop device, we need to resolve it to the actual device path to detach it correctly.
## Detach the loop device by resolving the symbolic link
sudo losetup -d $(readlink -f /dev/sdb)
## Remove the symbolic link
sudo rm /dev/sdb
Finally, let's clean up by removing the mount point directory we created.
sudo rmdir /mnt/data
You have now successfully completed the full cycle of adding, using, and removing a storage device.
Summary
Congratulations on completing the lab! You have successfully learned the essential workflow for configuring a new storage device in a Linux environment.
In this lab, you practiced the following key skills:
- Identifying new block devices using the
lsblkcommand. - Creating a filesystem on a raw disk with
mkfs.ext4. - Creating a mount point with
mkdir. - Mounting a filesystem to make it accessible using the
mountcommand. - Checking disk usage with
df -h. - Safely unmounting a filesystem with the
umountcommand. - Detaching a loop device with
losetup -dto simulate disk removal.
These are fundamental operations for managing storage on any Linux server or workstation. You can now apply this knowledge to manage real hard drives, SSDs, or USB drives. We encourage you to continue exploring more advanced storage topics such as Logical Volume Management (LVM), RAID, and different filesystem types.



