1. Overview

Modern Linux distributions can discover hardware changes and automatically mount drives. Besides automatic mounting, Linux has a lot of tools to query storage devices whether they are mounted or not.

In this tutorial, we’ll see various Linux commands to get information about block devices that aren’t mounted.

2. Test Case

For our testing purpose, we’ll use an unformatted USB flash drive. Because of this, the drive won’t automatically mount on our system. We can use the mount command to verify it:

$ mount | grep '^/dev'
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)

As we can see, only the root filesystem is printed, which corresponds to the /dev/sda1 partition. A key point here is that we’ve filtered the mount command output with grepAs a result, we printed partitions whose name starts with the /dev keyword. In other words, we’ve printed only the mounted partitions that reside in a system device.

Alternatively, we can use the df command that prints all mounted partitions together with disk usage information:

$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             1528676       0   1528676   0% /dev
tmpfs             309888    9012    300876   3% /run
/dev/sda1      112153320 9447624  96985532   9% /
...

Indeed, we can see that the only mounted block device is the sda1, the hard drive.

3. The lsblk Command

The lsblk command lists block devices. Specifically, it draws its information from the sysfs filesystem and the udev subsystem. We can use this command to find the drive letter assigned to a block device:

$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 111,8G 0 disk 
├─sda1 8:1 0 108,8G 0 part /
├─sda2 8:2 0 1K 0 part 
└─sda5 8:5 0 3G 0 part [SWAP]
sdb 8:16 1 3,7G 0 disk 
sr0 11:0 1 1024M 0 rom

As expected, we can see that the command printed two disks:

  1. sda: the hard drive of our system
  2. sdb: the USB flash drive

Another key point is that the command also prints storage capacity and the mount point if the drive is mounted. We notice that sdb has an empty mount point, meaning that it isn’t mounted.

In addition, we can use the -f option to print filesystem information about the devices:

$ lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda 
├─sda1 ext4 0b3e771e-a765-4c57-8ac9-5c4540d829b7 /
├─sda2 
└─sda5 swap eb4c6b72-118e-462d-8382-8383af211717 [SWAP]
sdb   

Here, we can see that the output is different than the previous example. Specifically, the command printed filesystem type, UUID, and label information. Since sdb isn’t formatted, no filesystem information is included.

4. Getting Information From the Filesystem

 The sysfs directory keeps information for block devices under the block sub-directory:

$ ls -l /sys/block
total 0
...
lrwxrwxrwx 1 root root 0 Δεκ 1 2005 sda -> ../devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
lrwxrwxrwx 1 root root 0 Μάι 9 22:19 sdb -> ../devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host6/target6:0:0/6:0:0:0/block/sdb

Indeed, the disk drives of our system were printed. Specifically, there’s a link for each device with the name of the device. Moreover, the link points to a directory in the devices sub-directory. There, the kernel keeps some files with interesting information about the device:

  • stat: for I/O statistics
  • dev: major and minor versions of the device
  • size: the size in sectors

Another way to find drives is to list the /dev directory:

$ ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5  /dev/sdb

As we can see the command printed the sdb drive. A key point here is that Linux assigns names that start with the sd string to SCSI drives. Thus, we’ve limited our listing to these devices in the above example.

5. The fdisk Command

The fdisk command with the -l option, prints information about partitions. If we don’t enter a partition name, it’ll list all available partitions. Particularly, it draws information from the /proc/partitions directory. Let’s run the command for the sdb device:

$ sudo fdisk -l /dev/sdb 
Disk /dev/sdb: 3,7 GiB, 3957325824 bytes, 7729152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

As we can see, the command printed a lot of useful information:

  • size, in GB, bytes, and sectors
  • the sector size
  • I/O size

As we’ve said, fdisk uses the /proc/partitions file to get a list of the available partitions:

$ cat /proc/partitions
major minor  #blocks  name
   8        0  117220824 sda
   8        1  114075648 sda1
   8        2          1 sda2
   8        5    3142656 sda5
  11        0    1048575 sr0
   8       16    3864576 sdb

Indeed, we can see a list of the partitions available on our system. The command listed the sdb drive as a single partition.

Nevertheless, the fdisk command doesn’t provide information about the mount point. So, we can combine fdisk with a command that provides this, like lsblk:

$ sudo fdisk -l /dev/sdb;lsblk /dev/sdb | awk '{print $7}'
Disk /dev/sdb: 3,7 GiB, 3957325824 bytes, 7729152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
MOUNTPOINT

Here we can see that mount point information is added, but it’s empty since the drive isn’t mounted. An important point here is that we’ve reduced fdisk‘s output to the mount point column with awk.

6. The parted Command

The parted command is another great tool for manipulating partitions. Particularly, we can run the parted command together with the -l option to get a list of the partitions of all block devices:

$ sudo parted -l
...
Error: /dev/sdb: unrecognized disk label
Model: Multi Flash Reader (SCSI)                                          
Disk /dev/sdb: 3957MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 

As we can see, the command printed a lot of useful information:

  • disk model
  • disk name and size
  • sector size
  • partition table type

Furthermore, since the drive isn’t formatted there’s no label assigned to it. Consequently, the command prints an error message denoting this.

Alternatively, we can also specify a device and use the print sub-command:

$ sudo parted /dev/sdb print
Error: /dev/sdb: unrecognized disk label
Model: Multi Flash Reader (SCSI)                                          
Disk /dev/sdb: 3957MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 

Indeed, we can see that the command produced the same output as before.

7. The lshw Command

The lshw command is useful if we want to get hardware information about a disk drive. If we run the command with no arguments, it’ll print information about all connected devices. Furthermore, we can narrow down the output with the class option:

$ sudo lshw -class storage
 *-USB 
      description: Mass storage device
      product: Mass Storage Device
      vendor: Generic
      physical id: 1
      bus info: usb@3:1
      logical name: scsi7
      version: 1.20
      serial: 058F091111B
      capabilities: usb-2.00 scsi emulated scsi-host
      configuration: driver=usb-storage maxpower=100mA speed=12Mbit/s
...

In the above example, we set the storage value to the class option. As a result, we got information only about storage devices. Indeed, we can see that there’s an external disk drive connected to the USB port. In addition, the command printed further information about the drive, like the vendor, the version, the serial number, etc.

Similarly to the fdisk command, lshw doesn’t provide an indication if a device is mounted or not. As a result, we can overcome this by using it together with a command like lsblk like we did earlier.

8. The file Command

The file command determines the file type of a file. In addition, we can use the -s option to print the filesystem type of a block device:

$ sudo file -s /dev/sdb
/dev/sdb: data

As expected, the command didn’t locate a filesystem. Instead, it correctly reported that it found some data in the drive. We can create an ext3 filesystem in the sdb drive and try again:

$ sudo file -s /dev/sdb
/dev/sdb: Linux rev 1.0 ext3 filesystem data, UUID=685012df-3ebd-43a2-803c-0d76e8387a12 (large files)

Indeed, we can see that the command correctly found that the filesystem type is ext3. As a result, we can find the filesystem type and some other information about a drive without mounting it.

9. Conclusion

In this article, we examined commands and tools for getting information about unmounted drives. Without a doubt, Linux offers many options when it comes to storage devices, partitions, and filesystems.