Keywords: Linux | Storage Devices | Block Devices | fdisk | lsblk | Bash Script
Abstract: This article provides an in-depth analysis of methods to identify all writable storage devices on a Linux machine, regardless of mount status. It covers commands such as reading /proc/partitions, using fdisk, lsblk, and others, with code examples and comparisons to assist system administrators and developers in efficient storage device detection.
In Linux systems, it is often necessary to identify all storage devices, including those that are not mounted, for tasks such as system administration, scripting, or troubleshooting. Based on the Q&A data and reference articles, this paper details various command-line methods, focusing on core commands like /proc/partitions and file -s, while supplementing with other tools. The content is structured logically, from basic to advanced techniques, with step-by-step explanations of each command's usage, advantages, disadvantages, and applicable scenarios, ensuring a comprehensive understanding of storage device detection.
Overview of Storage Device Detection Methods
Linux offers multiple approaches to list storage devices, including virtual file systems, disk utilities, and hardware listing commands. Each method has its characteristics and is suited for different contexts, such as script automation or interactive queries. Below, we introduce the main commands with code examples.
Using the /proc/partitions File
/proc/partitions is a virtual file that contains information about all block devices recognized by the kernel. Reading this file with the cat command provides details on major and minor numbers, block counts, and device names. For example:
cat /proc/partitionsSample output might show:
major minor #blocks name
8 0 117220824 sda
8 1 524288 sda1This method outputs concise data that is easy to parse, making it ideal for use in Bash scripts. To further determine the filesystem type of a partition, the file command with the -s option can be employed:
file -s /dev/sda1This may return results such as /dev/sda1: Linux rev 1.0 ext4 filesystem data, aiding in identifying filesystems on unmounted devices.
Using the fdisk Command
fdisk is a traditional disk partitioning tool. The fdisk -l command lists all disks and partitions with detailed information, including disk model, size, partition table type, and more. Note that this command often requires root privileges. Example command:
fdisk -lThe output is detailed but may not recognize all devices, such as certain external storage cards. Referencing the Q&A data, fdisk works well in environments like EC2 instances but has limitations in other scenarios.
Using the lsblk Command
The lsblk command lists block devices in a tree structure, clearly showing the hierarchy of disks and partitions, including mount points. Command usage:
lsblkSample output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 111.8G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda5 8:5 0 111.3G 0 part /This approach is intuitive and easy to read, suitable for quick overviews of device relationships, though it does not include full device paths in the output.
Other Supplementary Methods
Beyond the above commands, tools like lshw, parted, and sfdisk can be used. The lshw command filters hardware information to show disk details, including serial numbers and bus info, using the -class disk parameter:
lshw -class diskThe parted command offers functionality similar to fdisk for partition management:
parted -lsfdisk is an advanced version of fdisk, with output resembling parted. These tools are more effective in specific cases, such as when parted handles devices that fdisk cannot recognize.
Comparison and Best Practices
Integrating insights from the Q&A and reference articles, /proc/partitions combined with file -s is a robust method for detecting all writable storage devices, especially in scripting contexts. fdisk and parted provide detailed data but may require sudo, while lsblk is user-friendly for interactive use. In practice, select tools based on needs: for automation, prefer /proc/partitions; for in-depth analysis, use fdisk or parted. Avoid directly probing devices in /dev to enhance efficiency and security.