First, the situation: I've got a Linux computer with two eSATA drive bays that accept removable SSD drives. I'm trying to write a little GUI application that makes it easier for the user to mount/unmount/format/backup/etc the drives that he puts into these bays.
It all mostly works. One small problem, however, is that I don't know how to find out any information about what's on the inserted drive(s) until after the drives have been successfully mounted.
So, for example, if the user inserts a drive that I can't mount (e.g. because it is unformatted, or formatted with an unexpected filesystem), all my app can say about it is "Drive failed to mount".
This isn't very satisfactory, because if the drive is unformatted, the user will probably want to format it... but if the drive contains data from an unrecognized filesystem, the user will probably NOT want to format it.... or at least, I want to be able to warn him that by doing so he'll be erasing potentially valuable data.
So my question is: is there any method for querying some basic information (especially filesystem-type) from a drive that doesn't require that the drive already be mounted? Or do I just have to try to mount it with various known filesystems until one of the mount attempts succeeds, and give a vague "be careful" message if none of them do?
In case it matters, the paths I use to mount the drives in the drive bays are:
/dev/disk/by-path/pci-0000:00:1f.2-scsi-2:0:0:0
/dev/disk/by-path/pci-0000:00:1f.2-scsi-1:0:0:0
If the drives are unmounted there are several things you can do.
You can use a command like
fdisk -l
orsfdisk -l
to list the partitions. Just the partition type may give you some useful information if the partitions where setup correct.yIf it is present on your system you can use the command
vol_id
against a partition to return some useful details (part of the udev package on Debian). This will generally tell you what filesystem is actually being used.The command
lshw -class disk
will give you some details about the type of drive. You might want to use this if you are curious about the actual serial number of the drive.If you are sure the there is a particular filesystem like ext2/3 on it then you can use the filesystem specific tune2fs tool to examine more details.
Another useful tool is lsblk.
If you have parted installed you can run a command like this
Anyway past that I suggest you take a look at the udev or parted source.
Another useful command is
blkid
- it returns similar information tovol_id
but can also scan all devices in the system, rather than requiring a device to be passed in.To force
blkid
to scan all devices instead of using cached information, runblkid -c /dev/null
(you need read permission on the block devices so you'll usually need to run it as root)Here's one suggestion from IBM: SCSI - Hot add, remove, rescan of SCSI devices: Rescan of a SCSI Device. This will rescan that SCSI address for new devices, and then you'll be able to read the information in /var/log/messages . Some other disk tools will also work, without you mounting the drive.
I actually tried somethign slightly different yesterday, and it worked (RHEL4 system):
A simple overview of all partitions found by the kernel gives