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.

77 lines
1.4 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/upgrade/common.sh
  4. OWRT_VOLUMES=owrt-volumes
  5. part_fixup() {
  6. echo "write" | sfdisk --force -q -w never $1
  7. }
  8. get_free_area() {
  9. local found=
  10. sfdisk -q -F "$1" 2>/dev/null | while read start end sectors size; do
  11. case $start in
  12. *"Unpartitioned"* | *"Units:"* | *"Sector"* | *"Start"* )
  13. continue
  14. ;;
  15. [0-9]*)
  16. case "$size" in
  17. *"M")
  18. [ "${size%%M}" -lt 100 ] && continue
  19. ;;
  20. *"G" | *"T")
  21. ;;
  22. *"k" | *"b")
  23. continue
  24. ;;
  25. esac
  26. [ "$found" ] || echo "start=$start, size=$((end - start))"
  27. found=1
  28. ;;
  29. esac
  30. done
  31. }
  32. create_lvm_part() {
  33. local disk=$1
  34. local freepart
  35. freepart="$(get_free_area $disk)"
  36. if [ "$freepart" ]; then
  37. echo "$freepart, type=lvm, name=$OWRT_VOLUMES" | sfdisk --force -w never -a $disk
  38. partx -a $disk 1>/dev/null 2>/dev/null || true
  39. return 0
  40. else
  41. return 1
  42. fi
  43. }
  44. lvm_init() {
  45. lvm pvcreate -f $1
  46. lvm vgcreate "$2" $1
  47. lvm vgs
  48. }
  49. autopart_init() {
  50. local diskdev
  51. local lvmpart
  52. local diskserial
  53. export_bootdevice && export_partdevice diskdev 0
  54. [ "$diskdev" ] || return
  55. [ -e "/sys/class/block/$diskdev/device/serial" ] && diskserial=$(cat /sys/class/block/$diskdev/device/serial)
  56. part_fixup /dev/$diskdev
  57. create_lvm_part /dev/$diskdev || return
  58. lvmpart=$(get_partition_by_name $diskdev $OWRT_VOLUMES)
  59. [ "$lvmpart" ] || return
  60. lvm_init /dev/$lvmpart "${OWRT_VOLUMES}${diskserial:+-${diskserial:2}}"
  61. }
  62. autopart_init
  63. exit 0