Linux reading block device

Check Linux Block Device with Examples

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.

$ lsblk
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

Читайте также:  Running linux applications on mac

[ ~]$ sudo file -s /dev/xvdf
/dev/xvdf: data

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.

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.

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
nvme0n1
├─nvme0n1p1 xfs / 90e29211-2de8-4967-b0fb-16f51a6e464c /
└─nvme0n1p128
nvme2n1

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

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

Read/Write block directly to disk in linux kernel

As the title has said, I’m looking for how I can read/write blocks directly to disk in linux kernel space (bypassing the file system) and directly interact with block IO layer. After reading through some kernel codes, I realize bio is the structure I should be using to achieve such goal in block IO layer. But I don’t quite understand the structures of bio and haven’t figure out how exactly I can do that. Any helps? Thank you

2 Answers 2

If you’re only doing something simple, you don’t really need to mess with BIO. What you can do instead is to simply open the block device ( /dev/whatever ) as if it was a file. The kernel will do the right thing and will give you the «thin» wrapper for read/write operations.

In regard to opening the file from the kernel space, there are few questions here, already answered, like this one:

If you want to do anything more fancy, you will have to study the sources of the FS drivers (in the fs/ subdirectory) to hunt for examples.

I’ve looked at the link above. The solution doesn’t work any more for linux 2.6 and later, vfs_read() requires a buffer pointer from user space. And that user space pointer has been passed deeper into do_generic_file_aio_read() in mm/filemap.c. I got lost from this place. I’m thinking about how I can get data into kernel space instead of into user space. Any hints? THX

Читайте также:  Linux удалить скрытую папку

lxr.free-electrons.com/source/fs/exec.c#L799 — it seems, set_fs does exactly what you want (as mentioned in the linked question).

Already tried it out. Works like a charm. Thank you, oakad. For more information if any one else is looking at this issue, another useful link is : linuxjournal.com/article/8110?page=0,1 Though the link talks about this is not something you want to do, it shows how to do it anyway later in the article.

Источник

How to Use the lsblk Command to List Block Devices on Linux

The lsblk command lets you list block devices attached to your machine. Here’s how you can use it on Linux.

user listing block devices on a linux terminal

Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

lsblk is a command-line utility used for listing block devices on a Linux system. Block devices consist of storage devices that hold data in the form of blocks, which are, typically, hard disk drives (HDDs) or solid-state drives (SSDs).

The command displays information about block devices in a tree-like structure, with each device represented by a line in the output. The command gets its information from the sysfs file system. Note that lsblk doesn’t identify Random Access Memory (RAM) as a block device.

Installing lsblk on Linux

The lsblk command comes as part of the util-linux package. util-linux is a package of essential utilities for Linux systems that provides a wide range of functionality, including tools for managing files, disks, and system resources.

Some of the tools in the util-linux package include:

  • fdisk: A utility for partitioning disks
  • partx: Adds and removes partition definitions from the kernel
  • swapon: Enables and disables swap devices and files

Your system would most probably have the util-linux package already installed, but in case it’s not, you can install it as follows depending on your Linux distro.

On Debian-based distros such as Ubuntu or MX Linux:

sudo apt-get install util-linux 

On RHEL and its derivatives:

sudo yum install util-linux-ng 

On Arch-based Linux distros such as Manjaro:

Displaying Block Devices Using lsblk

To display all block devices on your system, simply run the command:

Источник

Оцените статью
Adblock
detector