[PATCH] read drive serial number

Kay Sievers kay.sievers at vrfy.org
Sun Aug 1 13:49:32 PDT 2004


On Sun, Aug 01, 2004 at 10:11:07PM +0200, David Zeuthen wrote:
> On Sun, 2004-08-01 at 00:30 +0200, Kay Sievers wrote:
> > Here is the code, that reads the serial number of a drive, directly
> > connected to an ATA or SCSI bus. HAL probes for the values and adds:
> > 
> > I changed the compute_udi for block devices. If we get a serial number
> > or a uuid we use it to compute the udi. 
> 
> I've added code such that if we don't get these we take the UDI of the
> physical device backing the block device (using the string property
> storage.physical_device) and append the SCSI LUN. However, this only
> works if there is SCSI inbetween so we may fall back to major/minor
> naming.
> 
> So, for my USB floppy drive I get (note that this particular device
> hasn't got a USB serial number)
> 
>  /org/freedesktop/Hal/devices/block_usb_57b_0_601_-1_noserial-lun0
> 
> and for one of my 6in1 readers I get these (note the USB serial number)
> 
>  /org/freedesktop/Hal/devices/block_usb_424_20fc_129_-1_00005000806E-lun0
>  /org/freedesktop/Hal/devices/block_usb_424_20fc_129_-1_00005000806E-lun1
>  /org/freedesktop/Hal/devices/block_usb_424_20fc_129_-1_00005000806E-lun2
>  /org/freedesktop/Hal/devices/block_usb_424_20fc_129_-1_00005000806E-lun3
> 
> which is kind of nice as it's very unique. 

Yes, looks very nice. We're getting closer :)

> At this point, I think the only case that remains to be solved are
> volumes missing both label and UUID - this is especially true for e.g.
> swap partitions. 

Should we use the parent udi and the partition number or the starting
disk block for the udi composing?

Attached is a trivial cleanup of the enum names, cause 'ALL' is not a very
good name in a global namespace. I've prepended the libname to all names.
Sorry, but I still not have CVS write access.

Thanks,
Kay
-------------- next part --------------
Index: hald/linux/block_class_device.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux/block_class_device.c,v
retrieving revision 1.48
diff -u -r1.48 block_class_device.c
--- hald/linux/block_class_device.c	1 Aug 2004 19:56:43 -0000	1.48
+++ hald/linux/block_class_device.c	1 Aug 2004 20:33:22 -0000
@@ -1071,7 +1071,7 @@
 	if (vid == NULL)
 		return;
 
-	rc = volume_id_probe(vid, ALL);
+	rc = volume_id_probe(vid, VOLUME_ID_ALL);
 	if (rc != 0) {
 		volume_id_close(vid);
 		return;
@@ -1296,7 +1296,7 @@
 
 		device_file = hal_device_property_get_string (d, "block.device");
 		did = drive_id_open_node(device_file);
-		if (drive_id_probe(did, DID_ATA) == 0) {
+		if (drive_id_probe(did, DRIVE_ID_ATA) == 0) {
 			if (did->serial[0] != '\0')
 				hal_device_property_set_string (stordev, 
 								"storage.serial",
@@ -1399,7 +1399,7 @@
 
 		device_file = hal_device_property_get_string (d, "block.device");
 		did = drive_id_open_node(device_file);
-		if (drive_id_probe(did, DID_SCSI) == 0) {
+		if (drive_id_probe(did, DRIVE_ID_SCSI) == 0) {
 			if (did->serial[0] != '\0')
 				hal_device_property_set_string (stordev,
 								"storage.serial",
Index: hald/linux/drive_id/drive_id.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux/drive_id/drive_id.c,v
retrieving revision 1.1
diff -u -r1.1 drive_id.c
--- hald/linux/drive_id/drive_id.c	1 Aug 2004 19:13:56 -0000	1.1
+++ hald/linux/drive_id/drive_id.c	1 Aug 2004 20:33:22 -0000
@@ -7,8 +7,8 @@
  *	of ATA or SCSI devices.
  *	
  *	Note:	Native interface access is needed. There is no way to get
- *		these kind from a device behind e.g. a USB adapter. A good
- *		bridge reads these values from the disk and provides them
+ *		these kind from a device behind a USB adapter. A good
+ *		bridge reads these values from the device and provides them
  *		as USB config strings.
  *
  *	This library is free software; you can redistribute it and/or
@@ -199,13 +199,13 @@
 		return -1;
 
 	switch(type) {
-	case DID_ATA:
+	case DRIVE_ID_ATA:
 		rc = probe_ata(id);
 		break;
-	case DID_SCSI:
+	case DRIVE_ID_SCSI:
 		rc = probe_scsi(id);
 		break;
-	case DID_ALL:
+	case DRIVE_ID_ALL:
 	default:
 		rc = probe_ata(id);
 		if (rc == 0)
Index: hald/linux/drive_id/drive_id.h
===================================================================
RCS file: /cvs/hal/hal/hald/linux/drive_id/drive_id.h,v
retrieving revision 1.1
diff -u -r1.1 drive_id.h
--- hald/linux/drive_id/drive_id.h	1 Aug 2004 19:13:56 -0000	1.1
+++ hald/linux/drive_id/drive_id.h	1 Aug 2004 20:33:22 -0000
@@ -21,6 +21,8 @@
 #ifndef _DRIVE_ID_H_
 #define _DRIVE_ID_H_
 
+#define DRIVE_ID_VERSION		002
+
 #define DRIVE_ID_VENDOR_SIZE		8
 #define DRIVE_ID_MODEL_SIZE		40
 #define DRIVE_ID_SERIAL_SIZE		20
@@ -30,9 +32,9 @@
 
 
 enum drive_type {
-	DID_ALL,
-	DID_SCSI,
-	DID_ATA
+	DRIVE_ID_ALL,
+	DRIVE_ID_SCSI,
+	DRIVE_ID_ATA
 };
 
 struct drive_id {
Index: hald/linux/volume_id/volume_id.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux/volume_id/volume_id.c,v
retrieving revision 1.17
diff -u -r1.17 volume_id.c
--- hald/linux/volume_id/volume_id.c	29 Jul 2004 20:58:37 -0000	1.17
+++ hald/linux/volume_id/volume_id.c	1 Aug 2004 20:33:23 -0000
@@ -283,10 +283,10 @@
 
 	if ((le32_to_cpu(es->feature_compat) &
 	     EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0) {
-		id->fs_type = EXT3;
+		id->fs_type = VOLUME_ID_EXT3;
 		id->fs_name = "ext3";
 	} else {
-		id->fs_type = EXT2;
+		id->fs_type = VOLUME_ID_EXT2;
 		id->fs_name = "ext2";
 	}
 
@@ -338,7 +338,7 @@
 	set_label_string(id, rs->label, 16);
 	set_uuid(id, rs->uuid, 16);
 
-	id->fs_type = REISER;
+	id->fs_type = VOLUME_ID_REISER;
 	id->fs_name = "reiser";
 
 	return 0;
@@ -372,7 +372,7 @@
 	set_label_string(id, xs->fname, 12);
 	set_uuid(id, xs->uuid, 16);
 
-	id->fs_type = XFS;
+	id->fs_type = VOLUME_ID_XFS;
 	id->fs_name = "xfs";
 
 	return 0;
@@ -406,7 +406,7 @@
 	set_label_string(id, js->label, 16);
 	set_uuid(id, js->uuid, 16);
 
-	id->fs_type = JFS;
+	id->fs_type = VOLUME_ID_JFS;
 	id->fs_name = "jfs";
 
 	return 0;
@@ -459,7 +459,7 @@
 	set_label_string(id, vs->label, 11);
 	set_uuid(id, vs->serno, 4);
 
-	id->fs_type = VFAT;
+	id->fs_type = VOLUME_ID_VFAT;
 	id->fs_name = "vfat";
 
 	return 0;
@@ -534,7 +534,7 @@
 	set_label_string(id, ms->label, 11);
 	set_uuid(id, ms->serno, 4);
 
-	id->fs_type = MSDOS;
+	id->fs_type = VOLUME_ID_MSDOS;
 	id->fs_name = "msdos";
 
 	return 0;
@@ -684,7 +684,7 @@
 		set_label_unicode16(id, vd->type.primary.ident.c, BE,31);
 
 found:
-	id->fs_type = UDF;
+	id->fs_type = VOLUME_ID_UDF;
 	id->fs_name = "udf";
 
 	return 0;
@@ -725,7 +725,7 @@
 	return -1;
 
 found:
-	id->fs_type = ISO9660;
+	id->fs_type = VOLUME_ID_ISO9660;
 	id->fs_name = "iso9660";
 
 	return 0;
@@ -907,7 +907,7 @@
 	return -1;
 
 found:
-	id->fs_type = UFS;
+	id->fs_type = VOLUME_ID_UFS;
 	id->fs_name = "ufs";
 
 	return 0;
@@ -1064,7 +1064,7 @@
 	part = (struct mac_partition *) buf;
 	if ((strncmp(part->signature, "PM", 2) == 0) &&
 	    (strncmp(part->type, "Apple_partition_map", 19) == 0)) {
-		id->fs_type = MACPARTMAP;
+		id->fs_type = VOLUME_ID_MACPARTMAP;
 		id->fs_name = "mac_partition_map";
 		return 0;
 	}
@@ -1147,7 +1147,7 @@
 		set_label_string(id, hfs->label, hfs->label_len) ;
 	}
 
-	id->fs_type = HFS;
+	id->fs_type = VOLUME_ID_HFS;
 	id->fs_name = "hfs";
 
 	return 0;
@@ -1204,7 +1204,7 @@
 	set_label_unicode16(id, key->unicode, BE, label_len);
 
 found:
-	id->fs_type = HFSPLUS;
+	id->fs_type = VOLUME_ID_HFSPLUS;
 	id->fs_name = "hfsplus";
 
 	return 0;
@@ -1361,7 +1361,7 @@
 	}
 
 found:
-	id->fs_type = NTFS;
+	id->fs_type = VOLUME_ID_NTFS;
 	id->fs_name = "ntfs";
 
 	return 0;
@@ -1387,7 +1387,7 @@
 	return -1;
 
 found:
-	id->fs_type = SWAP;
+	id->fs_type = VOLUME_ID_SWAP;
 	id->fs_name = "swap";
 
 	return 0;
@@ -1402,46 +1402,46 @@
 		return -EINVAL;
 
 	switch (fs_type) {
-	case EXT3:
-	case EXT2:
+	case VOLUME_ID_EXT3:
+	case VOLUME_ID_EXT2:
 		rc = probe_ext(id);
 		break;
-	case REISER:
+	case VOLUME_ID_REISER:
 		rc = probe_reiser(id);
 		break;
-	case XFS:
+	case VOLUME_ID_XFS:
 		rc = probe_xfs(id);
 		break;
-	case JFS:
+	case VOLUME_ID_JFS:
 		rc = probe_jfs(id);
 		break;
-	case MSDOS:
+	case VOLUME_ID_MSDOS:
 		rc = probe_msdos(id);
 		break;
-	case VFAT:
+	case VOLUME_ID_VFAT:
 		rc = probe_vfat(id);
 		break;
-	case UDF:
+	case VOLUME_ID_UDF:
 		rc = probe_udf(id);
 		break;
-	case ISO9660:
+	case VOLUME_ID_ISO9660:
 		rc = probe_iso9660(id);
 		break;
-	case MACPARTMAP:
-	case HFS:
-	case HFSPLUS:
+	case VOLUME_ID_MACPARTMAP:
+	case VOLUME_ID_HFS:
+	case VOLUME_ID_HFSPLUS:
 		rc = probe_hfs_hfsplus(id);
 		break;
-	case UFS:
+	case VOLUME_ID_UFS:
 		rc = probe_ufs(id);
 		break;
-	case NTFS:
+	case VOLUME_ID_NTFS:
 		rc = probe_ntfs(id);
 		break;
-	case SWAP:
+	case VOLUME_ID_SWAP:
 		rc = probe_swap(id);
 		break;
-	case ALL:
+	case VOLUME_ID_ALL:
 	default:
 		/* read only minimal buffer, cause of the slow floppies */
 		rc = probe_vfat(id);
@@ -1516,9 +1516,11 @@
 	struct volume_id *id;
 	int fd;
 
-	fd = open(path, O_RDONLY);
-	if (fd < 0)
+	fd = open(path, O_RDONLY | O_NONBLOCK);
+	if (fd < 0) {
+		dbg("unable to open '%s'", path);
 		return NULL;
+	}
 
 	id = volume_id_open_fd(fd);
 	if (id == NULL)
Index: hald/linux/volume_id/volume_id.h
===================================================================
RCS file: /cvs/hal/hal/hald/linux/volume_id/volume_id.h,v
retrieving revision 1.7
diff -u -r1.7 volume_id.h
--- hald/linux/volume_id/volume_id.h	29 Jul 2004 20:58:37 -0000	1.7
+++ hald/linux/volume_id/volume_id.h	1 Aug 2004 20:33:23 -0000
@@ -21,7 +21,7 @@
 #ifndef _VOLUME_ID_H_
 #define _VOLUME_ID_H_
 
-#define VOLUME_ID_VERSION		006
+#define VOLUME_ID_VERSION		007
 
 #define VOLUME_ID_LABEL_SIZE		64
 #define VOLUME_ID_UUID_SIZE		16
@@ -30,22 +30,22 @@
 
 
 enum filesystem_type {
-	ALL,
-	SWAP,
-	EXT2,
-	EXT3,
-	REISER,
-	XFS,
-	JFS,
-	MSDOS,
-	VFAT,
-	UDF,
-	ISO9660,
-	NTFS,
-	HFS,
-	HFSPLUS,
-	MACPARTMAP,
-	UFS
+	VOLUME_ID_ALL,
+	VOLUME_ID_SWAP,
+	VOLUME_ID_EXT2,
+	VOLUME_ID_EXT3,
+	VOLUME_ID_REISER,
+	VOLUME_ID_XFS,
+	VOLUME_ID_JFS,
+	VOLUME_ID_MSDOS,
+	VOLUME_ID_VFAT,
+	VOLUME_ID_UDF,
+	VOLUME_ID_ISO9660,
+	VOLUME_ID_NTFS,
+	VOLUME_ID_HFS,
+	VOLUME_ID_HFSPLUS,
+	VOLUME_ID_MACPARTMAP,
+	VOLUME_ID_UFS
 };
 
 struct volume_id {
-------------- next part --------------
_______________________________________________
hal mailing list
hal at freedesktop.org
http://freedesktop.org/mailman/listinfo/hal


More information about the Hal mailing list