refactor storage-device

This commit is contained in:
illiliti 2020-02-13 22:49:51 +03:00
parent 640ff670cf
commit 34f5adb14c
2 changed files with 76 additions and 70 deletions

View File

@ -43,5 +43,5 @@ Exceptions:
The "mdev.conf" configuration file is modified version from "mdev-like-a-boss" project, Copyright (c) 2012-2020, Piotr Karbowski <piotr.karbowski@gmail.com>. The "mdev.conf" configuration file is modified version from "mdev-like-a-boss" project, Copyright (c) 2012-2020, Piotr Karbowski <piotr.karbowski@gmail.com>.
Please consult the license notice in the file for terms and conditions. Please consult the license notice in the file for terms and conditions.
The "storage-device" script from "mdev-like-a-boss" project, Copyright (c) 2012-2020, Piotr Karbowski <piotr.karbowski@gmail.com>. The "storage-device" script is modified version from "mdev-like-a-boss" project, Copyright (c) 2012-2020, Piotr Karbowski <piotr.karbowski@gmail.com>.
Please consult the license notice in the file for terms and conditions. Please consult the license notice in the file for terms and conditions.

View File

@ -38,26 +38,27 @@
umask 077 umask 077
storage_dir="/dev/.mdev-like-a-boss" # TODO use /tmp
[ -d "${storage_dir}" ] || mkdir "${storage_dir}" storage_dir="/tmp/.mdev-like-a-boss"
[ -d "$storage_dir" ] || mkdir "$storage_dir"
[ "${MDEV}" ] || exit 2 [ "$MDEV" ] || exit 2
create_uuid_label_symlink() { create_uuid_label_symlink() {
local target_dir="/dev/disk/by-${1}" target_dir="/dev/disk/by-${1}"
local target_symlink="${target_dir}/${2}" target_symlink="${target_dir}/${2}"
[ -e "${target_symlink}" ] && return [ -e "$target_symlink" ] && return
mkdir -p "${target_dir}" mkdir -p "$target_dir"
ln -snf "/dev/${MDEV}" "${target_symlink}" ln -sf "/dev/${MDEV}" "$target_symlink"
echo "${target_symlink}" >"${storage_dir}/storage_symlink_${1}_${MDEV}" printf "%s\n" "$target_symlink" > "${storage_dir}/storage_symlink_${1}_${MDEV}"
} }
add_symlinks() { add_symlinks() {
# Skip temp cryptsetup nodes. # Skip temp cryptsetup nodes.
case "${MDEV}" in case "$MDEV" in
'dm-'[0-9]*) "dm-"[0-9]*)
case "$(cat "/sys/block/${MDEV}/dm/name")" in case "$(cat "/sys/block/${MDEV}/dm/name")" in
'temporary-cryptsetup-'[0-9]*) "temporary-cryptsetup-"[0-9]*)
return 0 return 0
;; ;;
esac esac
@ -65,26 +66,31 @@ add_symlinks() {
esac esac
if command -v blkid > /dev/null 2>&1; then if command -v blkid > /dev/null 2>&1; then
local field name value UUID LABEL TYPE PTTYPE PARTUUID blkid_output=$(blkid "/dev/${MDEV}")
local blkid_output="$(blkid "/dev/${MDEV}")"
eval "${blkid_output#*: }" eval "${blkid_output#*: }"
[ "${UUID}" ] && create_uuid_label_symlink 'uuid' "${UUID}" [ "$UUID" ] && create_uuid_label_symlink uuid "$UUID"
[ "${LABEL}" ] && create_uuid_label_symlink 'label' "${LABEL}" [ "$LABEL" ] && create_uuid_label_symlink label "$LABEL"
[ "${PARTUUID}" ] && create_uuid_label_symlink 'partuuid' "${PARTUUID}" [ "$PARTUUID" ] && create_uuid_label_symlink partuuid "$PARTUUID"
fi fi
if [ -f "/sys/block/${MDEV}/dm/name" ]; then if [ -f "/sys/block/${MDEV}/dm/name" ]; then
[ -d '/dev/mapper' ] || mkdir '/dev/mapper' [ -d /dev/mapper ] || mkdir /dev/mapper
if ! [ -c '/dev/mapper/control' ]; then if ! [ -c /dev/mapper/control ]; then
awk '$2 == "device-mapper" { foo = system("mknod /dev/mapper/control c 10 " $1); exit foo }' /proc/misc || exit 1 #awk '$2 == "device-mapper" { foo = system("mknod /dev/mapper/control c 10 " $1); exit foo }' /proc/misc || exit 1
while read -r dm; do
[ "${dm#* }" = device-mapper ] && {
mknod /dev/mapper/control c 10 "${dm% *}" || exit 1
}
done < /proc/misc
fi fi
local dmname="$(cat "/sys/block/${MDEV}/dm/name")"
if [ "${dmname}" ]; then dmname=$(cat "/sys/block/${MDEV}/dm/name")
local target_symlink="/dev/mapper/${dmname}" if [ "$dmname" ]; then
[ -e "${target_symlink}" ] && return target_symlink="/dev/mapper/${dmname}"
ln -snf "/dev/${MDEV}" "${target_symlink}" [ -e "$target_symlink" ] && return
echo "${target_symlink}" >"${storage_dir}/storage_symlink_mapper_${MDEV}" ln -sf "/dev/${MDEV}" "$target_symlink"
printf "%s\n" "$target_symlink" > "${storage_dir}/storage_symlink_mapper_${MDEV}"
fi fi
fi fi
} }
@ -92,37 +98,37 @@ add_symlinks() {
set_readahead() { set_readahead() {
read_ahead_kb_control="/sys/class/block/${MDEV}/queue/read_ahead_kb" read_ahead_kb_control="/sys/class/block/${MDEV}/queue/read_ahead_kb"
new_read_ahead_kb="2048" new_read_ahead_kb="2048"
if [ -f "${read_ahead_kb_control}" ]; then
read_ahead_kb="$(cat "${read_ahead_kb_control}")" if [ -f "$read_ahead_kb_control" ]; then
if [ "${read_ahead_kb}" -lt "${new_read_ahead_kb}" ]; then read_ahead_kb=$(cat "$read_ahead_kb_control")
logger -t mdev "Changing ${MDEV} read_ahead_kb from ${read_ahead_kb} to ${new_read_ahead_kb}" if [ "$read_ahead_kb" -lt "$new_read_ahead_kb" ]; then
echo -n "${new_read_ahead_kb}" >"${read_ahead_kb_control}" logger -t mdev "Changing $MDEV read_ahead_kb from $read_ahead_kb to $new_read_ahead_kb"
printf "%s" "$new_read_ahead_kb" > "$read_ahead_kb_control"
fi fi
fi fi
} }
drop_symlinks() { drop_symlinks() {
local type
for type in uuid label mapper; do for type in uuid label mapper; do
[ -f "${storage_dir}/storage_symlink_${type}_${MDEV}" ] || continue [ -f "${storage_dir}/storage_symlink_${type}_${MDEV}" ] || continue
local target_symlink="$(cat "${storage_dir}/storage_symlink_${type}_${MDEV}" 2>/dev/null)" target_symlink=$(cat "${storage_dir}/storage_symlink_${type}_${MDEV}" 2> /dev/null)
[ "${target_symlink}" ] || continue [ "$target_symlink" ] || continue
local target_symlink_device="$(readlink "${target_symlink}")" target_symlink_device=$(readlink "$target_symlink")
if [ "${target_symlink_device}" = "/dev/${MDEV}" ]; then if [ "$target_symlink_device" = "/dev/${MDEV}" ]; then
rm "${target_symlink}" rm "$target_symlink"
fi fi
rm "${storage_dir}/storage_symlink_${type}_${MDEV}" rm "${storage_dir}/storage_symlink_${type}_${MDEV}"
done done
} }
case "${ACTION}" in case "$ACTION" in
'add'|'') add | "")
add_symlinks add_symlinks
set_readahead set_readahead
;; ;;
'remove') remove)
drop_symlinks drop_symlinks
;; ;;
esac esac