qi: Bump to release 2.12
[dragora.git] / archive / dragora-installer / parts / MakeSwap
blob5559ce31e8639cfd531584d6184cea0a95bf7f9c
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 and addition of inactive/active swap devices
32 swaplist="$(fdisk -l | awk '/Linux 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?" 17 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}$$"
168             dialog --colors \
169              --backtitle "\\ZbInactive Swap device"  \
170              --title "SWAP FORMAT FOR $device"       \
171              --ok-label "Format"                     \
172              --cancel-label "Ignore & Continue"      \
173              --cr-wrap --checklist                   \
174 "A swap device has been chosen.
176 We offer the possibility to format it properly,
177 as well as you can simply ignore this part and
178 continue if you previously formatted it:
180 Device name: $device [$(get_size $device)]" 14 51 1 \
181 "$device"    "Check for bad blocks (slow)" off \
182             2> $tempfile || continue;
184             dialog --clear
186             echo "Making swap device $device ..."
187             echo ""
189             if test -s "$tempfile"
190             then
191                 mkswap -c "$device"
192             else
193                 mkswap "$device"
194             fi
196             echo ""
197             echo "Activating swap device (${device}) ..."
198             echo ""
200             swapon -v -f "$device"
201             sleep 6
202             add_fstabEntry "$device"
203         done
205         unset -v REPLY device tempfile
206     fi
209 ### ^ End Of 'Check for inactive swap devices'
211 unset -f add_fstabEntry get_size set_checklist