Advertisement
Guest User

Untitled

a guest
Aug 8th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. do_expand_rootfs() {
  6. ROOT_PART=$(cat /proc/cmdline | tr " " "\n" | grep "root=" | cut -f 2 -d '='| cut -f 3 -d '/')
  7. ROOT_DEV="/dev/$(dirname $(readlink /sys/class/block/${ROOT_PART}) | grep -o '[^/]*$')"
  8. BASE_DEV=${ROOT_DEV//p*}
  9. PART_NUM=${ROOT_PART#*p}
  10.  
  11. LAST_PART_NUM=$(parted ${BASE_DEV} -ms unit s p | tail -n 1 | cut -f 1 -d:)
  12. if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
  13. echo "/dev/disk/by-label/rootfs is not the last partition. Don't know how to expand"
  14. return 4
  15. fi
  16.  
  17. # Get the starting offset of the root partition
  18. PART_START=$(parted ${BASE_DEV} -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d:)
  19. # Remove suffix:
  20. PART_START=${PART_START%s}
  21. echo "resizing root partition, starts at offset: $PART_START"
  22. [ "$PART_START" ] || return 5
  23.  
  24. # Return value will likely be error for fdisk as it fails to reload the
  25. # partition table because the root fs is mounted
  26. fdisk ${BASE_DEV} <<EOF > /dev/null 2>&1
  27. p
  28. d
  29. $PART_NUM
  30. n
  31. p
  32. $PART_NUM
  33. $PART_START
  34.  
  35. p
  36. w
  37. EOF
  38.  
  39. sync
  40. echo "Probing the partition table... "
  41. partprobe
  42.  
  43. echo "Resizing the partition... "
  44. resize2fs ${ROOT_DEV}p${PART_NUM} || return $?
  45. }
  46.  
  47. case "$1" in
  48. start)
  49. reboot=false
  50.  
  51. echo "Expanding rootfs..."
  52.  
  53. if do_expand_rootfs ;
  54. then
  55. echo "Expanding partition success"
  56. else
  57. echo "Expanding rootfs has failed, see log files. Error code: $?"
  58. fi
  59.  
  60. update-rc.d -f resize-rootfs remove >/dev/null 2>&1
  61.  
  62. ;;
  63.  
  64. *)
  65. echo "Usage: $0 {start}" >&2
  66. exit 1
  67. ;;
  68. esac
  69.  
  70. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement