A block device is a storage device that moves data in sequences of bytes or bits (blocks). These devices support random access and generally use buffered I/O.
Examples include hard disks, CD-ROM drives, and flash drives. A block device can be physically attached to a computer or accessed remotely as if it were physically attached to the computer.
How to use block device on Linux
Device names like /dev/sdh and xvdh are used by Linux systems to describe block devices. The block device mapping is used by the Linux system to specify the block devices to attach to a Linux OS.
After a block device is attached to a host, it must be mounted by the operating system before we can access the storage device. When a block device is detached from a host, it is unmounted by the operating system and we can no longer access the storage device.
How to list block devices on Linux
Use the lsblk command to view our available disk devices and their mount points (if applicable) to help us determine the correct device name to use. The output of lsblk removes the /dev/ prefix from full device paths.
In the following example, the root device is /dev/nvme0n1, which has two partitions named nvme0n1p1 and nvme0n1p128. The attached volume is /dev/nvme1n1, which has no partitions and is not yet mounted.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1 259:0 0 10G 0 disk
nvme0n1 259:1 0 8G 0 disk
nvme0n1p1 259:2 0 8G 0 part /
nvme0n1p128 259:3 0 1M 0 part
How to check if there is a filesystem on the block device
Use the file -s command to get information about a specific device, such as its file system type. If the output shows simply data, as in the following example output, there is no file system on the device
[ ~]$ sudo file -s /dev/xvdf
If the device has a file system, the command shows information about the file system type. For example, the following output shows a root device with the XFS file system.
[ ~]$ sudo file -s /dev/xvda1
/dev/xvda1: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)
How to create a filesystem on the block device
If we have an empty volume, use the mkfs -t command to create a file system on the volume.
Warning: Do not use this command if we’re mounting a volume that already has data on it. Otherwise, we’ll format the volume and delete the existing data.
[ ~]$ sudo mkfs -t xfs /dev/xvdf
How to get the block device info on Linux
Use the lsblk -f command to get information about all of the devices attached to the instance.
[ ~]$ sudo lsblk -f
For example, the following output shows that there are three devices attached to the instances—nvme1n1, nvme0n1, and nvme2n1. The first column lists the devices and their partitions.
The FSTYPE column shows the file system type for each device. If the column is empty for a specific device, it means that the device does not have a file system. In this case, device nvme1n1 and partition nvme0n1p1 on device nvme0n1 are both formatted using the XFS file system, while device nvme2n1 and partition nvme0n1p128 on device nvme0n1 do not have file systems.
NAME FSTYPE LABEL UUID MOUNTPOINT
nvme1n1 xfs 7f939f28-6dcc-4315-8c42-6806080b94dd
├─nvme0n1p1 xfs / 90e29211-2de8-4967-b0fb-16f51a6e464c /
How to mount a block device in Linux
We can use the device name, such as /dev/xvdf, in /etc/fstab, but we recommend using the device’s 128-bit universally unique identifier (UUID) instead.
Device names can change, but the UUID persists throughout the life of the partition. By using the UUID, we reduce the chances that the system becomes unbootable after a hardware reconfiguration.
The fields are the UUID value returned by blkid (or lsblk for Ubuntu 18.04), the mount point, the file system, and the recommended file system mount options.
In the following example, we mount the device with UUID aebf131c-6957-451e-8d34-ec978d9581ae to mount point /data and we use the xfs file system. We also use the defaults and nofail flags. We specify 0 to prevent the file system from being dumped, and we specify 2 to indicate that it is a non-root device.
we need to add the following line to /etc/fstab then run mount /data.
UUID=aebf131c-6957-451e-8d34-ec978d9581ae /data xfs defaults,nofail 0 2