[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
/*
* 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 :
2004-05-12 11:52:52 +04:00
* KERNEL = " sd* " , PROGRAM = " /sbin/udev_volume_id -u " , SYMLINK = " %c "
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
*
* 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 0213 9 , USA .
*
*/
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
2004-09-19 10:22:27 +04:00
# include <string.h>
2004-05-12 11:52:52 +04:00
# include <ctype.h>
2004-09-05 20:05:36 +04:00
# include <sys/ioctl.h>
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
2004-05-12 11:52:52 +04:00
# include "../../libsysfs/sysfs/libsysfs.h"
2004-12-09 06:35:29 +03:00
# include "../../udev_utils.h"
2004-05-12 11:52:52 +04:00
# include "../../logging.h"
2005-01-31 07:28:44 +03:00
# include "volume_id/volume_id.h"
# include "volume_id/dasd/dasd.h"
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
2004-09-19 10:22:27 +04:00
# define BLKGETSIZE64 _IOR(0x12,114,size_t)
2004-05-12 11:52:52 +04:00
# ifdef LOG
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 )
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
{
struct volume_id * vid ;
2004-05-12 11:52:52 +04:00
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 ;
}
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
2004-05-12 11:52:52 +04:00
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 disk label from main device \n "
" \n " ;
static const char short_options [ ] = " htlud " ;
2004-10-19 09:57:41 +04:00
char sysfs_mnt_path [ SYSFS_PATH_MAX ] ;
2004-05-12 11:52:52 +04:00
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 = ' p ' ;
char print = ' a ' ;
static char name [ VOLUME_ID_LABEL_SIZE ] ;
int len , i , j ;
2004-09-05 20:05:36 +04:00
unsigned long long size ;
2004-05-12 11:52:52 +04:00
int rc = 1 ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
2004-10-19 09:57:41 +04:00
logging_init ( " udev_volume_id " ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
while ( 1 ) {
2004-09-05 20:05:36 +04:00
int option ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
option = getopt ( argc , argv , short_options ) ;
if ( option = = - 1 )
break ;
switch ( option ) {
case ' t ' :
print = ' t ' ;
2004-05-12 11:52:52 +04:00
continue ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
case ' l ' :
print = ' l ' ;
2004-05-12 11:52:52 +04:00
continue ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
case ' u ' :
print = ' u ' ;
2004-05-12 11:52:52 +04:00
continue ;
case ' d ' :
probe = ' d ' ;
continue ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
case ' h ' :
case ' ? ' :
default :
printf ( help ) ;
exit ( 1 ) ;
}
}
2004-05-12 11:52:52 +04:00
devpath = getenv ( " DEVPATH " ) ;
if ( devpath = = NULL ) {
printf ( " error DEVPATH empty \n " ) ;
goto exit ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
}
2004-10-19 09:57:41 +04:00
if ( sysfs_get_mnt_path ( sysfs_mnt_path , SYSFS_PATH_MAX ) ! = 0 ) {
2004-05-12 11:52:52 +04:00
printf ( " error getting sysfs mount path \n " ) ;
goto exit ;
}
2004-10-19 09:57:41 +04:00
strfieldcpy ( dev_path , sysfs_mnt_path ) ;
2004-05-12 11:52:52 +04:00
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 ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
}
2004-05-12 11:52:52 +04:00
switch ( probe ) {
case ' p ' :
/* open block device */
vid = open_classdev ( class_dev ) ;
if ( vid = = NULL )
goto exit ;
2004-09-05 20:05:36 +04:00
2004-09-19 10:22:27 +04:00
if ( ioctl ( vid - > fd , BLKGETSIZE64 , & size ) ! = 0 )
size = 0 ;
2004-09-05 20:05:36 +04:00
2005-01-31 07:28:44 +03:00
if ( volume_id_probe_all ( vid , 0 , size ) = = 0 )
2004-05-12 11:52:52 +04:00
goto print ;
break ;
case ' d ' :
2004-09-05 20:05:36 +04:00
/* if we are on a partition, open main block device instead */
2004-05-12 11:52:52 +04:00
class_dev_parent = sysfs_get_classdev_parent ( class_dev ) ;
2004-09-05 20:05:36 +04:00
if ( class_dev_parent ! = NULL )
2004-05-12 11:52:52 +04:00
vid = open_classdev ( class_dev_parent ) ;
2004-09-05 20:05:36 +04:00
else
2005-01-30 15:09:38 +03:00
vid = open_classdev ( class_dev ) ;
2004-05-12 11:52:52 +04:00
if ( vid = = NULL )
goto exit ;
2004-09-05 20:05:36 +04:00
2005-01-31 07:28:44 +03:00
if ( probe_ibm_partition ( vid ) = = 0 )
2004-05-12 11:52:52 +04:00
goto print ;
break ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
}
2004-05-12 11:52:52 +04:00
printf ( " unknown volume type \n " ) ;
goto exit ;
print :
2004-09-05 20:05:36 +04:00
len = strnlen ( vid - > label , VOLUME_ID_LABEL_SIZE ) ;
2004-05-12 11:52:52 +04:00
/* remove trailing spaces */
2004-09-05 20:05:36 +04:00
while ( len > 0 & & isspace ( vid - > label [ len - 1 ] ) )
2004-05-12 11:52:52 +04:00
len - - ;
name [ len ] = ' \0 ' ;
/* substitute chars */
i = 0 ;
j = 0 ;
while ( j < len ) {
2004-09-05 20:05:36 +04:00
switch ( vid - > label [ j ] ) {
2004-05-12 11:52:52 +04:00
case ' / ' :
break ;
case ' ' :
name [ i + + ] = ' _ ' ;
break ;
default :
2004-09-05 20:05:36 +04:00
name [ i + + ] = vid - > label [ j ] ;
2004-05-12 11:52:52 +04:00
}
j + + ;
}
name [ i ] = ' \0 ' ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
switch ( print ) {
case ' t ' :
2004-09-05 20:05:36 +04:00
printf ( " %s \n " , vid - > type ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
break ;
case ' l ' :
2004-09-05 20:05:36 +04:00
if ( name [ 0 ] = = ' \0 ' | | vid - > usage_id ! = VOLUME_ID_FILESYSTEM ) {
2004-05-12 11:52:52 +04:00
rc = 2 ;
goto exit ;
}
printf ( " %s \n " , name ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
break ;
case ' u ' :
2004-09-05 20:05:36 +04:00
if ( vid - > uuid [ 0 ] = = ' \0 ' | | vid - > usage_id ! = VOLUME_ID_FILESYSTEM ) {
2004-05-12 11:52:52 +04:00
rc = 2 ;
goto exit ;
}
2004-09-05 20:05:36 +04:00
printf ( " %s \n " , vid - > uuid ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
break ;
2004-05-12 11:52:52 +04:00
case ' a ' :
2005-01-31 07:28:44 +03:00
printf ( " F:%s \n " , vid - > usage ) ;
2004-09-05 20:05:36 +04:00
printf ( " T:%s \n " , vid - > type ) ;
printf ( " V:%s \n " , vid - > type_version ) ;
printf ( " L:%s \n " , vid - > label ) ;
2004-05-12 11:52:52 +04:00
printf ( " N:%s \n " , name ) ;
2004-09-05 20:05:36 +04:00
printf ( " U:%s \n " , vid - > uuid ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
}
2004-05-12 11:52:52 +04:00
rc = 0 ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
2004-05-12 11:52:52 +04:00
exit :
if ( class_dev ! = NULL )
sysfs_close_class_device ( class_dev ) ;
if ( vid ! = NULL )
volume_id_close ( vid ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
2004-10-19 09:57:41 +04:00
logging_close ( ) ;
2004-05-12 11:52:52 +04:00
exit ( rc ) ;
[PATCH] udev callout for reading filesystem labels
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.
2004-05-01 10:26:33 +04:00
}