Skip to contentSkip to conversation

Persistent device name using alias name

PATCH In review Nao Nishijima 3 participants 7 messages Jul 22, 2011 – Jul 29, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Persistent device name using alias name

Hi,

This patch series provide an "alias name" of the disk into kernel messages. Users can assign a preferred name to an alias name of the device.

A raw device name of a disk does not always point a same disk at each boot-up time. Therefore, users have to use persistent device names, which udev creates to always access the same disk. However, kernel messages still display the raw device names.

My proposal is that users can use and see persistent device names which were assigned by they because users expect same name to point same disk anytime.

Why need to modify kernel messages?
- We can see mapping of device names and persistent device names in udev log. If those logs output to syslog, we can search persistent device name from device name, but it can cause a large amount of syslog output.

- If we can use the persistent device names and can always see the same name on the kernel log, we don't need to pay additional cost for searching and picking a correct pair of device name and persistent device name from udev log.

- Kernel messages are output to serial console when kenel crashes, it's so hard to convert device name to alias name.

Of course, I am going to modify the commands using device name so that users can use alias names.

How to use:
1. Build and install the kernel with this series, and reboot with the kernel.

2. Make a script of get alias_name

[localhost]# vi /lib/udev/get_alias_name
#!/bin/sh -e
DEVNAME=echo $1 | sed -e 's/[0-9]//g'
echo "ALIAS=cat /sys/block/$DEVNAME/alias_name"
exit 0

And you should set an execute bit,
[localhost]# chmod +x /lib/udev/get_alias_name

3. Check disk's id
Here is an example to get the serial id and the path of the device.

[localhost]# udevadm info --query=property --path=/sys/block/sda \
| grep ID_SERIAL= ID_SERIAL=0QEMU_QEMU_HARDDISK_drive-scsi0-0-1

Some devices does not have the serial id. For such devices, you may use the device path.

[localhost]# udevadm info --query=property --path=/sys/block/sr0 \
| grep ID_PATH=
ID_PATH=pci-0000:00:01.1-scsi-1:0:0:0

4. Write udev rules as follows
(The user assigns "foo" to sda and "bar" to sr0)
We use ENV{ID_SERIAL} or ENV{ID_PATH} (get by 3) to identify a disk. And to assign automatically an "alias name", we use ATTR key.
If ENV{ALIAS} is empty, we use to get an "alias_name" by get_alias_name script.

[localhost]# vi /etc/udev/rules.d/70-alias_name.rules SUBSYSTEM!="block", GOTO="end"

# write alias name for sdX
KERNEL=="sd*[!0-9]", ACTION=="add", ATTR{alias_name}="foo", \
ENV{ID_SERIAL}=="0QEMU_QEMU_HARDDISK_drive-scsi0-0-1"
# write alias name for srX
KERNEL=="sr[0-9]", ACTION=="add", ATTR{alias_name}="bar", \
ENV{ID_PATH}=="pci-0000:00:01.1-scsi-1:0:0:0"
# make symlink
ENV{DEVTYPE}=="disk", ENV{ALIAS}=="?*", SYMLINK+="disk/by-alias/$env{ALIAS}"
ENV{DEVTYPE}=="partition", ENV{ALIAS}=="", \
IMPORT{program}="/lib/udev/get_alias_name %k"
ENV{DEVTYPE}=="partition", ENV{ALIAS}=="?*", \
SYMLINK+="disk/by-alias/$env{ALIAS}%n"

LABEL="end"

5. reboot
After reboot, we can see alias name in kernel messages.

[localhost]# ls -l /dev/disk/by-alias/
total 0

lrwxrwxrwx. 1 root root  9 Jul  1 21:21 bar -> ../../sr0
lrwxrwxrwx. 1 root root  9 Jul  1 21:21 foo -> ../../sda
lrwxrwxrwx. 1 root root 10 Jul  1 21:21 foo1 -> ../../sda1

[localhost]# dmesg
...
sd 2:0:0:0: [foo] sd_init_command: block=17382146, count=56 sd 2:0:0:0: [foo] block=17382146
sd 2:0:0:0: [foo] reading 56/56 512 byte blocks. sd 2:0:0:0: [foo] Send: 0xffff88007ab1a900
...

Currently, the user must add the naming rule manually for new devices. In the future, it is appended automatically, as like NIC.

Changes in v2:
- Change alias_name string to pointer
- Change alias_name writable to write at once
- Drop procfs patch

Best regards,

---

Joe Perches (1):
sd: modify printk for alias name

Nao Nishijima (2):
block: add a new attribute "alias name" in gendisk structure
sd: [BUGFIX] Use sd_printk instead of printk

 Documentation/ABI/testing/sysfs-block |   15 ++++++
 block/genhd.c                         |   84 +++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_lib.c               |   26 ++++++++++
 drivers/scsi/sd.c                     |   30 +++++++++++-
 drivers/scsi/sd.h                     |    8 +--
 include/linux/genhd.h                 |    4 ++
 include/scsi/scsi_device.h            |    8 +--
 7 files changed, 162 insertions(+), 13 deletions(-)

--
Nao Nishijima ([email protected])

#1 lore raw

PATCH v2 1/3 sd: [BUGFIX] Use sd_printk instead of printk

sd_ioctl still use printk for log output.
It should use sd_printk instead of printk, as well as other sd_*.

All SCSI messages should output via s*_printk instead of printk.

---

 drivers/scsi/sd.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/sd.c +4 -3
index 953773c..e434e5f 100644
@@ -1066,12 +1066,13 @@ static int sd_ioctl(struct block_device *bdev, fmode_t mode,
 		    unsigned int cmd, unsigned long arg)
 {
 	struct gendisk *disk = bdev->bd_disk;
-	struct scsi_device *sdp = scsi_disk(disk)->device;
+	struct scsi_disk *sdkp = scsi_disk(disk);
+	struct scsi_device *sdp = sdkp->device;
 	void __user *p = (void __user *)arg;
 	int error;
     
-	SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
-						disk->disk_name, cmd));
+	SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s,"
+		       " cmd=0x%x\n", disk->disk_name, cmd));
 
 	/*
 	 * If we are in the middle of error recovery, don't let anyone
#2 lore raw

PATCH v2 2/3 block: add a new attribute "alias name" in gendisk structure

In reply to Nao Nishijima

This patch allows the user to set an "alias name" of the disk via sysfs interface.

A raw device name of a disk does not always point a same disk at each boot-up time. Therefore, users have to use persistent device names, which udev creates to always access the same disk. However, kernel messages still display the raw device names.

This patch adds a new attribute "alias name" in gendisk structure. And if users set their preferred name to an "alias name" of the disk, it would be appeared in kernel messages. A disk can have an "alias name" which length is up to 255bytes. Users can use alphabets, numbers, '-' and '_' in alias name.

---

 Documentation/ABI/testing/sysfs-block |   15 ++++++
 block/genhd.c                         |   84 +++++++++++++++++++++++++++++++++
 include/linux/genhd.h                 |    4 ++
 3 files changed, 103 insertions(+), 0 deletions(-)
Documentation/ABI/testing/sysfs-block +15 -0
index c1eb41c..c42be33 100644
@@ -206,3 +206,18 @@ Description:
 		when a discarded area is read the discard_zeroes_data
 		parameter will be set to one. Otherwise it will be 0 and
 		the result of reading a discarded area is undefined.
+
+What:		/sys/block/<disk>/alias_name
+Date:		July 2011
+Contact:	Nao Nishijima <[email protected]>
+Description:
+		A raw device name of a disk does not always point a same disk
+		each boot-up time. Therefore, users have to use persistent
+		device names, which udev creates when the kernel finds a disk,
+		instead of raw device name. However, kernel doesn't show those
+		persistent names on its message.
+		This file can store an alias name of the disk and it would be
+		appeared in kernel messages if it is set. A disk can have an
+		alias name which length is up to 255bytes. Users can use
+		use alphabets, numbers, "-" and "_" in alias name. An alias
+		name is write at once.
block/genhd.c +84 -0

108 lines not on this page · raw message

include/linux/genhd.h +4 -0
index 300d758..e9ad85d 100644
@@ -21,6 +21,8 @@
 #define dev_to_part(device)	container_of((device), struct hd_struct, __dev)
 #define disk_to_dev(disk)	(&(disk)->part0.__dev)
 #define part_to_dev(part)	(&((part)->__dev))
+#define alias_name(disk)	((disk)->alias_name ? (disk)->alias_name \
+						    : (disk)->disk_name)
 
 extern struct device_type part_type;
 extern struct kobject *block_depr;
@@ -58,6 +60,7 @@ enum {
 
 #define DISK_MAX_PARTS			256
 #define DISK_NAME_LEN			32
+#define ALIAS_NAME_LEN			256
 
 #include <linux/major.h>
 #include <linux/device.h>
@@ -162,6 +165,7 @@ struct gendisk {
                                          * disks that can't be partitioned. */
 
 	char disk_name[DISK_NAME_LEN];	/* name of major driver */
+	char *alias_name;		/* alias name of disk */
 	char *(*devnode)(struct gendisk *gd, mode_t *mode);
 
 	unsigned int events;		/* supported events */
#3 lore raw

PATCH v2 3/3 sd: modify printk for alias name

In reply to Nao Nishijima

From: Joe Perches <[email protected]>

Reduce size of code and text of scmd_printk and sd_printk macros by converting to the macros to functions and using vsprintf extension %pV. This moves the code out-of-line and centralizes the code used to emit additional arguments.

Save ~32KB of space in an x86 allyesconfig.

$ size drivers/scsi/built-in.o*
   text	   data	    bss	    dec	    hex	filename
5860439	 135396	1393024	7388859	 70bebb	drivers/scsi/built-in.o.new
5882368	 135396	1395848	7413612	 711f6c	drivers/scsi/built-in.o.old
5887100	 135396	1397264	7419760	 713770	drivers/scsi/built-in.o.with_patch_1_and_2

---

 drivers/scsi/scsi_lib.c    |   26 ++++++++++++++++++++++++++
 drivers/scsi/sd.c          |   23 +++++++++++++++++++++++
 drivers/scsi/sd.h          |    8 +++-----
 include/scsi/scsi_device.h |    8 +++-----
 4 files changed, 55 insertions(+), 10 deletions(-)
drivers/scsi/scsi_lib.c +26 -0
index ec1803a..249c54c 100644
@@ -2571,3 +2571,29 @@ void scsi_kunmap_atomic_sg(void *virt)
 	kunmap_atomic(virt, KM_BIO_SRC_IRQ);
 }
 EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
+
+/* Logging utilities */
+
+int scmd_printk(const char *prefix, const struct scsi_cmnd *scmd,
+		const char *format, ...)
+{
+	struct va_format vaf;
+	va_list args;
+	int r;
+
+	va_start(args, format);
+
+	vaf.fmt = format;
+	vaf.va = &args;
+
+	if (scmd->request->rq_disk)
+		r = sdev_printk(prefix, scmd->device, "[%s] %pV",
+				alias_name(scmd->request->rq_disk), &vaf);
+	else
+		r = sdev_printk(prefix, scmd->device, "%pV", &vaf);
+
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(scmd_printk);
drivers/scsi/sd.c +23 -0
index e434e5f..bb1b3e5 100644
@@ -2889,3 +2889,26 @@ static void sd_print_result(struct scsi_disk *sdkp, int result)
 	scsi_show_result(result);
 }
 
+int sd_printk(const char *prefix, const struct scsi_disk *sdsk,
+	      const char *format, ...)
+{
+	struct va_format vaf;
+	va_list args;
+	int r;
+
+	va_start(args, format);
+
+	vaf.fmt = format;
+	vaf.va = &args;
+
+	if (sdsk->disk)
+		r = sdev_printk(prefix, sdsk->device, "[%s] %pV",
+				alias_name(sdsk->disk), &vaf);
+	else
+		r = sdev_printk(prefix, sdsk->device, "%pV", &vaf);
+
+	va_end(args);
+
+	return r;
+}
+EXPORT_SYMBOL(sd_printk);
drivers/scsi/sd.h +3 -5
index 6ad798b..46aa748 100644
@@ -88,11 +88,9 @@ static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
 	return container_of(disk->private_data, struct scsi_disk, driver);
 }
 
-#define sd_printk(prefix, sdsk, fmt, a...)				\
-        (sdsk)->disk ?							\
-	sdev_printk(prefix, (sdsk)->device, "[%s] " fmt,		\
-		    (sdsk)->disk->disk_name, ##a) :			\
-	sdev_printk(prefix, (sdsk)->device, fmt, ##a)
+extern __attribute__((format (printf, 3, 4)))
+int sd_printk(const char *prefix, const struct scsi_disk *sdsk,
+	      const char *format, ...);
 
 /*
  * A DIF-capable target device can be formatted with different
include/scsi/scsi_device.h +3 -5
index dd82e02..c79631b 100644
@@ -216,11 +216,9 @@ struct scsi_dh_data {
 #define sdev_printk(prefix, sdev, fmt, a...)	\
 	dev_printk(prefix, &(sdev)->sdev_gendev, fmt, ##a)
 
-#define scmd_printk(prefix, scmd, fmt, a...)				\
-        (scmd)->request->rq_disk ?					\
-	sdev_printk(prefix, (scmd)->device, "[%s] " fmt,		\
-		    (scmd)->request->rq_disk->disk_name, ##a) :		\
-	sdev_printk(prefix, (scmd)->device, fmt, ##a)
+extern __attribute__((format (printf, 3, 4)))
+int scmd_printk(const char *prefix, const struct scsi_cmnd *scmd,
+		const char *format, ...);
 
 enum scsi_target_state {
 	STARGET_CREATED = 1,
#4 lore raw
In reply to Nao Nishijima
Quoted 22 lines · Nao Nishijima

Hi,

This patch series provide an "alias name" of the disk into kernel messages. Users can assign a preferred name to an alias name of the device.

A raw device name of a disk does not always point a same disk at each boot-up time. Therefore, users have to use persistent device names, which udev creates to always access the same disk. However, kernel messages still display the raw device names.

My proposal is that users can use and see persistent device names which were assigned by they because users expect same name to point same disk anytime.

Why need to modify kernel messages?
- We can see mapping of device names and persistent device names in udev log. If those logs output to syslog, we can search persistent device name from device name, but it can cause a large amount of syslog output.

- If we can use the persistent device names and can always see the same name on the kernel log, we don't need to pay additional cost for searching and picking a correct pair of device name and persistent device name from udev log.

Quoted 6 lines

- Kernel messages are output to serial console when kenel crashes, it's so hard to convert device name to alias name.

Of course, I am going to modify the commands using device name so that users can use alias names.

Please do that first, and then you will not need any kernel changes in the first place :)

Anyway, you all know my objections to this patch series, but again, as I'm not the block subsystem maintainer, I really can't do anything.

greg k-h

#6 lore raw
In reply to Nao Nishijima

Hi James,

Could you review this patches?

Best regards,

Quoted 136 lines · Nao Nishijima

Hi,

This patch series provide an "alias name" of the disk into kernel messages. Users can assign a preferred name to an alias name of the device.

A raw device name of a disk does not always point a same disk at each boot-up time. Therefore, users have to use persistent device names, which udev creates to always access the same disk. However, kernel messages still display the raw device names.

My proposal is that users can use and see persistent device names which were assigned by they because users expect same name to point same disk anytime.

Why need to modify kernel messages?
- We can see mapping of device names and persistent device names in udev log. If those logs output to syslog, we can search persistent device name from device name, but it can cause a large amount of syslog output.

- If we can use the persistent device names and can always see the same name on the kernel log, we don't need to pay additional cost for searching and picking a correct pair of device name and persistent device name from udev log.

- Kernel messages are output to serial console when kenel crashes, it's so hard to convert device name to alias name.

Of course, I am going to modify the commands using device name so that users can use alias names.

How to use:
1. Build and install the kernel with this series, and reboot with the kernel.

2. Make a script of get alias_name

[localhost]# vi /lib/udev/get_alias_name
#!/bin/sh -e
DEVNAME=echo $1 | sed -e 's/[0-9]//g'
echo "ALIAS=cat /sys/block/$DEVNAME/alias_name"
exit 0

And you should set an execute bit,
[localhost]# chmod +x /lib/udev/get_alias_name

3. Check disk's id
Here is an example to get the serial id and the path of the device.

[localhost]# udevadm info --query=property --path=/sys/block/sda \
| grep ID_SERIAL= ID_SERIAL=0QEMU_QEMU_HARDDISK_drive-scsi0-0-1

Some devices does not have the serial id. For such devices, you may use the device path.

[localhost]# udevadm info --query=property --path=/sys/block/sr0 \
| grep ID_PATH=
ID_PATH=pci-0000:00:01.1-scsi-1:0:0:0

4. Write udev rules as follows
(The user assigns "foo" to sda and "bar" to sr0)
We use ENV{ID_SERIAL} or ENV{ID_PATH} (get by 3) to identify a disk. And to assign automatically an "alias name", we use ATTR key.
If ENV{ALIAS} is empty, we use to get an "alias_name" by get_alias_name script.

[localhost]# vi /etc/udev/rules.d/70-alias_name.rules SUBSYSTEM!="block", GOTO="end"

# write alias name for sdX
KERNEL=="sd*[!0-9]", ACTION=="add", ATTR{alias_name}="foo", \
ENV{ID_SERIAL}=="0QEMU_QEMU_HARDDISK_drive-scsi0-0-1"
# write alias name for srX
KERNEL=="sr[0-9]", ACTION=="add", ATTR{alias_name}="bar", \
ENV{ID_PATH}=="pci-0000:00:01.1-scsi-1:0:0:0"
# make symlink
ENV{DEVTYPE}=="disk", ENV{ALIAS}=="?*", SYMLINK+="disk/by-alias/$env{ALIAS}"
ENV{DEVTYPE}=="partition", ENV{ALIAS}=="", \
IMPORT{program}="/lib/udev/get_alias_name %k"
ENV{DEVTYPE}=="partition", ENV{ALIAS}=="?*", \
SYMLINK+="disk/by-alias/$env{ALIAS}%n"

LABEL="end"

5. reboot
After reboot, we can see alias name in kernel messages.

[localhost]# ls -l /dev/disk/by-alias/
total 0

lrwxrwxrwx. 1 root root  9 Jul  1 21:21 bar -> ../../sr0
lrwxrwxrwx. 1 root root  9 Jul  1 21:21 foo -> ../../sda
lrwxrwxrwx. 1 root root 10 Jul  1 21:21 foo1 -> ../../sda1

[localhost]# dmesg
...
sd 2:0:0:0: [foo] sd_init_command: block=17382146, count=56 sd 2:0:0:0: [foo] block=17382146
sd 2:0:0:0: [foo] reading 56/56 512 byte blocks. sd 2:0:0:0: [foo] Send: 0xffff88007ab1a900
...

Currently, the user must add the naming rule manually for new devices. In the future, it is appended automatically, as like NIC.

Changes in v2:
- Change alias_name string to pointer
- Change alias_name writable to write at once
- Drop procfs patch

Best regards,

---

Joe Perches (1):
sd: modify printk for alias name

Nao Nishijima (2):
block: add a new attribute "alias name" in gendisk structure
sd: [BUGFIX] Use sd_printk instead of printk

 Documentation/ABI/testing/sysfs-block |   15 ++++++
 block/genhd.c                         |   84 +++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_lib.c               |   26 ++++++++++
 drivers/scsi/sd.c                     |   30 +++++++++++-
 drivers/scsi/sd.h                     |    8 +--
 include/linux/genhd.h                 |    4 ++
 include/scsi/scsi_device.h            |    8 +--
 7 files changed, 162 insertions(+), 13 deletions(-)

--
Nao Nishijima ([email protected])

Signature · 4 lines
Nao NISHIJIMA
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., YOKOHAMA Research  Laboratory
Email: [email protected]