You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
2.9 KiB

  1. #!/bin/sh
  2. command -v lvm || return 1
  3. . /lib/functions.sh
  4. . /lib/upgrade/common.sh
  5. export_bootdevice
  6. [ "$BOOTDEV_MAJOR" ] || return 1
  7. export_partdevice rootdev 0
  8. [ "$rootdev" ] || return 1
  9. LVM_SUPPRESS_FD_WARNINGS=1
  10. case "$rootdev" in
  11. mtd*|\
  12. ram*|\
  13. ubi*)
  14. return 1
  15. esac
  16. lvs() {
  17. local cmd="$1"
  18. local cb="$2"
  19. local param="${3:+-S vg_name=${vgname} -S lv_name=~^r[ow]_$3\$}"
  20. local oIFS="$IFS"
  21. IFS=" "
  22. set -- $(LVM_SUPPRESS_FD_WARNINGS=1 $cmd -c $param)
  23. [ "$1" ] || {
  24. IFS="$oIFS"
  25. return 1
  26. }
  27. IFS=":"
  28. set -- $1
  29. IFS="$oIFS"
  30. $cb "$@"
  31. }
  32. pvvars() {
  33. case "${1:5}" in
  34. "$rootdev"*)
  35. partdev="$1"
  36. vgname="$2"
  37. ;;
  38. esac
  39. }
  40. vgvars() {
  41. [ "$1" = "$vgname" ] || return
  42. vgbs="${13}"
  43. vgts="${14}"
  44. vgus="${15}"
  45. vgfs="${16}"
  46. }
  47. lvvars() {
  48. lvpath="$1"
  49. lvsize=$(( 512 * $7 ))
  50. }
  51. freebytes() {
  52. echo $((vgfs * vgbs * 1024))
  53. }
  54. totalbytes() {
  55. echo $((vgts * vgbs * 1024))
  56. }
  57. existvol() {
  58. [ "$1" ] || return 1
  59. test -e "/dev/$vgname/ro_$1" || test -e "/dev/$vgname/rw_$1"
  60. return $?
  61. }
  62. getlvname() {
  63. lvs lvdisplay lvvars "$1"
  64. [ "$lvpath" ] && echo ${lvpath:5}
  65. }
  66. getdev() {
  67. existvol "$1" || return 1
  68. readlink /dev/$(getlvname "$1")
  69. }
  70. getsize() {
  71. lvs lvdisplay lvvars "$1"
  72. [ "$lvsize" ] && echo $lvsize
  73. }
  74. activatevol() {
  75. LVM_SUPPRESS_FD_WARNINGS=1 lvchange -a y "$(getlvname "$1")"
  76. }
  77. disactivatevol() {
  78. existvol "$1" || return 1
  79. LVM_SUPPRESS_FD_WARNINGS=1 lvchange -a n "$(getlvname "$1")"
  80. }
  81. getstatus() {
  82. lvs lvdisplay lvvars "$1"
  83. [ "$lvsize" ] || return 2
  84. existvol "$1" || return 1
  85. return 0
  86. }
  87. createvol() {
  88. local mode ret lvname
  89. case "$3" in
  90. ro)
  91. mode=r
  92. ;;
  93. rw)
  94. mode=rw
  95. ;;
  96. *)
  97. return 22
  98. ;;
  99. esac
  100. LVM_SUPPRESS_FD_WARNINGS=1 lvcreate -p $mode -a n -y -W n -Z n -n "${3}_${1}" -L "$2" $vgname
  101. ret=$?
  102. if [ ! $ret -eq 0 ] || [ "$mode" = "r" ]; then
  103. return $ret
  104. fi
  105. lvs lvdisplay lvvars "$1"
  106. [ "$lvpath" ] || return 22
  107. lvname=${lvpath:5}
  108. LVM_SUPPRESS_FD_WARNINGS=1 lvchange -a y /dev/$lvname || return 1
  109. if [ $lvsize -gt $(( 100 * 1024 * 1024 )) ]; then
  110. mkfs.f2fs -f -l "$1" $lvpath || return 1
  111. else
  112. mke2fs -F -L "$1" $lvpath || return 1
  113. fi
  114. return 0
  115. }
  116. removevol() {
  117. local lvname="$(getlvname "$1")"
  118. [ "$lvname" ] || return 2
  119. LVM_SUPPRESS_FD_WARNINGS=1 lvremove -y "$(getlvname "$1")"
  120. }
  121. updatevol() {
  122. lvs lvdisplay lvvars "$1"
  123. [ "$lvpath" ] || return 2
  124. [ $lvsize -ge $2 ] || return 27
  125. LVM_SUPPRESS_FD_WARNINGS=1 lvchange -a y -p rw ${lvpath:5}
  126. dd of=$lvpath
  127. case "$lvpath" in
  128. /dev/*/ro_*)
  129. LVM_SUPPRESS_FD_WARNINGS=1 lvchange -p r ${lvpath:5}
  130. ;;
  131. esac
  132. }
  133. lvs pvdisplay pvvars
  134. lvs vgdisplay vgvars
  135. cmd="$1"
  136. shift
  137. case "$cmd" in
  138. free)
  139. freebytes
  140. ;;
  141. total)
  142. totalbytes
  143. ;;
  144. create)
  145. createvol "$@"
  146. ;;
  147. remove)
  148. removevol "$@"
  149. ;;
  150. device)
  151. getdev "$@"
  152. ;;
  153. size)
  154. getsize "$@"
  155. ;;
  156. up)
  157. activatevol "$@"
  158. ;;
  159. down)
  160. disactivatevol "$@"
  161. ;;
  162. status)
  163. getstatus "$@"
  164. ;;
  165. write)
  166. updatevol "$@"
  167. ;;
  168. *)
  169. echo "unknown command"
  170. return 1
  171. ;;
  172. esac