* [PATCH] udev callout for reading filesystem labels
@ 2004-04-29 21:04 Kay Sievers
2004-04-30 13:35 ` Kevin P. Fleming
` (11 more replies)
0 siblings, 12 replies; 14+ messages in thread
From: Kay Sievers @ 2004-04-29 21:04 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 549 bytes --]
Hi,
here is a small udev toy, which enables udev to name partitions by
its filesystem label or uuid's.
The following udev rule:
KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
creates a symlink with the uuid read from the filesystem. If no label or
uuid is found the program exits with nonzero and the rule will fail.
ext2, ext3, reiserfs, xfs, jfs, vfat, msdos volume labels are supported,
ntfs and swap partitions can be recognized.
It's possible to compile with klibc and the static binary takes 13kb.
thanks,
Kay
[-- Attachment #2: 01-volumeid.patch --]
[-- Type: text/plain, Size: 18817 bytes --]
diff -Nru a/extras/volume_id/Makefile b/extras/volume_id/Makefile
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/Makefile Thu Apr 29 22:47:11 2004
@@ -0,0 +1,50 @@
+# Makefile for udev_volume_id
+#
+# Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+
+PROG = udev_volume_id
+
+all: $(PROG)
+
+prefix =
+exec_prefix = ${prefix}
+etcdir = ${prefix}/etc
+sbindir = ${exec_prefix}/sbin
+usrbindir = ${exec_prefix}/usr/bin
+usrsbindir = ${exec_prefix}/usr/sbin
+mandir = ${prefix}/usr/share/man
+devddir = ${etcdir}/dev.d/default
+configdir = ${etcdir}/udev/
+initdir = ${etcdir}/init.d/
+srcdir = .
+
+INSTALL = /usr/bin/install -c
+INSTALL_PROGRAM = ${INSTALL}
+INSTALL_DATA = ${INSTALL} -m 644
+INSTALL_SCRIPT = ${INSTALL_PROGRAM}
+
+override CFLAGS+=-Wall -fno-builtin
+OBJS = volume_id.o udev_volume_id.o
+HEADERS = volume_id.h
+
+$(OBJS): $(HEADERS)
+
+$(PROG): $(OBJS) $(HEADERS)
+ $(LD) $(LDFLAGS) -o $(PROG) $(CRT0) $(OBJS) $(LIB_OBJS) $(ARCH_LIB_OBJS)
+
+clean:
+ rm -f $(PROG) $(OBJS)
+
+spotless: clean
+
+install: all
+ $(INSTALL_PROGRAM) $(PROG) $(DESTDIR)$(usrsbindir)/$(PROG)
+
+uninstall:
+ - rm $(DESTDIR)$(usrsbindir)/$(PROG)
+
diff -Nru a/extras/volume_id/udev_volume_id.c b/extras/volume_id/udev_volume_id.c
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/udev_volume_id.c Thu Apr 29 22:47:11 2004
@@ -0,0 +1,120 @@
+/*
+ * udev_volume_id - udev callout to read filesystem label and uuid
+ *
+ * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
+ *
+ * sample udev rule for creation of a symlink with the filsystem uuid:
+ * KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "volume_id.h"
+
+int main(int argc, char *argv[])
+{
+ struct volume_id *vid;
+ const char help[] = "usage: udev_volume_id -m<minor> -M<major> [-t|-l|-u]\n";
+ int major = -1;
+ int minor = -1;
+ char *tail;
+ static const char short_options[] = "M:m:htlu";
+ int option;
+ char print = '\0';
+ int rc;
+
+
+ while (1) {
+ option = getopt(argc, argv, short_options);
+ if (option == -1)
+ break;
+
+ switch (option) {
+ case 'M':
+ major = (int) strtoul(optarg, &tail, 10);
+ if (tail[0] != '\0') {
+ printf("invalid major\n");
+ exit(1);
+ }
+ break;
+ case 'm':
+ minor = (int) strtoul(optarg, &tail, 10);
+ if (tail[0] != '\0') {
+ printf("invalid minor\n");
+ exit(1);
+ }
+ break;
+ case 't':
+ print = 't';
+ break;
+ case 'l':
+ print = 'l';
+ break;
+ case 'u':
+ print = 'u';
+ break;
+ case 'h':
+ case '?':
+ default:
+ printf(help);
+ exit(1);
+ }
+ }
+
+ if (major == -1 || minor == -1) {
+ printf(help);
+ exit(1);
+ }
+
+ vid = volume_id_open_dev_t(makedev(major, minor));
+ if (vid == NULL) {
+ printf("error open volume\n");
+ exit(1);
+ }
+
+ rc = volume_id_probe(vid, ALL);
+ if (rc != 0) {
+ printf("error probing volume\n");
+ exit(1);
+ }
+
+ switch (print) {
+ case 't':
+ printf("%s\n", vid->fs_name);
+ break;
+ case 'l':
+ if (vid->label_string[0] == '\0')
+ exit(2);
+ printf("%s\n", vid->label_string);
+ break;
+ case 'u':
+ if (vid->uuid_string[0] == '\0')
+ exit(2);
+ printf("%s\n", vid->uuid_string);
+ break;
+ default:
+ printf("T:%s\n", vid->fs_name);
+ printf("L:%s\n", vid->label_string);
+ printf("U:%s\n", vid->uuid_string);
+ }
+
+ volume_id_close(vid);
+
+ exit(0);
+}
diff -Nru a/extras/volume_id/volume_id.c b/extras/volume_id/volume_id.c
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/volume_id.c Thu Apr 29 22:47:11 2004
@@ -0,0 +1,550 @@
+/*
+ * volume_id - reads filesystem label and uuid
+ *
+ * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
+ *
+ * The superblock structs are taken from the libblkid living inside
+ * the e2fsprogs. This is a simple straightforward implementation for
+ * reading the label strings of only the most common filesystems.
+ * If you need a full featured library with attribute caching, support for
+ * much more partition/media types or non-root data access, you may have
+ * a look at:
+ * http://e2fsprogs.sourceforge.net.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <asm/types.h>
+
+#include "volume_id.h"
+
+
+#define bswap32(x) (__u32)((((__u32)(x) & 0xff000000u) >> 24) | \
+ (((__u32)(x) & 0x00ff0000u) >> 8) | \
+ (((__u32)(x) & 0x0000ff00u) << 8) | \
+ (((__u32)(x) & 0x000000ffu) << 24))
+
+#if (__BYTE_ORDER == __LITTLE_ENDIAN)
+#define cpu_to_le32(x) (x)
+#elif (__BYTE_ORDER == __BIG_ENDIAN)
+#define cpu_to_le32(x) bswap32(x)
+#endif
+
+#define VOLUME_ID_BUFFER_SIZE 0x11000 /* reiser offset is 64k */
+
+
+static void set_label(struct volume_id *id, char *buf, int count)
+{
+ int i;
+
+ memcpy(id->label, buf, count);
+
+ memcpy(id->label_string, buf, count);
+
+ /* remove trailing whitespace */
+ i = strlen(id->label_string);
+ while (i--) {
+ if (! isspace(id->label_string[i]))
+ break;
+ }
+ id->label_string[i+1] = '\0';
+}
+
+static void set_uuid(struct volume_id *id, unsigned char *buf, int count)
+{
+ int i;
+
+ memcpy(id->uuid, buf, count);
+
+ /* create string if uuid is set */
+ for (i = 0; i < count; i++)
+ if (buf[i] != 0)
+ goto set;
+ return;
+
+set:
+ switch(count) {
+ case 4:
+ sprintf(id->uuid_string, "%02X%02X-%02X%02X",
+ buf[3], buf[2], buf[1], buf[0]);
+ break;
+ case 16:
+ sprintf(id->uuid_string,
+ "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ buf[0], buf[1], buf[2], buf[3],
+ buf[4], buf[5],
+ buf[6], buf[7],
+ buf[8], buf[9],
+ buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
+ break;
+ }
+}
+
+static int open_superblock(struct volume_id *id)
+{
+ /* get buffer to read the first block */
+ if (id->buf == NULL) {
+ id->buf = malloc(VOLUME_ID_BUFFER_SIZE);
+ if (id->buf == NULL)
+ return -1;
+ }
+
+ /* try to read the first 64k, but at least the first block */
+ memset(id->buf, 0x00, VOLUME_ID_BUFFER_SIZE);
+ lseek(id->fd, 0, SEEK_SET);
+ if (read(id->fd, id->buf, VOLUME_ID_BUFFER_SIZE) < 0x200)
+ return -1;
+
+ return 0;
+}
+
+static void close_superblock(struct volume_id *id)
+{
+ if (id->buf != NULL) {
+ free(id->buf);
+ id->buf = NULL;
+ }
+}
+
+#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x00000004
+#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x00000008
+#define EXT_SUPERBLOCK_OFFSET 0x400
+static int probe_ext(struct volume_id *id)
+{
+ struct ext2_super_block {
+ __u32 s_inodes_count;
+ __u32 s_blocks_count;
+ __u32 s_r_blocks_count;
+ __u32 s_free_blocks_count;
+ __u32 s_free_inodes_count;
+ __u32 s_first_data_block;
+ __u32 s_log_block_size;
+ __u32 s_dummy3[7];
+ unsigned char s_magic[2];
+ __u16 s_state;
+ __u32 s_dummy5[8];
+ __u32 s_feature_compat;
+ __u32 s_feature_incompat;
+ __u32 s_feature_ro_compat;
+ unsigned char s_uuid[16];
+ char s_volume_name[16];
+ } *es;
+
+ es = (struct ext2_super_block *) (id->buf + EXT_SUPERBLOCK_OFFSET);
+
+ if (es->s_magic[0] != 0123 ||
+ es->s_magic[1] != 0357)
+ return -1;
+
+ set_label(id, es->s_volume_name, 16);
+ set_uuid(id, es->s_uuid, 16);
+
+ if ((cpu_to_le32(es->s_feature_compat) & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0) {
+ id->fs_type = EXT3;
+ id->fs_name = "ext3";
+ } else {
+ id->fs_type = EXT2;
+ id->fs_name = "ext2";
+ }
+
+ return 0;
+}
+
+#define REISER1_SUPERBLOCK_OFFSET 0x2000
+#define REISER_SUPERBLOCK_OFFSET 0x10000
+static int probe_reiser(struct volume_id *id)
+{
+ struct reiser_super_block {
+ __u32 rs_blocks_count;
+ __u32 rs_free_blocks;
+ __u32 rs_root_block;
+ __u32 rs_journal_block;
+ __u32 rs_journal_dev;
+ __u32 rs_orig_journal_size;
+ __u32 rs_dummy2[5];
+ __u16 rs_blocksize;
+ __u16 rs_dummy3[3];
+ unsigned char rs_magic[12];
+ __u32 rs_dummy4[5];
+ unsigned char rs_uuid[16];
+ char rs_label[16];
+ } *rs;
+
+ rs = (struct reiser_super_block *) &(id->buf[REISER1_SUPERBLOCK_OFFSET]);
+
+ if (strncmp(rs->rs_magic, "ReIsErFs", 8) == 0)
+ goto found;
+
+ rs = (struct reiser_super_block *) &(id->buf[REISER_SUPERBLOCK_OFFSET]);
+
+ if (strncmp(rs->rs_magic, "ReIsEr2Fs", 9) == 0)
+ goto found;
+ if (strncmp(rs->rs_magic, "ReIsEr3Fs", 9) == 0)
+ goto found;
+
+ return -1;
+
+found:
+ set_label(id, rs->rs_label, 16);
+ set_uuid(id, rs->rs_uuid, 16);
+
+ id->fs_type = REISER;
+ id->fs_name = "reiser";
+
+ return 0;
+}
+
+static int probe_xfs(struct volume_id *id)
+{
+ struct xfs_super_block {
+ unsigned char xs_magic[4];
+ __u32 xs_blocksize;
+ __u64 xs_dblocks;
+ __u64 xs_rblocks;
+ __u32 xs_dummy1[2];
+ unsigned char xs_uuid[16];
+ __u32 xs_dummy2[15];
+ char xs_fname[12];
+ __u32 xs_dummy3[2];
+ __u64 xs_icount;
+ __u64 xs_ifree;
+ __u64 xs_fdblocks;
+ } *xs;
+
+ xs = (struct xfs_super_block *) id->buf;
+
+ if (strncmp(xs->xs_magic, "XFSB", 4) != 0)
+ return -1;
+
+ set_label(id, xs->xs_fname, 12);
+ set_uuid(id, xs->xs_uuid, 16);
+
+ id->fs_type = XFS;
+ id->fs_name = "xfs";
+
+ return 0;
+}
+
+#define JFS_SUPERBLOCK_OFFSET 0x8000
+static int probe_jfs(struct volume_id *id)
+{
+ struct jfs_super_block {
+ unsigned char js_magic[4];
+ __u32 js_version;
+ __u64 js_size;
+ __u32 js_bsize;
+ __u32 js_dummy1;
+ __u32 js_pbsize;
+ __u32 js_dummy2[27];
+ unsigned char js_uuid[16];
+ unsigned char js_label[16];
+ unsigned char js_loguuid[16];
+ } *js;
+
+ js = (struct jfs_super_block *) &(id->buf[JFS_SUPERBLOCK_OFFSET]);
+
+ if (strncmp(js->js_magic, "JFS1", 4) != 0)
+ return -1;
+
+ set_label(id, js->js_label, 16);
+ set_uuid(id, js->js_uuid, 16);
+
+ id->fs_type = JFS;
+ id->fs_name = "jfs";
+
+ return 0;
+}
+
+static int probe_vfat(struct volume_id *id)
+{
+ struct vfat_super_block {
+ unsigned char vs_ignored[3];
+ unsigned char vs_sysid[8];
+ unsigned char vs_sector_size[2];
+ __u8 vs_cluster_size;
+ __u16 vs_reserved;
+ __u8 vs_fats;
+ unsigned char vs_dir_entries[2];
+ unsigned char vs_sectors[2];
+ unsigned char vs_media;
+ __u16 vs_fat_length;
+ __u16 vs_secs_track;
+ __u16 vs_heads;
+ __u32 vs_hidden;
+ __u32 vs_total_sect;
+ __u32 vs_fat32_length;
+ __u16 vs_flags;
+ __u8 vs_version[2];
+ __u32 vs_root_cluster;
+ __u16 vs_insfo_sector;
+ __u16 vs_backup_boot;
+ __u16 vs_reserved2[6];
+ unsigned char vs_unknown[3];
+ unsigned char vs_serno[4];
+ char vs_label[11];
+ unsigned char vs_magic[8];
+ unsigned char vs_dummy2[164];
+ unsigned char vs_pmagic[2];
+ } *vs;
+
+ vs = (struct vfat_super_block *) id->buf;
+
+ if (strncmp(vs->vs_magic, "MSWIN", 5) == 0)
+ goto found;
+ if (strncmp(vs->vs_magic, "FAT32 ", 8) == 0)
+ goto found;
+ return -1;
+
+found:
+ memcpy(id->label, vs->vs_label, 11);
+ memcpy(id->uuid, vs->vs_serno, 4);
+
+ id->fs_type = VFAT;
+ id->fs_name = "vfat";
+
+ return 0;
+}
+
+static int probe_msdos(struct volume_id *id)
+{
+ struct msdos_super_block {
+ unsigned char ms_ignored[3];
+ unsigned char ms_sysid[8];
+ unsigned char ms_sector_size[2];
+ __u8 ms_cluster_size;
+ __u16 ms_reserved;
+ __u8 ms_fats;
+ unsigned char ms_dir_entries[2];
+ unsigned char ms_sectors[2];
+ unsigned char ms_media;
+ __u16 ms_fat_length;
+ __u16 ms_secs_track;
+ __u16 ms_heads;
+ __u32 ms_hidden;
+ __u32 ms_total_sect;
+ unsigned char ms_unknown[3];
+ unsigned char ms_serno[4];
+ char ms_label[11];
+ unsigned char ms_magic[8];
+ unsigned char ms_dummy2[192];
+ unsigned char ms_pmagic[2];
+ } *ms;
+
+ ms = (struct msdos_super_block *) id->buf;
+
+ if (strncmp(ms->ms_magic, "MSDOS", 5) == 0)
+ goto found;
+ if (strncmp(ms->ms_magic, "FAT16 ", 8) == 0)
+ goto found;
+ if (strncmp(ms->ms_magic, "FAT12 ", 8) == 0)
+ goto found;
+ return -1;
+
+found:
+ set_label(id, ms->ms_label, 11);
+ set_uuid(id, ms->ms_serno, 4);
+
+ id->fs_type = MSDOS;
+ id->fs_name = "msdos";
+
+ return 0;
+}
+
+static int probe_ntfs(struct volume_id *id)
+{
+ struct ntfs_super_block {
+ char jump[3];
+ char oem_id[4];
+ } *ns;
+
+ ns = (struct ntfs_super_block *) id->buf;
+
+ if (strncmp(ns->oem_id, "NTFS", 4) != 0)
+ return -1;
+
+ id->fs_type = NTFS;
+ id->fs_name = "ntfs";
+
+ return 0;
+}
+
+static int probe_swap(struct volume_id *id)
+{
+ int magic;
+
+ /* huhh, the swap signature is on the end of the PAGE_SIZE */
+ for (magic = 0x1000; magic <= 0x4000; magic <<= 1) {
+ if (strncmp(&(id->buf[magic -10]), "SWAP-SPACE", 10) == 0)
+ goto found;
+ if (strncmp(&(id->buf[magic -10]), "SWAPSPACE2", 10) == 0)
+ goto found;
+ }
+ return -1;
+
+found:
+ id->fs_type = SWAP;
+ id->fs_name = "swap";
+
+ return 0;
+}
+
+/* probe volume for filesystem type and try to read label+uuid */
+int volume_id_probe(struct volume_id *id, enum filesystem_type fs_type)
+{
+ int rc;
+
+ if (id == NULL)
+ return -EINVAL;
+
+ if (open_superblock(id) != 0)
+ return -EACCES;
+
+ switch (fs_type) {
+ case EXT3:
+ case EXT2:
+ rc = probe_ext(id);
+ break;
+ case REISER:
+ rc = probe_reiser(id);
+ break;
+ case XFS:
+ rc = probe_xfs(id);
+ break;
+ case JFS:
+ rc = probe_jfs(id);
+ break;
+ case MSDOS:
+ rc = probe_msdos(id);
+ break;
+ case VFAT:
+ rc = probe_vfat(id);
+ break;
+ case NTFS:
+ rc = probe_ntfs(id);
+ break;
+ case SWAP:
+ rc = probe_swap(id);
+ break;
+ default:
+ rc = probe_ext(id);
+ if (rc == 0)
+ break;
+ rc = probe_reiser(id);
+ if (rc == 0)
+ break;
+ rc = probe_xfs(id);
+ if (rc == 0)
+ break;
+ rc = probe_jfs(id);
+ if (rc == 0)
+ break;
+ rc = probe_msdos(id);
+ if (rc == 0)
+ break;
+ rc = probe_vfat(id);
+ if (rc == 0)
+ break;
+ rc = probe_ntfs(id);
+ if (rc == 0)
+ break;
+ rc = probe_swap(id);
+ if (rc == 0)
+ break;
+ rc = -1;
+ }
+
+ if (rc == 0)
+ close_superblock(id);
+
+ return rc;
+}
+
+/* open volume by already open file descriptor */
+struct volume_id *volume_id_open_fd(int fd)
+{
+ struct volume_id *id;
+
+ id = malloc(sizeof(struct volume_id));
+ if (id == NULL)
+ return NULL;
+ memset(id, 0x00, sizeof(struct volume_id));
+
+ id->fd = fd;
+
+ return id;
+}
+
+/* open volume by device node */
+struct volume_id *volume_id_open_node(const char *path)
+{
+ struct volume_id *id;
+ int fd;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return NULL;
+
+ id = volume_id_open_fd(fd);
+ if (id == NULL)
+ return NULL;
+
+ /* close fd on device close */
+ id->fd_close = 1;
+
+ return id;
+}
+
+/* open volume by major/minor */
+struct volume_id *volume_id_open_dev_t(dev_t devt)
+{
+ struct volume_id *id;
+ char tmp_node[VOLUME_ID_PATH_MAX];
+
+ snprintf(tmp_node, VOLUME_ID_PATH_MAX,
+ "/tmp/volume-%u-%u", major(devt), minor(devt));
+ tmp_node[VOLUME_ID_PATH_MAX] = '\0';
+
+ /* create tempory node to open the block device */
+ if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
+ return NULL;
+
+ id = volume_id_open_node(tmp_node);
+
+ unlink(tmp_node);
+
+ return id;
+}
+
+/* free allocated volume info */
+void volume_id_close(struct volume_id *id)
+{
+ if (id == NULL)
+ return;
+
+ if (id->fd_close != 0)
+ close(id->fd);
+
+ close_superblock(id);
+
+ free(id);
+}
diff -Nru a/extras/volume_id/volume_id.h b/extras/volume_id/volume_id.h
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/volume_id.h Thu Apr 29 22:47:11 2004
@@ -0,0 +1,72 @@
+/*
+ * volume_id - reads partition label and uuid
+ *
+ * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef _VOLUME_ID_H_
+#define _VOLUME_ID_H_
+
+#define VOLUME_ID_VERSION 001
+
+#define VOLUME_ID_LABEL_SIZE 16
+#define VOLUME_ID_UUID_SIZE 16
+#define VOLUME_ID_UUID_STRING_SIZE 37
+#define VOLUME_ID_PATH_MAX 255
+
+
+enum filesystem_type {
+ ALL,
+ EXT2,
+ EXT3,
+ REISER,
+ XFS,
+ JFS,
+ MSDOS,
+ VFAT,
+ NTFS,
+ SWAP
+};
+
+struct volume_id {
+ char label[VOLUME_ID_LABEL_SIZE];
+ char label_string[VOLUME_ID_LABEL_SIZE+1];
+ unsigned char uuid[VOLUME_ID_UUID_SIZE];
+ char uuid_string[VOLUME_ID_UUID_STRING_SIZE];
+ enum filesystem_type fs_type;
+ char *fs_name;
+ int fd;
+ char *buf;
+ int fd_close;
+};
+
+/* open volume by already open file descriptor */
+extern struct volume_id *volume_id_open_fd(int fd);
+
+/* open volume by device node */
+extern struct volume_id *volume_id_open_node(const char *path);
+
+/* open volume by major/minor */
+extern struct volume_id *volume_id_open_dev_t(dev_t devt);
+
+/* probe volume for filesystem type and try to read label/uuid */
+extern int volume_id_probe(struct volume_id *id, enum filesystem_type fs_type);
+
+/* free allocated device info */
+extern void volume_id_close(struct volume_id *id);
+
+#endif
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
@ 2004-04-30 13:35 ` Kevin P. Fleming
2004-04-30 14:36 ` Oliver Neukum
` (10 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Kevin P. Fleming @ 2004-04-30 13:35 UTC (permalink / raw)
To: linux-hotplug
Kay Sievers wrote:
> here is a small udev toy, which enables udev to name partitions by
> its filesystem label or uuid's.
This is very cool, thanks Kay!
I notice that this is yet another hotplug-related tool that has to
create a temporary device node to be able to read the contents of the
block device. Has anyone considered extending the kernel to actually
have an open-by-major/minor syscall (obviously limited to root only)?
-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g.
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id149&alloc_idÅ66&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
2004-04-30 13:35 ` Kevin P. Fleming
@ 2004-04-30 14:36 ` Oliver Neukum
2004-04-30 22:29 ` Greg KH
` (9 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Oliver Neukum @ 2004-04-30 14:36 UTC (permalink / raw)
To: linux-hotplug
Am Freitag, 30. April 2004 15:35 schrieb Kevin P. Fleming:
> Kay Sievers wrote:
> > here is a small udev toy, which enables udev to name partitions by
> > its filesystem label or uuid's.
>
> This is very cool, thanks Kay!
>
> I notice that this is yet another hotplug-related tool that has to
> create a temporary device node to be able to read the contents of the
> block device. Has anyone considered extending the kernel to actually
> have an open-by-major/minor syscall (obviously limited to root only)?
How about passing an fd to the hotplug agent in the first place?
Regards
Oliver
-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g.
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id149&alloc_idÅ66&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
2004-04-30 13:35 ` Kevin P. Fleming
2004-04-30 14:36 ` Oliver Neukum
@ 2004-04-30 22:29 ` Greg KH
2004-05-05 1:14 ` Kay Sievers
` (8 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Greg KH @ 2004-04-30 22:29 UTC (permalink / raw)
To: linux-hotplug
On Thu, Apr 29, 2004 at 11:04:46PM +0200, Kay Sievers wrote:
> Hi,
> here is a small udev toy, which enables udev to name partitions by
> its filesystem label or uuid's.
>
> The following udev rule:
>
> KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
>
> creates a symlink with the uuid read from the filesystem. If no label or
> uuid is found the program exits with nonzero and the rule will fail.
>
> ext2, ext3, reiserfs, xfs, jfs, vfat, msdos volume labels are supported,
> ntfs and swap partitions can be recognized.
>
> It's possible to compile with klibc and the static binary takes 13kb.
Very nice, I was wondering who was going to use that library to make
such a tool. This is even better as we can use klibc for it.
Applied, thanks.
greg k-h
-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g.
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id149&alloc_idÅ66&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (2 preceding siblings ...)
2004-04-30 22:29 ` Greg KH
@ 2004-05-05 1:14 ` Kay Sievers
2004-05-05 21:24 ` Greg KH
` (7 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Kay Sievers @ 2004-05-05 1:14 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 1031 bytes --]
On Fri, Apr 30, 2004 at 03:29:54PM -0700, Greg KH wrote:
> On Thu, Apr 29, 2004 at 11:04:46PM +0200, Kay Sievers wrote:
> > Hi,
> > here is a small udev toy, which enables udev to name partitions by
> > its filesystem label or uuid's.
> >
> > The following udev rule:
> >
> > KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
> >
> > creates a symlink with the uuid read from the filesystem. If no label or
> > uuid is found the program exits with nonzero and the rule will fail.
> >
> > ext2, ext3, reiserfs, xfs, jfs, vfat, msdos volume labels are supported,
> > ntfs and swap partitions can be recognized.
> >
> > It's possible to compile with klibc and the static binary takes 13kb.
>
> Very nice, I was wondering who was going to use that library to make
> such a tool. This is even better as we can use klibc for it.
Here is a update, which supports iso9660 and udf labels.
Not very useful in the udev case, but I've added it for hal,
so we just catch up with the latest version.
thanks,
Kay
[-- Attachment #2: 01-volumeid-udf.patch --]
[-- Type: text/plain, Size: 25147 bytes --]
===== extras/volume_id/volume_id.c 1.1 vs edited =====
--- 1.1/extras/volume_id/volume_id.c Fri Apr 30 00:47:11 2004
+++ edited/extras/volume_id/volume_id.c Wed May 5 02:29:14 2004
@@ -11,19 +11,19 @@
* a look at:
* http://e2fsprogs.sourceforge.net.
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation version 2 of the License.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 675 Mass Ave, Cambridge, MA 02139, USA.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
@@ -38,6 +38,17 @@
#include "volume_id.h"
+#ifdef DEBUG
+#define dbg(format, arg...) \
+ do { \
+ printf("%s: " format "\n", __FUNCTION__ , ## arg); \
+ } while (0)
+#else
+#define dbg(format, arg...) do {} while (0)
+#endif
+
+#define bswap16(x) (__u16)((((__u16)(x) & 0x00ffu) << 8) | \
+ (((__u32)(x) & 0xff00u) >> 8))
#define bswap32(x) (__u32)((((__u32)(x) & 0xff000000u) >> 24) | \
(((__u32)(x) & 0x00ff0000u) >> 8) | \
@@ -45,19 +56,28 @@
(((__u32)(x) & 0x000000ffu) << 24))
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
-#define cpu_to_le32(x) (x)
+#define le16_to_cpu(x) (x)
+#define le32_to_cpu(x) (x)
#elif (__BYTE_ORDER == __BIG_ENDIAN)
-#define cpu_to_le32(x) bswap32(x)
+#define le16_to_cpu(x) bswap16(x)
+#define le32_to_cpu(x) bswap32(x)
#endif
-#define VOLUME_ID_BUFFER_SIZE 0x11000 /* reiser offset is 64k */
+/* size of superblock buffer, reiser block is at 64k */
+#define SB_BUFFER_SIZE 0x11000
+/* size of seek buffer 2k */
+#define SEEK_BUFFER_SIZE 0x800
-static void set_label(struct volume_id *id, char *buf, int count)
+static void set_label_raw(struct volume_id *id, char *buf, int count)
{
- int i;
+ memcpy(id->label_raw, buf, count);
+ id->label_raw_len = count;
+}
- memcpy(id->label, buf, count);
+static void set_label_string(struct volume_id *id, char *buf, int count)
+{
+ int i;
memcpy(id->label_string, buf, count);
@@ -90,7 +110,8 @@
break;
case 16:
sprintf(id->uuid_string,
- "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
+ "%02x%02x%02x%02x%02x%02x",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5],
buf[6], buf[7],
@@ -100,29 +121,67 @@
}
}
-static int open_superblock(struct volume_id *id)
+static char *get_buffer(struct volume_id *id, size_t off, size_t len)
{
- /* get buffer to read the first block */
- if (id->buf == NULL) {
- id->buf = malloc(VOLUME_ID_BUFFER_SIZE);
- if (id->buf == NULL)
- return -1;
- }
+ size_t buf_len;
- /* try to read the first 64k, but at least the first block */
- memset(id->buf, 0x00, VOLUME_ID_BUFFER_SIZE);
- lseek(id->fd, 0, SEEK_SET);
- if (read(id->fd, id->buf, VOLUME_ID_BUFFER_SIZE) < 0x200)
- return -1;
+ /* check if requested area fits in superblock buffer */
+ if (off + len <= SB_BUFFER_SIZE) {
+ if (id->sbbuf == NULL) {
+ id->sbbuf = malloc(SB_BUFFER_SIZE);
+ if (id->sbbuf == NULL)
+ return NULL;
+ }
+
+ /* check if we need to read */
+ if ((off + len) > id->sbbuf_len) {
+ dbg("read sbbuf len:0x%x", off + len);
+ lseek(id->fd, 0, SEEK_SET);
+ buf_len = read(id->fd, id->sbbuf, off + len);
+ id->sbbuf_len = buf_len;
+ if (buf_len < off + len)
+ return NULL;
+ }
- return 0;
+ return &(id->sbbuf[off]);
+ } else {
+ if (len > SEEK_BUFFER_SIZE)
+ len = SEEK_BUFFER_SIZE;
+
+ /* get seek buffer */
+ if (id->seekbuf == NULL) {
+ id->seekbuf = malloc(SEEK_BUFFER_SIZE);
+ if (id->seekbuf == NULL)
+ return NULL;
+ }
+
+ /* check if we need to read */
+ if ((off < id->seekbuf_off) ||
+ ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
+ dbg("read seekbuf off:0x%x len:0x%x", off, len);
+ lseek(id->fd, off, SEEK_SET);
+ buf_len = read(id->fd, id->seekbuf, len);
+ id->seekbuf_off = off;
+ id->seekbuf_len = buf_len;
+ if (buf_len < len)
+ return NULL;
+ }
+
+ return &(id->seekbuf[off - id->seekbuf_off]);
+ }
}
-static void close_superblock(struct volume_id *id)
+static void free_buffer(struct volume_id *id)
{
- if (id->buf != NULL) {
- free(id->buf);
- id->buf = NULL;
+ if (id->sbbuf != NULL) {
+ free(id->sbbuf);
+ id->sbbuf = NULL;
+ id->sbbuf_len = 0;
+ }
+ if (id->seekbuf != NULL) {
+ free(id->seekbuf);
+ id->seekbuf = NULL;
+ id->seekbuf_len = 0;
}
}
@@ -132,34 +191,39 @@
static int probe_ext(struct volume_id *id)
{
struct ext2_super_block {
- __u32 s_inodes_count;
- __u32 s_blocks_count;
- __u32 s_r_blocks_count;
- __u32 s_free_blocks_count;
- __u32 s_free_inodes_count;
- __u32 s_first_data_block;
- __u32 s_log_block_size;
- __u32 s_dummy3[7];
- unsigned char s_magic[2];
- __u16 s_state;
- __u32 s_dummy5[8];
- __u32 s_feature_compat;
- __u32 s_feature_incompat;
- __u32 s_feature_ro_compat;
- unsigned char s_uuid[16];
- char s_volume_name[16];
+ __u32 inodes_count;
+ __u32 blocks_count;
+ __u32 r_blocks_count;
+ __u32 free_blocks_count;
+ __u32 free_inodes_count;
+ __u32 first_data_block;
+ __u32 log_block_size;
+ __u32 dummy3[7];
+ unsigned char magic[2];
+ __u16 state;
+ __u32 dummy5[8];
+ __u32 feature_compat;
+ __u32 feature_incompat;
+ __u32 feature_ro_compat;
+ unsigned char uuid[16];
+ char volume_name[16];
} *es;
- es = (struct ext2_super_block *) (id->buf + EXT_SUPERBLOCK_OFFSET);
+ es = (struct ext2_super_block *)
+ get_buffer(id, EXT_SUPERBLOCK_OFFSET, 0x200);
+ if (es == NULL)
+ return -1;
- if (es->s_magic[0] != 0123 ||
- es->s_magic[1] != 0357)
+ if (es->magic[0] != 0123 ||
+ es->magic[1] != 0357)
return -1;
- set_label(id, es->s_volume_name, 16);
- set_uuid(id, es->s_uuid, 16);
+ set_label_raw(id, es->volume_name, 16);
+ set_label_string(id, es->volume_name, 16);
+ set_uuid(id, es->uuid, 16);
- if ((cpu_to_le32(es->s_feature_compat) & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0) {
+ if ((le32_to_cpu(es->feature_compat) &
+ EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0) {
id->fs_type = EXT3;
id->fs_name = "ext3";
} else {
@@ -175,38 +239,45 @@
static int probe_reiser(struct volume_id *id)
{
struct reiser_super_block {
- __u32 rs_blocks_count;
- __u32 rs_free_blocks;
- __u32 rs_root_block;
- __u32 rs_journal_block;
- __u32 rs_journal_dev;
- __u32 rs_orig_journal_size;
- __u32 rs_dummy2[5];
- __u16 rs_blocksize;
- __u16 rs_dummy3[3];
- unsigned char rs_magic[12];
- __u32 rs_dummy4[5];
- unsigned char rs_uuid[16];
- char rs_label[16];
+ __u32 blocks_count;
+ __u32 free_blocks;
+ __u32 root_block;
+ __u32 journal_block;
+ __u32 journal_dev;
+ __u32 orig_journal_size;
+ __u32 dummy2[5];
+ __u16 blocksize;
+ __u16 dummy3[3];
+ unsigned char magic[12];
+ __u32 dummy4[5];
+ unsigned char uuid[16];
+ char label[16];
} *rs;
- rs = (struct reiser_super_block *) &(id->buf[REISER1_SUPERBLOCK_OFFSET]);
+ rs = (struct reiser_super_block *)
+ get_buffer(id, REISER_SUPERBLOCK_OFFSET, 0x200);
+ if (rs == NULL)
+ return -1;
- if (strncmp(rs->rs_magic, "ReIsErFs", 8) == 0)
+ if (strncmp(rs->magic, "ReIsEr2Fs", 9) == 0)
+ goto found;
+ if (strncmp(rs->magic, "ReIsEr3Fs", 9) == 0)
goto found;
- rs = (struct reiser_super_block *) &(id->buf[REISER_SUPERBLOCK_OFFSET]);
+ rs = (struct reiser_super_block *)
+ get_buffer(id, REISER1_SUPERBLOCK_OFFSET, 0x200);
+ if (rs == NULL)
+ return -1;
- if (strncmp(rs->rs_magic, "ReIsEr2Fs", 9) == 0)
- goto found;
- if (strncmp(rs->rs_magic, "ReIsEr3Fs", 9) == 0)
+ if (strncmp(rs->magic, "ReIsErFs", 8) == 0)
goto found;
return -1;
found:
- set_label(id, rs->rs_label, 16);
- set_uuid(id, rs->rs_uuid, 16);
+ set_label_raw(id, rs->label, 16);
+ set_label_string(id, rs->label, 16);
+ set_uuid(id, rs->uuid, 16);
id->fs_type = REISER;
id->fs_name = "reiser";
@@ -217,27 +288,30 @@
static int probe_xfs(struct volume_id *id)
{
struct xfs_super_block {
- unsigned char xs_magic[4];
- __u32 xs_blocksize;
- __u64 xs_dblocks;
- __u64 xs_rblocks;
- __u32 xs_dummy1[2];
- unsigned char xs_uuid[16];
- __u32 xs_dummy2[15];
- char xs_fname[12];
- __u32 xs_dummy3[2];
- __u64 xs_icount;
- __u64 xs_ifree;
- __u64 xs_fdblocks;
+ unsigned char magic[4];
+ __u32 blocksize;
+ __u64 dblocks;
+ __u64 rblocks;
+ __u32 dummy1[2];
+ unsigned char uuid[16];
+ __u32 dummy2[15];
+ char fname[12];
+ __u32 dummy3[2];
+ __u64 icount;
+ __u64 ifree;
+ __u64 fdblocks;
} *xs;
- xs = (struct xfs_super_block *) id->buf;
+ xs = (struct xfs_super_block *) get_buffer(id, 0, 0x200);
+ if (xs == NULL)
+ return -1;
- if (strncmp(xs->xs_magic, "XFSB", 4) != 0)
+ if (strncmp(xs->magic, "XFSB", 4) != 0)
return -1;
- set_label(id, xs->xs_fname, 12);
- set_uuid(id, xs->xs_uuid, 16);
+ set_label_raw(id, xs->fname, 12);
+ set_label_string(id, xs->fname, 12);
+ set_uuid(id, xs->uuid, 16);
id->fs_type = XFS;
id->fs_name = "xfs";
@@ -249,25 +323,29 @@
static int probe_jfs(struct volume_id *id)
{
struct jfs_super_block {
- unsigned char js_magic[4];
- __u32 js_version;
- __u64 js_size;
- __u32 js_bsize;
- __u32 js_dummy1;
- __u32 js_pbsize;
- __u32 js_dummy2[27];
- unsigned char js_uuid[16];
- unsigned char js_label[16];
- unsigned char js_loguuid[16];
+ unsigned char magic[4];
+ __u32 version;
+ __u64 size;
+ __u32 bsize;
+ __u32 dummy1;
+ __u32 pbsize;
+ __u32 dummy2[27];
+ unsigned char uuid[16];
+ unsigned char label[16];
+ unsigned char loguuid[16];
} *js;
- js = (struct jfs_super_block *) &(id->buf[JFS_SUPERBLOCK_OFFSET]);
+ js = (struct jfs_super_block *)
+ get_buffer(id, JFS_SUPERBLOCK_OFFSET, 0x200);
+ if (js == NULL)
+ return -1;
- if (strncmp(js->js_magic, "JFS1", 4) != 0)
+ if (strncmp(js->magic, "JFS1", 4) != 0)
return -1;
- set_label(id, js->js_label, 16);
- set_uuid(id, js->js_uuid, 16);
+ set_label_raw(id, js->label, 16);
+ set_label_string(id, js->label, 16);
+ set_uuid(id, js->uuid, 16);
id->fs_type = JFS;
id->fs_name = "jfs";
@@ -278,46 +356,49 @@
static int probe_vfat(struct volume_id *id)
{
struct vfat_super_block {
- unsigned char vs_ignored[3];
- unsigned char vs_sysid[8];
- unsigned char vs_sector_size[2];
- __u8 vs_cluster_size;
- __u16 vs_reserved;
- __u8 vs_fats;
- unsigned char vs_dir_entries[2];
- unsigned char vs_sectors[2];
- unsigned char vs_media;
- __u16 vs_fat_length;
- __u16 vs_secs_track;
- __u16 vs_heads;
- __u32 vs_hidden;
- __u32 vs_total_sect;
- __u32 vs_fat32_length;
- __u16 vs_flags;
- __u8 vs_version[2];
- __u32 vs_root_cluster;
- __u16 vs_insfo_sector;
- __u16 vs_backup_boot;
- __u16 vs_reserved2[6];
- unsigned char vs_unknown[3];
- unsigned char vs_serno[4];
- char vs_label[11];
- unsigned char vs_magic[8];
- unsigned char vs_dummy2[164];
- unsigned char vs_pmagic[2];
+ unsigned char ignored[3];
+ unsigned char sysid[8];
+ unsigned char sector_size[2];
+ __u8 cluster_size;
+ __u16 reserved;
+ __u8 fats;
+ unsigned char dir_entries[2];
+ unsigned char sectors[2];
+ unsigned char media;
+ __u16 fat_length;
+ __u16 secs_track;
+ __u16 heads;
+ __u32 hidden;
+ __u32 total_sect;
+ __u32 fat32_length;
+ __u16 flags;
+ __u8 version[2];
+ __u32 root_cluster;
+ __u16 insfo_sector;
+ __u16 backup_boot;
+ __u16 reserved2[6];
+ unsigned char unknown[3];
+ unsigned char serno[4];
+ char label[11];
+ unsigned char magic[8];
+ unsigned char dummy2[164];
+ unsigned char pmagic[2];
} *vs;
- vs = (struct vfat_super_block *) id->buf;
+ vs = (struct vfat_super_block *) get_buffer(id, 0, 0x200);
+ if (vs == NULL)
+ return -1;
- if (strncmp(vs->vs_magic, "MSWIN", 5) == 0)
+ if (strncmp(vs->magic, "MSWIN", 5) == 0)
goto found;
- if (strncmp(vs->vs_magic, "FAT32 ", 8) == 0)
+ if (strncmp(vs->magic, "FAT32 ", 8) == 0)
goto found;
return -1;
found:
- memcpy(id->label, vs->vs_label, 11);
- memcpy(id->uuid, vs->vs_serno, 4);
+ set_label_raw(id, vs->label, 11);
+ set_label_string(id, vs->label, 11);
+ set_uuid(id, vs->serno, 4);
id->fs_type = VFAT;
id->fs_name = "vfat";
@@ -328,41 +409,44 @@
static int probe_msdos(struct volume_id *id)
{
struct msdos_super_block {
- unsigned char ms_ignored[3];
- unsigned char ms_sysid[8];
- unsigned char ms_sector_size[2];
- __u8 ms_cluster_size;
- __u16 ms_reserved;
- __u8 ms_fats;
- unsigned char ms_dir_entries[2];
- unsigned char ms_sectors[2];
- unsigned char ms_media;
- __u16 ms_fat_length;
- __u16 ms_secs_track;
- __u16 ms_heads;
- __u32 ms_hidden;
- __u32 ms_total_sect;
- unsigned char ms_unknown[3];
- unsigned char ms_serno[4];
- char ms_label[11];
- unsigned char ms_magic[8];
- unsigned char ms_dummy2[192];
- unsigned char ms_pmagic[2];
+ unsigned char ignored[3];
+ unsigned char sysid[8];
+ unsigned char sector_size[2];
+ __u8 cluster_size;
+ __u16 reserved;
+ __u8 fats;
+ unsigned char dir_entries[2];
+ unsigned char sectors[2];
+ unsigned char media;
+ __u16 fat_length;
+ __u16 secs_track;
+ __u16 heads;
+ __u32 hidden;
+ __u32 total_sect;
+ unsigned char unknown[3];
+ unsigned char serno[4];
+ char label[11];
+ unsigned char magic[8];
+ unsigned char dummy2[192];
+ unsigned char pmagic[2];
} *ms;
- ms = (struct msdos_super_block *) id->buf;
+ ms = (struct msdos_super_block *) get_buffer(id, 0, 0x200);
+ if (ms == NULL)
+ return -1;
- if (strncmp(ms->ms_magic, "MSDOS", 5) == 0)
+ if (strncmp(ms->magic, "MSDOS", 5) == 0)
goto found;
- if (strncmp(ms->ms_magic, "FAT16 ", 8) == 0)
+ if (strncmp(ms->magic, "FAT16 ", 8) == 0)
goto found;
- if (strncmp(ms->ms_magic, "FAT12 ", 8) == 0)
+ if (strncmp(ms->magic, "FAT12 ", 8) == 0)
goto found;
return -1;
found:
- set_label(id, ms->ms_label, 11);
- set_uuid(id, ms->ms_serno, 4);
+ set_label_raw(id, ms->label, 11);
+ set_label_string(id, ms->label, 11);
+ set_uuid(id, ms->serno, 4);
id->fs_type = MSDOS;
id->fs_name = "msdos";
@@ -370,6 +454,218 @@
return 0;
}
+#define UDF_VSD_OFFSET 0x8000
+static int probe_udf(struct volume_id *id)
+{
+ struct volume_descriptor {
+ struct descriptor_tag {
+ __u16 id;
+ __u16 version;
+ unsigned char checksum;
+ unsigned char reserved;
+ __u16 serial;
+ __u16 crc;
+ __u16 crc_len;
+ __u32 location;
+ } tag;
+ union {
+ struct anchor_descriptor {
+ __u32 length;
+ __u32 location;
+ } anchor;
+ struct primary_descriptor {
+ __u32 seq_num;
+ __u32 desc_num;
+ struct dstring {
+ char clen;
+ char c[31];
+ } ident;
+ } primary;
+ } type;
+ } *vd;
+
+ struct volume_structure_descriptor {
+ unsigned char type;
+ char id[5];
+ unsigned char version;
+ } *vsd;
+
+ size_t bs;
+ size_t b;
+ int type;
+ int count;
+ int loc;
+ int clen;
+ int i,j;
+ int c;
+
+ vsd = (struct volume_structure_descriptor *)
+ get_buffer(id, UDF_VSD_OFFSET, 0x200);
+ if (vsd == NULL)
+ return -1;
+
+ if (strncmp(vsd->id, "NSR02", 5) == 0)
+ goto blocksize;
+ if (strncmp(vsd->id, "NSR03", 5) == 0)
+ goto blocksize;
+ if (strncmp(vsd->id, "BEA01", 5) == 0)
+ goto blocksize;
+ if (strncmp(vsd->id, "BOOT2", 5) == 0)
+ goto blocksize;
+ if (strncmp(vsd->id, "CD001", 5) == 0)
+ goto blocksize;
+ if (strncmp(vsd->id, "CDW02", 5) == 0)
+ goto blocksize;
+ if (strncmp(vsd->id, "TEA03", 5) == 0)
+ goto blocksize;
+ return -1;
+
+blocksize:
+ /* search the next VSD to get the logical block size of the volume */
+ for (bs = 0x800; bs < 0x8000; bs += 0x800) {
+ vsd = (struct volume_structure_descriptor *)
+ get_buffer(id, UDF_VSD_OFFSET + bs, 0x800);
+ if (vsd == NULL)
+ return -1;
+ dbg("test for blocksize: 0x%x", bs);
+ if (vsd->id[0] != '\0')
+ goto nsr;
+ }
+ return -1;
+
+nsr:
+ /* search the list of VSDs for a NSR descriptor */
+ for (b = 0; b < 64; b++) {
+ vsd = (struct volume_structure_descriptor *)
+ get_buffer(id, UDF_VSD_OFFSET + (b * bs), 0x800);
+ if (vsd == NULL)
+ return -1;
+
+ dbg("vsd: %c%c%c%c%c",
+ vsd->id[0], vsd->id[1], vsd->id[2], vsd->id[3], vsd->id[4]);
+
+ if (vsd->id[0] == '\0')
+ return -1;
+ if (strncmp(vsd->id, "NSR02", 5) == 0)
+ goto anchor;
+ if (strncmp(vsd->id, "NSR03", 5) == 0)
+ goto anchor;
+ }
+ return -1;
+
+anchor:
+ /* read anchor volume descriptor */
+ vd = (struct volume_descriptor *) get_buffer(id, 256 * bs, 0x200);
+ if (vd == NULL)
+ return -1;
+
+ type = le16_to_cpu(vd->tag.id);
+ if (type != 2) /* TAG_ID_AVDP */
+ goto found;
+
+ /* get desriptor list address and block count */
+ count = le32_to_cpu(vd->type.anchor.length) / bs;
+ loc = le32_to_cpu(vd->type.anchor.location);
+ dbg("0x%x descriptors starting at logical secor 0x%x", count, loc);
+
+ /* pick the primary descriptor from the list */
+ for (b = 0; b < count; b++) {
+ vd = (struct volume_descriptor *)
+ get_buffer(id, (loc + b) * bs, 0x200);
+ if (vd == NULL)
+ return -1;
+
+ type = le16_to_cpu(vd->tag.id);
+ dbg("descriptor type %i", type);
+
+ /* check validity */
+ if (type == 0)
+ goto found;
+ if (le32_to_cpu(vd->tag.location) != loc + b)
+ goto found;
+
+ if (type == 1) /* TAG_ID_PVD */
+ goto pvd;
+ }
+ goto found;
+
+pvd:
+ set_label_raw(id, &(vd->type.primary.ident.clen), 32);
+
+ clen = vd->type.primary.ident.clen;
+ dbg("label string charsize=%i bit", clen);
+ if (clen == 8) {
+ set_label_string(id, vd->type.primary.ident.c, 31);
+ } else if (clen == 16) {
+ /* convert unicode OSTA dstring to UTF-8 */
+ j = 0;
+ for (i = 0; i < 32; i += 2) {
+ c = (vd->type.primary.ident.c[i] << 8) |
+ vd->type.primary.ident.c[i+1];
+ if (c == 0) {
+ id->label_string[j] = '\0';
+ break;
+ }else if (c < 0x80U) {
+ id->label_string[j++] = (char) c;
+ } else if (c < 0x800U) {
+ id->label_string[j++] = (char) (0xc0 | (c >> 6));
+ id->label_string[j++] = (char) (0x80 | (c & 0x3f));
+ } else {
+ id->label_string[j++] = (char) (0xe0 | (c >> 12));
+ id->label_string[j++] = (char) (0x80 | ((c >> 6) & 0x3f));
+ id->label_string[j++] = (char) (0x80 | (c & 0x3f));
+ }
+ }
+ }
+
+found:
+ id->fs_type = UDF;
+ id->fs_name = "udf";
+
+ return 0;
+}
+
+#define ISO_SUPERBLOCK_OFFSET 0x8000
+static int probe_iso9660(struct volume_id *id)
+{
+ union iso_super_block {
+ struct iso_header {
+ unsigned char type;
+ char id[5];
+ unsigned char version;
+ unsigned char unused1;
+ char system_id[32];
+ char volume_id[32];
+ } iso;
+ struct hs_header {
+ char foo[8];
+ unsigned char type;
+ char id[4];
+ unsigned char version;
+ } hs;
+ } *is;
+
+ is = (union iso_super_block *)
+ get_buffer(id, ISO_SUPERBLOCK_OFFSET, 0x200);
+ if (is == NULL)
+ return -1;
+
+ if (strncmp(is->iso.id, "CD001", 5) == 0) {
+ set_label_raw(id, is->iso.volume_id, 32);
+ set_label_string(id, is->iso.volume_id, 32);
+ goto found;
+ }
+ if (strncmp(is->hs.id, "CDROM", 5) == 0)
+ goto found;
+ return -1;
+
+found:
+ id->fs_type = ISO9660;
+ id->fs_name = "iso9660";
+
+ return 0;
+}
+
static int probe_ntfs(struct volume_id *id)
{
struct ntfs_super_block {
@@ -377,7 +673,9 @@
char oem_id[4];
} *ns;
- ns = (struct ntfs_super_block *) id->buf;
+ ns = (struct ntfs_super_block *) get_buffer(id, 0, 0x200);
+ if (ns == NULL)
+ return -1;
if (strncmp(ns->oem_id, "NTFS", 4) != 0)
return -1;
@@ -388,15 +686,21 @@
return 0;
}
+#define LARGEST_PAGESIZE 0x4000
static int probe_swap(struct volume_id *id)
{
- int magic;
+ char *sig;
+ size_t page;
/* huhh, the swap signature is on the end of the PAGE_SIZE */
- for (magic = 0x1000; magic <= 0x4000; magic <<= 1) {
- if (strncmp(&(id->buf[magic -10]), "SWAP-SPACE", 10) == 0)
+ for (page = 0x1000; page <= LARGEST_PAGESIZE; page <<= 1) {
+ sig = get_buffer(id, page-10, 10);
+ if (sig == NULL)
+ return -1;
+
+ if (strncmp(sig, "SWAP-SPACE", 10) == 0)
goto found;
- if (strncmp(&(id->buf[magic -10]), "SWAPSPACE2", 10) == 0)
+ if (strncmp(sig, "SWAPSPACE2", 10) == 0)
goto found;
}
return -1;
@@ -416,9 +720,6 @@
if (id == NULL)
return -EINVAL;
- if (open_superblock(id) != 0)
- return -EACCES;
-
switch (fs_type) {
case EXT3:
case EXT2:
@@ -439,13 +740,22 @@
case VFAT:
rc = probe_vfat(id);
break;
+ case UDF:
+ rc = probe_udf(id);
+ break;
+ case ISO9660:
+ rc = probe_iso9660(id);
+ break;
case NTFS:
rc = probe_ntfs(id);
break;
case SWAP:
rc = probe_swap(id);
break;
+ case ALL:
default:
+ /* fill buffer with maximum */
+ get_buffer(id, 0, SB_BUFFER_SIZE);
rc = probe_ext(id);
if (rc == 0)
break;
@@ -464,6 +774,12 @@
rc = probe_vfat(id);
if (rc == 0)
break;
+ rc = probe_udf(id);
+ if (rc == 0)
+ break;
+ rc = probe_iso9660(id);
+ if (rc == 0)
+ break;
rc = probe_ntfs(id);
if (rc == 0)
break;
@@ -473,8 +789,10 @@
rc = -1;
}
+ /* If the filestystem in recognized, we free the allocated buffers,
+ otherwise they will stay in place for the possible next probe call */
if (rc == 0)
- close_superblock(id);
+ free_buffer(id);
return rc;
}
@@ -544,7 +862,7 @@
if (id->fd_close != 0)
close(id->fd);
- close_superblock(id);
+ free_buffer(id);
free(id);
}
===== extras/volume_id/volume_id.h 1.1 vs edited =====
--- 1.1/extras/volume_id/volume_id.h Fri Apr 30 00:47:11 2004
+++ edited/extras/volume_id/volume_id.h Tue May 4 23:56:47 2004
@@ -3,27 +3,27 @@
*
* Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 675 Mass Ave, Cambridge, MA 02139, USA.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
*
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _VOLUME_ID_H_
#define _VOLUME_ID_H_
-#define VOLUME_ID_VERSION 001
+#define VOLUME_ID_VERSION 002
-#define VOLUME_ID_LABEL_SIZE 16
+#define VOLUME_ID_LABEL_SIZE 32
#define VOLUME_ID_UUID_SIZE 16
#define VOLUME_ID_UUID_STRING_SIZE 37
#define VOLUME_ID_PATH_MAX 255
@@ -38,20 +38,27 @@
JFS,
MSDOS,
VFAT,
+ UDF,
+ ISO9660,
NTFS,
SWAP
};
struct volume_id {
- char label[VOLUME_ID_LABEL_SIZE];
- char label_string[VOLUME_ID_LABEL_SIZE+1];
- unsigned char uuid[VOLUME_ID_UUID_SIZE];
- char uuid_string[VOLUME_ID_UUID_STRING_SIZE];
- enum filesystem_type fs_type;
- char *fs_name;
- int fd;
- char *buf;
- int fd_close;
+ char label_raw[VOLUME_ID_LABEL_SIZE];
+ size_t label_raw_len;
+ char label_string[VOLUME_ID_LABEL_SIZE+1];
+ unsigned char uuid[VOLUME_ID_UUID_SIZE];
+ char uuid_string[VOLUME_ID_UUID_STRING_SIZE];
+ enum filesystem_type fs_type;
+ char *fs_name;
+ int fd;
+ char *sbbuf;
+ size_t sbbuf_len;
+ char *seekbuf;
+ size_t seekbuf_off;
+ size_t seekbuf_len;
+ int fd_close;
};
/* open volume by already open file descriptor */
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (3 preceding siblings ...)
2004-05-05 1:14 ` Kay Sievers
@ 2004-05-05 21:24 ` Greg KH
2004-05-06 9:30 ` Arnd Bergmann
` (6 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Greg KH @ 2004-05-05 21:24 UTC (permalink / raw)
To: linux-hotplug
On Wed, May 05, 2004 at 03:14:36AM +0200, Kay Sievers wrote:
>
> Here is a update, which supports iso9660 and udf labels.
> Not very useful in the udev case, but I've added it for hal,
> so we just catch up with the latest version.
Applied, thanks.
greg k-h
-------------------------------------------------------
This SF.Net email is sponsored by Sleepycat Software
Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
deliver higher performing products faster, at low TCO.
http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (4 preceding siblings ...)
2004-05-05 21:24 ` Greg KH
@ 2004-05-06 9:30 ` Arnd Bergmann
2004-05-06 20:18 ` Kay Sievers
` (5 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Arnd Bergmann @ 2004-05-06 9:30 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 1545 bytes --]
On Thursday 29 April 2004 23:04, Kay Sievers wrote:
> Hi,
> here is a small udev toy, which enables udev to name partitions by
> its filesystem label or uuid's.
>
> The following udev rule:
>
> KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
>
> creates a symlink with the uuid read from the filesystem. If no label or
> uuid is found the program exits with nonzero and the rule will fail.
Cool. I've tried to do something similar for dasd device labels before
but not come up with a working solution yet.
Do you think that udev_volume_id can be extended so that it can also
read disk labels and not just partition labels? The method for
detecting and reading a dasd label is the same as for fs labels,
and I have the necessary parsing code here.
The trouble is that the label is stored in the partition table, not
in the partition itself, so I would need to pass the minor number
from e.g. block/dasda/dev instead of block/dasda/dasda{1,2,3}/dev
to udev_volume_id.
> +/* open volume by major/minor */
> +struct volume_id *volume_id_open_dev_t(dev_t devt)
> +{
> + struct volume_id *id;
> + char tmp_node[VOLUME_ID_PATH_MAX];
> +
> + snprintf(tmp_node, VOLUME_ID_PATH_MAX,
> + "/tmp/volume-%u-%u", major(devt), minor(devt));
> + tmp_node[VOLUME_ID_PATH_MAX] = '\0';
This looks like a malicious user can easily prevent it from working
by creating files in /tmp. You might need to use something similar
to tmpfile(3) here.
Arnd <><
[-- Attachment #2: signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (5 preceding siblings ...)
2004-05-06 9:30 ` Arnd Bergmann
@ 2004-05-06 20:18 ` Kay Sievers
2004-05-06 22:59 ` Kay Sievers
` (4 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Kay Sievers @ 2004-05-06 20:18 UTC (permalink / raw)
To: linux-hotplug
On Thu, May 06, 2004 at 11:30:23AM +0200, Arnd Bergmann wrote:
> On Thursday 29 April 2004 23:04, Kay Sievers wrote:
> > Hi,
> > here is a small udev toy, which enables udev to name partitions by
> > its filesystem label or uuid's.
> >
> > The following udev rule:
> >
> > KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
> >
> > creates a symlink with the uuid read from the filesystem. If no label or
> > uuid is found the program exits with nonzero and the rule will fail.
>
> Cool. I've tried to do something similar for dasd device labels before
> but not come up with a working solution yet.
>
> Do you think that udev_volume_id can be extended so that it can also
> read disk labels and not just partition labels? The method for
> detecting and reading a dasd label is the same as for fs labels,
> and I have the necessary parsing code here.
Sure, we can do this. How much code do we need, for the parsing?
> The trouble is that the label is stored in the partition table, not
> in the partition itself, so I would need to pass the minor number
> from e.g. block/dasda/dev instead of block/dasda/dasda{1,2,3}/dev
> to udev_volume_id.
>
> > +/* open volume by major/minor */
> > +struct volume_id *volume_id_open_dev_t(dev_t devt)
> > +{
> > + struct volume_id *id;
> > + char tmp_node[VOLUME_ID_PATH_MAX];
> > +
> > + snprintf(tmp_node, VOLUME_ID_PATH_MAX,
> > + "/tmp/volume-%u-%u", major(devt), minor(devt));
> > + tmp_node[VOLUME_ID_PATH_MAX] = '\0';
>
> This looks like a malicious user can easily prevent it from working
> by creating files in /tmp. You might need to use something similar
> to tmpfile(3) here.
Hmm, I really like to be able to compile with klibc and it doesn't
support tmpfile(). What about simply unlinking it, before creating the node?
thanks,
Kay
-------------------------------------------------------
This SF.Net email is sponsored by Sleepycat Software
Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
deliver higher performing products faster, at low TCO.
http://www.sleepycat.com/telcomwpreg.php?From_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (6 preceding siblings ...)
2004-05-06 20:18 ` Kay Sievers
@ 2004-05-06 22:59 ` Kay Sievers
2004-05-07 14:02 ` Kay Sievers
` (3 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Kay Sievers @ 2004-05-06 22:59 UTC (permalink / raw)
To: linux-hotplug
On Fri, May 07, 2004 at 12:44:01AM +0200, Arnd Bergmann wrote:
> Kay Sievers <kay.sievers@vrfy.org> schrieb am 06.05.2004, 22:18:41:
> > On Thu, May 06, 2004 at 11:30:23AM +0200, Arnd Bergmann wrote:
> > > On Thursday 29 April 2004 23:04, Kay Sievers wrote:
> >
> > Sure, we can do this. How much code do we need, for the parsing?
>
> The attachment contains the code from the linux kernel that does
> the equivalent, somewhat adapted to user interfaces (but probably
> not functional due to lack of sleep). I'll try to do a working
> patch tomorrow.
>
> What would you do to find out the disk minor when udev is
> called for a partition of the disk? Is this handled by udev
> already or do we need additional logic in the callout program?
Hmm, we can take libsysfs's get_parent() for the partition, to get the main
device.
> > > This looks like a malicious user can easily prevent it from working
> > > by creating files in /tmp. You might need to use something similar
> > > to tmpfile(3) here.
> >
> > Hmm, I really like to be able to compile with klibc and it doesn't
> > support tmpfile(). What about simply unlinking it, before creating the node?
>
> Well, I did not mean tmpfile itself, just something similar enough.
> Unlinking the file certainly avoids the security problem, but it
> sounds a little rude. Are you sure there is no race when udev is
> called multiple times for the same device node and we unlink before
> the other udev opens the node?
We may include the PID in the filename?
thanks,
Kay
-------------------------------------------------------
This SF.Net email is sponsored by Sleepycat Software
Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
deliver higher performing products faster, at low TCO.
http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (7 preceding siblings ...)
2004-05-06 22:59 ` Kay Sievers
@ 2004-05-07 14:02 ` Kay Sievers
2004-05-07 18:25 ` Arnd Bergmann
` (2 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Kay Sievers @ 2004-05-07 14:02 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 1320 bytes --]
On Fri, May 07, 2004 at 12:59:16AM +0200, Kay Sievers wrote:
> On Fri, May 07, 2004 at 12:44:01AM +0200, Arnd Bergmann wrote:
> > Kay Sievers <kay.sievers@vrfy.org> schrieb am 06.05.2004, 22:18:41:
> > > On Thu, May 06, 2004 at 11:30:23AM +0200, Arnd Bergmann wrote:
> > > > On Thursday 29 April 2004 23:04, Kay Sievers wrote:
> > >
> > > Sure, we can do this. How much code do we need, for the parsing?
> >
> > The attachment contains the code from the linux kernel that does
> > the equivalent, somewhat adapted to user interfaces (but probably
> > not functional due to lack of sleep). I'll try to do a working
> > patch tomorrow.
> >
> > What would you do to find out the disk minor when udev is
> > called for a partition of the disk? Is this handled by udev
> > already or do we need additional logic in the callout program?
>
> Hmm, we can take libsysfs's get_parent() for the partition, to get the main
> device.
Hi Arnd,
here is a untested first try. udev_volume_id uses libsysfs to get the
major/minor numbers now and therefore relies on the DEVPATH environment
variable instead of the command line options:
If called with the -d option, it tries to read the dasd label from the
main device too.
KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -l -d", NAME="%c"
Is this what you had in mind?
thanks,
Kay
[-- Attachment #2: 01-dasd-volume_id.patch --]
[-- Type: text/plain, Size: 13267 bytes --]
diff -Nru a/extras/volume_id/Makefile b/extras/volume_id/Makefile
--- a/extras/volume_id/Makefile Fri May 7 15:55:58 2004
+++ b/extras/volume_id/Makefile Fri May 7 15:55:58 2004
@@ -29,8 +29,19 @@
INSTALL_SCRIPT = ${INSTALL_PROGRAM}
override CFLAGS+=-Wall -fno-builtin
-OBJS = volume_id.o udev_volume_id.o
-HEADERS = volume_id.h
+
+SYSFS = ../../libsysfs/sysfs_bus.o \
+ ../../libsysfs/sysfs_class.o \
+ ../../libsysfs/sysfs_device.o \
+ ../../libsysfs/sysfs_dir.o \
+ ../../libsysfs/sysfs_driver.o \
+ ../../libsysfs/sysfs_utils.o \
+ ../../libsysfs/dlist.o
+
+
+
+OBJS = volume_id.o udev_volume_id.o dasdlabel.o $(SYSFS)
+HEADERS = volume_id.h dasdlabel.h
$(OBJS): $(HEADERS)
diff -Nru a/extras/volume_id/dasdlabel.c b/extras/volume_id/dasdlabel.c
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/dasdlabel.c Fri May 7 15:55:58 2004
@@ -0,0 +1,163 @@
+/*
+ * dasdlabel.c
+ *
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include "dasdlabel.h"
+
+static unsigned char EBCtoASC[256] =
+{
+/* 0x00 NUL SOH STX ETX *SEL HT *RNL DEL */
+ 0x00, 0x01, 0x02, 0x03, 0x07, 0x09, 0x07, 0x7F,
+/* 0x08 -GE -SPS -RPT VT FF CR SO SI */
+ 0x07, 0x07, 0x07, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
+/* 0x10 DLE DC1 DC2 DC3 -RES -NL BS -POC
+ -ENP ->LF */
+ 0x10, 0x11, 0x12, 0x13, 0x07, 0x0A, 0x08, 0x07,
+/* 0x18 CAN EM -UBS -CU1 -IFS -IGS -IRS -ITB
+ -IUS */
+ 0x18, 0x19, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+/* 0x20 -DS -SOS FS -WUS -BYP LF ETB ESC
+ -INP */
+ 0x07, 0x07, 0x1C, 0x07, 0x07, 0x0A, 0x17, 0x1B,
+/* 0x28 -SA -SFE -SM -CSP -MFA ENQ ACK BEL
+ -SW */
+ 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x06, 0x07,
+/* 0x30 ---- ---- SYN -IR -PP -TRN -NBS EOT */
+ 0x07, 0x07, 0x16, 0x07, 0x07, 0x07, 0x07, 0x04,
+/* 0x38 -SBS -IT -RFF -CU3 DC4 NAK ---- SUB */
+ 0x07, 0x07, 0x07, 0x07, 0x14, 0x15, 0x07, 0x1A,
+/* 0x40 SP RSP ä ---- */
+ 0x20, 0xFF, 0x83, 0x84, 0x85, 0xA0, 0x07, 0x86,
+/* 0x48 . < ( + | */
+ 0x87, 0xA4, 0x9B, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
+/* 0x50 & ---- */
+ 0x26, 0x82, 0x88, 0x89, 0x8A, 0xA1, 0x8C, 0x07,
+/* 0x58 ß ! $ * ) ; */
+ 0x8D, 0xE1, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAA,
+/* 0x60 - / ---- Ä ---- ---- ---- */
+ 0x2D, 0x2F, 0x07, 0x8E, 0x07, 0x07, 0x07, 0x8F,
+/* 0x68 ---- , % _ > ? */
+ 0x80, 0xA5, 0x07, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
+/* 0x70 --- ---- ---- ---- ---- ---- ---- */
+ 0x07, 0x90, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+/* 0x78 * ` : # @ ' = " */
+ 0x70, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
+/* 0x80 * a b c d e f g */
+ 0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
+/* 0x88 h i ---- ---- ---- */
+ 0x68, 0x69, 0xAE, 0xAF, 0x07, 0x07, 0x07, 0xF1,
+/* 0x90 ° j k l m n o p */
+ 0xF8, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
+/* 0x98 q r ---- ---- */
+ 0x71, 0x72, 0xA6, 0xA7, 0x91, 0x07, 0x92, 0x07,
+/* 0xA0 ~ s t u v w x */
+ 0xE6, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+/* 0xA8 y z ---- ---- ---- ---- */
+ 0x79, 0x7A, 0xAD, 0xAB, 0x07, 0x07, 0x07, 0x07,
+/* 0xB0 ^ ---- § ---- */
+ 0x5E, 0x9C, 0x9D, 0xFA, 0x07, 0x07, 0x07, 0xAC,
+/* 0xB8 ---- [ ] ---- ---- ---- ---- */
+ 0xAB, 0x07, 0x5B, 0x5D, 0x07, 0x07, 0x07, 0x07,
+/* 0xC0 { A B C D E F G */
+ 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+/* 0xC8 H I ---- ö ---- */
+ 0x48, 0x49, 0x07, 0x93, 0x94, 0x95, 0xA2, 0x07,
+/* 0xD0 } J K L M N O P */
+ 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
+/* 0xD8 Q R ---- ü */
+ 0x51, 0x52, 0x07, 0x96, 0x81, 0x97, 0xA3, 0x98,
+/* 0xE0 \ S T U V W X */
+ 0x5C, 0xF6, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+/* 0xE8 Y Z ---- Ö ---- ---- ---- */
+ 0x59, 0x5A, 0xFD, 0x07, 0x99, 0x07, 0x07, 0x07,
+/* 0xF0 0 1 2 3 4 5 6 7 */
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+/* 0xF8 8 9 ---- ---- Ü ---- ---- ---- */
+ 0x38, 0x39, 0x07, 0x07, 0x9A, 0x07, 0x07, 0x07
+};
+
+static void vtoc_ebcdic_dec (unsigned char *source, unsigned char *target, int l)
+{
+ int i;
+
+ for (i = 0; i < l; i++)
+ target[i]=EBCtoASC[(unsigned char)(source[i])];
+}
+
+/*
+ * struct dasd_information_t
+ * represents any data about the data, which is visible to userspace
+ */
+typedef struct dasd_information_t {
+ unsigned int devno; /* S/390 devno */
+ unsigned int real_devno; /* for aliases */
+ unsigned int schid; /* S/390 subchannel identifier */
+ unsigned int cu_type : 16; /* from SenseID */
+ unsigned int cu_model : 8; /* from SenseID */
+ unsigned int dev_type : 16; /* from SenseID */
+ unsigned int dev_model : 8; /* from SenseID */
+ unsigned int open_count;
+ unsigned int req_queue_len;
+ unsigned int chanq_len; /* length of chanq */
+ char type[4]; /* from discipline.name, 'none' for unknown */
+ unsigned int status; /* current device level */
+ unsigned int label_block; /* where to find the VOLSER */
+ unsigned int FBA_layout; /* fixed block size (like AIXVOL) */
+ unsigned int characteristics_size;
+ unsigned int confdata_size;
+ char characteristics[64]; /* from read_device_characteristics */
+ char configuration_data[256]; /* from read_configuration_data */
+} dasd_information_t;
+
+#define _IOC_NRBITS 8
+#define _IOC_TYPEBITS 8
+#define _IOC_SIZEBITS 14
+#define _IOC_DIRBITS 2
+#define _IOC_NRMASK ((1 << _IOC_NRBITS)-1)
+#define _IOC_TYPEMASK ((1 << _IOC_TYPEBITS)-1)
+#define _IOC_SIZEMASK ((1 << _IOC_SIZEBITS)-1)
+#define _IOC_DIRMASK ((1 << _IOC_DIRBITS)-1)
+#define _IOC_NRSHIFT 0
+#define _IOC_TYPESHIFT (_IOC_NRSHIFT+_IOC_NRBITS)
+#define _IOC_SIZESHIFT (_IOC_TYPESHIFT+_IOC_TYPEBITS)
+#define _IOC_DIRSHIFT (_IOC_SIZESHIFT+_IOC_SIZEBITS)
+#define DASD_IOCTL_LETTER 'D'
+
+#define BIODASDINFO _IOR(DASD_IOCTL_LETTER,1,dasd_information_t)
+#define BLKSSZGET _IO(0x12,104)
+
+int probe_ibm_partition(int fd, char *out)
+{
+ int blocksize;
+ dasd_information_t info;
+ char name[7] = {0,};
+ unsigned char data[16];
+
+ if (ioctl(fd, BIODASDINFO, (unsigned long)&info) != 0)
+ return -1;
+
+ if (ioctl(fd, BLKSSZGET, (unsigned long)&blocksize))
+ return -1;
+
+ lseek(fd, info.label_block * blocksize, SEEK_SET);
+ if (read(fd, &data, 16) != 16)
+ return -1;
+
+ if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
+ strncpy(name, data + 8, 6);
+ else
+ strncpy(name, data + 4, 6);
+
+ vtoc_ebcdic_dec(name, out, 6);
+
+ return 0;
+}
diff -Nru a/extras/volume_id/dasdlabel.h b/extras/volume_id/dasdlabel.h
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/dasdlabel.h Fri May 7 15:55:58 2004
@@ -0,0 +1,11 @@
+/*
+ * dasdlabel.h
+ *
+ */
+
+#ifndef _DASDLABEL_H_
+#define _DASDLABEL_H_
+
+extern int probe_ibm_partition(int fd, char *out);
+
+#endif
diff -Nru a/extras/volume_id/udev_volume_id.c b/extras/volume_id/udev_volume_id.c
--- a/extras/volume_id/udev_volume_id.c Fri May 7 15:55:58 2004
+++ b/extras/volume_id/udev_volume_id.c Fri May 7 15:55:58 2004
@@ -4,7 +4,7 @@
* Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
*
* sample udev rule for creation of a symlink with the filsystem uuid:
- * KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
+ * KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -u", SYMLINK="%c"
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -25,19 +25,58 @@
#include <stdlib.h>
#include <unistd.h>
+#include "../../udev_lib.h"
+#include "../../libsysfs/sysfs/libsysfs.h"
#include "volume_id.h"
+#include "dasdlabel.h"
-int main(int argc, char *argv[])
+static struct volume_id *open_classdev(struct sysfs_class_device *class_dev)
{
struct volume_id *vid;
- const char help[] = "usage: udev_volume_id -m<minor> -M<major> [-t|-l|-u]\n";
- int major = -1;
- int minor = -1;
- char *tail;
- static const char short_options[] = "M:m:htlu";
+ struct sysfs_attribute *attr;
+ int major, minor;
+
+ attr = sysfs_get_classdev_attr(class_dev, "dev");
+
+ if (attr == NULL) {
+ printf("error reading 'dev' attribute\n");
+ return NULL;
+ }
+
+ if (sscanf(attr->value, "%u:%u", &major, &minor) != 2) {
+ printf("error getting major/minor number\n");
+ return NULL;
+ }
+
+ vid = volume_id_open_dev_t(makedev(major, minor));
+ if (vid == NULL) {
+ printf("error open volume\n");
+ return NULL;
+ }
+
+ return vid;
+}
+
+int main(int argc, char *argv[])
+{
+ const char help[] = "usage: udev_volume_id [-t|-l|-u|-d]\n"
+ " -t filesystem type\n"
+ " -l filesystem label\n"
+ " -u filesystem uuid\n"
+ " -d also try to read dasd label from parent device\n"
+ "\n";
+ static const char short_options[] = "htlud";
int option;
+ char sysfs_path[SYSFS_PATH_MAX];
+ char dev_path[SYSFS_PATH_MAX];
+ struct sysfs_class_device *class_dev = NULL;
+ struct sysfs_class_device *class_dev_parent = NULL;
+ struct volume_id *vid = NULL;
+ char *devpath;
char print = '\0';
- int rc;
+ int dasd = 0;
+ char dasd_label[7];
+ int rc = 1;
while (1) {
@@ -46,29 +85,18 @@
break;
switch (option) {
- case 'M':
- major = (int) strtoul(optarg, &tail, 10);
- if (tail[0] != '\0') {
- printf("invalid major\n");
- exit(1);
- }
- break;
- case 'm':
- minor = (int) strtoul(optarg, &tail, 10);
- if (tail[0] != '\0') {
- printf("invalid minor\n");
- exit(1);
- }
- break;
case 't':
print = 't';
- break;
+ continue;
case 'l':
print = 'l';
- break;
+ continue;
case 'u':
print = 'u';
- break;
+ continue;
+ case 'd':
+ dasd = 1;
+ continue;
case 'h':
case '?':
default:
@@ -77,23 +105,57 @@
}
}
- if (major == -1 || minor == -1) {
- printf(help);
- exit(1);
+ devpath = getenv("DEVPATH");
+ if (devpath == NULL) {
+ printf("error DEVPATH empty\n");
+ goto exit;
}
- vid = volume_id_open_dev_t(makedev(major, minor));
- if (vid == NULL) {
- printf("error open volume\n");
- exit(1);
+ if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
+ printf("error getting sysfs mount path\n");
+ goto exit;
}
- rc = volume_id_probe(vid, ALL);
- if (rc != 0) {
- printf("error probing volume\n");
- exit(1);
+ strfieldcpy(dev_path, sysfs_path);
+ strfieldcat(dev_path, devpath);
+
+ class_dev = sysfs_open_class_device_path(dev_path);
+ if (class_dev == NULL) {
+ printf("error getting class device\n");
+ goto exit;
+ }
+
+ vid = open_classdev(class_dev);
+ if (vid != NULL) {
+ if (volume_id_probe(vid, ALL) == 0)
+ goto print;
}
+ if (dasd == 0)
+ goto exit;
+
+ /* if we are on a partition, close it and open main block device */
+ class_dev_parent = sysfs_get_classdev_parent(class_dev);
+ if (class_dev_parent != NULL) {
+ volume_id_close(vid);
+ vid = open_classdev(class_dev_parent);
+ if (vid == NULL) {
+ printf("error open main block device\n");
+ goto exit;
+ }
+ }
+
+ if (probe_ibm_partition(vid->fd, dasd_label) == 0) {
+ vid->fs_name = "dasd";
+ strncpy(vid->label_string, dasd_label, 6);
+ goto print;
+ }
+
+ printf("unknown volume type\n");
+ goto exit;
+
+
+print:
switch (print) {
case 't':
printf("%s\n", vid->fs_name);
@@ -114,7 +176,10 @@
printf("U:%s\n", vid->uuid_string);
}
- volume_id_close(vid);
-
- exit(0);
+exit:
+ if (class_dev != NULL)
+ sysfs_close_class_device(class_dev);
+ if (vid != NULL)
+ volume_id_close(vid);
+ exit(rc);
}
diff -Nru a/extras/volume_id/volume_id.c b/extras/volume_id/volume_id.c
--- a/extras/volume_id/volume_id.c Fri May 7 15:55:58 2004
+++ b/extras/volume_id/volume_id.c Fri May 7 15:55:58 2004
@@ -839,10 +839,11 @@
char tmp_node[VOLUME_ID_PATH_MAX];
snprintf(tmp_node, VOLUME_ID_PATH_MAX,
- "/tmp/volume-%u-%u", major(devt), minor(devt));
+ "/tmp/volume-%u-%u-%u", getpid(), major(devt), minor(devt));
tmp_node[VOLUME_ID_PATH_MAX] = '\0';
/* create tempory node to open the block device */
+ unlink(tmp_node);
if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
return NULL;
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (8 preceding siblings ...)
2004-05-07 14:02 ` Kay Sievers
@ 2004-05-07 18:25 ` Arnd Bergmann
2004-05-07 21:52 ` Kay Sievers
2004-05-08 11:29 ` Kay Sievers
11 siblings, 0 replies; 14+ messages in thread
From: Arnd Bergmann @ 2004-05-07 18:25 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1.1: Type: text/plain, Size: 1267 bytes --]
On Friday 07 May 2004 16:02, Kay Sievers wrote:
> Hi Arnd,
> here is a untested first try. udev_volume_id uses libsysfs to get the
> major/minor numbers now and therefore relies on the DEVPATH environment
> variable instead of the command line options:
> If called with the -d option, it tries to read the dasd label from the
> main device too.
>
> KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -l -d", NAME="%c"
>
> Is this what you had in mind?
It is almost what I wanted. This patch goes on top of yours,
changing the following things:
- Return 0 on success again. This was probably an oversight of yours.
- Call set_label_string() to normalize the dasd label like fs labels.
- Improve set_label_string() to fix up other problems with the label
beside just trailing blanks.
- Don't try to look for an fs label if told to get the dasd label.
Otherwise we would practically always find a valid fs signature and
then no label if there is none set. This is also more consistant
with the old devfs behavior that always created node by dasd label.
- add an entry in udev.rules to actually read the dasd label (drop
that if you feel it does not belong in the default config).
With these changes, it's working now.
Arnd <><
[-- Attachment #1.2: 02-dasd-volume_id.patch --]
[-- Type: text/x-diff, Size: 3617 bytes --]
diff -u -r udev-025-dasdvolid/extras/volume_id/udev_volume_id.c udev-025/extras/volume_id/udev_volume_id.c
--- udev-025-dasdvolid/extras/volume_id/udev_volume_id.c 2004-05-07 19:32:03.000000000 +0200
+++ udev-025/extras/volume_id/udev_volume_id.c 2004-05-07 20:04:32.000000000 +0200
@@ -63,7 +63,7 @@
" -t filesystem type\n"
" -l filesystem label\n"
" -u filesystem uuid\n"
- " -d also try to read dasd label from parent device\n"
+ " -d read dasd label from parent device\n"
"\n";
static const char short_options[] = "htlud";
int option;
@@ -126,13 +126,11 @@
}
vid = open_classdev(class_dev);
- if (vid != NULL) {
+ if (vid != NULL && dasd == 0) {
if (volume_id_probe(vid, ALL) == 0)
goto print;
- }
-
- if (dasd == 0)
goto exit;
+ }
/* if we are on a partition, close it and open main block device */
class_dev_parent = sysfs_get_classdev_parent(class_dev);
@@ -147,7 +145,7 @@
if (probe_ibm_partition(vid->fd, dasd_label) == 0) {
vid->fs_name = "dasd";
- strncpy(vid->label_string, dasd_label, 6);
+ set_label_string(vid, dasd_label, 6);
goto print;
}
@@ -175,6 +173,7 @@
printf("L:%s\n", vid->label_string);
printf("U:%s\n", vid->uuid_string);
}
+ rc = 0;
exit:
if (class_dev != NULL)
diff -u -r udev-025-dasdvolid/extras/volume_id/volume_id.c udev-025/extras/volume_id/volume_id.c
--- udev-025-dasdvolid/extras/volume_id/volume_id.c 2004-05-07 19:32:03.000000000 +0200
+++ udev-025/extras/volume_id/volume_id.c 2004-05-07 20:00:45.000000000 +0200
@@ -75,19 +75,24 @@
id->label_raw_len = count;
}
-static void set_label_string(struct volume_id *id, char *buf, int count)
+void set_label_string(struct volume_id *id, char *buf, int count)
{
int i;
memcpy(id->label_string, buf, count);
- /* remove trailing whitespace */
- i = strlen(id->label_string);
+ /* remove trailing whitespace and garbage */
+ i = strnlen(id->label_string, VOLUME_ID_LABEL_SIZE);
while (i--) {
- if (! isspace(id->label_string[i]))
+ id->label_string[i+1] = '\0';
+ if (isalnum(id->label_string[i]))
break;
}
- id->label_string[i+1] = '\0';
+ /* replace remaining garbage with '_' */
+ while (i--) {
+ if (!isalnum(id->label_string[i]))
+ id->label_string[i] = '_';
+ }
}
static void set_uuid(struct volume_id *id, unsigned char *buf, int count)
diff -u -r udev-025-dasdvolid/extras/volume_id/volume_id.h udev-025/extras/volume_id/volume_id.h
--- udev-025-dasdvolid/extras/volume_id/volume_id.h 2004-05-06 02:00:26.000000000 +0200
+++ udev-025/extras/volume_id/volume_id.h 2004-05-07 20:04:05.000000000 +0200
@@ -76,4 +76,7 @@
/* free allocated device info */
extern void volume_id_close(struct volume_id *id);
+/* set the label, eliminating unprintable output */
+extern void set_label_string(struct volume_id *id, char *buf, int count);
+
#endif
--- udev-025-dasdvolid/etc/udev/udev.rules 2004-05-07 20:11:36.000000000 +0200
+++ udev-025/etc/udev/udev.rules 2004-05-07 20:11:18.000000000 +0200
@@ -45,3 +45,10 @@
# raw devices
KERNEL="raw[0-9]*", NAME="raw/%k"
+
+# s390 block devices
+KERNEL="dasd*[a-z]", PROGRAM="/sbin/udev_volume_id -d -l" RESULT="[0-9A-Z]*", SYMLINK="dasd/%c/disc dasd/%b/disc"
+KERNEL="dasd*[0-9]", PROGRAM="/sbin/udev_volume_id -d -l" RESULT="[0-9A-Z]*", SYMLINK="dasd/%c/part%n dasd/%b/part%n"
+KERNEL="dasd*[a-z]", SYMLINK="dasd/%b/disc"
+KERNEL="dasd*[0-9]", SYMLINK="dasd/%b/part%n"
+KERNEL="dcssblk*", NAME="%k", SYMLINK="dcssblk/%b"
[-- Attachment #2: signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (9 preceding siblings ...)
2004-05-07 18:25 ` Arnd Bergmann
@ 2004-05-07 21:52 ` Kay Sievers
2004-05-08 11:29 ` Kay Sievers
11 siblings, 0 replies; 14+ messages in thread
From: Kay Sievers @ 2004-05-07 21:52 UTC (permalink / raw)
To: linux-hotplug
On Fri, May 07, 2004 at 08:25:34PM +0200, Arnd Bergmann wrote:
> - Call set_label_string() to normalize the dasd label like fs labels.
> - Improve set_label_string() to fix up other problems with the label
> beside just trailing blanks.
> -static void set_label_string(struct volume_id *id, char *buf, int count)
> +void set_label_string(struct volume_id *id, char *buf, int count)
> {
> int i;
>
> memcpy(id->label_string, buf, count);
>
> - /* remove trailing whitespace */
> - i = strlen(id->label_string);
> + /* remove trailing whitespace and garbage */
> + i = strnlen(id->label_string, VOLUME_ID_LABEL_SIZE);
> while (i--) {
> - if (! isspace(id->label_string[i]))
> + id->label_string[i+1] = '\0';
> + if (isalnum(id->label_string[i]))
> break;
> }
> - id->label_string[i+1] = '\0';
> + /* replace remaining garbage with '_' */
> + while (i--) {
> + if (!isalnum(id->label_string[i]))
> + id->label_string[i] = '_';
Hmm, doesn't it break UTF-8 characters, which are common in udf labels?
Should we better do it only for dasd and keep the set_label_string() private?
Kay
-------------------------------------------------------
This SF.Net email is sponsored by Sleepycat Software
Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
deliver higher performing products faster, at low TCO.
http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Re: [PATCH] udev callout for reading filesystem labels
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
` (10 preceding siblings ...)
2004-05-07 21:52 ` Kay Sievers
@ 2004-05-08 11:29 ` Kay Sievers
11 siblings, 0 replies; 14+ messages in thread
From: Kay Sievers @ 2004-05-08 11:29 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 1648 bytes --]
On Sat, May 08, 2004 at 12:54:01AM +0200, Arnd Bergmann wrote:
> Kay Sievers <kay.sievers@vrfy.org> schrieb am 07.05.2004, 23:52:56:
>
> > Hmm, doesn't it break UTF-8 characters, which are common in udf labels?
> Oops, that's probably right.
>
> > Should we better do it only for dasd and keep the set_label_string() private?
>
> I had first done something special for dasd until I noticed that some
> function like that already exists. Actually for dasd labels, it should
> be completely sufficient to do the trailing space elimination because
> the tools for setting the label do most of the sanitizing, but treating
> dasd labels different from fs labels during output would be pointless.
>
> What I wanted to avoid is trouble from invalid labels, indepent of
> disk vs. fs labels. Note that udev does not really behave well if
> there are spaces within the label and though that is invalid for dasd,
> it might not be for some other label type.
>
> Aside from whitespace, which is interpreted by udev, we at least need
> to filter out '/' characters as well as "." and "..", which are special
> to the file system. Ignoring all non-alphanumerical characters was just
> my safe guess.
Arnd,
here is a new patch on top of the volume_id of the current bk tree.
Your fixes are integrated, the leading whitespace of the volume strings
is removed, spaces replaced by underscore, and slashes are skipped.
More special character handling can easily be added in the switch().
The rules are placed in it's own file in the volume_id directory, as we
support multiple config files, the user just need to drop it in /etc/udev/ruled.d/.
thanks,
Kay
[-- Attachment #2: 01-dasd-volume_id.patch --]
[-- Type: text/plain, Size: 17491 bytes --]
diff -Nru a/extras/volume_id/Makefile b/extras/volume_id/Makefile
--- a/extras/volume_id/Makefile Sat May 8 13:10:47 2004
+++ b/extras/volume_id/Makefile Sat May 8 13:10:47 2004
@@ -1,6 +1,6 @@
# Makefile for udev_volume_id
#
-# Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
+# Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -29,8 +29,19 @@
INSTALL_SCRIPT = ${INSTALL_PROGRAM}
override CFLAGS+=-Wall -fno-builtin
-OBJS = volume_id.o udev_volume_id.o
-HEADERS = volume_id.h
+
+SYSFS = ../../libsysfs/sysfs_bus.o \
+ ../../libsysfs/sysfs_class.o \
+ ../../libsysfs/sysfs_device.o \
+ ../../libsysfs/sysfs_dir.o \
+ ../../libsysfs/sysfs_driver.o \
+ ../../libsysfs/sysfs_utils.o \
+ ../../libsysfs/dlist.o
+
+
+
+OBJS = volume_id.o udev_volume_id.o dasdlabel.o $(SYSFS)
+HEADERS = volume_id.h dasdlabel.h
$(OBJS): $(HEADERS)
diff -Nru a/extras/volume_id/dasdlabel.c b/extras/volume_id/dasdlabel.c
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/dasdlabel.c Sat May 8 13:10:47 2004
@@ -0,0 +1,178 @@
+/*
+ * dasdlabel - read label from s390 block device
+ *
+ * Copyright (C) 2004 Arnd Bergmann <arnd@arndb.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include "dasdlabel.h"
+
+static unsigned char EBCtoASC[256] =
+{
+/* 0x00 NUL SOH STX ETX *SEL HT *RNL DEL */
+ 0x00, 0x01, 0x02, 0x03, 0x07, 0x09, 0x07, 0x7F,
+/* 0x08 -GE -SPS -RPT VT FF CR SO SI */
+ 0x07, 0x07, 0x07, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
+/* 0x10 DLE DC1 DC2 DC3 -RES -NL BS -POC
+ -ENP ->LF */
+ 0x10, 0x11, 0x12, 0x13, 0x07, 0x0A, 0x08, 0x07,
+/* 0x18 CAN EM -UBS -CU1 -IFS -IGS -IRS -ITB
+ -IUS */
+ 0x18, 0x19, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+/* 0x20 -DS -SOS FS -WUS -BYP LF ETB ESC
+ -INP */
+ 0x07, 0x07, 0x1C, 0x07, 0x07, 0x0A, 0x17, 0x1B,
+/* 0x28 -SA -SFE -SM -CSP -MFA ENQ ACK BEL
+ -SW */
+ 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x06, 0x07,
+/* 0x30 ---- ---- SYN -IR -PP -TRN -NBS EOT */
+ 0x07, 0x07, 0x16, 0x07, 0x07, 0x07, 0x07, 0x04,
+/* 0x38 -SBS -IT -RFF -CU3 DC4 NAK ---- SUB */
+ 0x07, 0x07, 0x07, 0x07, 0x14, 0x15, 0x07, 0x1A,
+/* 0x40 SP RSP ä ---- */
+ 0x20, 0xFF, 0x83, 0x84, 0x85, 0xA0, 0x07, 0x86,
+/* 0x48 . < ( + | */
+ 0x87, 0xA4, 0x9B, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
+/* 0x50 & ---- */
+ 0x26, 0x82, 0x88, 0x89, 0x8A, 0xA1, 0x8C, 0x07,
+/* 0x58 ß ! $ * ) ; */
+ 0x8D, 0xE1, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAA,
+/* 0x60 - / ---- Ä ---- ---- ---- */
+ 0x2D, 0x2F, 0x07, 0x8E, 0x07, 0x07, 0x07, 0x8F,
+/* 0x68 ---- , % _ > ? */
+ 0x80, 0xA5, 0x07, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
+/* 0x70 --- ---- ---- ---- ---- ---- ---- */
+ 0x07, 0x90, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+/* 0x78 * ` : # @ ' = " */
+ 0x70, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
+/* 0x80 * a b c d e f g */
+ 0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
+/* 0x88 h i ---- ---- ---- */
+ 0x68, 0x69, 0xAE, 0xAF, 0x07, 0x07, 0x07, 0xF1,
+/* 0x90 ° j k l m n o p */
+ 0xF8, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
+/* 0x98 q r ---- ---- */
+ 0x71, 0x72, 0xA6, 0xA7, 0x91, 0x07, 0x92, 0x07,
+/* 0xA0 ~ s t u v w x */
+ 0xE6, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+/* 0xA8 y z ---- ---- ---- ---- */
+ 0x79, 0x7A, 0xAD, 0xAB, 0x07, 0x07, 0x07, 0x07,
+/* 0xB0 ^ ---- § ---- */
+ 0x5E, 0x9C, 0x9D, 0xFA, 0x07, 0x07, 0x07, 0xAC,
+/* 0xB8 ---- [ ] ---- ---- ---- ---- */
+ 0xAB, 0x07, 0x5B, 0x5D, 0x07, 0x07, 0x07, 0x07,
+/* 0xC0 { A B C D E F G */
+ 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+/* 0xC8 H I ---- ö ---- */
+ 0x48, 0x49, 0x07, 0x93, 0x94, 0x95, 0xA2, 0x07,
+/* 0xD0 } J K L M N O P */
+ 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
+/* 0xD8 Q R ---- ü */
+ 0x51, 0x52, 0x07, 0x96, 0x81, 0x97, 0xA3, 0x98,
+/* 0xE0 \ S T U V W X */
+ 0x5C, 0xF6, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+/* 0xE8 Y Z ---- Ö ---- ---- ---- */
+ 0x59, 0x5A, 0xFD, 0x07, 0x99, 0x07, 0x07, 0x07,
+/* 0xF0 0 1 2 3 4 5 6 7 */
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+/* 0xF8 8 9 ---- ---- Ü ---- ---- ---- */
+ 0x38, 0x39, 0x07, 0x07, 0x9A, 0x07, 0x07, 0x07
+};
+
+static void vtoc_ebcdic_dec (unsigned char *source, unsigned char *target, int l)
+{
+ int i;
+
+ for (i = 0; i < l; i++)
+ target[i]=EBCtoASC[(unsigned char)(source[i])];
+}
+
+/*
+ * struct dasd_information_t
+ * represents any data about the data, which is visible to userspace
+ */
+typedef struct dasd_information_t {
+ unsigned int devno; /* S/390 devno */
+ unsigned int real_devno; /* for aliases */
+ unsigned int schid; /* S/390 subchannel identifier */
+ unsigned int cu_type : 16; /* from SenseID */
+ unsigned int cu_model : 8; /* from SenseID */
+ unsigned int dev_type : 16; /* from SenseID */
+ unsigned int dev_model : 8; /* from SenseID */
+ unsigned int open_count;
+ unsigned int req_queue_len;
+ unsigned int chanq_len; /* length of chanq */
+ char type[4]; /* from discipline.name, 'none' for unknown */
+ unsigned int status; /* current device level */
+ unsigned int label_block; /* where to find the VOLSER */
+ unsigned int FBA_layout; /* fixed block size (like AIXVOL) */
+ unsigned int characteristics_size;
+ unsigned int confdata_size;
+ char characteristics[64]; /* from read_device_characteristics */
+ char configuration_data[256]; /* from read_configuration_data */
+} dasd_information_t;
+
+#define _IOC_NRBITS 8
+#define _IOC_TYPEBITS 8
+#define _IOC_SIZEBITS 14
+#define _IOC_DIRBITS 2
+#define _IOC_NRMASK ((1 << _IOC_NRBITS)-1)
+#define _IOC_TYPEMASK ((1 << _IOC_TYPEBITS)-1)
+#define _IOC_SIZEMASK ((1 << _IOC_SIZEBITS)-1)
+#define _IOC_DIRMASK ((1 << _IOC_DIRBITS)-1)
+#define _IOC_NRSHIFT 0
+#define _IOC_TYPESHIFT (_IOC_NRSHIFT+_IOC_NRBITS)
+#define _IOC_SIZESHIFT (_IOC_TYPESHIFT+_IOC_TYPEBITS)
+#define _IOC_DIRSHIFT (_IOC_SIZESHIFT+_IOC_SIZEBITS)
+#define DASD_IOCTL_LETTER 'D'
+
+#define BIODASDINFO _IOR(DASD_IOCTL_LETTER,1,dasd_information_t)
+#define BLKSSZGET _IO(0x12,104)
+
+int probe_ibm_partition(int fd, char *out)
+{
+ int blocksize;
+ dasd_information_t info;
+ char name[7] = {0,};
+ unsigned char data[16];
+
+ if (ioctl(fd, BIODASDINFO, (unsigned long)&info) != 0)
+ return -1;
+
+ if (ioctl(fd, BLKSSZGET, (unsigned long)&blocksize))
+ return -1;
+
+ lseek(fd, info.label_block * blocksize, SEEK_SET);
+ if (read(fd, &data, 16) != 16)
+ return -1;
+
+ if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
+ strncpy(name, data + 8, 6);
+ else
+ strncpy(name, data + 4, 6);
+
+ vtoc_ebcdic_dec(name, out, 6);
+
+ return 0;
+}
diff -Nru a/extras/volume_id/dasdlabel.h b/extras/volume_id/dasdlabel.h
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/dasdlabel.h Sat May 8 13:10:47 2004
@@ -0,0 +1,26 @@
+/*
+ * dasdlabel - read label from s390 block device
+ *
+ * Copyright (C) 2004 Arnd Bergmann <arnd@arndb.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef _DASDLABEL_H_
+#define _DASDLABEL_H_
+
+extern int probe_ibm_partition(int fd, char *out);
+
+#endif
diff -Nru a/extras/volume_id/s390-dasd.rules b/extras/volume_id/s390-dasd.rules
--- /dev/null Wed Dec 31 16:00:00 1969
+++ b/extras/volume_id/s390-dasd.rules Sat May 8 13:10:47 2004
@@ -0,0 +1,7 @@
+# s390 block devices named by disk label
+KERNEL="dasd*[a-z]", PROGRAM="/sbin/udev_volume_id -d -l" RESULT="[0-9A-Z]*", SYMLINK="dasd/%c/disc dasd/%b/disc"
+KERNEL="dasd*[0-9]", PROGRAM="/sbin/udev_volume_id -d -l" RESULT="[0-9A-Z]*", SYMLINK="dasd/%c/part%n dasd/%b/part%n"
+KERNEL="dasd*[a-z]", SYMLINK="dasd/%b/disc"
+KERNEL="dasd*[0-9]", SYMLINK="dasd/%b/part%n"
+KERNEL="dcssblk*", NAME="%k", SYMLINK="dcssblk/%b"
+
diff -Nru a/extras/volume_id/udev_volume_id.c b/extras/volume_id/udev_volume_id.c
--- a/extras/volume_id/udev_volume_id.c Sat May 8 13:10:47 2004
+++ b/extras/volume_id/udev_volume_id.c Sat May 8 13:10:47 2004
@@ -4,7 +4,7 @@
* Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
*
* sample udev rule for creation of a symlink with the filsystem uuid:
- * KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
+ * KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -u", SYMLINK="%c"
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -24,21 +24,75 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <ctype.h>
+#include "../../libsysfs/sysfs/libsysfs.h"
+#include "../../udev_lib.h"
+#include "../../logging.h"
#include "volume_id.h"
+#include "dasdlabel.h"
-int main(int argc, char *argv[])
+#ifdef LOG
+unsigned char logname[LOGNAME_SIZE];
+void log_message(int level, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ vsyslog(level, format, args);
+ va_end(args);
+}
+#endif
+
+static struct volume_id *open_classdev(struct sysfs_class_device *class_dev)
{
struct volume_id *vid;
- const char help[] = "usage: udev_volume_id -m<minor> -M<major> [-t|-l|-u]\n";
- int major = -1;
- int minor = -1;
- char *tail;
- static const char short_options[] = "M:m:htlu";
- int option;
- char print = '\0';
- int rc;
+ struct sysfs_attribute *attr;
+ int major, minor;
+
+ attr = sysfs_get_classdev_attr(class_dev, "dev");
+
+ if (attr == NULL) {
+ printf("error reading 'dev' attribute\n");
+ return NULL;
+ }
+
+ if (sscanf(attr->value, "%u:%u", &major, &minor) != 2) {
+ printf("error getting major/minor number\n");
+ return NULL;
+ }
+
+ vid = volume_id_open_dev_t(makedev(major, minor));
+ if (vid == NULL) {
+ printf("error open volume\n");
+ return NULL;
+ }
+ return vid;
+}
+
+int main(int argc, char *argv[])
+{
+ const char help[] = "usage: udev_volume_id [-t|-l|-u|-d]\n"
+ " -t filesystem type\n"
+ " -l filesystem label\n"
+ " -u filesystem uuid\n"
+ " -d dasd label from parent device\n"
+ "\n";
+ static const char short_options[] = "htlud";
+ int option;
+ char sysfs_path[SYSFS_PATH_MAX];
+ char dev_path[SYSFS_PATH_MAX];
+ struct sysfs_class_device *class_dev = NULL;
+ struct sysfs_class_device *class_dev_parent = NULL;
+ struct volume_id *vid = NULL;
+ char *devpath;
+ char probe = 'a';
+ char print = 'a';
+ char dasd_label[7];
+ static char name[VOLUME_ID_LABEL_SIZE];
+ int len, i, j;
+ int rc = 1;
while (1) {
option = getopt(argc, argv, short_options);
@@ -46,29 +100,18 @@
break;
switch (option) {
- case 'M':
- major = (int) strtoul(optarg, &tail, 10);
- if (tail[0] != '\0') {
- printf("invalid major\n");
- exit(1);
- }
- break;
- case 'm':
- minor = (int) strtoul(optarg, &tail, 10);
- if (tail[0] != '\0') {
- printf("invalid minor\n");
- exit(1);
- }
- break;
case 't':
print = 't';
- break;
+ continue;
case 'l':
print = 'l';
- break;
+ continue;
case 'u':
print = 'u';
- break;
+ continue;
+ case 'd':
+ probe = 'd';
+ continue;
case 'h':
case '?':
default:
@@ -77,44 +120,114 @@
}
}
- if (major == -1 || minor == -1) {
- printf(help);
- exit(1);
+ devpath = getenv("DEVPATH");
+ if (devpath == NULL) {
+ printf("error DEVPATH empty\n");
+ goto exit;
}
- vid = volume_id_open_dev_t(makedev(major, minor));
- if (vid == NULL) {
- printf("error open volume\n");
- exit(1);
+ if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
+ printf("error getting sysfs mount path\n");
+ goto exit;
+ }
+
+ strfieldcpy(dev_path, sysfs_path);
+ strfieldcat(dev_path, devpath);
+
+ class_dev = sysfs_open_class_device_path(dev_path);
+ if (class_dev == NULL) {
+ printf("error getting class device\n");
+ goto exit;
+ }
+
+ switch(probe) {
+ case 'a' :
+ vid = open_classdev(class_dev);
+ if (vid == NULL)
+ goto exit;
+ if (volume_id_probe(vid, ALL) == 0)
+ goto print;
+ break;
+ case 'd' :
+ /* if we are on a partition, close it and open main block device */
+ class_dev_parent = sysfs_get_classdev_parent(class_dev);
+ if (class_dev_parent != NULL) {
+ volume_id_close(vid);
+ vid = open_classdev(class_dev_parent);
+ } else {
+ vid = open_classdev(class_dev_parent);
+ }
+ if (vid == NULL)
+ goto exit;
+ if (probe_ibm_partition(vid->fd, dasd_label) == 0) {
+ vid->fs_name = "dasd";
+ strncpy(vid->label_string, dasd_label, 6);
+ vid->label_string[6] = '\0';
+ goto print;
+ }
+ break;
}
- rc = volume_id_probe(vid, ALL);
- if (rc != 0) {
- printf("error probing volume\n");
- exit(1);
+ printf("unknown volume type\n");
+ goto exit;
+
+
+print:
+ len = strnlen(vid->label_string, VOLUME_ID_LABEL_SIZE);
+
+ /* remove trailing spaces */
+ while (len > 0 && isspace(vid->label_string[len-1]))
+ len--;
+ name[len] = '\0';
+
+ /* substitute chars */
+ i = 0;
+ j = 0;
+ while (j < len) {
+ switch(vid->label_string[j]) {
+ case '/' :
+ break;
+ case ' ' :
+ name[i++] = '_';
+ break;
+ default :
+ name[i++] = vid->label_string[j];
+ }
+ j++;
}
+ name[i] = '\0';
switch (print) {
case 't':
printf("%s\n", vid->fs_name);
break;
case 'l':
- if (vid->label_string[0] == '\0')
- exit(2);
- printf("%s\n", vid->label_string);
+ if (name[0] == '\0') {
+ rc = 2;
+ goto exit;
+ }
+ printf("%s\n", name);
break;
case 'u':
- if (vid->uuid_string[0] == '\0')
- exit(2);
+ if (vid->uuid_string[0] == '\0') {
+ rc = 2;
+ goto exit;
+ }
printf("%s\n", vid->uuid_string);
break;
- default:
+ case 'a':
printf("T:%s\n", vid->fs_name);
printf("L:%s\n", vid->label_string);
+ printf("N:%s\n", name);
printf("U:%s\n", vid->uuid_string);
}
+ rc = 0;
- volume_id_close(vid);
+exit:
+ if (class_dev != NULL)
+ sysfs_close_class_device(class_dev);
+ if (vid != NULL)
+ volume_id_close(vid);
- exit(0);
+ exit(rc);
}
diff -Nru a/extras/volume_id/volume_id.c b/extras/volume_id/volume_id.c
--- a/extras/volume_id/volume_id.c Sat May 8 13:10:47 2004
+++ b/extras/volume_id/volume_id.c Sat May 8 13:10:47 2004
@@ -82,7 +82,7 @@
memcpy(id->label_string, buf, count);
/* remove trailing whitespace */
- i = strlen(id->label_string);
+ i = strnlen(id->label_string, count);
while (i--) {
if (! isspace(id->label_string[i]))
break;
@@ -839,10 +839,11 @@
char tmp_node[VOLUME_ID_PATH_MAX];
snprintf(tmp_node, VOLUME_ID_PATH_MAX,
- "/tmp/volume-%u-%u", major(devt), minor(devt));
+ "/tmp/volume-%u-%u-%u", getpid(), major(devt), minor(devt));
tmp_node[VOLUME_ID_PATH_MAX] = '\0';
/* create tempory node to open the block device */
+ unlink(tmp_node);
if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
return NULL;
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] udev callout for reading filesystem labels
@ 2004-05-08 16:52 arndb
0 siblings, 0 replies; 14+ messages in thread
From: arndb @ 2004-05-08 16:52 UTC (permalink / raw)
To: linux-hotplug
Kay Sievers <kay.sievers@vrfy.org> schrieb am 08.05.2004, 13:29:32:
> Arnd,
> here is a new patch on top of the volume_id of the current bk tree.
>
> Your fixes are integrated, the leading whitespace of the volume strings
> is removed, spaces replaced by underscore, and slashes are skipped.
> More special character handling can easily be added in the switch().
>
> The rules are placed in it's own file in the volume_id directory, as we
> support multiple config files, the user just need to drop it in /etc/udev/ruled.d/.
Yes, this looks really good now, thanks for your effort!
I did a little research now and found that there are at least two
other partition formats besides IBM dasd label that support disk names.
The ones I found are Solaris-x86 and Atari disklabels, both of which
are pretty uncommon. If we ever want to support those as well, the
meaning of the '-d' switch can be changed from 'dasd name' to 'disk
name'.
Arnd <><
-------------------------------------------------------
This SF.Net email is sponsored by Sleepycat Software
Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
deliver higher performing products faster, at low TCO.
http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2004-05-08 16:52 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-29 21:04 [PATCH] udev callout for reading filesystem labels Kay Sievers
2004-04-30 13:35 ` Kevin P. Fleming
2004-04-30 14:36 ` Oliver Neukum
2004-04-30 22:29 ` Greg KH
2004-05-05 1:14 ` Kay Sievers
2004-05-05 21:24 ` Greg KH
2004-05-06 9:30 ` Arnd Bergmann
2004-05-06 20:18 ` Kay Sievers
2004-05-06 22:59 ` Kay Sievers
2004-05-07 14:02 ` Kay Sievers
2004-05-07 18:25 ` Arnd Bergmann
2004-05-07 21:52 ` Kay Sievers
2004-05-08 11:29 ` Kay Sievers
-- strict thread matches above, loose matches on Subject: below --
2004-05-08 16:52 arndb
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).