MakeSwap 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # This file is part of the 'dragora-installer'.
  2. #
  3. # Purpose: Swap detector, formatter.
  4. # A function to produce the fstab entry for the physical swap partition
  5. add_fstabEntry()
  6. {
  7. printf '%-43s %-14s %-12s %-16s %-3s %s\n' \
  8. "$1" "swap" "swap" "defaults" "0" "0" >> "${SUBTMPDIR}/fstab"
  9. }
  10. # Get and print device size
  11. get_size()
  12. {
  13. lsblk --noheadings --nodeps --output SIZE "$1"
  14. }
  15. # Add device names to the checklist (to be shown)
  16. set_checklist()
  17. {
  18. while IFS= read -r line
  19. do
  20. echo "\"${line}\" \"$(get_size $line)\" ON \\" >> "$2"
  21. done < "$1"
  22. # End Of
  23. echo " 2> \${SUBTMPDIR}/$3" >> "$2"
  24. }
  25. ### Detection and addition of inactive/active swap devices
  26. swaplist="$(fdisk -l | awk '/Linux swap/{ print $1 }')"
  27. if test -z "$swaplist"
  28. then
  29. dialog --colors \
  30. --backtitle "\\ZbSwap detection" \
  31. --title "NO SWAP PARTITION DETECTED" \
  32. --cr-wrap --yesno \
  33. "No swap partition has been detected.
  34. Setting up a swap partition is recommended because
  35. it will enable the system to use available physical
  36. memory (RAM) more efficiently and also provide a
  37. safeguard in situations where RAM is exhausted.
  38. If you want to make use of hibernation (suspend to
  39. disk), a swap partition of sufficient size is
  40. required.
  41. Would you like to proceed without a swap partition?" 17 55 || \
  42. {
  43. printf '%s' \
  44. "
  45. Please create a swap partition using fdisk(8) or cfdisk(8).
  46. Then re-run the installer to give it the proper formatting.
  47. You can also use mkswap(8) and swapon(8) to format and
  48. activate the partition in advance.
  49. "
  50. exit 99;
  51. }
  52. unset add_fstabEntry get_size set_checklist swaplist
  53. return 0
  54. fi
  55. rm -f "${SUBTMPDIR}/MakeSwap_active.list" \
  56. "${SUBTMPDIR}/MakeSwap_inactive.list"
  57. for device in $swaplist
  58. do
  59. if swapon --noheadings --show=NAME | grep -q "$device"
  60. then
  61. echo "$device" >> "${SUBTMPDIR}/MakeSwap_active.list"
  62. else
  63. echo "$device" >> "${SUBTMPDIR}/MakeSwap_inactive.list"
  64. fi
  65. done
  66. unset -v device swaplist
  67. ### ^ End Of 'Detection, addition of active and inactive swap devices'
  68. ### Check for active swap devices
  69. if test -s "${SUBTMPDIR}/MakeSwap_active.list"
  70. then
  71. # Compose output file to be shown
  72. printf '%s\n' \
  73. 'dialog --colors \' \
  74. ' --backtitle "\\ZbActive Swap devices" \' \
  75. ' --title "ACTIVE SWAP DEVICES" \' \
  76. ' --nocancel --cr-wrap --checklist \' \
  77. '"The following swap devices have already been activated:' \
  78. '' \
  79. 'By default all partitions are marked to be added to the' \
  80. 'file system representation table (/etc/fstab) in order to' \
  81. 'be loaded at boot time. You can deselect the partition(s)' \
  82. 'you do not want to add or load at system startup using' \
  83. 'the \\Z3[Space]\\Zn key, then press \\Z3[Enter]\Zn to continue." \
  84. 16 65 3 \' > "${SUBTMPDIR}/MakeSwap_active"
  85. set_checklist "${SUBTMPDIR}/MakeSwap_active.list" \
  86. "${SUBTMPDIR}/MakeSwap_active" return-MakeSwap_active
  87. fi
  88. if test -s "${SUBTMPDIR}/MakeSwap_active"
  89. then
  90. . "${SUBTMPDIR}/MakeSwap_active"
  91. if test -s "${SUBTMPDIR}/return-MakeSwap_active"
  92. then
  93. # Append new line for reading
  94. echo "" >> "${SUBTMPDIR}/return-MakeSwap_active"
  95. IFS= read -r REPLY < "${SUBTMPDIR}/return-MakeSwap_active" || exit 2;
  96. for device in $REPLY
  97. do
  98. add_fstabEntry "$device"
  99. done
  100. unset -v REPLY device
  101. fi
  102. fi
  103. ### ^ End Of 'Check for active swap devices'
  104. ### Check for inactive swap devices
  105. if test -s "${SUBTMPDIR}/MakeSwap_inactive.list"
  106. then
  107. # Compose output file to be shown
  108. printf '%s\n' \
  109. 'dialog --colors \' \
  110. ' --backtitle "\\ZbInactive Swap devices" \' \
  111. ' --title "INACTIVE SWAP DEVICES" \' \
  112. ' --nocancel --cr-wrap --checklist \' \
  113. '"The following swap partitions has been detected:' \
  114. '' \
  115. 'By default all partitions found will be formatted and added' \
  116. 'to the file system representation table (/etc/fstab) in order' \
  117. 'to be loaded at boot time. You can deselect the partition(s)' \
  118. 'you do not want to format or load at system startup using' \
  119. 'the \\Z3[Space]\\Zn key, then press \\Z3[Enter]\Zn to continue." \
  120. 16 65 3 \' > "${SUBTMPDIR}/MakeSwap_inactive"
  121. set_checklist "${SUBTMPDIR}/MakeSwap_inactive.list" \
  122. "${SUBTMPDIR}/MakeSwap_inactive" return-MakeSwap_inactive
  123. fi
  124. if test -s "${SUBTMPDIR}/MakeSwap_inactive"
  125. then
  126. . "${SUBTMPDIR}/MakeSwap_inactive"
  127. if test -s "${SUBTMPDIR}/return-MakeSwap_inactive"
  128. then
  129. # Append new line for reading
  130. echo "" >> "${SUBTMPDIR}/return-MakeSwap_inactive"
  131. IFS= read -r REPLY < "${SUBTMPDIR}/return-MakeSwap_inactive" || exit 2;
  132. for device in $REPLY
  133. do
  134. # Set up temporary random files related to the menu
  135. tempfile="${SUBTMPDIR}/return_MakeSwap_format${RANDOM-0}$$"
  136. dialog --colors \
  137. --backtitle "\\ZbInactive Swap device" \
  138. --title "SWAP FORMAT FOR $device" \
  139. --ok-label "Format" \
  140. --cancel-label "Ignore & Continue" \
  141. --cr-wrap --checklist \
  142. "A swap device has been chosen.
  143. We offer the possibility to format it properly,
  144. as well as you can simply ignore this part and
  145. continue if you previously formatted it:
  146. Device name: $device [$(get_size $device)]" 14 51 1 \
  147. "$device" "Check for bad blocks (slow)" off \
  148. 2> $tempfile || continue;
  149. dialog --clear
  150. echo "Making swap device $device ..."
  151. echo ""
  152. if test -s "$tempfile"
  153. then
  154. mkswap -c "$device"
  155. else
  156. mkswap "$device"
  157. fi
  158. echo ""
  159. echo "Activating swap device (${device}) ..."
  160. echo ""
  161. swapon -v -f "$device"
  162. sleep 6
  163. add_fstabEntry "$device"
  164. done
  165. unset -v REPLY device tempfile
  166. fi
  167. fi
  168. ### ^ End Of 'Check for inactive swap devices'
  169. unset -f add_fstabEntry get_size set_checklist