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