MakeFS 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # This file is part of the 'dragora-installer'.
  2. #
  3. # Purpose: Set Linux partition(s) and mount points.
  4. # Add or refresh list of partitions taking initial
  5. # list of partitions and temporary fstab into account
  6. makeMenu()
  7. {
  8. while read -r line
  9. do
  10. test -n "$line" || continue
  11. # Get device name and size
  12. device="${line%% *}"
  13. size="$(lsblk --noheadings --nodeps --output SIZE "$device")"
  14. # Get device mount point and file system type
  15. devpoint="$(awk -v patr="$device" '$1 == patr { print $2 }' \
  16. "${SUBTMPDIR}/fstab" 2> /dev/null)"
  17. formatfs="$(awk -v patr="$device" '$1 == patr { print $3 }' \
  18. "${SUBTMPDIR}/fstab" 2> /dev/null)"
  19. if test -z "$formatfs" || test "$formatfs" = defaults
  20. then
  21. formatfs="$(blkid -s TYPE -o value "$device" 2> /dev/null)"
  22. fi
  23. # Write entry to be showed on the MakeFS menu
  24. echo "\"${device}\" \"${formatfs:-Linux}${size} $devpoint\" \\" \
  25. >> "${SUBTMPDIR}/MakeFS"
  26. done < "${SUBTMPDIR}/partitions"
  27. # End Of
  28. echo ' 2> "${SUBTMPDIR}/return-MakeFS" || _code=$?' \
  29. >> "${SUBTMPDIR}/MakeFS"
  30. }
  31. # Ask to assign a valid mount point
  32. askMountPoint()
  33. {
  34. dialog --colors \
  35. --backtitle "\\ZbSetting Linux partitions: Mount point for $1" \
  36. --title "MOUNT POINT" \
  37. --no-cancel --ok-label "Continue" \
  38. --cr-wrap --inputbox \
  39. "A mount point can be assigned to the partition.
  40. * Consider declaring a complete path such as \"/storage\".
  41. * If you declare the single slash \"/\" it will be
  42. designated for the (main) system partition.
  43. Note: If you use directory names that contain spaces,
  44. the same will be converted using the octal character
  45. 040, which is interpreted by the file system table.
  46. Enter a mount point for \"${1}\":
  47. (If you leave this blank, any previous entries will be deleted)." 21 62 \
  48. 2> "${SUBTMPDIR}/return-MakeFS_askMountPoint" || return 0
  49. # We remove possible single/double quotes from the answer.
  50. # Also we convert any blank space (tab or space) into the
  51. # octal space character (040) to be handled by fstab(5).
  52. # This is in case that the user introduce a directory with
  53. # blank spaces. We are not managing the octal tab character
  54. # (011) since TAB is a functional key in dialog(1).
  55. mntpoint="$(sed -e "s/[\\'\"]//g" -e 's/[[:blank:]]/\\040/g' \
  56. "${SUBTMPDIR}/return-MakeFS_askMountPoint")"
  57. if test -n "$mntpoint"
  58. then
  59. # Determine the sixth field for the fstab
  60. case $mntpoint in
  61. /) fs_passno=1;;
  62. *) fs_passno=2;;
  63. esac
  64. # Refresh file system type information
  65. formatfs="$(blkid -s TYPE -o value "$1" 2> /dev/null)"
  66. # Write fstab entry
  67. printf '%-43s %-14s %-12s %-16s %-3s %s\n' \
  68. "$1" "$mntpoint" "$formatfs" "defaults" "1" "${fs_passno}" \
  69. >> "${SUBTMPDIR}/fstab"
  70. unset -v fs_passno formatfs
  71. # Remove possible duplicates
  72. if test "$(grep -c "^$1" "${SUBTMPDIR}"/fstab)" -gt 1
  73. then
  74. repeated="$(grep -m 1 -n "^$1" "${SUBTMPDIR}"/fstab)"
  75. repeated="${repeated%%:*}"
  76. sed -i "${repeated}d" "${SUBTMPDIR}/fstab"
  77. unset -v repeated
  78. fi
  79. else # Remove the whole fstab entry
  80. if test -n "$1" && test -s "${SUBTMPDIR}/fstab"
  81. then
  82. escape_slash="$(printf '%s' "$1" | sed 's/\//\\\//g')"
  83. sed -i "/${escape_slash}/d" "${SUBTMPDIR}/fstab"
  84. unset -v escape_slash
  85. fi
  86. fi
  87. unset -v mntpoint
  88. }
  89. _mkfs()
  90. {
  91. helper=$1
  92. devname=$2
  93. dialog --colors \
  94. --backtitle "\\ZbPartitions - ${devname}" \
  95. --title "FILE SYSTEM FORMAT" \
  96. --ok-label "Format" --cr-wrap --checklist \
  97. "The following device can be formatted using the \"${helper}\" helper:
  98. $(fdisk -l "$devname" | head -n 1)" 13 55 1 \
  99. "$devname" "Check for bad blocks (slow)" off \
  100. 2> "${SUBTMPDIR}/return-MakeFS_mkfs" || return $?
  101. progressfile="${SUBTMPDIR}/MakeFS-${helper}.progress.${RANDOM-0}$$"
  102. if test -s "${SUBTMPDIR}/return-MakeFS_mkfs"
  103. then
  104. slowcmd "$helper -c $devname" "$progressfile" | \
  105. dialog --colors \
  106. --backtitle "\\ZbPartitions: Formatting and checking partitions" \
  107. --title "PROGRESS" \
  108. --sleep 3 --progressbox \
  109. "Formatting $devname - Checking for bad blocks..." $(( LINES / 2 )) $COLUMNS
  110. else
  111. slowcmd "$helper $devname" "$progressfile" | \
  112. dialog --colors \
  113. --backtitle "\\ZbPartitions: Formatting partitions" \
  114. --title "PROGRESS" \
  115. --sleep 3 --progressbox \
  116. "Formatting $devname - No bad blocks check." $(( LINES / 2 )) $COLUMNS
  117. fi
  118. unset -v helper devname progressfile
  119. }
  120. _mkreiser4()
  121. {
  122. devname=$1
  123. progressfile="${SUBTMPDIR}/MakeFS-mkfs.reiser4.progress.${RANDOM-0}$$"
  124. slowcmd "mkfs.reiser4 -yf $devname" "$progressfile" | \
  125. dialog --colors \
  126. --backtitle "\\ZbPartitions: Formatting partitions" \
  127. --title "PROGRESS" \
  128. --sleep 3 --progressbox \
  129. "Formatting $devname - No bad blocks support." $(( LINES / 2 )) $COLUMNS
  130. unset -v devname progressfile
  131. }
  132. _mkxfs()
  133. {
  134. devname=$1
  135. progressfile="${SUBTMPDIR}/MakeFS-mkfs.xfs.progress.${RANDOM-0}$$"
  136. slowcmd "mkfs.xfs -f $devname" "$progressfile" | \
  137. dialog --colors \
  138. --backtitle "\\ZbPartitions: Formatting partitions" \
  139. --title "PROGRESS" \
  140. --sleep 3 --progressbox \
  141. "Formatting $devname - No bad blocks support." $(( LINES / 2 )) $COLUMNS
  142. unset -v devname progressfile
  143. }
  144. formatMenu()
  145. {
  146. dialog --colors \
  147. --backtitle "\\Zb${1}: Setting a file system" \
  148. --title "FILE SYSTEM SELECTION" \
  149. --default-item "ext3" \
  150. --item-help --cr-wrap --menu \
  151. "Choose a file system for '${1}':" 13 58 6 \
  152. "ext2" "Second extended filesystem" "ext2 remains the preferred file system for flash-based storage media due to its lack of a journal" \
  153. "ext3" "Third extended filesystem" "ext3 is suitable for both 32-bit and 64-bit systems" \
  154. "ext4" "Fourth extended filesystem" "We suggest using it more for a 64-bit system than for a 32-bit system" \
  155. "reiser4" "Reiser4 filesystem" "Reiser4 is a successor of ReiserFS (a general-purpose, journaling file system)" \
  156. "jfs" "JFS filesystem" "Actual usage of jfs in GNU/Linux is uncommon, as ext4 typically offers better performance" \
  157. "xfs" "XFS filesystem" "xfs is a high-performance 64-bit journaling file system suitable for real-time applications" \
  158. 2> "${SUBTMPDIR}/return-MakeFS_formatMenu" || _status=$?
  159. if test -n "$_status" && test $_status -ne 0
  160. then
  161. return "$_status";
  162. fi
  163. unset -v _status
  164. # Try to unmount the device if it is already mounted
  165. umount "$1" > /dev/null 2>&1 || true
  166. # Check for selected options to give the proper format
  167. case "$(cat -- "${SUBTMPDIR}"/return-MakeFS_formatMenu)" in
  168. ext2)
  169. _mkfs mkfs.ext2 "$1"
  170. ;;
  171. ext3)
  172. _mkfs mkfs.ext3 "$1"
  173. ;;
  174. ext4)
  175. _mkfs mkfs.ext4 "$1"
  176. ;;
  177. reiser4)
  178. _mkreiser4 "$1"
  179. ;;
  180. jfs)
  181. _mkfs jfs_mkfs "$1"
  182. ;;
  183. xfs)
  184. _mkxfs "$1"
  185. ;;
  186. esac
  187. }
  188. # Compose partition menu to be displayed
  189. while true
  190. do
  191. cat << EOF > "${SUBTMPDIR}/MakeFS"
  192. dialog --colors \\
  193. --backtitle "\\ZbPartitions and mount points" \\
  194. --title "Setting Linux partitions" \\
  195. --ok-button "Establish" --extra-button \\
  196. --extra-label "Done" \\
  197. --cr-wrap --menu \\
  198. "The following Linux partitions has been detected:
  199. Selecting a partition gives you the possibility to set up
  200. a file system or a mount point for the boot stage of the system.
  201. Once this has been established, choose the \\"Done\\" button to
  202. continue with the installation." 16 65 5 \\
  203. EOF
  204. makeMenu && . "${SUBTMPDIR}/MakeFS"
  205. case "${_code:-$?}" in
  206. 0)
  207. partition="$(cat -- "${SUBTMPDIR}"/return-MakeFS)"
  208. if test -z "$partition"
  209. then
  210. echo "${PROGRAM}: Error the partition list is empty." 1>&2
  211. exit 99;
  212. fi
  213. if test -n "$(blkid -s TYPE -o value "$partition" 2> /dev/null)"
  214. then
  215. dialog --colors \
  216. --backtitle "\\ZbPartitions and mount points" \
  217. --title "$partition" \
  218. --no-cancel --cr-wrap --menu \
  219. "Selected device already contains a file system!
  220. You can assign a mount point for system startup, you also
  221. have the ability to re-format the partition. If you choose
  222. the latter, you will lose all your data and will be asked
  223. for a mounting point.
  224. What would you like to do for '${partition}'?" 17 64 3 \
  225. "0" "Assign a mount point" \
  226. "1" "Format again (data loss risk)" \
  227. "<" "Return to previous menu" \
  228. 2> "${SUBTMPDIR}/return-MakeFS_case" || continue;
  229. case "$(cat -- "${SUBTMPDIR}"/return-MakeFS_case)" in
  230. 0) askMountPoint "$partition";;
  231. 1) formatMenu "$partition" && askMountPoint "$partition";;
  232. esac
  233. else
  234. formatMenu "$partition" && askMountPoint "$partition"
  235. fi
  236. ;;
  237. 3)
  238. # To verify the existence of the designated system mount point
  239. if grep -qs -m 1 '/ ' "${SUBTMPDIR}/fstab"
  240. then
  241. break;
  242. else
  243. dialog --colors \
  244. --backtitle "\\ZbPartitions and mount points: Unknown mount point for root partition" \
  245. --title "NO MOUNT POINT FOR MAIN SYSTEM" \
  246. --ok-label "Return" --cr-wrap --msgbox \
  247. "
  248. There is no declared mount point for the root partition.
  249. Please go back and assign a mount point for the
  250. root partition. Remember that the mount point must
  251. be declared as \"/\" (without quotes and spaces).
  252. " 11 60 || { unset -v _code ; continue; }
  253. fi
  254. ;;
  255. *)
  256. exit "$_code"
  257. ;;
  258. esac
  259. unset -v _code; # To take the value again.
  260. done
  261. unset makeMenu _mkfs _mkxfs FormatMenu _code