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.

366 lines
6.6 KiB

  1. #!/bin/sh
  2. cmd="$1"
  3. shift
  4. if [ "$cmd" = "name" ]; then
  5. echo "LVM"
  6. return 0
  7. fi
  8. command -v lvm >/dev/null || return 1
  9. . /lib/functions.sh
  10. . /lib/upgrade/common.sh
  11. . /usr/share/libubox/jshn.sh
  12. export_bootdevice
  13. [ "$BOOTDEV_MAJOR" ] || return 1
  14. export_partdevice rootdev 0
  15. [ "$rootdev" ] || return 1
  16. case "$rootdev" in
  17. mtd*|\
  18. ram*|\
  19. ubi*)
  20. return 1
  21. esac
  22. lvm_cmd() {
  23. local cmd="$1"
  24. shift
  25. LVM_SUPPRESS_FD_WARNINGS=1 lvm "$cmd" "$@"
  26. return $?
  27. }
  28. pvs() {
  29. lvm_cmd pvs --reportformat json --units b "$@"
  30. }
  31. vgs() {
  32. lvm_cmd vgs --reportformat json --units b "$@"
  33. }
  34. lvs() {
  35. lvm_cmd lvs --reportformat json --units b "$@"
  36. }
  37. freebytes() {
  38. echo $((vg_free_count * vg_extent_size))
  39. }
  40. totalbytes() {
  41. echo $((vg_extent_count * vg_extent_size))
  42. }
  43. existvol() {
  44. [ "$1" ] || return 1
  45. test -e "/dev/$vg_name/ro_$1" || test -e "/dev/$vg_name/rw_$1"
  46. return $?
  47. }
  48. vg_name=
  49. exportpv() {
  50. local reports rep pv pvs
  51. vg_name=
  52. json_init
  53. json_load "$(pvs -o vg_name -S "pv_name=~^/dev/$rootdev.*\$")"
  54. json_select report
  55. json_get_keys reports
  56. for rep in $reports; do
  57. json_select "$rep"
  58. json_select pv
  59. json_get_keys pvs
  60. for pv in $pvs; do
  61. json_select "$pv"
  62. json_get_vars vg_name
  63. json_select ..
  64. break
  65. done
  66. json_select ..
  67. break
  68. done
  69. }
  70. vg_extent_size=
  71. vg_extent_count=
  72. vg_free_count=
  73. exportvg() {
  74. local reports rep vg vgs
  75. vg_extent_size=
  76. vg_extent_count=
  77. vg_free_count=
  78. json_init
  79. json_load "$(vgs -o vg_extent_size,vg_extent_count,vg_free_count -S "vg_name=$vg_name")"
  80. json_select report
  81. json_get_keys reports
  82. for rep in $reports; do
  83. json_select "$rep"
  84. json_select vg
  85. json_get_keys vgs
  86. for vg in $vgs; do
  87. json_select "$vg"
  88. json_get_vars vg_extent_size vg_extent_count vg_free_count
  89. vg_extent_size=${vg_extent_size%B}
  90. json_select ..
  91. break
  92. done
  93. json_select ..
  94. break
  95. done
  96. }
  97. lv_active=
  98. lv_name=
  99. lv_full_name=
  100. lv_path=
  101. lv_dm_path=
  102. lv_size=
  103. exportlv() {
  104. local reports rep lv lvs
  105. lv_active=
  106. lv_name=
  107. lv_full_name=
  108. lv_path=
  109. lv_dm_path=
  110. lv_size=
  111. json_init
  112. json_load "$(lvs -o lv_active,lv_name,lv_full_name,lv_size,lv_path,lv_dm_path -S "lv_name=~^[rw][owp]_$1\$ && vg_name=$vg_name")"
  113. json_select report
  114. json_get_keys reports
  115. for rep in $reports; do
  116. json_select "$rep"
  117. json_select lv
  118. json_get_keys lvs
  119. for lv in $lvs; do
  120. json_select "$lv"
  121. json_get_vars lv_active lv_name lv_full_name lv_size lv_path lv_dm_path
  122. lv_size=${lv_size%B}
  123. json_select ..
  124. break
  125. done
  126. json_select ..
  127. break
  128. done
  129. }
  130. getdev() {
  131. existvol "$1" || return 1
  132. exportlv "$1"
  133. echo "$lv_dm_path"
  134. }
  135. getsize() {
  136. exportlv "$1"
  137. [ "$lv_size" ] && echo "$lv_size"
  138. }
  139. activatevol() {
  140. exportlv "$1"
  141. [ "$lv_path" ] || return 2
  142. case "$lv_path" in
  143. /dev/*/wo_*|\
  144. /dev/*/wp_*)
  145. return 22
  146. ;;
  147. *)
  148. [ "$lv_active" = "active" ] && return 0
  149. lvm_cmd lvchange -k n "$lv_full_name" || return $?
  150. lvm_cmd lvchange -a y "$lv_full_name" || return $?
  151. ubus send block.volume "{\"name\": \"$1\", \"action\": \"up\", \"mode\": \"${lv_name:0:2}\", \"device\": \"$lv_dm_path\"}"
  152. return 0
  153. ;;
  154. esac
  155. }
  156. disactivatevol() {
  157. exportlv "$1"
  158. [ "$lv_path" ] || return 2
  159. case "$lv_path" in
  160. /dev/*/wo_*|\
  161. /dev/*/wp_*)
  162. return 22
  163. ;;
  164. *)
  165. [ "$lv_active" = "active" ] || return 0
  166. lvm_cmd lvchange -a n "$lv_full_name" || return $?
  167. lvm_cmd lvchange -k y "$lv_full_name" || return $?
  168. ubus send block.volume "{\"name\": \"$1\", \"action\": \"down\", \"mode\": \"${lv_name:0:2}\", \"device\": \"$lv_dm_path\"}"
  169. return 0
  170. ;;
  171. esac
  172. }
  173. getstatus() {
  174. exportlv "$1"
  175. [ "$lv_full_name" ] || return 2
  176. existvol "$1" || return 1
  177. return 0
  178. }
  179. createvol() {
  180. local mode lvmode ret
  181. local volsize=$(($2))
  182. [ "$volsize" ] || return 22
  183. exportlv "$1"
  184. [ "$lv_size" ] && return 17
  185. size_ext=$((volsize / vg_extent_size))
  186. [ $((size_ext * vg_extent_size)) -lt $volsize ] && size_ext=$((size_ext + 1))
  187. case "$3" in
  188. ro|wo)
  189. lvmode=r
  190. mode=wo
  191. ;;
  192. rw)
  193. lvmode=rw
  194. mode=wp
  195. ;;
  196. *)
  197. return 22
  198. ;;
  199. esac
  200. lvm_cmd lvcreate -p "$lvmode" -a n -y -W n -Z n -n "${mode}_$1" -l "$size_ext" "$vg_name"
  201. ret=$?
  202. if [ ! $ret -eq 0 ] || [ "$lvmode" = "r" ]; then
  203. return $ret
  204. fi
  205. exportlv "$1"
  206. [ "$lv_full_name" ] || return 22
  207. lvm_cmd lvchange -a y "$lv_full_name" || return 1
  208. if [ "$lv_size" -gt $(( 100 * 1024 * 1024 )) ]; then
  209. mkfs.f2fs -f -l "$1" "$lv_path"
  210. ret=$?
  211. [ $ret != 0 ] && [ $ret != 134 ] && return 1
  212. else
  213. mke2fs -F -L "$1" "$lv_path" || return 1
  214. fi
  215. lvm_cmd lvrename "$vg_name" "wp_$1" "rw_$1"
  216. exportlv "$1"
  217. return 0
  218. }
  219. removevol() {
  220. exportlv "$1"
  221. [ "$lv_full_name" ] || return 2
  222. lvm_cmd lvremove -y "$lv_full_name"
  223. }
  224. updatevol() {
  225. exportlv "$1"
  226. [ "$lv_full_name" ] || return 2
  227. [ "$lv_size" -ge "$2" ] || return 27
  228. case "$lv_path" in
  229. /dev/*/wo_*)
  230. lvm_cmd lvchange -p rw "$lv_full_name"
  231. lvm_cmd lvchange -a y "$lv_full_name"
  232. dd of="$lv_path"
  233. lvm_cmd lvchange -a n "$lv_full_name"
  234. lvm_cmd lvchange -p r "$lv_full_name"
  235. lvm_cmd lvrename "$lv_full_name" "${lv_full_name%%/*}/ro_$1"
  236. return 0
  237. ;;
  238. default)
  239. return 22
  240. ;;
  241. esac
  242. }
  243. listvols() {
  244. local reports rep lv lvs lv_name lv_size lv_mode volname
  245. volname=${1:-.*}
  246. json_init
  247. json_load "$(lvs -o lv_name,lv_size -S "lv_name=~^[rw][owp]_$volname\$ && vg_name=$vg_name")"
  248. json_select report
  249. json_get_keys reports
  250. for rep in $reports; do
  251. json_select "$rep"
  252. json_select lv
  253. json_get_keys lvs
  254. for lv in $lvs; do
  255. json_select "$lv"
  256. json_get_vars lv_name lv_size
  257. lv_mode="${lv_name:0:2}"
  258. lv_name="${lv_name:3}"
  259. lv_size=${lv_size%B}
  260. echo "$lv_name $lv_mode $lv_size"
  261. json_select ..
  262. done
  263. json_select ..
  264. break
  265. done
  266. }
  267. boot() {
  268. local reports rep lv lvs lv_name lv_dm_path lv_mode volname
  269. json_init
  270. json_load "$(lvs -o lv_name,lv_dm_path -S "lv_name=~^[rw][ow]_.*\$ && vg_name=$vg_name && lv_active=active")"
  271. json_select report
  272. json_get_keys reports
  273. for rep in $reports; do
  274. json_select "$rep"
  275. json_select lv
  276. json_get_keys lvs
  277. for lv in $lvs; do
  278. json_select "$lv"
  279. json_get_vars lv_name lv_dm_path
  280. lv_mode="${lv_name:0:2}"
  281. lv_name="${lv_name:3}"
  282. ubus send block.volume "{\"name\": \"$lv_name\", \"action\": \"up\", \"mode\": \"$lv_mode\", \"device\": \"$lv_dm_path\"}"
  283. json_select ..
  284. done
  285. json_select ..
  286. break
  287. done
  288. }
  289. exportpv
  290. exportvg
  291. case "$cmd" in
  292. align)
  293. echo "$vg_extent_size"
  294. ;;
  295. free)
  296. freebytes
  297. ;;
  298. total)
  299. totalbytes
  300. ;;
  301. boot)
  302. boot
  303. ;;
  304. list)
  305. listvols "$@"
  306. ;;
  307. create)
  308. createvol "$@"
  309. ;;
  310. remove)
  311. removevol "$@"
  312. ;;
  313. device)
  314. getdev "$@"
  315. ;;
  316. size)
  317. getsize "$@"
  318. ;;
  319. up)
  320. activatevol "$@"
  321. ;;
  322. down)
  323. disactivatevol "$@"
  324. ;;
  325. status)
  326. getstatus "$@"
  327. ;;
  328. write)
  329. updatevol "$@"
  330. ;;
  331. *)
  332. echo "unknown command"
  333. return 1
  334. ;;
  335. esac