Linux is an open source operating system that is widely used in the server field. Linux systems support a variety of hardware devices and provide a wealth of commands and tools to manage these devices. Among them, block devices are an important part of the Linux system and can be used to store data, such as hard drives, SSDs and other devices. This article will introduce how to quickly view block device information in a Linux system and provide specific code examples.
1. Common commands for viewing block device information
lsblk
After running the above command, all block device information in the system will be listed, including device name, size, mount point and other details. .
fdisk -l /dev/sda
The above command will list the partition information of the device /dev/sda, including partition number, starting sector, size, etc.
blkid
The above command will list the UUID information of all block devices in the system to uniquely identify each block device.
2. Use code examples to view block device information
The following is a simple Python code example to obtain information about all block devices in the system and output it to the console:
import os def get_block_devices(): block_devices = [] devices = os.listdir('/sys/block/') for device in devices: if device.startswith('sd') or device.startswith('nvme'): device_path = os.path.join('/sys/block/', device) with open(os.path.join(device_path, 'size')) as f: size = int(f.read().strip()) * 512 / 1024 / 1024 / 1024 block_devices.append({'device': device, 'size': size}) return block_devices if __name__ == '__main__': block_devices = get_block_devices() for device in block_devices: print('Device: %s, Size: %.2fGB' % (device['device'], device['size']))
The above code is written in Python. It obtains the information of all block devices in the system by reading the files in the /sys/block/ directory, and outputs the device name and size information.
Through the above commands and code examples, we can quickly understand how to view block device information in Linux systems. These tools and codes can help us better manage and understand the storage devices in the system, and improve the operating efficiency and stability of the system. Hope this article helps you!
The above is the detailed content of A concise guide on how to view Linux block device information. For more information, please follow other related articles on the PHP Chinese website!