Permanently changing the ownership (or group) of LVM volume

10,751

On Debian (and hopefully your distro as well) all the LVM metadata is already loaded into udev (by some of the rules in /lib/udev/rules.d). So you can use a rules file like this:

$ cat /etc/udev/rules.d/92-local-oracle-permissions.rules 
ENV{DM_VG_NAME}=="vgRandom" ENV{DM_LV_NAME}=="ora_users_*" OWNER="oracle"
ENV{DM_VG_NAME}=="vgRandom" ENV{DM_LV_NAME}=="ora_undo_*"  OWNER="oracle"
ENV{DM_VG_NAME}=="vgSeq"    ENV{DM_LV_NAME}=="ora_redo_*"  OWNER="oracle"

You can use udevadm to find out what kinds of things you can base your udev rules on. All the E: lines can be found in ENV in udev, e.g., the E: DM_LV_NAME=ora_data line matched by one of the above rules:

# udevadm info --query=all --name /dev/dm-2 
P: /devices/virtual/block/dm-2
N: dm-2
L: -100
S: block/253:2
S: mapper/vgRandom-ora_data
S: disk/by-id/dm-name-vgRandom-ora_data
S: disk/by-id/dm-uuid-LVM-d6wXWIzc7xWJkx3Tx3o4Q9huEG1ajakYr0SLSl5as3C6RoydA66sgNHxBZdpem89
S: disk/by-uuid/787651c2-e4c7-40e2-b0fc-1a3978098dce
S: vgRandom/ora_data
E: UDEV_LOG=3
E: DEVPATH=/devices/virtual/block/dm-2
E: MAJOR=253
E: MINOR=2
E: DEVNAME=/dev/dm-2
E: DEVTYPE=disk
E: SUBSYSTEM=block
E: DM_UDEV_PRIMARY_SOURCE_FLAG=1
E: DM_NAME=vgRandom-ora_data
E: DM_UUID=LVM-d6wXWIzc7xWJkx3Tx3o4Q9huEG1ajakYr0SLSl5as3C6RoydA66sgNHxBZdpem89
E: DM_SUSPENDED=0
E: DM_UDEV_RULES=1
E: DM_VG_NAME=vgRandom
E: DM_LV_NAME=ora_data
E: DEVLINKS=/dev/block/253:2 /dev/mapper/vgRandom-ora_data /dev/disk/by-id/dm-name-vgRandom-ora_data /dev/disk/by-id/dm-uuid-LVM-d6wXWIzc7xWJkx3Tx3o4Q9huEG1ajakYr0SLSl5as3C6RoydA66sgNHxBZdpem89 /dev/disk/by-uuid/787651c2-e4c7-40e2-b0fc-1a3978098dce /dev/vgRandom/ora_data
E: ID_FS_UUID=787651c2-e4c7-40e2-b0fc-1a3978098dce
E: ID_FS_UUID_ENC=787651c2-e4c7-40e2-b0fc-1a3978098dce
E: ID_FS_VERSION=1.0
E: ID_FS_TYPE=ext4
E: ID_FS_USAGE=filesystem
E: FSTAB_NAME=/dev/mapper/vgRandom-ora_data
E: FSTAB_DIR=/opt/oracle/oracle/oradata
E: FSTAB_TYPE=ext4
E: FSTAB_OPTS=noatime
E: FSTAB_FREQ=0
E: FSTAB_PASSNO=3

Also, you can match on sysfs attributes, in either ATTR (device only) or ATTRS (parents too). You can see all the attributes like this:

# udevadm info --attribute-walk --name /dev/dm-2 

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/virtual/block/dm-2':
    KERNEL=="dm-2"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{range}=="1"
    ATTR{ext_range}=="1"
    ATTR{removable}=="0"
    ATTR{ro}=="0"
    ATTR{size}=="41943040"
    ATTR{alignment_offset}=="0"
    ATTR{discard_alignment}=="0"
    ATTR{capability}=="10"
    ATTR{stat}=="36383695        0 4435621936 124776016 29447978        0 3984603551 342671312        0 191751864 467456484"
    ATTR{inflight}=="       0        0"

Though that matching is more useful for non-virtual devices (e.g., you'll get a lot of output if you try it on /dev/sda1).

Share:
10,751

Related videos on Youtube

Šimon Tóth
Author by

Šimon Tóth

Backend oriented Software developer and Researcher with industry experience starting in 2004.

Updated on September 18, 2022

Comments

  • Šimon Tóth
    Šimon Tóth almost 2 years

    How can I permanently change the ownership (or at least the group) of a LVM volume?

    I figured that I have to use udev, but I don't know how the rule should look like?

    Let's say I want to change the ownership of LVM/disk to user/group virtualbox, how would I do that?

    • Admin
      Admin over 12 years
      Which OS are you talking about? Which LVM (OS or Veritas?)