1#!/bin/sh2# Copyright (c) 2012-2019, Piotr Karbowski <piotr.karbowski@gmail.com>3# All rights reserved.4#5# Redistribution and use in source and binary forms, with or without modification, are6# permitted provided that the following conditions are met:7#8# * Redistributions of source code must retain the above copyright notice, this list9# of conditions and the following disclaimer.10# * Redistributions in binary form must reproduce the above copyright notice, this list11# of conditions and the following disclaimer in the documentation and/or other12# materials provided with the distribution.13# * Neither the name of the Piotr Karbowski nor the names of its contributors may be14# used to endorse or promote products derived from this software without specific15# prior written permission.16#17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY18# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF19# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL20# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT22# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2627# This script meant to create /dev/disk/by-* and /dev/mapper/* symlinks.28# and remove them after storage device is removed.29# the /dev/disk/by-* handling based on the idea and proof of concept from BitJam.3031# debug32#exec >> /run/debug-mdev 2>&133#set -x34#echo '### ENV:'35#env36#echo '### CODE:'37#3839umask 0774041storage_dir="/dev/.mdev-like-a-boss"42[ -d "${storage_dir}" ] || mkdir "${storage_dir}"4344[ "${MDEV}" ] || exit 24546create_uuid_label_symlink() {47 local target_dir="/dev/disk/by-${1}"48 local target_symlink="${target_dir}/${2}"49 [ -e "${target_symlink}" ] && return50 mkdir -p "${target_dir}"51 ln -snf "/dev/${MDEV}" "${target_symlink}"52 echo "${target_symlink}" >"${storage_dir}/storage_symlink_${1}_${MDEV}"53}5455add_symlinks() {56 # Skip temp cryptsetup nodes.57 case "${MDEV}" in58 'dm-'[0-9]*)59 case "$(cat "/sys/block/${MDEV}/dm/name")" in60 'temporary-cryptsetup-'[0-9]*)61 return 062 ;;63 esac64 ;;65 esac6667 if command -v blkid >/dev/null 2>&1; then68 local field name value UUID LABEL TYPE PTTYPE PARTUUID69 local blkid_output="$(blkid "/dev/${MDEV}")"70 eval "${blkid_output#*: }"7172 [ "${UUID}" ] && create_uuid_label_symlink 'uuid' "${UUID}"73 [ "${LABEL}" ] && create_uuid_label_symlink 'label' "${LABEL}"74 [ "${PARTUUID}" ] && create_uuid_label_symlink 'partuuid' "${PARTUUID}"75 fi7677 if [ -f "/sys/block/${MDEV}/dm/name" ]; then78 [ -d '/dev/mapper' ] || mkdir '/dev/mapper'79 if ! [ -c '/dev/mapper/control' ]; then80 awk '$2 == "device-mapper" { foo = system("mknod /dev/mapper/control c 10 " $1); exit foo }' /proc/misc || exit 181 fi82 local dmname="$(cat "/sys/block/${MDEV}/dm/name")"83 if [ "${dmname}" ]; then84 local target_symlink="/dev/mapper/${dmname}"85 [ -e "${target_symlink}" ] && return86 ln -snf "/dev/${MDEV}" "${target_symlink}"87 echo "${target_symlink}" >"${storage_dir}/storage_symlink_mapper_${MDEV}"88 fi89 fi90}9192set_readahead() {93 read_ahead_kb_control="/sys/class/block/${MDEV}/queue/read_ahead_kb"94 new_read_ahead_kb="2048"95 if [ -f "${read_ahead_kb_control}" ]; then96 read_ahead_kb="$(cat "${read_ahead_kb_control}")"97 if [ "${read_ahead_kb}" -lt "${new_read_ahead_kb}" ]; then98 logger -t mdev "Changing ${MDEV} read_ahead_kb from ${read_ahead_kb} to ${new_read_ahead_kb}"99 echo -n "${new_read_ahead_kb}" >"${read_ahead_kb_control}"100 fi101 fi102}103104# media change detection105set_eventpoll() {106 [ -f /sys/block/$MDEV/events ] || return 0107 case "$(cat /sys/block/$MDEV/events)" in108 'media_change')109 echo 2000 > /sys/block/$MDEV/events_poll_msecs110 ;;111 esac112}113114drop_symlinks() {115 local type116 for type in uuid label mapper; do117 [ -f "${storage_dir}/storage_symlink_${type}_${MDEV}" ] || continue118 local target_symlink="$(cat "${storage_dir}/storage_symlink_${type}_${MDEV}" 2>/dev/null)"119 [ "${target_symlink}" ] || continue120121 local target_symlink_device="$(readlink "${target_symlink}")"122 if [ "${target_symlink_device}" = "/dev/${MDEV}" ]; then123 rm "${target_symlink}"124 fi125 rm "${storage_dir}/storage_symlink_${type}_${MDEV}"126 done127}128129case "${ACTION}" in130 'add'|'')131 add_symlinks132 set_readahead133 set_eventpoll134 ;;135 'remove')136 drop_symlinks137 ;;138esac139