Skip to content

Commit 1e7c3a5

Browse files
rpardinijacobweinstock
authored andcommitted
hook-mdev: add persistent-storage script from upstream Alpine (ipsis-literis)
- original persistent-storage from upstream Alpine: - source https://gitlab.alpinelinux.org/alpine/mdev-conf/-/blob/master/persistent-storage?ref_type=heads - revision a21d1053dd5ca37538b0435d66c58a30cb273658 - this is being added as reference for future rebases; it will be shellfmt'ed and modified in later commits Signed-off-by: Ricardo Pardini <[email protected]>
1 parent 454ff09 commit 1e7c3a5

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

images/hook-mdev/persistent-storage

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/bin/sh
2+
3+
symlink_action() {
4+
case "$ACTION" in
5+
add) ln -sf "$1" "$2";;
6+
remove) rm -f "$2";;
7+
esac
8+
}
9+
10+
sanitise_file() {
11+
sed -E -e 's/^\s+//' -e 's/\s+$//' -e 's/ /_/g' "$@" 2>/dev/null
12+
}
13+
14+
sanitise_string() {
15+
echo "$@" | sanitise_file
16+
}
17+
18+
blkid_encode_string() {
19+
# Rewrites string similar to libblk's blkid_encode_string
20+
# function which is used by udev/eudev.
21+
echo "$@" | sed -e 's| |\\x20|g'
22+
}
23+
24+
: ${SYSFS:=/sys}
25+
26+
# cdrom symlink
27+
case "$MDEV" in
28+
sr*|xvd*)
29+
caps="$(cat $SYSFS/block/$MDEV/capability 2>/dev/null)"
30+
if [ $(( 0x${caps:-0} & 8 )) -gt 0 ] || [ "$(cat $SYSFS/block/$MDEV/removable 2>/dev/null)" = "1" ]; then
31+
symlink_action $MDEV cdrom
32+
fi
33+
esac
34+
35+
36+
# /dev/block symlinks
37+
mkdir -p block
38+
if [ -f "$SYSFS/class/block/$MDEV/dev" ]; then
39+
maj_min=$(sanitise_file "$SYSFS/class/block/$MDEV/dev")
40+
symlink_action ../$MDEV block/${maj_min}
41+
fi
42+
43+
44+
# by-id symlinks
45+
mkdir -p disk/by-id
46+
47+
if [ -f "$SYSFS/class/block/$MDEV/partition" ]; then
48+
# This is a partition of a device, find out its parent device
49+
_parent_dev="$(basename $(${SBINDIR:-/usr/bin}/readlink -f "$SYSFS/class/block/$MDEV/.."))"
50+
51+
partition=$(cat $SYSFS/class/block/$MDEV/partition 2>/dev/null)
52+
case "$partition" in
53+
[0-9]*) partsuffix="-part$partition";;
54+
esac
55+
# Get name, model, serial, wwid from parent device of the partition
56+
_check_dev="$_parent_dev"
57+
else
58+
_check_dev="$MDEV"
59+
fi
60+
61+
model=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/model")
62+
name=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/name")
63+
serial=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/serial")
64+
# Special case where block devices have serials attached to the block itself, like virtio-blk
65+
: ${serial:=$(sanitise_file "$SYSFS/class/block/$_check_dev/serial")}
66+
wwid=$(sanitise_file "$SYSFS/class/block/$_check_dev/wwid")
67+
: ${wwid:=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/wwid")}
68+
69+
# Sets variables LABEL, PARTLABEL, PARTUUID, TYPE, UUID depending on
70+
# blkid output (busybox blkid will not provide PARTLABEL or PARTUUID)
71+
eval $(blkid /dev/$MDEV | cut -d: -f2-)
72+
73+
if [ -n "$wwid" ]; then
74+
case "$MDEV" in
75+
nvme*) symlink_action ../../$MDEV disk/by-id/nvme-${wwid}${partsuffix};;
76+
esac
77+
case "$wwid" in
78+
naa.*) symlink_action ../../$MDEV disk/by-id/wwn-0x${wwid#naa.}${partsuffix};;
79+
esac
80+
fi
81+
82+
if [ -n "$serial" ]; then
83+
if [ -n "$model" ]; then
84+
case "$MDEV" in
85+
nvme*) symlink_action ../../$MDEV disk/by-id/nvme-${model}_${serial}${partsuffix};;
86+
sd*) symlink_action ../../$MDEV disk/by-id/ata-${model}_${serial}${partsuffix};;
87+
esac
88+
fi
89+
if [ -n "$name" ]; then
90+
case "$MDEV" in
91+
mmcblk*) symlink_action ../../$MDEV disk/by-id/mmc-${name}_${serial}${partsuffix};;
92+
esac
93+
fi
94+
95+
# virtio-blk
96+
case "$MDEV" in
97+
vd*) symlink_action ../../$MDEV disk/by-id/virtio-${serial}${partsuffix};;
98+
esac
99+
fi
100+
101+
# by-label, by-partlabel, by-partuuid, by-uuid symlinks
102+
if [ -n "$LABEL" ]; then
103+
mkdir -p disk/by-label
104+
symlink_action ../../$MDEV disk/by-label/"$(blkid_encode_string "$LABEL")"
105+
fi
106+
if [ -n "$PARTLABEL" ]; then
107+
mkdir -p disk/by-partlabel
108+
symlink_action ../../$MDEV disk/by-partlabel/"$(blkid_encode_string "$PARTLABEL")"
109+
fi
110+
if [ -n "$PARTUUID" ]; then
111+
mkdir -p disk/by-partuuid
112+
symlink_action ../../$MDEV disk/by-partuuid/"$PARTUUID"
113+
fi
114+
if [ -n "$UUID" ]; then
115+
mkdir -p disk/by-uuid
116+
symlink_action ../../$MDEV disk/by-uuid/"$UUID"
117+
fi
118+
119+
# nvme EBS storage symlinks
120+
if [ "${MDEV#nvme}" != "$MDEV" ] && [ "$model" = "Amazon_Elastic_Block_Store" ] && command -v nvme >/dev/null; then
121+
n=30
122+
while [ $n -gt 0 ]; do
123+
ebs_alias=$(nvme id-ctrl -b /dev/$_check_dev \
124+
| dd bs=32 skip=96 count=1 2>/dev/null \
125+
| sed -nre '/^(\/dev\/)?(s|xv)d[a-z]{1,2} /p' \
126+
| tr -d ' ')
127+
if [ -n "$ebs_alias" ]; then
128+
symlink_action "$MDEV" ${ebs_alias#/dev/}$partition
129+
break
130+
fi
131+
n=$((n - 1))
132+
sleep 0.1
133+
done
134+
fi
135+
136+
# backwards compatibility with /dev/usbdisk for /dev/sd*
137+
if [ "${MDEV#sd}" != "$MDEV" ]; then
138+
sysdev=$(readlink $SYSFS/class/block/$MDEV)
139+
case "$sysdev" in
140+
*usb[0-9]*)
141+
# require vfat for devices without partition
142+
if ! [ -e $SYSFS/block/$MDEV ] || [ TYPE="vfat" ]; then
143+
symlink_action $MDEV usbdisk
144+
fi
145+
;;
146+
esac
147+
fi
148+

0 commit comments

Comments
 (0)