30s2disk-check 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. #
  3. # Stefan Seyfried, SUSE Linux Products GmbH, 2006
  4. # mostly taken from the powersave project
  5. . "${PM_FUNCTIONS}"
  6. # sanity check the environment if resume will be possible after hibernate
  7. checkhibernate()
  8. {
  9. echo "INFO: checking for suspend-to-disk prerequisites..."
  10. if [ -z "$IMAGE_SIZE" ]; then
  11. IMAGE_SIZE="`awk '/^MemTotal:/ { print int($2*1024*.45) }' /proc/meminfo`"
  12. fi
  13. # Default to partition style hibernation
  14. SWAP_TYPE=partition
  15. read CMDLINE < /proc/cmdline
  16. for CMD in $CMDLINE; do
  17. case $CMD in
  18. resume=*)
  19. RESUME=${CMD#*=}
  20. ;;
  21. resume_offset=*)
  22. # Swap type is file as resume_offset is given
  23. SWAP_TYPE=file
  24. ;;
  25. esac
  26. done
  27. if [ -z "$RESUME" ]; then
  28. # No resume= given in /proc/cmdline, failing.
  29. echo "ERROR: no resume parameter on kernel commandline, can not suspend"
  30. echo "no resume parameter on kernel commandline" >> $INHIBIT
  31. return 1
  32. fi
  33. if [ "$SWAP_TYPE" = file ] && [ "$SLEEP_MODULE" = "uswsusp" ]; then
  34. # File-style hibernating is not supported by uswsusp
  35. echo "ERROR: You must use the kernel hibernate method or tuxonice when suspending to swapfile, not uswsusp"
  36. echo "suspend to swapfile with uswsusp method not kernel" >> $INHIBIT
  37. return 1
  38. fi
  39. # the resume device can be a symlink, e.g. with LVM/EVMS or with
  40. # /dev/disk/by-id/foo
  41. RESUME=$(readlink -f $RESUME)
  42. OK=
  43. while read DEV TYPE SIZE USED PRI; do
  44. if [ "$TYPE" = "file" ] && [ "$SWAP_TYPE" = "file" ] && [ "$RESUME" = "$(df "$DEV" | tail -n +2 | cut -d ' ' -f 1)" ]; then
  45. OK=1
  46. break
  47. fi
  48. [ "$TYPE" != "partition" ] && continue
  49. [ "$DEV" != "$RESUME" ] && continue
  50. FREE=$[($SIZE-$USED)*1024] # get free space on DEV
  51. if [ $FREE -lt $IMAGE_SIZE ]; then
  52. IMAGE_SIZE=$[$FREE-10*1024*1024]
  53. fi
  54. OK=1
  55. break # we found the partition, no need to look further
  56. done < /proc/swaps
  57. if [ -z "$OK" ]; then
  58. if [ "$SWAP_TYPE" = "file" ]; then
  59. MSG_TYPE="swap file"
  60. else
  61. MSG_TYPE="resume partition"
  62. fi
  63. echo "ERROR: $MSG_TYPE '$RESUME' not active, can not suspend"
  64. echo "$MSG_TYPE '$RESUME' not active" >> $INHIBIT
  65. return 1
  66. fi
  67. if [ "$SLEEP_MODULE" = "kernel" ]; then
  68. echo " using kernel suspend method"
  69. read DEV < /sys/power/resume
  70. if [ "$DEV" = "0:0" ]; then
  71. echo "ERROR: no resume partition set up in /sys/power/resume"
  72. # maybe "resume=..." was given, but initrd did not set up
  73. # /sys/power/resume correctly.
  74. echo "resume device not correctly setup in /sys/power/resume" >> \
  75. $INHIBIT
  76. return 1
  77. fi
  78. X=$(stat -Lc '$((0x%t)):$((0x%T))' $RESUME)
  79. RDEV=$(eval echo $X)
  80. if [ "$DEV" != "$RDEV" ]; then
  81. echo "ERROR: /sys/power/resume ($DEV) disagrees with resume= parameter ($RDEV)"
  82. echo " can not suspend."
  83. echo "/sys/power/resume disagrees with resume= parameter" >> \
  84. $INHIBIT
  85. return 1
  86. fi
  87. if [ -n "$IMAGE_SIZE" -a -w /sys/power/image_size ]; then
  88. echo " setting image size to $IMAGE_SIZE"
  89. echo "$IMAGE_SIZE" > /sys/power/image_size 2>/dev/null
  90. fi
  91. fi
  92. if [ "$SLEEP_MODULE" = "uswsusp" ]; then
  93. echo " using userspace suspend method, temp. config file $S2DISK_CONF"
  94. if [ "$S2DISK_CONF" != "/etc/suspend.conf" ]; then
  95. rm -f $S2DISK_CONF
  96. # Generate S2DISK_CONF
  97. echo " setting resume device to $RESUME"
  98. echo "resume device = $RESUME" >> $S2DISK_CONF
  99. if [ -n "$IMAGE_SIZE" ]; then
  100. echo " setting image size to $IMAGE_SIZE"
  101. echo "image size = $IMAGE_SIZE" >> $S2DISK_CONF
  102. fi
  103. # add the parameters from /etc/suspend.conf to /var/lib/s2disk.conf
  104. if [ -e /etc/suspend.conf ]; then
  105. echo "# parameters taken from /etc/suspend.conf:" >> $S2DISK_CONF
  106. sed '/^[[:space:]]*\(#\|$\)/d;' /etc/suspend.conf >> $S2DISK_CONF
  107. echo " adding these parameters from /etc/suspend.conf:"
  108. sed '/^[[:space:]]*\(#\|$\)/d;s/^/ /;' /etc/suspend.conf
  109. fi
  110. else
  111. # don't overwrite the user's config file, but warn about the issue
  112. echo "WARNING: S2DISK_CONF is set to /etc/suspend.conf"
  113. echo " Disabling automatic generation of config file."
  114. echo " This is most likely a configuration error and will cause problems"
  115. echo " unless you configure /etc/suspend.conf completely on your own!"
  116. fi
  117. fi
  118. return 0
  119. }
  120. # this only makes sense on hibernate or on suspend-hybrid
  121. case $1 in
  122. hibernate)
  123. ;;
  124. suspend)
  125. if [ "$2" != suspend_hybrid ]; then
  126. exit 0
  127. fi
  128. ;;
  129. *)
  130. exit $NA
  131. ;;
  132. esac
  133. [ -e /etc/pm/config.d/$1 ] && . /etc/pm/config.d/$1
  134. if [ -z "$SLEEP_MODULE" ]; then
  135. # Decide the SLEEP_MODULE if not given
  136. if [ -x /usr/sbin/s2disk -a -c /dev/snapshot ]; then
  137. SLEEP_MODULE="uswsusp"
  138. else
  139. SLEEP_MODULE="kernel"
  140. fi
  141. fi
  142. if ! checkhibernate; then
  143. echo "WARNING: $INHIBIT will be created to prevent suspending!"
  144. touch $INHIBIT
  145. exit 1
  146. fi
  147. exit 0