Skip to contentSkip to conversation

udev - create all partitions of blockdevice

PATCH In review Kay Sievers 4 participants 7 messages Feb 16, 2004 – Mar 7, 2004 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udev - create all partitions of blockdevice

Here is the first try to create all partitons of a blockdevice, since removable media devices may need to acces the expected partition to revalidate the media.

It uses the attribute syntax introduced with the last %s{file} patch. I'm using this with my multi-slot-flash-card-reader:

SYSFS{model}="USB Storage-SMC ", NAME{all_partitions}="smartmedia" SYSFS{model}="USB Storage-CFC ", NAME{all_partitions}="compactflash" SYSFS{model}="USB Storage-MSC ", NAME{all_partitions}="memorystick" SYSFS{model}="USB Storage-MMC ", NAME{all_partitions}="multimedia"

and I get:

tree /udev/
/udev/

  |-- memorystick
  |-- memorystick1
  |-- memorystick10
  |-- memorystick11
  |-- memorystick12
  |-- memorystick13
  |-- memorystick14
  |-- memorystick15
  |-- memorystick2
  |-- memorystick3
  |-- memorystick4
  |-- memorystick5
  |-- memorystick6
  |-- memorystick7
  |-- memorystick8
  |-- memorystick9
  |-- multimedia
  |-- multimedia1
  |-- multimedia10
  |-- multimedia11
  |-- multimedia12
  |-- multimedia13
  |-- multimedia14
  |-- multimedia15
  |-- multimedia2
  |-- multimedia3
  |-- multimedia4
  |-- multimedia5
  |-- multimedia6
  |-- multimedia7
  |-- multimedia8
  |-- multimedia9

...

If needed, we can make the number of partions to create adjustable with the attribute?

thanks,
Kay

namedev.c +1 -1
diff -Nru a/namedev.c b/namedev.c
@@ -820,7 +820,7 @@
 	/* substitute placeholder */
 	apply_format(udev, udev->name, class_dev, sysfs_device);
 	apply_format(udev, udev->symlink, class_dev, sysfs_device);
-
+	udev->partitions = dev->partitions;
 done:
 	perm = find_perm(udev->name);
 	if (perm) {
namedev.h +23 -18

54 lines not on this page · raw message

namedev_parse.c +14 -8
diff -Nru a/namedev_parse.c b/namedev_parse.c
@@ -91,13 +91,6 @@
 	char *pos;
 	char *attr;
 
-	attr = strchr(str, '_');
-	if (attr != NULL) {
-		attr++;
-		dbg("attribute='%s'", attr);
-		return attr;
-	}
-
 	attr = strchr(str, '{');
 	if (attr != NULL) {
 		attr++;
@@ -111,6 +104,13 @@
 		return attr;
 	}
 
+	attr = strchr(str, '_');
+	if (attr != NULL) {
+		attr++;
+		dbg("attribute='%s'", attr);
+		return attr;
+	}
+
 	return NULL;
 }
 
@@ -221,7 +221,13 @@
 				continue;
 			}
 
-			if (strcasecmp(temp2, FIELD_NAME) == 0) {
+			if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
+				attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
+				if (attr != NULL)
+					if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
+						dbg_parse("creation of partition nodes requested");
+						dev.partitions = PARTITIONS_COUNT;
+					}
 				strfieldcpy(dev.name, temp3);
 				continue;
 			}
test/udev-test.pl +10 -1
diff -Nru a/test/udev-test.pl b/test/udev-test.pl
@@ -350,7 +350,16 @@
 EOF
 	},
 	{
-		desc     => "sysfs parent heirachy",
+		desc     => "create all possible partitions",
+		subsys   => "block",
+		devpath  => "block/sda",
+		expected => "boot_disk15" ,
+		conf     => <<EOF
+BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME{all_partitions}="boot_disk"
+EOF
+	},
+	{
+		desc     => "sysfs parent hierarchy",
 		subsys   => "tty",
 		devpath  => "class/tty/ttyUSB0",
 		expected => "visor" ,
udev-add.c +46 -25

100 lines not on this page · raw message

udev-remove.c +12 -1
diff -Nru a/udev-remove.c b/udev-remove.c
@@ -66,9 +66,11 @@
 static int delete_node(struct udevice *dev)
 {
 	char filename[255];
+	char partitionname[255];
 	char *symlinks;
 	char *linkname;
 	int retval;
+	int i;
 
 	strncpy(filename, udev_root, sizeof(filename));
 	strncat(filename, dev->name, sizeof(filename));
@@ -81,11 +83,20 @@
 		return retval;
 	}
 
+	/* remove partition nodes */
+	if (dev->partitions > 0) {
+		info("removing partitions '%s[1-%i]'", filename, dev->partitions);
+		for (i = 1; i <= dev->partitions; i++) {
+			sprintf(partitionname, "%s%i", filename, i);
+			unlink(partitionname);
+		}
+	}
+
 	/* remove subdirectories */
 	if (strchr(dev->name, '/'))
 		delete_path(filename);
 
-	if (*dev->symlink) {
+	if (dev->symlink[0] != '\0') {
 		symlinks = dev->symlink;
 		while (1) {
 			linkname = strsep(&symlinks, " ");
udev.h +1 -0
diff -Nru a/udev.h b/udev.h
@@ -46,6 +46,7 @@
 	int minor;
 	unsigned int mode;	/* not mode_t due to conflicting definitions in different libcs */
 	char symlink[NAME_SIZE];
+	int partitions;
 
 	/* private data that help us in building strings */
 	char bus_id[SYSFS_NAME_LEN];
#1 lore raw
Quoted 48 lines · Kay Sievers

Here is the first try to create all partitons of a blockdevice, since removable media devices may need to acces the expected partition to revalidate the media.

It uses the attribute syntax introduced with the last %s{file} patch. I'm using this with my multi-slot-flash-card-reader:

SYSFS{model}="USB Storage-SMC ", NAME{all_partitions}="smartmedia" SYSFS{model}="USB Storage-CFC ", NAME{all_partitions}="compactflash" SYSFS{model}="USB Storage-MSC ", NAME{all_partitions}="memorystick" SYSFS{model}="USB Storage-MMC ", NAME{all_partitions}="multimedia"

and I get:

tree /udev/
/udev/

  |-- memorystick
  |-- memorystick1
  |-- memorystick10
  |-- memorystick11
  |-- memorystick12
  |-- memorystick13
  |-- memorystick14
  |-- memorystick15
  |-- memorystick2
  |-- memorystick3
  |-- memorystick4
  |-- memorystick5
  |-- memorystick6
  |-- memorystick7
  |-- memorystick8
  |-- memorystick9
  |-- multimedia
  |-- multimedia1
  |-- multimedia10
  |-- multimedia11
  |-- multimedia12
  |-- multimedia13
  |-- multimedia14
  |-- multimedia15
  |-- multimedia2
  |-- multimedia3
  |-- multimedia4
  |-- multimedia5
  |-- multimedia6
  |-- multimedia7
  |-- multimedia8
  |-- multimedia9
Quoted 5 lines

...

If needed, we can make the number of partions to create adjustable with the attribute?

Nice, this is great. I don't think we need to change the number of partitions.

I've applied this, and checked in a fix for udev-add.c if 'fake' was true (which it is for udevtest). I'll go test this for my devices with partitions.

thanks,

greg k-h

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Kay Sievers
Quoted 16 lines · Greg KH
Quoted 7 lines · Kay Sievers

Here is the first try to create all partitons of a blockdevice, since removable media devices may need to acces the expected partition to revalidate the media.

It uses the attribute syntax introduced with the last %s{file} patch. I'm using this with my multi-slot-flash-card-reader:

Quoted 7 lines

SYSFS{model}="USB Storage-SMC ", NAME{all_partitions}="smartmedia" SYSFS{model}="USB Storage-CFC ", NAME{all_partitions}="compactflash" SYSFS{model}="USB Storage-MSC ", NAME{all_partitions}="memorystick" SYSFS{model}="USB Storage-MMC ", NAME{all_partitions}="multimedia"

If needed, we can make the number of partions to create adjustable with the attribute?

Quoted 6 lines

Nice, this is great. I don't think we need to change the number of partitions.

I've applied this, and checked in a fix for udev-add.c if 'fake' was true (which it is for udevtest). I'll go test this for my devices with partitions.

Should we export the scsi 'removable' flag via sysfs attribute for use with all_partitions? Kernel patch is a trivial:

=== drivers/scsi/scsi_sysfs.c 1.39 vs edited ==--- 1.39/drivers/scsi/scsi_sysfs.c Fri Jan 16 15:24:41 2004
+++ edited/drivers/scsi/scsi_sysfs.c Tue Feb 17 00:56:50 2004
@@ -266,6 +266,7 @@
sdev_rd_attr (model, "%.16s\n");
sdev_rd_attr (rev, "%.4s\n");
sdev_rw_attr_bit (online);
+sdev_rd_attr (removable, "%d\n");

static ssize_t
store_rescan_field (struct device *dev, const char *buf, size_t count)
@@ -295,6 +296,7 @@
&dev_attr_online,
&dev_attr_rescan,
&dev_attr_delete,
+ &dev_attr_removable,
NULL
};

So you could have rules such as:

KERNEL="sd*", BUS="scsi", SYSFS{removable}="1", NAME{all_partitions}="%k" KERNEL="sd*", BUS="scsi", SYSFS{removable}="0", NAME="%k"

AFAIUI CD-ROM never wants all_partitions even though they are marked removable. Tape is also removable. So, the KERNEL="sd*".

Though it won't help for non-scsi (implies non-usb mass storage) media.

A global rule affecting all partitions or naming would be nice, so you would not have to have a separate rule, but then like Kay's example rules above, if you are specifically naming a device, you already know it is removable, and can set all_partitions.

For example, you only need the first rule below, but if in some simliar case, you required two rules, that is ugly:

SYSFS{model}="USB Storage-SMC ", SYSFS{removable}="1", NAME{all_partitions}="smartmedia" SYSFS{model}="USB Storage-SMC ", SYSFS{removable}="0", NAME="smartmedia"

-- Patrick Mansfield

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to Kay Sievers
Quoted 20 lines · Patrick Mansfield
Quoted 19 lines · Greg KH
Quoted 7 lines · Kay Sievers

Here is the first try to create all partitons of a blockdevice, since removable media devices may need to acces the expected partition to revalidate the media.

It uses the attribute syntax introduced with the last %s{file} patch. I'm using this with my multi-slot-flash-card-reader:

Quoted 7 lines

SYSFS{model}="USB Storage-SMC ", NAME{all_partitions}="smartmedia" SYSFS{model}="USB Storage-CFC ", NAME{all_partitions}="compactflash" SYSFS{model}="USB Storage-MSC ", NAME{all_partitions}="memorystick" SYSFS{model}="USB Storage-MMC ", NAME{all_partitions}="multimedia"

If needed, we can make the number of partions to create adjustable with the attribute?

Nice, this is great. I don't think we need to change the number of partitions.

Quoted 6 lines
Quoted 3 lines

I've applied this, and checked in a fix for udev-add.c if 'fake' was true (which it is for udevtest). I'll go test this for my devices with partitions.

Should we export the scsi 'removable' flag via sysfs attribute for use with all_partitions? Kernel patch is a trivial:

<snip patch>

Yes, this is a good idea.

Then we can just trigger off of this, right? Will this flag always be correct, even for the very cheap flash readers on the market?

Quoted 7 lines

So you could have rules such as:

KERNEL="sd*", BUS="scsi", SYSFS{removable}="1", NAME{all_partitions}="%k" KERNEL="sd*", BUS="scsi", SYSFS{removable}="0", NAME="%k"

AFAIUI CD-ROM never wants all_partitions even though they are marked removable. Tape is also removable. So, the KERNEL="sd*".

Yeah, that would make sense.

Quoted 1 line

Though it won't help for non-scsi (implies non-usb mass storage) media.

Do you know of any? Old zip drives use scsi, right? What about floppy drives?

Quoted 10 lines

A global rule affecting all partitions or naming would be nice, so you would not have to have a separate rule, but then like Kay's example rules above, if you are specifically naming a device, you already know it is removable, and can set all_partitions.

For example, you only need the first rule below, but if in some simliar case, you required two rules, that is ugly:

SYSFS{model}="USB Storage-SMC ", SYSFS{removable}="1", NAME{all_partitions}="smartmedia" SYSFS{model}="USB Storage-SMC ", SYSFS{removable}="0", NAME="smartmedia"

Yeah, it's cumbersome, I agree. Any one have any other ideas on how to do this?

thanks,

greg k-h

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#4 lore raw
In reply to Kay Sievers
Quoted 7 lines · Greg KH
Quoted 2 lines · Patrick Mansfield

Should we export the scsi 'removable' flag via sysfs attribute for use with all_partitions? Kernel patch is a trivial:

<snip patch>

Yes, this is a good idea.

More general patch below.

Quoted 2 lines

Then we can just trigger off of this, right? Will this flag always be correct, even for the very cheap flash readers on the market?

Yes for the first question.

And it should not matter for the second:

If a device gets it wrong and says media is not removable, the user would have to manually recheck partitions - maybe by removing (via software or hardware) and adding the device or via fdisk. Then udev would get hotplugs for the partitions. So such borken devices will interact OK way with udev.

Quoted 8 lines
Quoted 7 lines

So you could have rules such as:

KERNEL="sd*", BUS="scsi", SYSFS{removable}="1", NAME{all_partitions}="%k" KERNEL="sd*", BUS="scsi", SYSFS{removable}="0", NAME="%k"

AFAIUI CD-ROM never wants all_partitions even though they are marked removable. Tape is also removable. So, the KERNEL="sd*".

Quoted 6 lines

Yeah, that would make sense.

Quoted 1 line

Though it won't help for non-scsi (implies non-usb mass storage) media.

Do you know of any? Old zip drives use scsi, right? What about floppy drives?

No, but I know less about linux and non-scsi removable media than the little I know about scsi removable media, though I'm learning.

Here is a better patch to have the block layer export an attribute based on the gd->flags GENHD_FL_REMOVABLE. Then it can be used for all block devices. I'm not sure if non-ide floppy drivers set it. Jens or others need to OK this.

But a simple udev rule will not stop hd cdrom's (and you can't use KERNEL="sd*" with this method), but st won't have the field.

I ran with this patch:

drivers/block/genhd.c +11 -0
diff -uprN -X /home/patman/dontdiff base-2.6/drivers/block/genhd.c blk-attr-2.6/drivers/block/genhd.c
@@ -352,6 +352,12 @@ static ssize_t disk_range_read(struct ge
 {
 	return sprintf(page, "%d\n", disk->minors);
 }
+static ssize_t disk_removable_read(struct gendisk * disk, char *page)
+{
+	return sprintf(page, "%d\n",
+		       (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
+
+}
 static ssize_t disk_size_read(struct gendisk * disk, char *page)
 {
 	return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
@@ -394,6 +400,10 @@ static struct disk_attribute disk_attr_r
 	.attr = {.name = "range", .mode = S_IRUGO },
 	.show	= disk_range_read
 };
+static struct disk_attribute disk_attr_removable = {
+	.attr = {.name = "removable", .mode = S_IRUGO },
+	.show	= disk_removable_read
+};
 static struct disk_attribute disk_attr_size = {
 	.attr = {.name = "size", .mode = S_IRUGO },
 	.show	= disk_size_read
@@ -406,6 +416,7 @@ static struct disk_attribute disk_attr_s
 static struct attribute * default_attrs[] = {
 	&disk_attr_dev.attr,
 	&disk_attr_range.attr,
+	&disk_attr_removable.attr,
 	&disk_attr_size.attr,
 	&disk_attr_stat.attr,
 	NULL,

And then I see:

[root@laptop root]# for i in /sys/block/*; do echo $i removable is $(cat $i/removable); done /sys/block/fd0 removable is 1
/sys/block/hda removable is 0
/sys/block/hdc removable is 1
/sys/block/sda removable is 1

hda is my boot/root disk. hdc my cd drive.

For sda, both my fairly cheap dazzle xd card reader and my olympus c750 give the same result.

(The ide floppy is the bay thing on my laptop, though it has a cd drive in it.)

-- Patrick Mansfield

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#5 lore raw
In reply to Kay Sievers
Quoted 46 lines · Patrick Mansfield

I ran with this patch:

diff -uprN -X /home/patman/dontdiff base-2.6/drivers/block/genhd.c blk-attr-2.6/drivers/block/genhd.c

drivers/block/genhd.c +11 -0
@@ -352,6 +352,12 @@ static ssize_t disk_range_read(struct ge
 {
 	return sprintf(page, "%d\n", disk->minors);
 }
+static ssize_t disk_removable_read(struct gendisk * disk, char *page)
+{
+	return sprintf(page, "%d\n",
+		       (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
+
+}
 static ssize_t disk_size_read(struct gendisk * disk, char *page)
 {
 	return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
@@ -394,6 +400,10 @@ static struct disk_attribute disk_attr_r
 	.attr = {.name = "range", .mode = S_IRUGO },
 	.show	= disk_range_read
 };
+static struct disk_attribute disk_attr_removable = {
+	.attr = {.name = "removable", .mode = S_IRUGO },
+	.show	= disk_removable_read
+};
 static struct disk_attribute disk_attr_size = {
 	.attr = {.name = "size", .mode = S_IRUGO },
 	.show	= disk_size_read
@@ -406,6 +416,7 @@ static struct disk_attribute disk_attr_s
 static struct attribute * default_attrs[] = {
 	&disk_attr_dev.attr,
 	&disk_attr_range.attr,
+	&disk_attr_removable.attr,
 	&disk_attr_size.attr,
 	&disk_attr_stat.attr,
 	NULL,

And then I see:

[root@laptop root]# for i in /sys/block/*; do echo $i removable is $(cat $i/removable); done /sys/block/fd0 removable is 1
/sys/block/hda removable is 0
/sys/block/hdc removable is 1
/sys/block/sda removable is 1

Quoted 7 lines

hda is my boot/root disk. hdc my cd drive.

For sda, both my fairly cheap dazzle xd card reader and my olympus c750 give the same result.

(The ide floppy is the bay thing on my laptop, though it has a cd drive in it.)

This doesnt work for firewire at least.

/sys/block/hda removable is 0
/sys/block/hdb removable is 1
/sys/block/ram0 removable is 0
/sys/block/sda removable is 0

Can we mark all sbp2 and usb-storage devices as removeable somehow? They can disappear at any time.

./drivers/usb/storage/isd200.c does modify the InquiryData as example.

Signature · 14 lines
USB is for mice, FireWire is for men!

sUse lINUX ag, nÜRNBERG


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id70&alloc_id638&op�k
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#6 lore raw
In reply to Kay Sievers
Quoted 2 lines · Olaf Hering

Can we mark all sbp2 and usb-storage devices as removeable somehow? They can disappear at any time.

ok, that doesnt really matter because it meant removeable media, not device.

but, did anyone review that, can it go into the main kernel?

Signature · 14 lines
USB is for mice, FireWire is for men!

sUse lINUX ag, nÜRNBERG


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id70&alloc_id638&op�k
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel