device-mapper.rc-2.02.105-r2 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/sbin/openrc-run
  2. # Copyright 1999-2016 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. depend() {
  5. # As of .67-r1, we call ALL lvm start/stop scripts with --sysinit, that
  6. # means dmeventd is NOT notified, as it cannot be safely running
  7. before dmeventd checkfs fsck
  8. after modules
  9. }
  10. dm_in_proc() {
  11. local retval=0
  12. for x in devices misc ; do
  13. grep -qs 'device-mapper' /proc/${x}
  14. retval=$((${retval} + $?))
  15. done
  16. return ${retval}
  17. }
  18. # char **build_dmsetup_command(volume)
  19. #
  20. # Returns complete dmsetup command given single volume name
  21. build_dmsetup_command() {
  22. local count dmsetup_cmd
  23. # Number of lines mentioning volume name
  24. count=$(grep -v -e '^[[:space:]]*\(#\|$\)' /etc/dmtab | grep -c ${1})
  25. # If there's just one line:
  26. if [ ${count} -eq 1 ] ; then
  27. echo "echo $(grep -v -e '^[[:space:]]*\(#\|$\)' /etc/dmtab | \
  28. grep ${1} | awk '{$1=""; print $0}') | /sbin/dmsetup create ${1}"
  29. # For all cases with more lines:
  30. elif [ ${count} -gt 1 ] ; then
  31. for c in $( seq 1 ${count} ) ; do
  32. if [ ${c} -eq 1 ] ; then
  33. # Heavy escaping in awk-statement because we cannot use apostrophes
  34. dmsetup_cmd="echo -e $(grep -v -e '^[[:space:]]*\(#\|$\)' /etc/dmtab | \
  35. grep ${1} | awk NR==${c}\ \{\$1=\"\"\;\ print\ \$0\})"
  36. else
  37. # Append starting with newline
  38. dmsetup_cmd="${dmsetup_cmd}\\\\n \
  39. $(grep -v -e '^[[:space:]]*\(#\|$\)' /etc/dmtab | \
  40. grep ${1} | awk NR==${c}\ \{\$1=\"\"\;\ print\ \$0\})"
  41. fi
  42. done
  43. echo "${dmsetup_cmd} | /sbin/dmsetup create ${1}"
  44. fi
  45. return 0
  46. }
  47. # char **get_new_dm_volumes(void)
  48. #
  49. # Return unique volumes from /etc/dmtab
  50. get_new_dm_volumes() {
  51. local volume
  52. # Filter comments and blank lines
  53. grep -v -e '^[[:space:]]*\(#\|$\)' /etc/dmtab | \
  54. awk '{ print $1 }' | \
  55. uniq | \
  56. while read volume ; do
  57. # If it exists, skip it
  58. dmvolume_exists "${volume%:}" && continue
  59. echo "${volume%:}"
  60. done
  61. return 0
  62. }
  63. # int dmvolume_exists(volume)
  64. #
  65. # Return true if volume exists in DM table
  66. dmvolume_exists() {
  67. local x line volume=$1
  68. [ -z "${volume}" ] && return 1
  69. /sbin/dmsetup ls 2>/dev/null | \
  70. while read line ; do
  71. for x in ${line} ; do
  72. # the following conditonal return only breaks out
  73. # of the while loop, as it is running in a pipe.
  74. [ "${x}" = "${volume}" ] && return 1
  75. # We only want to check the volume name
  76. break
  77. done
  78. done
  79. # if 1 was returned from the above loop, then indicate that
  80. # volume exists
  81. [ $? = 1 ] && return 0
  82. # otherwise the loop exited normally and the volume does not
  83. # exist
  84. return 1
  85. }
  86. # int is_empty_dm_volume(volume)
  87. #
  88. # Return true if the volume exists in DM table, but is empty/non-valid
  89. is_empty_dm_volume() {
  90. local table volume=$1
  91. set -- $(/sbin/dmsetup table 2>/dev/null | grep -e "^${volume}:")
  92. [ "${volume}" = "$1" -a -z "$2" ]
  93. }
  94. start() {
  95. if [ -e /proc/modules ] && ! dm_in_proc ; then
  96. modprobe dm-mod 2>/dev/null
  97. fi
  98. # Ensure the dirs exist for locking and running
  99. checkpath -q -d -m 0700 -o root:root /run/lvm /run/lock/lvm
  100. local x volume
  101. if [ -x /sbin/dmsetup -a -c /dev/mapper/control -a -f /etc/dmtab ] ; then
  102. [ -n "$(get_new_dm_volumes)" ] && \
  103. einfo " Setting up device-mapper volumes:"
  104. get_new_dm_volumes | \
  105. while read x ; do
  106. [ -n "${x}" ] || continue
  107. volume="${x##* }"
  108. ebegin " Creating volume: ${volume}"
  109. if ! eval $(build_dmsetup_command ${volume}) >/dev/null 2>/dev/null ; then
  110. eend 1 " Error creating volume: ${volume}"
  111. # dmsetup still adds an empty volume in some cases,
  112. # so lets remove it
  113. is_empty_dm_volume "${volume}" && \
  114. /sbin/dmsetup remove "${volume}" 2>/dev/null
  115. else
  116. eend 0
  117. fi
  118. done
  119. fi
  120. }