dragora-installer: Preparing new changes
[dragora.git] / archive / dragora-installer / parts / MakeSwap
blob94c1a78dc3d995f76ce86cca6a87bd95caccee1b
1 # This file is part of the 'dragora-installer'.
3 # Purpose: Swap detector, formatter.
5 # A function to produce the fstab entry for the physical swap partition
6 add_fstabEntry()
8     printf '%-43s %-14s %-12s %-16s %-3s %s\n' \
9      "$1" "swap" "swap" "defaults" "0" "0" >> "${SUBTMPDIR}/fstab"
12 # Get and print device size
13 get_size()
15     lsblk --noheadings --nodeps --output SIZE "$1"
18 # Add device names to the checklist (to be shown)
19 set_checklist()
21     while IFS= read -r line
22     do
23         echo "\"${line}\" \"$(get_size $line)\" ON \\" >> "$2"
24     done < "$1"
26     # End Of
27     echo " 2> \${SUBTMPDIR}/$3" >> "$2"
30 ### Detection, addition of active and inactive swap devices
32 swaplist="$(blkid -s TYPE | awk -F ":" '/swap/{ print $1 }')"
34 if test -z "$swaplist"
35 then
36     dialog --colors \
37      --backtitle "\\ZbSwap detection" \
38      --title "NO SWAP PARTITION DETECTED" \
39      --cr-wrap --yesno \
40 "No swap partition has been detected.
42 Setting up a swap partition is recommended because
43 it will enable the system to use available physical
44 memory (RAM) more efficiently and also provide a
45 safeguard in situations where RAM is exhausted.
47 If you want to make use of hibernation (suspend to
48 disk), a swap partition of sufficient size is
49 required.
51 Would you like to proceed without a swap partition?" 16 55 || \
52     {
53         printf '%s' \
55 Please create a swap partition using fdisk(8) or cfdisk(8).
56 Then re-run the installer to give it the proper formatting.
58 You can also use mkswap(8) and swapon(8) to format and
59 activate the partition in advance.
61         exit 99;
62     }
63     unset add_fstabEntry get_size set_checklist swaplist
64     return 0
67 rm -f "${SUBTMPDIR}/MakeSwap_active.list" \
68       "${SUBTMPDIR}/MakeSwap_inactive.list"
69 for device in $swaplist
71     if swapon --noheadings --show=NAME | grep -q "$device"
72     then
73         echo "$device" >> "${SUBTMPDIR}/MakeSwap_active.list"
74     else
75         echo "$device" >> "${SUBTMPDIR}/MakeSwap_inactive.list"
76     fi
77 done
78 unset -v device swaplist
80 ### ^ End Of 'Detection, addition of active and inactive swap devices'
82 ### Check for active swap devices
84 if test -s "${SUBTMPDIR}/MakeSwap_active.list"
85 then
86     # Compose output file to be shown
88     printf '%s\n' \
89      'dialog --colors \'                                           \
90      ' --backtitle "\\ZbActive Swap devices" \'                    \
91      ' --title "ACTIVE SWAP DEVICES" \'                            \
92      ' --nocancel --cr-wrap --checklist \'                         \
93 '"The following swap devices have already been activated:'         \
94 ''                                                                 \
95 'By default all partitions are marked to be added to the'          \
96 'file system representation table (/etc/fstab) in order to'        \
97 'be loaded at boot time.  You can deselect the partition(s)'       \
98 'you do not want to add or load at system startup using'           \
99 'the \\Z3[Space]\\Zn key, then press \\Z3[Enter]\Zn to continue."  \
100 16 65 3 \' > "${SUBTMPDIR}/MakeSwap_active"
102     set_checklist "${SUBTMPDIR}/MakeSwap_active.list" \
103      "${SUBTMPDIR}/MakeSwap_active" return-MakeSwap_active
106 if test -s "${SUBTMPDIR}/MakeSwap_active"
107 then
108     . "${SUBTMPDIR}/MakeSwap_active"
110     if test -s "${SUBTMPDIR}/return-MakeSwap_active"
111     then
112         # Append new line for reading
113         echo "" >> "${SUBTMPDIR}/return-MakeSwap_active"
115         IFS= read -r REPLY < "${SUBTMPDIR}/return-MakeSwap_active" || exit 2;
117         for device in $REPLY
118         do
119             add_fstabEntry "$device"
120         done
122         unset -v REPLY device
123     fi
126 ### ^ End Of 'Check for active swap devices'
128 ### Check for inactive swap devices
130 if test -s "${SUBTMPDIR}/MakeSwap_inactive.list"
131 then
132     # Compose output file to be shown
134     printf '%s\n' \
135      'dialog --colors \'                                           \
136      ' --backtitle "\\ZbInactive Swap devices" \'                  \
137      ' --title "INACTIVE SWAP DEVICES" \'                          \
138      ' --nocancel --cr-wrap --checklist \'                         \
139 '"The following swap partitions has been detected:'                \
140 ''                                                                 \
141 'By default all partitions found will be formatted and added'      \
142 'to the file system representation table (/etc/fstab) in order'    \
143 'to be loaded at boot time.  You can deselect the partition(s)'    \
144 'you do not want to format or load at system startup using'        \
145 'the \\Z3[Space]\\Zn key, then press \\Z3[Enter]\Zn to continue."  \
146 16 65 3 \' > "${SUBTMPDIR}/MakeSwap_inactive"
148     set_checklist "${SUBTMPDIR}/MakeSwap_inactive.list" \
149      "${SUBTMPDIR}/MakeSwap_inactive" return-MakeSwap_inactive
152 if test -s "${SUBTMPDIR}/MakeSwap_inactive"
153 then
154     . "${SUBTMPDIR}/MakeSwap_inactive"
156     if test -s "${SUBTMPDIR}/return-MakeSwap_inactive"
157     then
158         # Append new line for reading
159         echo "" >> "${SUBTMPDIR}/return-MakeSwap_inactive"
161         IFS= read -r REPLY < "${SUBTMPDIR}/return-MakeSwap_inactive" || exit 2;
163         for device in $REPLY
164         do
165             # Set up temporary random files related to the menu
166             tempfile="${SUBTMPDIR}/return_MakeSwap_format${RANDOM-0}$$"
167             progressfile="${tempfile}.progress"
169             dialog --colors \
170              --backtitle "\\ZbInactive Swap device"  \
171              --title "SWAP FORMAT FOR $device"       \
172              --ok-label "Format"                     \
173              --cancel-label "Ignore & Continue"      \
174              --cr-wrap --checklist                   \
175 "A swap device has been chosen.
177 We offer the possibility to format it properly,
178 as well as you can simply ignore this part and
179 continue if you previously formatted it:
181 Device name: $device [$(get_size $device)]" 14 51 1 \
182 "$device"    "Check for bad blocks (slow)" off \
183             2> $tempfile || continue;
185             if test -s "$tempfile"
186             then
187                 slowcmd "mkswap -c $device" "$progressfile" | \
188                  dialog --colors \
189                   --backtitle "\\ZbSwap format for $device" \
190                   --title "STATUS" \
191                   --sleep 3 --progressbox \
192 "Formatting $device - Checking for bad blocks..." $(( LINES / 2 )) $COLUMNS
193             else
194                 slowcmd "mkswap $device" "$progressfile" | \
195                  dialog --colors \
196                   --backtitle "\\ZbSwap format for $device" \
197                   --title "STATUS" \
198                   --sleep 3 --progressbox \
199 "Formatting $device - No bad blocks check." $(( LINES / 2 )) $COLUMNS
200             fi
202             # Activate device, adding the entry for the fstab
204             slowcmd "swapon -v -f $device" "$progressfile" | \
205              dialog --colors \
206               --backtitle "\\ZbSwap activation for $device" \
207               --title "STATUS" \
208               --sleep 3 --progressbox \
209 "Activating swap device $device ..." $(( LINES / 2 )) $COLUMNS
210             add_fstabEntry "$device"
211         done
213         unset -v REPLY device tempfile progressfile
214     fi
217 ### ^ End Of 'Check for inactive swap devices'
219 unset -f add_fstabEntry get_size set_checklist