Linux – Change owner of LVM LV’s device node in RHEL5

hard drivelinuxlvmudev

First time using serverfault, please excuse any breaches of etiquette.

I've created several LVM2 logical volumes in local storage on a server, and would like one of the device nodes – not the filesystem or mount point – to be owned by a user/group other than root:root.

This is pretty much a default RHEL5 server. I understand that the device node is created dynamically at boot time after LVM scans the disks for pv/vg/lvs. I'm not really sure how udev, mapper, and lvm interact to create these nodes, and the configuration file specs are a little cryptic for someone without much experience.

There will be more lvs to follow that also need alternate permissions, but I'd ideally like to keep the other lvs in the volume group at root:root, and only change specific ones.

Can anyone help me figure this one out? I've been googling for hours.

Thanks in advance,
Tony


Update:

I accomplished this through the following. It may be a roundabout way of doing things, but this is only a temporary environment (famous last words!). Oh, I may also want to remove the lines that print to /tmp/foo.

[root@xxxxxxx rules.d]# **cat /etc/udev/rules.d/11-lvm.rules**
ACTION=="add|change", KERNEL=="dm-*", PROGRAM="/bin/sh -c /etc/udev/scripts/oracle_perms.sh"

[root@xxxxxxx rules.d]# **cat /etc/udev/scripts/oracle_perms.sh**
#!/bin/bash

echo "DEVPATH=$DEVPATH" >> /tmp/foo
MAJMIN=`cat /sys${DEVPATH}/dev`
echo "MAJMIN=$MAJMIN" >> /tmp/foo
MAJ=`echo ${MAJMIN} | awk -F: '{ print $1 }'`
MIN=`echo ${MAJMIN} | awk -F: '{ print $2 }'`
DEVNODE=`/sbin/dmsetup info -j ${MAJ} -m ${MIN} | grep Name | awk '{ print $2 }'`
echo "DEVNODE=${DEVNODE}" >> /tmp/foo
echo "${DEVNODE}" | grep ora >/dev/null 2>&1
if [ "$?" == "0" ]; then
  echo "Making change...." >> /tmp/foo
  chown oracle:dba /dev/mapper/${DEVNODE}
  chmod 660 /dev/mapper/${DEVNODE}
  ls -l /dev/mapper/${DEVNODE} >> /tmp/foo
else
  echo "No 'ora' name detected. No change necessary." >> /tmp/foo
fi

Note that the solution above automagically changes ANY LV that's created with "ora" in the name. Hey, it works for now.

Best Answer

The following should work (adapt as necessary):

# cat /etc/udev/rules.d/99-custom.rules
ENV{DM_NAME}=="system-test", ACTION=="add|change", MODE="0664", OWNER="michael", GROUP="disk", PROGRAM="/bin/logger /dev/$env{DM_NAME} owner changed to michael", SYMLINK+="oracle-$env{DM_NAME}"

Then you should get the result:

# lvcreate -L 1G /dev/system -n test
# ls -al /dev/dm-9 /dev/oracle-system-test /dev/system/test /dev/mapper/system-test
brw-rw-r-- 1 michael disk 253, 9 2009-08-08 01:20 /dev/dm-9
brw-r----- 1 root    disk 253, 9 2009-08-08 01:20 /dev/mapper/system-test
lrwxrwxrwx 1 root    root      4 2009-08-08 01:20 /dev/oracle-system-test -> dm-9
lrwxrwxrwx 1 root    root     23 2009-08-08 01:20 /dev/system/test -> /dev/mapper/system-test

I wasn't able to figure out how to modify the actual device node created by LVM, but I was able to modify the device-mapper node. I then added a symlink for oracle to access it with which is a bit of a hack, but works.

(my earlier answer was a little rushed and untested. But I got really curious how to do it right :)