I'd like to write a script that backups data on a USB device every time the latter is getting plugged in a random USB slot. Now I know about the KERNEL=sdX
identifier but the problem is that depending on the slot and on whether other devices are already plugged in the sdX
will change. What would be the best way to identify my USB device in udev ? I've been thinking about using partuuid
but I don't know how and I don't know where the partuuid is stored in the first place or how it is worked out.
2 Answers
If you partition your device using gdisk
as opposed to fdisk
you will create a GPT-style partition table which, for each partition, can hold a PARTUUID and a PARTLABEL. The c
command in gdisk
allows you to set the partition name, ie the PARTLABEL.
These partition ids do not change when you reformat the partition by creating a new filesystem on it.
Most filesystems allow you to get or set a UUID and LABEL on the filesystem. The means to do so depends on the filesystem type, eg for ext3/4 it is tune2fs
with -U
and -L
. These appear as UUID and LABEL in Linux.
Do not confuse these 2 sets of ids. blkid
will show all of them:
/dev/sdb1: LABEL="MYLABEL" UUID="A369-D716" TYPE="vfat" PARTLABEL="My new partition" \
PARTUUID="f0817245-d3ce-408b-9457-e33dfc04aebe"
You can see the udev attributes with
$ udevadm info -p $(udevadm info -q path -n /dev/sdb1)
...
E: ID_FS_LABEL=MYLABEL
E: ID_FS_LABEL_ENC=MYLABEL
E: ID_FS_UUID=A369-D716
E: ID_FS_UUID_ENC=A369-D716
E: ID_PART_ENTRY_UUID=f0817245-d3ce-408b-9457-e33dfc04aebe
E: ID_PART_TABLE_UUID=7301d696-b66d-4f85-8272-7dd5e29175ae
E: ID_PART_ENTRY_NAME=My\x20new\x20partition
-
the label name is stored somewhere in the GUID table ? Commented Jul 31, 2016 at 14:54
-
-
How do I access the label attribute in udev, I can't find the label attribute Commented Jul 31, 2016 at 15:24
-
-
yeah I saw that but can you actually use ATTRS{ID_FS_LABEL_ENC} I'm not sure those are attributes usable in a udev rule. Gonna try anwyay Commented Jul 31, 2016 at 16:01
I've done something similar, using the UUID (Universally Unique Identifier library) of the USB device.
You can see UUIDs with blkid command:
# blkid
You can find more information about blkid, looking into it's man page
# man blkid