OSDN Git Service

rm -i arg from xed launch
[pacbang-linux/pbif.git] / pbif
1 # !/bin/bash
2 #
3 # Arch Base Installation Framework (version 2.2.3 - 26-Mar-2016)
4 #
5 # Written by Carl Duff for PacBang Linux
6 #
7 # This program is free software, provided under the GNU General Public License
8 # as published by the Free Software Foundation. So feel free to copy, distribute,
9 # or modify it as you wish.
10 #
11
12 ######################################################################
13 ##                                                                                                                                      ##
14 ##                   Installer Variables                                                        ##
15 ##                                                                                                                                      ##
16 ######################################################################
17
18 #                                                                                                                       #
19 # ISO Specific Variables. READ CAREFULLY FOR YOUR DISTRO        #
20 #                                                                                                                       #
21
22 # This MUST be the same as listed in /etc/hosts
23 ISO_HOST="pacbang"                                      # ISO Host Name
24
25 # This MUST be the same as the live account
26 ISO_USER="paclive"                                                      # Live user account.
27
28 # Call this what you like
29 VERSION="Pacbang Installation Framework 2.2.3"          # Installer Name / Version
30
31 # This should not need to be changed
32 TRANS_SRC="/pbif-master"                                                        # Dir where translation files are stored
33
34 #                                                               #
35 # Other variables for installer #
36 #                                                               #
37
38 # Editor for modifying configs
39 TEXTEDITOR="xed"
40
41 # Create a temporary file to store menu selections
42 ANSWER="/tmp/.abif"
43
44 # Installation
45 BOOTLOADER="n/a"                                                                # Which bootloader has been installed?
46 KEYMAP="us"                                                                     # Virtual console keymap. Default is "us"
47 XKBMAP="us"                                                                     # X11 keyboard layout. Default is "us"
48 ZONE=""                                                                         # For time
49 SUBZONE=""                                                                      # For time
50 LOCALE="en_US.UTF-8"                                                            # System locale. Default is "en_US.UTF-8"
51
52 # Architecture
53 ARCHI=$(uname -m)                                                               # Display whether 32 or 64 bit system
54 SYSTEM="Unknown"                                                                # Display whether system is BIOS or UEFI. Default is "unknown"
55 ROOT_PART=""                                                                    # ROOT partition
56 UEFI_PART=""                                                                            # UEFI partition
57 UEFI_MOUNT=""                                                                   # UEFI mountpoint
58 INST_DEV=""                                                                     # Device where system has been installed
59 HIGHLIGHT=0                                                                     # Highlight items for Main Menu
60 HIGHLIGHT_SUB=0                                                                 # Highlight items for submenus
61 SUB_MENU=""                                                                     # Submenu to be highlighted
62
63 # Logical Volume Management
64 LVM=0                                                                           # Logical Volume Management Detected?
65 LVM_SEP_BOOT=0                                                                  # 1 = Seperate /boot, 2 = seperate /boot & LVM
66 LVM_VG=""                                                                       # Name of volume group to create or use
67 LVM_VG_MB=0                                                                     # MB remaining of VG
68 LVM_LV_NAME=""                                                                  # Name of LV to create or use
69 LV_SIZE_INVALID=0                                                               # Is LVM LV size entered valid?
70 VG_SIZE_TYPE=""                                                                 # Is VG in Gigabytes or Megabytes?
71
72 # LUKS
73 LUKS=0                                                                          # Luks Detected?
74 LUKS_DEV=""                                                                                     # If encrypted, partition
75 LUKS_NAME=""                                                                            # Name given to encrypted partition
76 LUKS_UUID=""                                                                            # UUID used for comparison purposes
77 LUKS_OPT=""                                                                                     # Default or user-defined?
78
79 # Installation
80 MOUNTPOINT="/mnt"                                                               # Installation
81 AIROOTIMG=""                                                            # Root image to install
82 BYPASS="$MOUNTPOINT/bypass/"                                            # Root image mountpoint
83 BTRFS=0                                                                 # BTRFS used? "1" = btrfs alone, "2" = btrfs + subvolume(s)
84 MOUNT_OPTS="/tmp/.mnt_opts"                                             # Filesystem Mount options
85 FS_OPTS=""                                                                                      # FS mount options available
86 CHK_NUM=16                                                                                      # Used for FS mount options checklist length
87
88 # Language Support
89 CURR_LOCALE="en_US.UTF-8"                                               # Default Locale
90 FONT=""                                                                 # Set new font if necessary
91
92 # Edit Files
93 FILE=""                                                                 # Which file is to be opened?
94
95 ######################################################################
96 ##                                                                                                                                      ##
97 ##                        Core Functions                                                        ##
98 ##                                                                                                                                      ##
99 ######################################################################
100
101 # Add locale on-the-fly and sets source translation file for installer
102 select_language() {
103     
104     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " Select Language " --menu "\nLanguage / sprache / taal / språk / lingua / idioma / nyelv / língua" 0 0 9 \
105         "1" $"English            (en_**)" \
106         "2" $"Español            (es_ES)" \
107         "3" $"Português [Brasil] (pt_BR)" \
108         "4" $"Português          (pt_PT)" \
109         "5" $"Français           (fr_FR)" \
110         "6" $"Russkiy            (ru_RU)" \
111         "7" $"Italiano           (it_IT)" \
112         "8" $"Nederlands         (nl_NL)" \
113         "9" $"Magyar             (hu_HU)" 2>${ANSWER}
114
115         case $(cat ${ANSWER}) in
116         "1") source ${TRANS_SRC}/english.trans
117              CURR_LOCALE="en_US.UTF-8"
118              ;;
119         "2") source ${TRANS_SRC}/spanish.trans
120              CURR_LOCALE="es_ES.UTF-8"
121              ;; 
122         "3") source ${TRANS_SRC}/portuguese_brasil.trans
123                          CURR_LOCALE="pt_BR.UTF-8"
124                          ;;
125         "4") source ${TRANS_SRC}/portuguese.trans
126                          CURR_LOCALE="pt_PT.UTF-8"
127                          ;;             
128                 "5") source ${TRANS_SRC}/french.trans
129              CURR_LOCALE="fr_FR.UTF-8"
130              ;;  
131         "6") source ${TRANS_SRC}/russian.trans
132              CURR_LOCALE="ru_RU.UTF-8"
133              FONT="LatKaCyrHeb-14.psfu"
134              ;;
135         "7") source ${TRANS_SRC}/italian.trans
136              CURR_LOCALE="it_IT.UTF-8"
137              ;;
138         "8") source ${TRANS_SRC}/dutch.trans
139              CURR_LOCALE="nl_NL.UTF-8"
140              ;;
141         "9") source ${TRANS_SRC}/hungarian.trans
142              CURR_LOCALE="hu_HU.UTF-8"
143              FONT="lat2-16.psfu"
144              ;;
145           *) exit 0
146              ;;
147     esac
148         
149     # Generate the chosen locale and set the language
150     sed -i "s/#${CURR_LOCALE}/${CURR_LOCALE}/" /etc/locale.gen
151     locale-gen >/dev/null 2>&1
152     export LANG=${CURR_LOCALE}
153     [[ $FONT != "" ]] && setfont $FONT
154 }
155
156
157
158 # Check user is root, and that there is an active internet connection
159 # Seperated the checks into seperate "if" statements for readability.
160 check_requirements() {
161         
162   dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ChkTitle " --infobox "$_PlsWaitBody" 0 0
163   sleep 2
164   
165   if [[ $(whoami) != "root" ]]; then
166      dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_Erritle " --infobox "$_RtFailBody" 0 0
167      sleep 2
168      exit 1
169   fi
170   
171   # The error log is also cleared, just in case something is there from a previous use of the installer.
172   dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ReqMetTitle " --infobox "$_ReqMetBody" 0 0
173   sleep 2   
174   clear
175   echo "" > /tmp/.errlog
176 }
177
178 # Adapted from AIS. Checks if system is made by Apple, whether the system is BIOS or UEFI,
179 # and for LVM and/or LUKS.
180 id_system() {
181         
182     # Apple System Detection
183     if [[ "$(cat /sys/class/dmi/id/sys_vendor)" == 'Apple Inc.' ]] || [[ "$(cat /sys/class/dmi/id/sys_vendor)" == 'Apple Computer, Inc.' ]]; then
184       modprobe -r -q efivars || true  # if MAC
185     else
186       modprobe -q efivarfs            # all others
187     fi
188     
189     # BIOS or UEFI Detection
190     if [[ -d "/sys/firmware/efi/" ]]; then
191       # Mount efivarfs if it is not already mounted
192       if [[ -z $(mount | grep /sys/firmware/efi/efivars) ]]; then
193         mount -t efivarfs efivarfs /sys/firmware/efi/efivars
194       fi
195       SYSTEM="UEFI"
196     else
197       SYSTEM="BIOS"
198     fi
199          
200 }   
201  
202
203 # Adapted from AIS. An excellent bit of code!
204 arch_chroot() {
205     arch-chroot $MOUNTPOINT /bin/bash -c "${1}"
206 }  
207
208 # If there is an error, display it, clear the log and then go back to the main menu (no point in continuing).
209 check_for_error() {
210
211  if [[ $? -eq 1 ]] && [[ $(cat /tmp/.errlog | grep -i "error") != "" ]]; then
212     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$(cat /tmp/.errlog)" 0 0
213     echo "" > /tmp/.errlog
214     main_menu
215  fi
216    
217 }
218
219 # Ensure that a partition is mounted
220 check_mount() {
221
222     if [[ $(lsblk -o MOUNTPOINT | grep ${MOUNTPOINT}) == "" ]]; then
223        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_ErrNoMount" 0 0
224        main_menu
225     fi
226
227 }
228
229 # Ensure that Arch has been installed
230 check_base() {
231
232     if [[ ! -e ${MOUNTPOINT}/etc ]]; then
233         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_ErrNoBase" 0 0
234         main_menu
235     fi
236     
237 }
238
239 # Simple code to show devices / partitions.
240 show_devices() {
241      lsblk -o NAME,MODEL,TYPE,FSTYPE,SIZE,MOUNTPOINT | grep "disk\|part\|lvm\|crypt\|NAME\|MODEL\|TYPE\|FSTYPE\|SIZE\|MOUNTPOINT" > /tmp/.devlist
242      dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_DevShowOpt " --textbox /tmp/.devlist 0 0
243 }
244
245
246
247 ######################################################################
248 ##                                                                                                                                      ##
249 ##                 Configuration Functions                                                      ##
250 ##                                                                                                                                      ##
251 ######################################################################
252
253 # virtual console keymap
254 set_keymap() { 
255         
256         KEYMAPS=""
257     for i in $(ls -R /usr/share/kbd/keymaps | grep "map.gz" | sed 's/\.map\.gz//g' | sort); do
258         KEYMAPS="${KEYMAPS} ${i} -"
259     done
260     
261     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_VCKeymapTitle " \
262     --menu "$_VCKeymapBody" 20 40 16 ${KEYMAPS} 2>${ANSWER} || prep_menu 
263     KEYMAP=$(cat ${ANSWER})
264     
265         echo -e "KEYMAP=${KEYMAP}\nFONT=${FONT}" > /tmp/vconsole.conf
266   }
267
268 # Set keymap for X11
269  set_xkbmap() {
270          
271         XKBMAP_LIST=""
272         keymaps_xkb=("af al am at az ba bd be bg br bt bw by ca cd ch cm cn cz de dk ee es et eu fi fo fr gb ge gh gn gr hr hu ie il in iq ir is it jp ke kg kh kr kz la lk lt lv ma md me mk ml mm mn mt mv ng nl no np pc ph pk pl pt ro rs ru se si sk sn sy tg th tj tm tr tw tz ua us uz vn za")
273     
274         for i in ${keymaps_xkb}; do
275         XKBMAP_LIST="${XKBMAP_LIST} ${i} -"
276     done
277         
278     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepKBLayout " --menu "$_XkbmapBody" 0 0 16 ${XKBMAP_LIST} 2>${ANSWER} || install_graphics_menu
279     XKBMAP=$(cat ${ANSWER} |sed 's/_.*//')
280     echo -e "Section "\"InputClass"\"\nIdentifier "\"system-keyboard"\"\nMatchIsKeyboard "\"on"\"\nOption "\"XkbLayout"\" "\"${XKBMAP}"\"\nEndSection" > /tmp/01-keyboard-layout.conf
281  
282         setxkbmap $XKBMAP 2>/tmp/.errlog
283     check_for_error
284  
285 }
286
287 # locale array generation code adapted from the Manjaro 0.8 installer
288 set_locale() {
289
290   LOCALES=""    
291   for i in $(cat /etc/locale.gen | grep -v "#  " | sed 's/#//g' | sed 's/ UTF-8//g' | grep .UTF-8); do
292       LOCALES="${LOCALES} ${i} -"
293   done
294
295   dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseSysLoc " --menu "$_localeBody" 0 0 12 ${LOCALES} 2>${ANSWER} || config_base_menu 
296   
297   LOCALE=$(cat ${ANSWER})
298
299   echo "LANG=\"${LOCALE}\"" > ${MOUNTPOINT}/etc/locale.conf
300   sed -i "s/#${LOCALE}/${LOCALE}/" ${MOUNTPOINT}/etc/locale.gen 2>/tmp/.errlog
301   arch_chroot "locale-gen" >/dev/null 2>>/tmp/.errlog
302   check_for_error
303 }
304
305 # Set Zone and Sub-Zone
306 set_timezone() {
307
308     ZONE=""
309     for i in $(cat /usr/share/zoneinfo/zone.tab | awk '{print $3}' | grep "/" | sed "s/\/.*//g" | sort -ud); do
310       ZONE="$ZONE ${i} -"
311     done
312     
313      dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseTimeHC " --menu "$_TimeZBody" 0 0 10 ${ZONE} 2>${ANSWER} || config_base_menu
314      ZONE=$(cat ${ANSWER}) 
315     
316      SUBZONE=""
317      for i in $(cat /usr/share/zoneinfo/zone.tab | awk '{print $3}' | grep "${ZONE}/" | sed "s/${ZONE}\///g" | sort -ud); do
318         SUBZONE="$SUBZONE ${i} -"
319      done
320          
321      dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseTimeHC " --menu "$_TimeSubZBody" 0 0 11 ${SUBZONE} 2>${ANSWER} || config_base_menu
322      SUBZONE=$(cat ${ANSWER}) 
323     
324      dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseTimeHC " --yesno "$_TimeZQ ${ZONE}/${SUBZONE}?" 0 0 
325      
326      if [[ $? -eq 0 ]]; then
327         arch_chroot "ln -sf /usr/share/zoneinfo/${ZONE}/${SUBZONE} /etc/localtime" 2>/tmp/.errlog
328         check_for_error
329      else
330         config_base_menu
331      fi
332 }
333
334 set_hw_clock() {
335         
336    dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseTimeHC " --menu "$_HwCBody" 0 0 2 \
337         "utc" "-" "localtime" "-" 2>${ANSWER}   
338
339     [[ $(cat ${ANSWER}) != "" ]] && arch_chroot "hwclock --systohc --$(cat ${ANSWER})"  2>/tmp/.errlog && check_for_error
340 }
341
342 # Generate the installed system's FSTAB
343 generate_fstab() {
344
345     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseFstab " --menu "$_FstabBody" 0 0 4 \
346         "genfstab -U -p" "$_FstabDevUUID" \
347         "genfstab -p" "$_FstabDevName" \
348         "genfstab -L -p" "$_FstabDevLabel" \
349         "genfstab -t PARTUUID -p" "$_FstabDevPtUUID" 2>${ANSWER}
350         
351         if [[ $(cat ${ANSWER}) != "" ]]; then
352                 if [[ $SYSTEM == "BIOS" ]] && [[ $(cat ${ANSWER}) == "genfstab -t PARTUUID -p" ]]; then
353                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_FstabErr" 0 0
354                         generate_fstab
355                 else
356                         $(cat ${ANSWER}) ${MOUNTPOINT} > ${MOUNTPOINT}/etc/fstab 2>/tmp/.errlog
357                         check_for_error
358                         [[ -f ${MOUNTPOINT}/swapfile ]] && sed -i "s/\\${MOUNTPOINT}//" ${MOUNTPOINT}/etc/fstab
359                 fi
360         fi
361
362     # Determine if there is a swapfile before copying over appropriate OB configs
363         if [[ $(cat $MOUNTPOINT/etc/fstab | grep "swap") != "" ]]; then
364         # rc.xml -- swap/hibernate
365             cp -f /inst/rc2.xml $MOUNTPOINT/etc/skel/.config/openbox/menu.xml 2>/tmp/.errlog
366                 cp -f /inst/rc2.xml $MOUNTPOINT/root/.config/openbox/menu.xml 2>/tmp/.errlog
367                 cp -f /inst/rc2.xml $MOUNTPOINT/home/$ISO_USER/.config/openbox/menu.xml 2>/tmp/.errlog
368         # menu.xml -- swap/hibernate
369             cp -f /inst/menu2.xml $MOUNTPOINT/etc/skel/.config/openbox/menu.xml 2>/tmp/.errlog
370                 cp -f /inst/menu2.xml $MOUNTPOINT/root/.config/openbox/menu.xml 2>/tmp/.errlog
371                 cp -f /inst/menu2.xml $MOUNTPOINT/home/$ISO_USER/.config/openbox/menu.xml 2>/tmp/.errlog
372         else
373         # rc.xml -- no swap
374                 cp -f /inst/rc.xml $MOUNTPOINT/etc/skel/.config/openbox/menu.xml 2>/tmp/.errlog
375         cp -f /inst/rc.xml $MOUNTPOINT/root/.config/openbox/menu.xml 2>/tmp/.errlog
376                 cp -f /inst/rc.xml $MOUNTPOINT/home/$ISO_USER/.config/openbox/menu.xml 2>/tmp/.errlog
377             # rc.xml -- no swap
378             cp -f /inst/menu.xml $MOUNTPOINT/etc/skel/.config/openbox/menu.xml 2>/tmp/.errlog
379             cp -f /inst/menu.xml $MOUNTPOINT/root/.config/openbox/menu.xml 2>/tmp/.errlog
380                 cp -f /inst/menu.xml $MOUNTPOINT/home/$ISO_USER/.config/openbox/menu.xml 2>/tmp/.errlog
381         fi
382         check_for_error
383
384 }
385
386 # Set the installed system's hostname
387 set_hostname() {
388
389    dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseHost " --inputbox "$_HostNameBody" 0 0 "pacbang" 2>${ANSWER} || config_base_menu
390
391    echo "$(cat ${ANSWER})" > ${MOUNTPOINT}/etc/hostname 2>/tmp/.errlog
392    echo -e "#ip-address\thostname.domain.org\thostname\n127.0.0.1\tlocalhost.localdomain\tlocalhost\n::1\tlocalhost\n127.0.1.1\t$(cat ${ANSWER}).localdomain\t$(cat ${ANSWER})" > ${MOUNTPOINT}/etc/hosts 2>>/tmp/.errlog
393    check_for_error
394 }
395
396 # Set the installed system's root password
397 set_root_password() {
398
399     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrRoot " --clear --insecure --passwordbox "$_PassRtBody" 0 0 2> ${ANSWER} || config_base_menu
400     PASSWD=$(cat ${ANSWER})
401     
402     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrRoot " --clear --insecure --passwordbox "$_PassReEntBody" 0 0 2> ${ANSWER} || config_base_menu
403     PASSWD2=$(cat ${ANSWER})
404     
405     if [[ $PASSWD == $PASSWD2 ]]; then 
406        echo -e "${PASSWD}\n${PASSWD}" > /tmp/.passwd
407        arch_chroot "passwd root" < /tmp/.passwd >/dev/null 2>/tmp/.errlog
408        rm /tmp/.passwd
409        check_for_error
410     else
411        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_PassErrBody" 0 0
412        set_root_password
413     fi
414
415 }
416
417 # Create new user(s) for installed system. First user is created by renaming the live account.
418 # All others are brand new.
419 create_new_user() {
420
421         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_NUsrTitle " --inputbox "$_NUsrBody" 0 0 "" 2>${ANSWER} || config_base_menu
422         USER=$(cat ${ANSWER})
423         
424         # Loop while user name is blank, has spaces, or has capital letters in it.
425         while [[ ${#USER} -eq 0 ]] || [[ $USER =~ \ |\' ]] || [[ $USER =~ [^a-z0-9\ ] ]]; do
426                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_NUsrTitle " --inputbox "$_NUsrErrBody" 0 0 "" 2>${ANSWER} || config_base_menu
427                 USER=$(cat ${ANSWER})
428         done
429         
430         # Enter password. This step will only be reached where the loop has been skipped or broken.
431         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrNew " --clear --insecure --passwordbox "$_PassNUsrBody $USER\n\n" 0 0 2> ${ANSWER} || config_base_menu
432         PASSWD=$(cat ${ANSWER}) 
433     
434         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrNew " --clear --insecure --passwordbox "$_PassReEntBody" 0 0 2> ${ANSWER} || config_base_menu
435         PASSWD2=$(cat ${ANSWER}) 
436     
437         # loop while passwords entered do not match.
438         while [[ $PASSWD != $PASSWD2 ]]; do
439                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_PassErrBody" 0 0
440               
441                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrNew " --clear --insecure --passwordbox "$_PassNUsrBody $USER\n\n" 0 0 2> ${ANSWER} || config_base_menu
442                 PASSWD=$(cat ${ANSWER}) 
443     
444                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrNew " --clear --insecure --passwordbox "$_PassReEntBody" 0 0 2> ${ANSWER} || config_base_menu
445                 PASSWD2=$(cat ${ANSWER}) 
446         done      
447     
448         # create new user. This step will only be reached where the password loop has been skipped or broken.  
449         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfUsrNew " --infobox "$_NUsrSetBody" 0 0
450         sleep 2
451         echo -e "${PASSWD}\n${PASSWD}" > /tmp/.passwd
452
453     # If the first (or only) user account, then change the live account
454         if [[ -e ${MOUNTPOINT}/home/$ISO_USER ]]; then
455            arch_chroot "passwd $ISO_USER" < /tmp/.passwd >/dev/null 2>>/tmp/.errlog
456            check_for_error
457         
458            # Distro-specific configuration for entered username
459            sed -i "s/$ISO_USER/$USER/g" ${MOUNTPOINT}/home/$ISO_USER/.gtkrc-2.0 2>/tmp/.errlog
460         
461            # Convert live account to entered username - group, password, folder, and ownership
462            sed -i "s/$ISO_USER/$USER/g" ${MOUNTPOINT}/etc/group 2>>/tmp/.errlog
463            sed -i "s/$ISO_USER/$USER/g" ${MOUNTPOINT}/etc/gshadow 2>>/tmp/.errlog
464            sed -i "s/$ISO_USER/$USER/g" ${MOUNTPOINT}/etc/passwd 2>>/tmp/.errlog
465            sed -i "s/$ISO_USER/$USER/g" ${MOUNTPOINT}/etc/shadow 2>>/tmp/.errlog
466            mv ${MOUNTPOINT}/home/$ISO_USER ${MOUNTPOINT}/home/$USER 2>>/tmp/.errlog
467            chown -R $USER:users ${MOUNTPOINT}/home/$USER 2>>/tmp/.errlog
468         
469            # Change sudoers file to require passwords for sudo commands
470            sed -i '/%wheel ALL=(ALL) ALL/s/^#//' ${MOUNTPOINT}/etc/sudoers 2>>/tmp/.errlog
471            sed -i '/%wheel ALL=(ALL) ALL NOPASSWD: ALL/s/#%wheel ALL=(ALL) ALL NOPASSWD: ALL//' ${MOUNTPOINT}/etc/sudoers 2>>/tmp/.errlog
472            check_for_error
473         else
474            # If the live account has already been changed, create a new user account
475            arch_chroot "useradd ${USER} -m -g users -G wheel,storage,power,network,video,audio,lp -s /bin/bash" 2>/tmp/.errlog   
476            arch_chroot "passwd ${USER}" < /tmp/.passwd >/dev/null 2>>/tmp/.errlog  
477      
478            # Set up basic configuration files and ownership for new account
479            arch_chroot "cp -R /etc/skel/ /home/${USER}" 2>>/tmp/.errlog
480            arch_chroot "chown -R ${USER}:users /home/${USER}" 2>>/tmp/.errlog
481            check_for_error
482     fi
483        rm /tmp/.passwd
484 }
485
486 run_mkinitcpio() {
487         
488         clear
489
490         KERNEL=""
491
492         # If LVM and/or LUKS used, add the relevant hook(s)
493         ([[ $LVM -eq 1 ]] && [[ $LUKS -eq 0 ]]) && sed -i 's/block filesystems/block lvm2 filesystems/g' ${MOUNTPOINT}/etc/mkinitcpio.conf 2>/tmp/.errlog
494     ([[ $LVM -eq 1 ]] && [[ $LUKS -eq 1 ]]) && sed -i 's/block filesystems/block encrypt lvm2 filesystems/g' ${MOUNTPOINT}/etc/mkinitcpio.conf 2>/tmp/.errlog
495     ([[ $LVM -eq 0 ]] && [[ $LUKS -eq 1 ]]) && sed -i 's/block filesystems/block encrypt filesystems/g' ${MOUNTPOINT}/etc/mkinitcpio.conf 2>/tmp/.errlog
496     check_for_error
497     
498         arch_chroot "mkinitcpio -p linux" 2>>/tmp/.errlog
499         check_for_error
500  
501 }
502
503 ######################################################################
504 ##                                                                                                                                      ##
505 ##            System and Partitioning Functions                                         ##
506 ##                                                                                                                                      ##
507 ######################################################################
508
509
510
511 # Unmount partitions.
512 umount_partitions(){
513         
514   MOUNTED=""
515   MOUNTED=$(mount | grep "${MOUNTPOINT}" | awk '{print $3}' | sort -r)
516   swapoff -a
517   
518   for i in ${MOUNTED[@]}; do
519       umount $i >/dev/null 2>>/tmp/.errlog
520   done
521   
522   check_for_error
523
524 }
525
526 # Revised to deal with partion sizes now being displayed to the user
527 confirm_mount() {
528     if [[ $(mount | grep $1) ]]; then   
529       dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_MntStatusTitle " --infobox "$_MntStatusSucc" 0 0
530       sleep 2
531       PARTITIONS=$(echo $PARTITIONS | sed "s~${PARTITION} [0-9]*[G-M]~~" | sed "s~${PARTITION} [0-9]*\.[0-9]*[G-M]~~" | sed s~${PARTITION}$' -'~~)
532       NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS - 1 ))
533     else
534       dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_MntStatusTitle " --infobox "$_MntStatusFail" 0 0
535       sleep 2
536       prep_menu
537     fi
538 }
539
540 # This function does not assume that the formatted device is the Root installation device as 
541 # more than one device may be formatted. Root is set in the mount_partitions function.
542 select_device() {
543         
544     DEVICE=""
545     devices_list=$(lsblk -lno NAME,SIZE,TYPE | grep 'disk' | awk '{print "/dev/" $1 " " $2}' | sort -u);
546     
547     for i in ${devices_list[@]}; do
548         DEVICE="${DEVICE} ${i}"
549     done
550     
551     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_DevSelTitle " --menu "$_DevSelBody" 0 0 4 ${DEVICE} 2>${ANSWER} || prep_menu
552     DEVICE=$(cat ${ANSWER})
553  
554   }
555
556 # Finds all available partitions according to type(s) specified and generates a list 
557 # of them. This also includes partitions on different devices.
558 find_partitions() {
559
560         PARTITIONS=""
561         NUMBER_PARTITIONS=0     
562         partition_list=$(lsblk -lno NAME,SIZE,TYPE | grep $INCLUDE_PART | sed 's/part$/\/dev\//g' | sed 's/lvm$\|crypt$/\/dev\/mapper\//g' | awk '{print $3$1 " " $2}' | sort -u)
563
564     for i in ${partition_list}; do
565         PARTITIONS="${PARTITIONS} ${i}"
566         NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS + 1 ))
567     done
568     
569     # Double-partitions will be counted due to counting sizes, so fix    
570     NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS / 2 ))
571
572         # Deal with partitioning schemes appropriate to mounting, lvm, and/or luks.
573         case $INCLUDE_PART in
574         'part\|lvm\|crypt') # Deal with incorrect partitioning for main mounting function
575
576                 if ([[ $SYSTEM == "UEFI" ]] && [[ $NUMBER_PARTITIONS -lt 2 ]]) || ([[ $SYSTEM == "BIOS" ]] && [[ $NUMBER_PARTITIONS -eq 0 ]]); then
577                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_PartErrBody" 0 0
578                         create_partitions
579                 fi
580                 ;;
581         'part\|crypt') # Ensure there is at least one partition for LVM 
582                 if [[ $NUMBER_PARTITIONS -eq 0 ]]; then
583                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_LvmPartErrBody" 0 0
584                         create_partitions
585                 fi
586                 ;;
587         'part\|lvm') # Ensure there are at least two partitions for LUKS
588                 if [[ $NUMBER_PARTITIONS -lt 2 ]]; then
589                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_LuksPartErrBody" 0 0
590                         create_partitions
591                 fi
592                 ;;
593         esac
594     
595 }
596
597
598 # Create partitions.
599 create_partitions(){
600
601         # Securely destroy all data on a given device.
602         secure_wipe(){
603                 
604                 # Warn the user. If they proceed, wipe the selected device.
605                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PartOptWipe " --yesno "$_AutoPartWipeBody1 ${DEVICE} $_AutoPartWipeBody2" 0 0
606                 if [[ $? -eq 0 ]]; then
607                         
608                         clear
609                         wipe -Ifre ${DEVICE}
610                         
611                         # Alternate dd command - requires pv to be installed
612                         #dd if=/dev/zero | pv | dd of=${DEVICE} iflag=nocache oflag=direct bs=4096 2>/tmp/.errlog
613                 else
614                         create_partitions
615                 fi
616         }
617
618
619         # BIOS and UEFI
620         auto_partition(){
621                 
622                 # Provide warning to user
623                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepPartDisk " --yesno "$_AutoPartBody1 $DEVICE $_AutoPartBody2 $_AutoPartBody3" 0 0
624                 
625                 if [[ $? -eq 0 ]]; then
626                         
627                         # Find existing partitions (if any) to remove
628                         parted -s ${DEVICE} print | awk '/^ / {print $1}' > /tmp/.del_parts
629                 
630                         for del_part in $(tac /tmp/.del_parts); do
631                                 parted -s ${DEVICE} rm ${del_part} 2>/tmp/.errlog
632                                 check_for_error
633                         done
634          
635                         # Identify the partition table
636                         part_table=$(parted -s ${DEVICE} print | grep -i 'partition table' | awk '{print $3}')
637          
638                         # Create partition table if one does not already exist
639                         ([[ $SYSTEM == "BIOS" ]] && [[ $part_table != "msdos" ]]) && parted -s ${DEVICE} mklabel msdos 2>/tmp/.errlog
640                         ([[ $SYSTEM == "UEFI" ]] && [[ $part_table != "gpt" ]]) && parted -s ${DEVICE} mklabel gpt 2>/tmp/.errlog
641                         check_for_error
642                         
643                         # Create partitions (same basic partitioning scheme for BIOS and UEFI)
644                         if [[ $SYSTEM == "BIOS" ]]; then
645                                 parted -s ${DEVICE} mkpart primary ext3 1MiB 513MiB 2>/tmp/.errlog
646                                 parted -s ${DEVICE} set 1 boot on 2>>/tmp/.errlog
647                         else
648                                 parted -s ${DEVICE} mkpart primary fat32 1MiB 513MiB 2>/tmp/.errlog
649                                 parted -s ${DEVICE} set 1 esp on 2>>/tmp/.errlog
650                         fi
651                         
652                         # Create root partition
653                         parted -s ${DEVICE} mkpart primary ext3 513MiB 100% 2>>/tmp/.errlog
654                         
655                         check_for_error
656
657                         # Show created partitions
658                         lsblk ${DEVICE} -o NAME,TYPE,FSTYPE,SIZE > /tmp/.devlist
659                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title "" --textbox /tmp/.devlist 0 0
660                 else
661                         create_partitions
662                 fi
663
664         }
665
666     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title "$_PartToolTitle" --menu "$_PartToolBody" 0 0 5 \
667     "$_PartOptWipe" "BIOS & UEFI" \
668     "$_PartOptAuto" "BIOS & UEFI" \
669         "gparted" "BIOS & UEFI" \
670         "cfdisk" "BIOS/MBR" \
671         "parted" "UEFI/GPT" 2>${ANSWER}
672
673         clear
674         # If something selected
675         if [[ $(cat ${ANSWER}) != "" ]]; then
676                 if ([[ $(cat ${ANSWER}) != "$_PartOptWipe" ]] &&  [[ $(cat ${ANSWER}) != "$_PartOptAuto" ]]); then
677                         $(cat ${ANSWER}) ${DEVICE}
678                 else
679                         [[ $(cat ${ANSWER}) == "$_PartOptWipe" ]] && secure_wipe && create_partitions
680                         [[ $(cat ${ANSWER}) == "$_PartOptAuto" ]] && auto_partition
681                 fi
682         fi
683         
684 }       
685
686
687 # Set static list of filesystems rather than on-the-fly. Partially as most require additional flags, and 
688 # partially because some don't seem to be viable.
689 # Set static list of filesystems rather than on-the-fly.
690 select_filesystem(){
691
692         # prep variables
693         fs_opts=""
694         CHK_NUM=0
695
696         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_FSTitle " --menu "$_FSBody" 0 0 12 \
697         "$_FSSkip" "-" \
698         "btrfs" "mkfs.btrfs -f" \
699         "ext2" "mkfs.ext2 -q" \
700         "ext3" "mkfs.ext3 -q" \
701         "ext4" "mkfs.ext4 -q" \
702         "f2fs" "mkfs.f2fs -f" \
703         "jfs" "mkfs.jfs -q" \
704         "nilfs2" "mkfs.nilfs2 -q" \
705         "ntfs" "mkfs.ntfs -q" \
706         "reiserfs" "mkfs.reiserfs -q" \
707         "vfat" "mkfs.vfat -F32" \
708         "xfs" "mkfs.xfs -f" 2>${ANSWER} 
709         
710         case $(cat ${ANSWER}) in
711                 "$_FSSkip")     FILESYSTEM="$_FSSkip" ;;
712                 "btrfs")        FILESYSTEM="mkfs.btrfs -f"      
713                                         CHK_NUM=16
714                                         fs_opts="autodefrag compress=zlib compress=lzo compress=no compress-force=zlib compress-force=lzo discard noacl noatime nodatasum nospace_cache recovery skip_balance space_cache ssd ssd_spread"
715                                         modprobe btrfs
716                                         ;;
717                 "ext2")         FILESYSTEM="mkfs.ext2 -q" ;;
718                 "ext3")         FILESYSTEM="mkfs.ext3 -q" ;;
719                 "ext4")         FILESYSTEM="mkfs.ext4 -q"
720                                         CHK_NUM=8
721                                         fs_opts="data=journal data=writeback dealloc discard noacl noatime nobarrier nodelalloc"
722                                         ;;
723                 "f2fs")         FILESYSTEM="mkfs.f2fs -f"
724                                         fs_opts="data_flush disable_roll_forward disable_ext_identify discard fastboot flush_merge inline_xattr inline_data inline_dentry no_heap noacl nobarrier noextent_cache noinline_data norecovery"
725                                         CHK_NUM=16
726                                         modprobe f2fs
727                                         ;;
728                 "jfs")          FILESYSTEM="mkfs.jfs -q" 
729                                         CHK_NUM=4
730                                         fs_opts="discard errors=continue errors=panic nointegrity"
731                                         ;;
732                 "nilfs2")       FILESYSTEM="mkfs.nilfs2 -q" 
733                                         CHK_NUM=7
734                                         fs_opts="discard nobarrier errors=continue errors=panic order=relaxed order=strict norecovery"
735                                         ;;
736                 "ntfs")         FILESYSTEM="mkfs.ntfs -q" ;;
737                 "reiserfs") FILESYSTEM="mkfs.reiserfs -q"
738                                         CHK_NUM=5
739                                         fs_opts="acl nolog notail replayonly user_xattr"
740                                         ;;
741                 "vfat")         FILESYSTEM="mkfs.vfat -F32" ;;
742                 "xfs")          FILESYSTEM="mkfs.xfs -f" 
743                                         CHK_NUM=9
744                                         fs_opts="discard filestreams ikeep largeio noalign nobarrier norecovery noquota wsync"
745                                         ;;
746                 *)                      prep_menu ;;
747         esac
748         
749         # Warn about formatting!
750         if [[ $FILESYSTEM != $_FSSkip ]]; then
751                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_FSTitle " --yesno "\n$FILESYSTEM $PARTITION\n\n" 0 0
752                 if [[ $? -eq 0 ]]; then
753                         ${FILESYSTEM} ${PARTITION} >/dev/null 2>/tmp/.errlog
754                         check_for_error
755                 else
756                         select_filesystem
757                 fi
758         fi
759
760
761   }
762   
763 mount_partitions() {
764
765         # This subfunction allows for special mounting options to be applied for relevant fs's.
766         # Seperate subfunction for neatness.
767         mount_opts() {
768
769                 FS_OPTS=""
770                 echo "" > ${MOUNT_OPTS}
771                 
772                 for i in ${fs_opts}; do
773                         FS_OPTS="${FS_OPTS} ${i} - off"
774                 done
775
776                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $(echo $FILESYSTEM | sed "s/.*\.//g" | sed "s/-.*//g") " --checklist "$_btrfsMntBody" 0 0 $CHK_NUM \
777                 $FS_OPTS 2>${MOUNT_OPTS}
778                 
779                 # Now clean up the file
780                 sed -i 's/ /,/g' ${MOUNT_OPTS}
781                 sed -i '$s/,$//' ${MOUNT_OPTS}
782
783                 # If mount options selected, confirm choice 
784                 if [[ $(cat ${MOUNT_OPTS}) != "" ]]; then
785                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_MntStatusTitle " --yesno "\n${_btrfsMntConfBody}$(cat ${MOUNT_OPTS})\n" 10 75
786                         [[ $? -eq 1 ]] && mount_opts
787                 fi 
788            
789         }
790
791         # Subfunction to save repetition of code
792         mount_current_partition(){
793                 
794                 # Make the mount directory
795                 mkdir -p ${MOUNTPOINT}${MOUNT} 2>/tmp/.errlog
796                 
797                 # Get mounting options for appropriate filesystems
798                 [[ $fs_opts != "" ]] && mount_opts
799                         
800                 # Use special mounting options if selected, else standard mount
801                 if [[ $(cat ${MOUNT_OPTS}) != "" ]]; then
802                         mount -o $(cat ${MOUNT_OPTS}) ${PARTITION} ${MOUNTPOINT}${MOUNT} 2>>/tmp/.errlog
803                 else
804                         mount ${PARTITION} ${MOUNTPOINT}${MOUNT} 2>>/tmp/.errlog
805                 fi
806                 
807                 check_for_error
808                 confirm_mount ${MOUNTPOINT}${MOUNT}
809
810                 # Identify if mounted partition is type "crypt" (LUKS on LVM, or LUKS alone)
811                 if [[ $(lsblk -lno TYPE ${PARTITION} | grep "crypt") != "" ]]; then
812
813                         # cryptname for bootloader configuration either way
814                         LUKS=1
815                         LUKS_NAME=$(echo ${PARTITION} | sed "s~^/dev/mapper/~~g")
816
817                         # Check if LUKS on LVM (parent = lvm /dev/mapper/...) 
818                         cryptparts=$(lsblk -lno NAME,FSTYPE,TYPE | grep "lvm" | grep -i "crypto_luks" | uniq | awk '{print "/dev/mapper/"$1}')
819                         for i in ${cryptparts}; do
820                                 if [[ $(lsblk -lno NAME ${i} | grep $LUKS_NAME) != "" ]]; then
821                                         LUKS_DEV="$LUKS_DEV cryptdevice=${i}:$LUKS_NAME"
822                                         LVM=1
823                                         break;
824                                 fi
825                         done
826                         
827                         # Check if LUKS alone (parent = part /dev/...)
828                         cryptparts=$(lsblk -lno NAME,FSTYPE,TYPE | grep "part" | grep -i "crypto_luks" | uniq | awk '{print "/dev/"$1}')
829                         for i in ${cryptparts}; do
830                                 if [[ $(lsblk -lno NAME ${i} | grep $LUKS_NAME) != "" ]]; then
831                                         LUKS_UUID=$(lsblk -lno UUID,TYPE,FSTYPE ${i} | grep "part" | grep -i "crypto_luks" | awk '{print $1}')
832                                         LUKS_DEV="$LUKS_DEV cryptdevice=UUID=$LUKS_UUID:$LUKS_NAME"
833                                         break;
834                                 fi
835                         done
836                 
837                 # If LVM logical volume....
838                 elif [[ $(lsblk -lno TYPE ${PARTITION} | grep "lvm") != "" ]]; then
839                         LVM=1
840                         
841                         # First get crypt name (code above would get lv name)
842                         cryptparts=$(lsblk -lno NAME,TYPE,FSTYPE | grep "crypt" | grep -i "lvm2_member" | uniq | awk '{print "/dev/mapper/"$1}')
843                         for i in ${cryptparts}; do
844                                 if [[ $(lsblk -lno NAME ${i} | grep $(echo $PARTITION | sed "s~^/dev/mapper/~~g")) != "" ]]; then
845                                         LUKS_NAME=$(echo ${i} | sed s~/dev/mapper/~~g)
846                                         break;
847                                 fi
848                         done
849                         
850                         # Now get the device (/dev/...) for the crypt name
851                         cryptparts=$(lsblk -lno NAME,FSTYPE,TYPE | grep "part" | grep -i "crypto_luks" | uniq | awk '{print "/dev/"$1}')
852                         for i in ${cryptparts}; do
853                                 if [[ $(lsblk -lno NAME ${i} | grep $LUKS_NAME) != "" ]]; then
854                                         # Create UUID for comparison
855                                         LUKS_UUID=$(lsblk -lno UUID,TYPE,FSTYPE ${i} | grep "part" | grep -i "crypto_luks" | awk '{print $1}')
856                                         
857                                         # Check if not already added as a LUKS DEVICE (i.e. multiple LVs on one crypt). If not, add.
858                                         if [[ $(echo $LUKS_DEV | grep $LUKS_UUID) == "" ]]; then
859                                                 LUKS_DEV="$LUKS_DEV cryptdevice=UUID=$LUKS_UUID:$LUKS_NAME"
860                                                 LUKS=1
861                                         fi
862                                         
863                                         break;
864                                 fi
865                         done
866                 fi
867
868                 
869         }
870
871         # Seperate function due to ability to cancel
872         make_swap(){
873
874                 # Ask user to select partition or create swapfile
875                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --menu "$_SelSwpBody" 0 0 7 "$_SelSwpNone" $"-" "$_SelSwpFile" $"-" ${PARTITIONS} 2>${ANSWER} || prep_menu  
876                 
877                 if [[ $(cat ${ANSWER}) != "$_SelSwpNone" ]]; then    
878                         PARTITION=$(cat ${ANSWER})
879                 
880                         if [[ $PARTITION == "$_SelSwpFile" ]]; then
881                                 total_memory=$(grep MemTotal /proc/meminfo | awk '{print $2/1024}' | sed 's/\..*//')
882                                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SelSwpFile " --inputbox "\nM = MB, G = GB\n" 9 30 "${total_memory}M" 2>${ANSWER} || make_swap
883                                 m_or_g=$(cat ${ANSWER})
884                 
885                                 while [[ $(echo ${m_or_g: -1} | grep "M\|G") == "" ]]; do
886                                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SelSwpFile " --msgbox "\n$_SelSwpFile $_ErrTitle: M = MB, G = GB\n\n" 0 0
887                                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SelSwpFile " --inputbox "\nM = MB, G = GB\n" 9 30 "${total_memory}M" 2>${ANSWER} || make_swap
888                                         m_or_g=$(cat ${ANSWER})
889                                 done
890
891                                 fallocate -l ${m_or_g} ${MOUNTPOINT}/swapfile 2>/tmp/.errlog
892                                 chmod 600 ${MOUNTPOINT}/swapfile 2>>/tmp/.errlog
893                                 mkswap ${MOUNTPOINT}/swapfile 2>>/tmp/.errlog
894                                 swapon ${MOUNTPOINT}/swapfile 2>>/tmp/.errlog
895                                 check_for_error
896                                 
897                         else # Swap Partition
898                                 # Warn user if creating a new swap
899                                 if [[ $(lsblk -o FSTYPE  ${PARTITION} | grep -i "swap") != "swap" ]]; then
900                                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --yesno "\nmkswap ${PARTITION}\n\n" 0 0
901                                         [[ $? -eq 0 ]] && mkswap ${PARTITION} >/dev/null 2>/tmp/.errlog || mount_partitions
902                                 fi
903                                 # Whether existing to newly created, activate swap
904                                 swapon  ${PARTITION} >/dev/null 2>>/tmp/.errlog
905                                 check_for_error
906                                 # Since a partition was used, remove that partition from the list
907                                 PARTITIONS=$(echo $PARTITIONS | sed "s~${PARTITION} [0-9]*[G-M]~~" | sed "s~${PARTITION} [0-9]*\.[0-9]*[G-M]~~" | sed s~${PARTITION}$' -'~~)
908                                 NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS - 1 ))
909                         fi
910                 fi
911
912         }
913
914         ####                                                            ####
915         #### MOUNTING FUNCTION BEGINS HERE  ####
916         ####                                                            ####
917
918         # prep variables
919         MOUNT=""
920         LUKS_NAME=""
921         LUKS_DEV=""
922         LUKS_UUID=""
923         LUKS=0
924         LVM=0
925         
926         # Warn users that they CAN mount partitions without formatting them!
927         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --msgbox "$_WarnMount1 '$_FSSkip' $_WarnMount2" 0 0
928
929     # LVM Detection. If detected, activate.
930     lvm_detect
931
932         # Ensure partitions are unmounted (i.e. where mounted previously), and then list available partitions
933         INCLUDE_PART='part\|lvm\|crypt'
934     umount_partitions
935         find_partitions
936         
937         # Identify and mount root
938         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --menu "$_SelRootBody" 0 0 7 ${PARTITIONS} 2>${ANSWER} || prep_menu
939         PARTITION=$(cat ${ANSWER})
940     ROOT_PART=${PARTITION}
941         
942         # Format with FS (or skip)
943         select_filesystem
944                 
945         # Make the directory and mount. Also identify LUKS and/or LVM
946         mount_current_partition
947
948         # Identify and create swap, if applicable
949         make_swap
950         
951     # Extra Step for VFAT UEFI Partition. This cannot be in an LVM container.
952     if [[ $SYSTEM == "UEFI" ]]; then
953     
954        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --menu "$_SelUefiBody" 0 0 7 ${PARTITIONS} 2>${ANSWER} || prep_menu  
955        PARTITION=$(cat ${ANSWER})
956        UEFI_PART=${PARTITION}
957        
958        # If it is already a fat/vfat partition...
959        if [[ $(fsck -N $PARTITION | grep fat) ]]; then
960           dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --yesno "$_FormUefiBody $PARTITION $_FormUefiBody2" 0 0 && mkfs.vfat -F32 ${PARTITION} >/dev/null 2>/tmp/.errlog
961        else 
962           mkfs.vfat -F32 ${PARTITION} >/dev/null 2>/tmp/.errlog
963        fi
964        check_for_error
965              
966        # Inform users of the mountpoint options and consequences       
967        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --menu "$_MntUefiBody"  0 0 2 \
968            "/boot" "systemd-boot"\
969            "/boot/efi" "-" 2>${ANSWER}
970                    
971            [[ $(cat ${ANSWER}) != "" ]] && UEFI_MOUNT=$(cat ${ANSWER}) || prep_menu
972        
973        mkdir -p ${MOUNTPOINT}${UEFI_MOUNT} 2>/tmp/.errlog
974        mount ${PARTITION} ${MOUNTPOINT}${UEFI_MOUNT} 2>>/tmp/.errlog
975        check_for_error
976        confirm_mount ${MOUNTPOINT}${UEFI_MOUNT}           
977     fi
978     
979     # All other partitions
980         while [[ $NUMBER_PARTITIONS > 0 ]]; do 
981                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart " --menu "$_ExtPartBody" 0 0 7 "$_Done" $"-" ${PARTITIONS} 2>${ANSWER} || prep_menu 
982                 PARTITION=$(cat ${ANSWER})
983              
984                 if [[ $PARTITION == $_Done ]]; then
985                         break;
986                 else
987                         MOUNT=""
988                         select_filesystem
989                  
990                         # Ask user for mountpoint. Don't give /boot as an example for UEFI systems!
991                         [[ $SYSTEM == "UEFI" ]] && MNT_EXAMPLES="/home\n/var" || MNT_EXAMPLES="/boot\n/home\n/var"
992                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart $PARTITON " --inputbox "$_ExtPartBody1$MNT_EXAMPLES\n" 0 0 "/" 2>${ANSWER} || prep_menu
993                         MOUNT=$(cat ${ANSWER})
994                 
995                         # loop while the mountpoint specified is incorrect (is only '/', is blank, or has spaces). 
996                         while [[ ${MOUNT:0:1} != "/" ]] || [[ ${#MOUNT} -le 1 ]] || [[ $MOUNT =~ \ |\' ]]; do
997                                 # Warn user about naming convention
998                                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_ExtErrBody" 0 0
999                                 # Ask user for mountpoint again
1000                                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMntPart $PARTITON " --inputbox "$_ExtPartBody1$MNT_EXAMPLES\n" 0 0 "/" 2>${ANSWER} || prep_menu
1001                                 MOUNT=$(cat ${ANSWER})                     
1002                         done
1003
1004                         # Create directory and mount.
1005                         mount_current_partition
1006                 
1007                         # Determine if a seperate /boot is used. 0 = no seperate boot, 1 = seperate non-lvm boot, 
1008                         # 2 = seperate lvm boot. For Grub configuration
1009                         if  [[ $MOUNT == "/boot" ]]; then
1010                                 [[ $(lsblk -lno TYPE ${PARTITION} | grep "lvm") != "" ]] && LVM_SEP_BOOT=2 || LVM_SEP_BOOT=1
1011                         fi
1012                 
1013                 fi
1014         done
1015 }       
1016
1017 ######################################################################
1018 ##                                                                                                                                      ##
1019 ##             Encryption (dm_crypt) Functions                                  ##
1020 ##                                                                                                                                      ##
1021 ######################################################################
1022
1023 # Had to write it in this way due to (bash?) bug(?), as if/then statements in a single
1024 # "create LUKS" function for default and "advanced" modes were interpreted as commands,
1025 # not mere string statements. Not happy with it, but it works...
1026
1027 # Save repetition of code.
1028 luks_password(){
1029
1030     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLUKS " --clear --insecure --passwordbox "$_LuksPassBody" 0 0 2> ${ANSWER} || prep_menu
1031     PASSWD=$(cat ${ANSWER})
1032     
1033     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLUKS " --clear --insecure --passwordbox "$_PassReEntBody" 0 0 2> ${ANSWER} || prep_menu
1034     PASSWD2=$(cat ${ANSWER})
1035     
1036     if [[ $PASSWD != $PASSWD2 ]]; then 
1037        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_PassErrBody" 0 0
1038        luks_password
1039     fi
1040
1041 }
1042
1043 luks_open(){
1044
1045         LUKS_ROOT_NAME=""
1046         INCLUDE_PART='part\|crypt\|lvm'
1047     umount_partitions
1048     find_partitions
1049         
1050         # Select encrypted partition to open 
1051         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksOpen " --menu "$_LuksMenuBody" 0 0 7 ${PARTITIONS} 2>${ANSWER} || luks_menu
1052         PARTITION=$(cat ${ANSWER})
1053         
1054         # Enter name of the Luks partition and get password to open it
1055         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksOpen " --inputbox "$_LuksOpenBody" 10 50 "cryptroot" 2>${ANSWER} || luks_menu
1056     LUKS_ROOT_NAME=$(cat ${ANSWER})
1057         luks_password
1058         
1059         # Try to open the luks partition with the credentials given. If successful show this, otherwise
1060         # show the error
1061         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksOpen " --infobox "$_PlsWaitBody" 0 0
1062         echo $PASSWD | cryptsetup open --type luks ${PARTITION} ${LUKS_ROOT_NAME} 2>/tmp/.errlog
1063         check_for_error
1064
1065         lsblk -o NAME,TYPE,FSTYPE,SIZE,MOUNTPOINT ${PARTITION} | grep "crypt\|NAME\|MODEL\|TYPE\|FSTYPE\|SIZE" > /tmp/.devlist
1066     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_DevShowOpt " --textbox /tmp/.devlist 0 0
1067         
1068         luks_menu
1069 }
1070
1071 luks_setup(){
1072
1073         modprobe -a dm-mod dm_crypt
1074         INCLUDE_PART='part\|lvm'
1075     umount_partitions
1076         find_partitions
1077         
1078         # Select partition to encrypt 
1079         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksEncrypt " --menu "$_LuksCreateBody" 0 0 7 ${PARTITIONS} 2>${ANSWER} || luks_menu
1080         PARTITION=$(cat ${ANSWER})
1081
1082         # Enter name of the Luks partition and get password to create it
1083         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksEncrypt " --inputbox "$_LuksOpenBody" 10 50 "cryptroot" 2>${ANSWER} || luks_menu
1084     LUKS_ROOT_NAME=$(cat ${ANSWER})
1085     luks_password
1086 }
1087
1088 luks_default() {
1089         
1090     # Encrypt selected partition or LV with credentials given
1091         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksEncrypt " --infobox "$_PlsWaitBody" 0 0
1092         sleep 2
1093         echo $PASSWD | cryptsetup -q luksFormat ${PARTITION} 2>/tmp/.errlog
1094         
1095         # Now open the encrypted partition or LV
1096         echo $PASSWD | cryptsetup open ${PARTITION} ${LUKS_ROOT_NAME} 2>/tmp/.errlog
1097         check_for_error
1098         
1099 }
1100
1101 luks_key_define() {
1102         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLUKS " --inputbox "$_LuksCipherKey" 0 0 "-s 512 -c aes-xts-plain64" 2>${ANSWER} || luks_menu
1103
1104         # Encrypt selected partition or LV with credentials given
1105         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksEncryptAdv " --infobox "$_PlsWaitBody" 0 0
1106         sleep 2
1107
1108         echo $PASSWD | cryptsetup -q $(cat ${ANSWER}) luksFormat ${PARTITION} 2>/tmp/.errlog
1109         check_for_error
1110
1111         # Now open the encrypted partition or LV
1112         echo $PASSWD | cryptsetup open ${PARTITION} ${LUKS_ROOT_NAME} 2>/tmp/.errlog
1113         check_for_error
1114
1115 }
1116
1117 luks_show(){
1118         
1119         echo -e ${_LuksEncruptSucc} > /tmp/.devlist
1120         lsblk -o NAME,TYPE,FSTYPE,SIZE ${PARTITION} | grep "part\|crypt\|NAME\|TYPE\|FSTYPE\|SIZE" >> /tmp/.devlist
1121     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LuksEncrypt " --textbox /tmp/.devlist 0 0
1122
1123         luks_menu
1124 }
1125
1126 luks_menu() {
1127
1128         LUKS_OPT=""
1129
1130         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLUKS " --menu "$_LuksMenuBody$_LuksMenuBody2$_LuksMenuBody3" 0 0 4 \
1131         "$_LuksOpen" "cryptsetup open --type luks" \
1132         "$_LuksEncrypt" "cryptsetup -q luksFormat" \
1133         "$_LuksEncryptAdv" "cryptsetup -q -s -c luksFormat" \
1134         "$_Back" "-" 2>${ANSWER}
1135
1136         case $(cat ${ANSWER}) in
1137                 "$_LuksOpen")           luks_open ;;
1138                 "$_LuksEncrypt")        luks_setup
1139                                                         luks_default
1140                                                         luks_show ;;
1141                 "$_LuksEncryptAdv")     luks_setup
1142                                                         luks_key_define
1143                                                         luks_show ;;
1144                 *)                                      prep_menu ;;
1145         esac
1146
1147         luks_menu       
1148
1149 }
1150
1151
1152 ######################################################################
1153 ##                                                                                                                                      ##
1154 ##             Logical Volume Management Functions                              ##
1155 ##                                                                                                                                      ##
1156 ######################################################################
1157
1158 # LVM Detection.
1159 lvm_detect() {
1160         
1161   LVM_PV=$(pvs -o pv_name --noheading 2>/dev/null)
1162   LVM_VG=$(vgs -o vg_name --noheading 2>/dev/null)
1163   LVM_LV=$(lvs -o vg_name,lv_name --noheading --separator - 2>/dev/null)
1164   
1165         if [[ $LVM_LV != "" ]] && [[ $LVM_VG != "" ]] && [[ $LVM_PV != "" ]]; then
1166                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLVM " --infobox "$_LvmDetBody" 0 0
1167                 modprobe dm-mod 2>/tmp/.errlog
1168                 check_for_error
1169                 vgscan >/dev/null 2>&1
1170                 vgchange -ay >/dev/null 2>&1
1171         fi
1172 }
1173
1174 lvm_show_vg(){
1175
1176         VG_LIST=""
1177         vg_list=$(lvs --noheadings | awk '{print $2}' | uniq)
1178
1179         for i in ${vg_list}; do
1180                 VG_LIST="${VG_LIST} ${i} $(vgdisplay ${i} | grep -i "vg size" | awk '{print $3$4}')"
1181         done
1182
1183         # If no VGs, no point in continuing
1184         if [[ $VG_LIST == "" ]]; then
1185                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_LvmVGErr" 0 0
1186                 lvm_menu
1187         fi
1188
1189         # Select VG
1190         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLVM " --menu "$_LvmSelVGBody" 0 0 5 \
1191         ${VG_LIST} 2>${ANSWER} || lvm_menu
1192 }
1193
1194 # Create Volume Group and Logical Volumes
1195 lvm_create() {
1196
1197 # subroutine to save a lot of repetition.
1198 check_lv_size() {
1199
1200         LV_SIZE_INVALID=0
1201         chars=0
1202
1203         # Check to see if anything was actually entered and if first character is '0'
1204         ([[ ${#LVM_LV_SIZE} -eq 0 ]] || [[ ${LVM_LV_SIZE:0:1} -eq "0" ]]) && LV_SIZE_INVALID=1
1205
1206         # If not invalid so far, check for non numberic characters other than the last character
1207         if [[ $LV_SIZE_INVALID -eq 0 ]]; then
1208                 while [[ $chars -lt $(( ${#LVM_LV_SIZE} - 1 )) ]]; do
1209                         [[ ${LVM_LV_SIZE:chars:1} != [0-9] ]] && LV_SIZE_INVALID=1 && break;
1210                         chars=$(( chars + 1 ))
1211                 done
1212         fi
1213         
1214         # If not invalid so far, check that last character is a M/m or G/g
1215         if [[ $LV_SIZE_INVALID -eq 0 ]]; then
1216                 LV_SIZE_TYPE=$(echo ${LVM_LV_SIZE:$(( ${#LVM_LV_SIZE} - 1 )):1})
1217                 
1218                 case $LV_SIZE_TYPE in
1219                 "m"|"M"|"g"|"G") LV_SIZE_INVALID=0 ;;
1220                 *) LV_SIZE_INVALID=1 ;;
1221                 esac
1222                 
1223         fi
1224
1225         # If not invalid so far, check whether the value is greater than or equal to the LV remaining Size.
1226         # If not, convert into MB for VG space remaining.      
1227         if [[ ${LV_SIZE_INVALID} -eq 0 ]]; then
1228   
1229                 case ${LV_SIZE_TYPE} in
1230                 "G"|"g") if [[ $(( $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) * 1000 )) -ge ${LVM_VG_MB} ]]; then
1231                                         LV_SIZE_INVALID=1
1232                                  else
1233                                         LVM_VG_MB=$(( LVM_VG_MB - $(( $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) * 1000 )) ))
1234                                  fi
1235                                  ;;
1236                 "M"|"m") if [[ $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) -ge ${LVM_VG_MB} ]]; then
1237                                         LV_SIZE_INVALID=1
1238                                  else
1239                                         LVM_VG_MB=$(( LVM_VG_MB - $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) ))
1240                                  fi
1241                                  ;;
1242                 *) LV_SIZE_INVALID=1
1243                                  ;;
1244                 esac
1245
1246         fi  
1247
1248 }
1249
1250         #                                                       #
1251         # LVM Create Starts Here        #
1252         #                                                       #
1253
1254         # Prep Variables
1255         LVM_VG=""
1256         VG_PARTS=""
1257         LVM_VG_MB=0
1258
1259         # Find LVM appropriate partitions.
1260         INCLUDE_PART='part\|crypt'
1261         umount_partitions
1262         find_partitions
1263     # Amend partition(s) found for use in check list
1264     PARTITIONS=$(echo $PARTITIONS | sed 's/M\|G\|T/& off/g')
1265     
1266     # Name the Volume Group
1267     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --inputbox "$_LvmNameVgBody" 0 0 "" 2>${ANSWER} || prep_menu
1268     LVM_VG=$(cat ${ANSWER})
1269
1270     # Loop while the Volume Group name starts with a "/", is blank, has spaces, or is already being used
1271     while [[ ${LVM_VG:0:1} == "/" ]] || [[ ${#LVM_VG} -eq 0 ]] || [[ $LVM_VG =~ \ |\' ]] || [[ $(lsblk | grep ${LVM_VG}) != "" ]]; do
1272         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title "$_ErrTitle" --msgbox "$_LvmNameVgErr" 0 0
1273               
1274         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --inputbox "$_LvmNameVgBody" 0 0 "" 2>${ANSWER} || prep_menu
1275         LVM_VG=$(cat ${ANSWER})
1276     done
1277     
1278     # Select the partition(s) for the Volume Group
1279     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --checklist "$_LvmPvSelBody $_UseSpaceBar" 0 0 7 ${PARTITIONS} 2>${ANSWER} || prep_menu 
1280     [[ $(cat ${ANSWER}) != "" ]] && VG_PARTS=$(cat ${ANSWER}) || prep_menu
1281     
1282     # Once all the partitions have been selected, show user. On confirmation, use it/them in 'vgcreate' command.
1283     # Also determine the size of the VG, to use for creating LVs for it.
1284     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --yesno "$_LvmPvConfBody1${LVM_VG} $_LvmPvConfBody2${VG_PARTS}" 0 0
1285     
1286     if [[ $? -eq 0 ]]; then
1287        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --infobox "$_LvmPvActBody1${LVM_VG}.$_PlsWaitBody" 0 0
1288        sleep 1
1289        vgcreate -f ${LVM_VG} ${VG_PARTS} >/dev/null 2>/tmp/.errlog
1290        check_for_error
1291        
1292                 # Once created, get size and size type for display and later number-crunching for lv creation
1293                 VG_SIZE=$(vgdisplay $LVM_VG | grep 'VG Size' | awk '{print $3}' | sed 's/\..*//')
1294                 VG_SIZE_TYPE=$(vgdisplay $LVM_VG | grep 'VG Size' | awk '{print $4}')
1295
1296                 # Convert the VG size into GB and MB. These variables are used to keep tabs on space available and remaining
1297                 [[ ${VG_SIZE_TYPE:0:1} == "G" ]] && LVM_VG_MB=$(( VG_SIZE * 1000 )) || LVM_VG_MB=$VG_SIZE
1298        
1299        dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --msgbox "$_LvmPvDoneBody1 '${LVM_VG}' $_LvmPvDoneBody2 (${VG_SIZE} ${VG_SIZE_TYPE}).\n\n" 0 0
1300     else
1301        lvm_menu
1302     fi
1303
1304         #
1305         # Once VG created, create Logical Volumes
1306         #
1307         
1308         # Specify number of Logical volumes to create.
1309         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --radiolist "$_LvmLvNumBody1 ${LVM_VG}. $_LvmLvNumBody2" 0 0 9 \
1310         "1" "-" off "2" "-" off "3" "-" off "4" "-" off "5" "-" off "6" "-" off "7" "-" off "8" "-" off "9 " "-" off 2>${ANSWER}
1311     
1312         [[ $(cat ${ANSWER}) == "" ]] && lvm_menu || NUMBER_LOGICAL_VOLUMES=$(cat ${ANSWER})
1313
1314         # Loop while the number of LVs is greater than 1. This is because the size of the last LV is automatic.
1315         while [[ $NUMBER_LOGICAL_VOLUMES -gt 1 ]]; do
1316                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "$_LvmLvNameBody1" 0 0 "lvol" 2>${ANSWER} || prep_menu
1317                 LVM_LV_NAME=$(cat ${ANSWER})
1318
1319                 # Loop if preceeded with a "/", if nothing is entered, if there is a space, or if that name already exists.
1320                 while [[ ${LVM_LV_NAME:0:1} == "/" ]] || [[ ${#LVM_LV_NAME} -eq 0 ]] || [[ ${LVM_LV_NAME} =~ \ |\' ]] || [[ $(lsblk | grep ${LVM_LV_NAME}) != "" ]]; do
1321                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_LvmLvNameErrBody" 0 0
1322                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "$_LvmLvNameBody1" 0 0 "lvol" 2>${ANSWER} || prep_menu
1323                         LVM_LV_NAME=$(cat ${ANSWER})
1324                 done
1325
1326                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n${LVM_VG}: ${VG_SIZE}${VG_SIZE_TYPE} (${LVM_VG_MB}MB $_LvmLvSizeBody1).$_LvmLvSizeBody2" 0 0 "" 2>${ANSWER} || prep_menu
1327                 LVM_LV_SIZE=$(cat ${ANSWER})          
1328                 check_lv_size 
1329           
1330                 # Loop while an invalid value is entered.
1331                 while [[ $LV_SIZE_INVALID -eq 1 ]]; do
1332                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_LvmLvSizeErrBody" 0 0
1333                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n${LVM_VG}: ${VG_SIZE}${VG_SIZE_TYPE} (${LVM_VG_MB}MB $_LvmLvSizeBody1).$_LvmLvSizeBody2" 0 0 "" 2>${ANSWER} || prep_menu
1334                         LVM_LV_SIZE=$(cat ${ANSWER})          
1335                         check_lv_size
1336                 done
1337           
1338                 # Create the LV
1339                 lvcreate -L ${LVM_LV_SIZE} ${LVM_VG} -n ${LVM_LV_NAME} 2>/tmp/.errlog
1340                 check_for_error
1341                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --msgbox "\n$_Done\n\nLV ${LVM_LV_NAME} (${LVM_LV_SIZE}) $_LvmPvDoneBody2.\n\n" 0 0
1342                 NUMBER_LOGICAL_VOLUMES=$(( NUMBER_LOGICAL_VOLUMES - 1 ))
1343         done
1344     
1345         # Now the final LV. Size is automatic.      
1346         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "$_LvmLvNameBody1 $_LvmLvNameBody2 (${LVM_VG_MB}MB)." 0 0 "lvol" 2>${ANSWER} || prep_menu
1347         LVM_LV_NAME=$(cat ${ANSWER})
1348
1349         # Loop if preceeded with a "/", if nothing is entered, if there is a space, or if that name already exists.
1350         while [[ ${LVM_LV_NAME:0:1} == "/" ]] || [[ ${#LVM_LV_NAME} -eq 0 ]] || [[ ${LVM_LV_NAME} =~ \ |\' ]] || [[ $(lsblk | grep ${LVM_LV_NAME}) != "" ]]; do
1351                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_LvmLvNameErrBody" 0 0
1352                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "$_LvmLvNameBody1 $_LvmLvNameBody2 (${LVM_VG_MB}MB)." 0 0 "lvol" 2>${ANSWER} || prep_menu
1353                 LVM_LV_NAME=$(cat ${ANSWER})
1354         done
1355
1356         # Create the final LV
1357         lvcreate -l +100%FREE ${LVM_VG} -n ${LVM_LV_NAME} 2>/tmp/.errlog
1358         check_for_error
1359         NUMBER_LOGICAL_VOLUMES=$(( NUMBER_LOGICAL_VOLUMES - 1 ))
1360         LVM=1
1361         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmCreateVG " --yesno "$_LvmCompBody" 0 0 \
1362         && show_devices || lvm_menu
1363
1364 }
1365
1366 lvm_del_vg(){
1367
1368         # Generate list of VGs for selection
1369         lvm_show_vg
1370         
1371         # Ask for confirmation
1372         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmDelVG " --yesno "$_LvmDelQ" 0 0
1373         
1374         # if confirmation given, delete
1375         if [[ $? -eq 0 ]]; then
1376                 vgremove -f $(cat ${ANSWER}) >/dev/null 2>&1
1377         fi
1378         
1379         lvm_menu
1380 }
1381
1382 lvm_del_all(){
1383
1384         LVM_PV=$(pvs -o pv_name --noheading 2>/dev/null)
1385         LVM_VG=$(vgs -o vg_name --noheading 2>/dev/null)
1386         LVM_LV=$(lvs -o vg_name,lv_name --noheading --separator - 2>/dev/null)
1387         
1388         # Ask for confirmation
1389         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_LvmDelLV " --yesno "$_LvmDelQ" 0 0
1390         
1391         # if confirmation given, delete
1392         if [[ $? -eq 0 ]]; then
1393         
1394                 for i in ${LVM_LV}; do
1395                         lvremove -f /dev/mapper/${i} >/dev/null 2>&1
1396                 done
1397
1398                 for i in ${LVM_VG}; do
1399                         vgremove -f ${i} >/dev/null 2>&1
1400                 done
1401
1402                 for i in ${LV_PV}; do
1403                         pvremove -f ${i} >/dev/null 2>&1
1404                 done
1405
1406         fi
1407         
1408         lvm_menu
1409 }
1410
1411 lvm_menu(){
1412
1413         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLVM $_PrepLVM2 " --infobox "$_PlsWaitBody" 0 0
1414         sleep 1
1415         lvm_detect
1416
1417         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepLVM $_PrepLVM2 " --menu "$_LvmMenu" 0 0 4 \
1418         "$_LvmCreateVG" "vgcreate -f, lvcreate -L -n" \
1419         "$_LvmDelVG" "vgremove -f" \
1420         "$_LvMDelAll" "lvrmeove, vgremove, pvremove -f" \
1421         "$_Back" "-" 2>${ANSWER}
1422         
1423         case $(cat ${ANSWER}) in
1424                 "$_LvmCreateVG")        lvm_create ;;
1425                 "$_LvmDelVG")           lvm_del_vg ;;
1426                 "$_LvMDelAll")          lvm_del_all ;;
1427                 *)                                      prep_menu ;;
1428         esac
1429
1430
1431 }
1432
1433 ######################################################################
1434 ##                                                                                                                                      ##
1435 ##                    Installation Functions                                            ##
1436 ##                                                                                                                                      ##
1437 ######################################################################  
1438
1439 install_root(){
1440
1441   clear
1442
1443   # Change installation method depending on use of img or sfs
1444   if [[ -e /run/archiso/sfs/airootfs/airootfs.img ]]; then
1445      AIROOTIMG="/run/archiso/sfs/airootfs/airootfs.img"
1446      mkdir -p ${BYPASS} 2>/tmp/.errlog
1447          mount ${AIROOTIMG} ${BYPASS} 2>>/tmp/.errlog
1448          rsync -a --progress ${BYPASS} ${MOUNTPOINT}/ 2>>/tmp/.errlog
1449          umount -l ${BYPASS}
1450   else
1451      AIROOTIMG="/run/archiso/sfs/airootfs/"
1452      rsync -a --progress ${AIROOTIMG} ${MOUNTPOINT}/ 2>/tmp/.errlog
1453   fi
1454
1455   check_for_error
1456   
1457   # Keyboard config for vc and x11
1458   [[ -e /tmp/vconsole.conf ]] && cp /tmp/vconsole.conf ${MOUNTPOINT}/etc/vconsole.conf 2>>/tmp/.errlog
1459   [[ -e /tmp/01-keyboard-layout.conf ]] && cp -f /tmp/01-keyboard-layout.conf ${MOUNTPOINT}/etc/X11/xorg.conf.d/$(ls ${MOUNTPOINT}/etc/X11/xorg.conf.d/ | grep "keyboard") 2>>/tmp/.errlog
1460   
1461   # set up kernel for mkiniticpio
1462   cp /run/archiso/bootmnt/arch/boot/${ARCHI}/vmlinuz ${MOUNTPOINT}/boot/vmlinuz-linux 2>>/tmp/.errlog
1463
1464   # copy over new mirrorlist
1465   cp /etc/pacman.d/mirrorlist ${MOUNTPOINT}/etc/pacman.d/mirrorlist 2>>/tmp/.errlog
1466   
1467   # Clean up installation
1468   [[ -d ${MOUNTPOINT}${TRANS_SRC} ]] && rm -R ${MOUNTPOINT}${TRANS_SRC}>>/tmp/.errlog
1469   rm -rf ${MOUNTPOINT}/vomi 2>>/tmp/.errlog
1470   rm -rf ${BYPASS} 2>>/tmp/.errlog
1471   rm -rf ${MOUNTPOINT}/source 2>>/tmp/.errlog
1472   rm -rf ${MOUNTPOINT}/src 2>>/tmp/.errlog
1473   rmdir ${MOUNTPOINT}/bypass 2>>/tmp/.errlog
1474   rmdir ${MOUNTPOINT}/src 2>>/tmp/.errlog
1475   rmdir ${MOUNTPOINT}/source 2>>/tmp/.errlog
1476   rm -f ${MOUNTPOINT}/etc/sudoers.d/g_wheel 2>>/tmp/.errlog
1477   rm -f ${MOUNTPOINT}/var/lib/NetworkManager/NetworkManager.state 2>>/tmp/.errlog
1478   rm -f ${MOUNTPOINT}/update-pbif 2>>/tmp/.errlog
1479   sed -i 's/.*pam_wheel\.so/#&/' ${MOUNTPOINT}/etc/pam.d/su 2>>/tmp/.errlog
1480  
1481   # clean out archiso files from install
1482   find ${MOUNTPOINT}/usr/lib/initcpio -name archiso* -type f -exec rm '{}' \;
1483  
1484   # systemd
1485   rm -R ${MOUNTPOINT}/etc/systemd/system/getty@tty1.service.d 2>>/tmp/.errlog
1486   rm ${MOUNTPOINT}/etc/systemd/system/default.target 2>>/tmp/.errlog
1487
1488   # Journal
1489   sed -i 's/volatile/auto/g' ${MOUNTPOINT}/etc/systemd/journald.conf 2>>/tmp/.errlog
1490  
1491   # Stop pacman complaining
1492   arch_chroot "mkdir -p /var/lib/pacman/sync" 2>>/tmp/.errlog
1493   arch_chroot "touch /var/lib/pacman/sync/{core.db,extra.db,community.db}" 2>>/tmp/.errlog
1494
1495   # Fix NetworkManager
1496   arch_chroot "systemctl enable NetworkManager -f" 2>>/tmp/.errlog
1497
1498   # Keyboard config for vc and x11
1499   [[ -e /tmp/vconsole.conf ]] && cp /tmp/vconsole.conf ${MOUNTPOINT}/etc/vconsole.conf 2>>/tmp/.errlog
1500   [[ -e /tmp/01-keyboard-layout.conf ]] && cp -f /tmp/01-keyboard-layout.conf ${MOUNTPOINT}/etc/X11/xorg.conf.d/$(ls ${MOUNTPOINT}/etc/X11/xorg.conf.d/ | grep "keyboard") 2>>/tmp/.errlog
1501   
1502   # Display Manager
1503   arch_chroot "systemctl enable lxdm" 2>>/tmp/.errlog
1504
1505   # Copy our predefined lxdm.conf
1506   cp -f /inst/lxdm.conf ${MOUNTPOINT}/etc/lxdm/lxdm.conf 2>>/tmp/.errlog
1507
1508   # Clean install files
1509   [[ -d ${MOUNTPOINT}/inst ]] && rm -R ${MOUNTPOINT}/inst &> /dev/null 2>>/tmp/.errlog
1510
1511   check_for_error
1512   
1513   # Virtualbox Guest
1514   [[ $(lspci | grep -i "vga" | sed 's/.*://' | sed 's/(.*//' | sed 's/^[ \t]*//' | grep -i "virtualbox") != "" ]] && arch_chroot "systemctl enable vboxservice"
1515   
1516 }
1517
1518 # Install Bootloader
1519 install_bootloader() {
1520
1521 bios_bootloader() {     
1522         
1523    dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title "$_InstBiosBtTitle" --menu "$_InstBiosBtBody" 0 0 3 \
1524         "grub" "-" "syslinux [MBR]" "-" "syslinux [/]" "-" 2>${ANSWER}
1525         
1526         if [[ $(cat ${ANSWER}) == "grub" ]];then
1527                 select_device
1528                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " Grub-install " --infobox "$_PlsWaitBody" 0 0
1529                 arch_chroot "grub-install --target=i386-pc --recheck $DEVICE" 2>/tmp/.errlog
1530                 check_for_error
1531                 
1532                 arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg" 2>/tmp/.errlog
1533                 check_for_error
1534
1535                 # if /boot is LVM (whether using a seperate /boot mount or not), amend grub
1536                 if ( [[ $LVM -eq 1 ]] && [[ $LVM_SEP_BOOT -eq 0 ]] ) || [[ $LVM_SEP_BOOT -eq 2 ]]; then
1537                         sed -i "s/GRUB_PRELOAD_MODULES=\"\"/GRUB_PRELOAD_MODULES=\"lvm\"/g" ${MOUNTPOINT}/etc/default/grub
1538                 fi
1539
1540                 # If encryption used amend grub
1541                 [[ $LUKS_DEV != "" ]] && sed -i "s~GRUB_CMDLINE_LINUX=.*~GRUB_CMDLINE_LINUX=\"$LUKS_DEV\"~g" ${MOUNTPOINT}/etc/default/grub
1542                                 
1543                 arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg" 2>>/tmp/.errlog
1544                 check_for_error
1545                 BOOTLOADER="grub"
1546                 
1547         elif ([[ $(cat ${ANSWER}) == "syslinux [MBR]" ]] || [[ $(cat ${ANSWER}) == "syslinux [/]" ]]);then
1548                 [[ $(cat ${ANSWER}) == "syslinux [MBR]" ]] && arch_chroot "syslinux-install_update -iam" 2>/tmp/.errlog
1549                 [[ $(cat ${ANSWER}) == "syslinux [/]" ]] && arch_chroot "syslinux-install_update -i" 2>/tmp/.errlog
1550                 check_for_error
1551              
1552                 # Amend configuration file. First remove all existing entries, then input new ones.     
1553                 sed -i '/^LABEL.*$/,$d' ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1554                                 
1555                 # First the "main" entries
1556                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux\n\tLINUX ../vmlinuz-linux\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1557                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-lts.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux LTS\n\tLINUX ../vmlinuz-linux-lts\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-lts.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1558                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-grsec.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux Grsec\n\tLINUX ../vmlinuz-linux-grsec\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-grsec.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1559                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-zen.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux Zen\n\tLINUX ../vmlinuz-linux-zen\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-zen.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1560                                 
1561                 # Second the "fallback" entries
1562                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux Fallback\n\tLINUX ../vmlinuz-linux\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-fallback.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1563                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-lts.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux Fallback LTS\n\tLINUX ../vmlinuz-linux-lts\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-lts-fallback.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1564                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-grsec.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux Fallback Grsec\n\tLINUX ../vmlinuz-linux-grsec\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-grsec-fallback.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1565                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-zen.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL $ISO_HOST Linux Fallbacl Zen\n\tLINUX ../vmlinuz-linux-zen\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-zen-fallback.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1566                                 
1567                 # Third, amend for LUKS
1568                 [[ $LUKS_DEV != "" ]] && sed -i "s~rw~$LUKS_DEV rw~g" ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1569                                 
1570                 # Finally, re-add the "default" entries
1571                 echo -e "\n\nLABEL hdt\n\tMENU LABEL HDT (Hardware Detection Tool)\n\tCOM32 hdt.c32" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1572                 echo -e "\n\nLABEL reboot\n\tMENU LABEL Reboot\n\tCOM32 reboot.c32" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1573                 echo -e "\n\n#LABEL windows\n\t#MENU LABEL Windows\n\t#COM32 chain.c32\n\t#APPEND root=/dev/sda2 rw" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1574                 echo -e "\n\nLABEL poweroff\n\tMENU LABEL Poweroff\n\tCOM32 poweroff.c32" ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
1575                 
1576                 BOOTLOADER="syslinux"
1577         fi
1578
1579 }
1580
1581 uefi_bootloader() {
1582
1583     #Ensure again that efivarfs is mounted
1584         [[ -z $(mount | grep /sys/firmware/efi/efivars) ]] && mount -t efivarfs efivarfs /sys/firmware/efi/efivars
1585
1586     dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_InstUefiBtTitle " --menu "$_InstUefiBtBody" 0 0 2 \
1587     "grub" "-" "systemd-boot" "/boot" 2>${ANSWER}
1588
1589         if [[ $(cat ${ANSWER}) == "grub" ]];then
1590
1591                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " Grub-install " --infobox "$_PlsWaitBody" 0 0
1592                 arch_chroot "grub-install --target=x86_64-efi --efi-directory=${UEFI_MOUNT} --bootloader-id=arch_grub --recheck" 2>/tmp/.errlog
1593                                 
1594                 # If encryption used amend grub
1595                 [[ $LUKS_DEV != "" ]] && sed -i "s~GRUB_CMDLINE_LINUX=.*~GRUB_CMDLINE_LINUX=\"$LUKS_DEV\"~g" ${MOUNTPOINT}/etc/default/grub
1596                                 
1597                 # Generate config file
1598                 arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg" 2>>/tmp/.errlog
1599                 check_for_error
1600
1601                 # Ask if user wishes to set Grub as the default bootloader and act accordingly
1602                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_InstUefiBtTitle " --yesno "$_SetBootDefBody ${UEFI_MOUNT}/EFI/boot $_SetBootDefBody2" 0 0
1603                         
1604                 if [[ $? -eq 0 ]]; then
1605                         arch_chroot "mkdir ${UEFI_MOUNT}/EFI/boot" 2>/tmp/.errlog
1606                         arch_chroot "cp -r ${UEFI_MOUNT}/EFI/arch_grub/grubx64.efi ${UEFI_MOUNT}/EFI/boot/bootx64.efi" 2>>/tmp/.errlog
1607                         check_for_error
1608                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_InstUefiBtTitle " --infobox "\nGrub $_SetDefDoneBody" 0 0
1609                         sleep 2
1610                 fi
1611                 
1612                 BOOTLOADER="grub"
1613
1614         elif [[ $(cat ${ANSWER}) == "systemd-boot" ]];then
1615         echo ${UEFI_MOUNT}
1616         
1617                 #arch_chroot "bootctl --path=${UEFI_MOUNT} install" 2>/tmp/.errlog
1618                 bootctl --path=${MOUNTPOINT}${UEFI_MOUNT} install 2>/tmp/.errlog
1619                 check_for_error
1620                                 
1621                 # Deal with LVM Root
1622                 [[ $(echo $ROOT_PART | grep "/dev/mapper/") != "" ]] && bl_root=$ROOT_PART \
1623                 || bl_root=$"PARTUUID="$(blkid -s PARTUUID ${ROOT_PART} | sed 's/.*=//g' | sed 's/"//g')
1624                                         
1625                 # Create default config files. First the loader
1626                 echo -e "default  $ISO_HOST\ntimeout  10" > ${MOUNTPOINT}${UEFI_MOUNT}/loader/loader.conf 2>/tmp/.errlog
1627                                 
1628                 # Second, the kernel conf files
1629                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux.img ]] && echo -e "title\t$ISO_HOST Linux\nlinux\t/vmlinuz-linux\ninitrd\t/initramfs-linux.img\noptions\troot=${bl_root} rw" > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/$ISO_HOST.conf
1630                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-lts.img ]] && echo -e "title\t$ISO_HOST Linux LTS\nlinux\t/vmlinuz-linux-lts\ninitrd\t/initramfs-linux-lts.img\noptions\troot=${bl_root} rw" > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/$ISO_HOST-lts.conf
1631                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-grsec.img ]] && echo -e "title\t$ISO_HOST Linux Grsec\nlinux\t/vmlinuz-linux-grsec\ninitrd\t/initramfs-linux-grsec.img\noptions\troot=${bl_root} rw" > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/$ISO_HOST-grsec.conf
1632                 [[ -e ${MOUNTPOINT}/boot/initramfs-linux-zen.img ]] && echo -e "title\t$ISO_HOST Linux Zen\nlinux\t/vmlinuz-linux-zen\ninitrd\t/initramfs-linux-zen.img\noptions\troot=${bl_root} rw" > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/$ISO_HOST-zen.conf
1633
1634                 # Finally, amend kernel conf files for LUKS
1635                 sysdconf=$(ls ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/$ISO_HOST*.conf)
1636                 for i in ${sysdconf}; do
1637                         [[ $LUKS_DEV != "" ]] && sed -i "s~rw~$LUKS_DEV rw~g" ${i}
1638                 done
1639                 
1640                 BOOTLOADER="systemd-boot"
1641         fi
1642
1643 }
1644         #                                                                       #
1645         # Bootloader function begins here       #
1646         #                                                                       #
1647     check_mount
1648     # Set the default PATH variable
1649     arch_chroot "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/core_perl" 2>/tmp/.errlog
1650     check_for_error
1651
1652     if [[ $SYSTEM == "BIOS" ]]; then
1653        bios_bootloader
1654     else
1655        uefi_bootloader
1656     fi
1657 }
1658
1659 ######################################################################
1660 ##                                                                                                                                      ##
1661 ##                 Main Interfaces                                                              ##
1662 ##                                                                                                                                      ##
1663 ######################################################################
1664
1665 security_menu(){
1666
1667         if [[ $SUB_MENU != "security_menu" ]]; then
1668            SUB_MENU="security_menu"
1669            HIGHLIGHT_SUB=1
1670         else
1671            if [[ $HIGHLIGHT_SUB != 4 ]]; then
1672               HIGHLIGHT_SUB=$(( HIGHLIGHT_SUB + 1 ))
1673            fi
1674         fi
1675
1676     dialog --default-item ${HIGHLIGHT_SUB} --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecMenuTitle " --menu "$_SecMenuBody" 0 0 4 \
1677         "1" "$_SecJournTitle" \
1678         "2" "$_SecCoreTitle" \
1679         "3" "$_SecKernTitle" \
1680         "4" "$_Back" 2>${ANSWER}
1681
1682         HIGHLIGHT_SUB=$(cat ${ANSWER})
1683         case $(cat ${ANSWER}) in
1684         "1") # systemd-journald
1685                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecJournTitle " --menu "$_SecJournBody" 0 0 7 \
1686                         "$_Edit" "/etc/systemd/journald.conf" \
1687                         "10M" "SystemMaxUse=10M" \
1688                         "20M" "SystemMaxUse=20M" \
1689                         "50M" "SystemMaxUse=50M" \
1690                         "100M" "SystemMaxUse=100M" \
1691                         "200M" "SystemMaxUse=200M" \
1692                         "$_Disable" "Storage=none" 2>${ANSWER}
1693
1694                         if [[ $(cat ${ANSWER}) != "" ]]; then
1695                                 if  [[ $(cat ${ANSWER}) == "$_Disable" ]]; then
1696                                         sed -i "s/#Storage.*\|Storage.*/Storage=none/g" ${MOUNTPOINT}/etc/systemd/journald.conf
1697                                         sed -i "s/SystemMaxUse.*/#&/g" ${MOUNTPOINT}/etc/systemd/journald.conf
1698                                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecJournTitle " --infobox "\n$_Done!\n\n" 0 0
1699                                         sleep 2
1700                                 elif [[ $(cat ${ANSWER}) == "$_Edit" ]]; then
1701                                         nano ${MOUNTPOINT}/etc/systemd/journald.conf
1702                                 else
1703                                         sed -i "s/#SystemMaxUse.*\|SystemMaxUse.*/SystemMaxUse=$(cat ${ANSWER})/g" ${MOUNTPOINT}/etc/systemd/journald.conf
1704                                         sed -i "s/Storage.*/#&/g" ${MOUNTPOINT}/etc/systemd/journald.conf
1705                                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecJournTitle " --infobox "\n$_Done!\n\n" 0 0
1706                                         sleep 2
1707                                 fi
1708                         fi
1709                         ;;
1710                 "2") # core dump
1711                          dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecCoreTitle " --menu "$_SecCoreBody" 0 0 2 \
1712                         "$_Disable" "Storage=none" "$_Edit" "/etc/systemd/coredump.conf" 2>${ANSWER}
1713                         
1714                         if [[ $(cat ${ANSWER}) == "$_Disable" ]]; then
1715                                 sed -i "s/#Storage.*\|Storage.*/Storage=none/g" ${MOUNTPOINT}/etc/systemd/coredump.conf
1716                                 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecCoreTitle " --infobox "\n$_Done!\n\n" 0 0
1717                                 sleep 2
1718                         elif [[ $(cat ${ANSWER}) == "$_Edit" ]]; then
1719                                 nano ${MOUNTPOINT}/etc/systemd/coredump.conf
1720                         fi
1721           ;;
1722         "3") # Kernel log access 
1723                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecKernTitle " --menu "\nKernel logs may contain information an attacker can use to identify and exploit kernel vulnerabilities, including sensitive memory addresses.\n\nIf systemd-journald logging has not been disabled, it is possible to create a rule in /etc/sysctl.d/ to disable access to these logs unless using root privilages (e.g. via sudo).\n" 0 0 2 \
1724                         "$_Disable" "kernel.dmesg_restrict = 1" "$_Edit" "/etc/systemd/coredump.conf.d/custom.conf" 2>${ANSWER}
1725                         
1726                         case $(cat ${ANSWER}) in
1727                         "$_Disable")    echo "kernel.dmesg_restrict = 1" > ${MOUNTPOINT}/etc/sysctl.d/50-dmesg-restrict.conf
1728                                                         dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SecKernTitle " --infobox "\n$_Done!\n\n" 0 0
1729                                                         sleep 2 ;;
1730                         "$_Edit")               [[ -e ${MOUNTPOINT}/etc/sysctl.d/50-dmesg-restrict.conf ]] && nano ${MOUNTPOINT}/etc/sysctl.d/50-dmesg-restrict.conf \
1731                                                         || dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SeeConfErrTitle " --msgbox "$_SeeConfErrBody1" 0 0 ;;
1732                         esac
1733              ;;
1734           *) main_menu
1735                         ;;
1736     esac
1737     
1738     security_menu
1739 }
1740
1741
1742 # Greet the user when first starting the installer
1743 greeting() {
1744
1745 dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_WelTitle $VERSION " --msgbox "$_WelBody" 0 0       
1746
1747 }
1748
1749 # Preparation
1750 prep_menu() {
1751         
1752         if [[ $SUB_MENU != "prep_menu" ]]; then
1753            SUB_MENU="prep_menu"
1754            HIGHLIGHT_SUB=1
1755         else
1756            if [[ $HIGHLIGHT_SUB != 8 ]]; then
1757               HIGHLIGHT_SUB=$(( HIGHLIGHT_SUB + 1 ))
1758            fi
1759         fi
1760         
1761         dialog --default-item ${HIGHLIGHT_SUB} --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_PrepMenuTitle " --menu "$_PrepMenuBody" 0 0 8 \
1762         "1" "$_VCKeymapTitle" \
1763         "2" "$_PrepKBLayout" \
1764         "3" "$_DevShowOpt" \
1765         "4" "$_PrepPartDisk" \
1766         "5" "$_PrepLUKS" \
1767         "6" "$_PrepLVM $_PrepLVM2" \
1768         "7" "$_PrepMntPart" \
1769         "8" "$_Back" 2>${ANSWER}
1770
1771     HIGHLIGHT_SUB=$(cat ${ANSWER})
1772         case $(cat ${ANSWER}) in
1773         "1") set_keymap 
1774              ;;
1775         "2") set_xkbmap
1776                         ;;
1777         "3") show_devices
1778              ;;
1779         "4") umount_partitions
1780              select_device
1781              create_partitions
1782              ;;
1783         "5") luks_menu
1784                         ;;
1785         "6") lvm_menu
1786              ;;
1787         "7") mount_partitions
1788              ;;        
1789           *) main_menu
1790              ;;
1791     esac
1792     
1793     prep_menu   
1794         
1795 }
1796
1797 # Base Installation
1798 install_root_menu() {
1799
1800         if [[ $SUB_MENU != "install_base_menu" ]]; then
1801            SUB_MENU="install_base_menu"
1802            HIGHLIGHT_SUB=1
1803         else
1804            if [[ $HIGHLIGHT_SUB != 4 ]]; then
1805               HIGHLIGHT_SUB=$(( HIGHLIGHT_SUB + 1 ))
1806            fi
1807         fi
1808
1809    dialog --default-item ${HIGHLIGHT_SUB} --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title "$_InstBsMenuTitle" --menu "$_InstBseMenuBody" 0 0 4 \
1810         "1" "$_InstBse" \
1811         "2" "$_MMRunMkinit" \
1812         "3" "$_InstBootldr" \
1813         "4" "$_Back" 2>${ANSWER}        
1814         
1815         HIGHLIGHT_SUB=$(cat ${ANSWER})
1816         case $(cat ${ANSWER}) in
1817         "1") install_root
1818              ;;
1819         "2") run_mkinitcpio
1820              ;;
1821         "3") install_bootloader
1822              ;;
1823           *) main_menu
1824              ;;
1825      esac
1826     
1827     install_root_menu   
1828 }
1829
1830 # Base Configuration
1831 config_base_menu() {
1832         
1833         # Set the default PATH variable
1834     arch_chroot "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/core_perl" 2>/tmp/.errlog
1835         check_for_error
1836         
1837         if [[ $SUB_MENU != "config_base_menu" ]]; then
1838            SUB_MENU="config_base_menu"
1839            HIGHLIGHT_SUB=1
1840         else
1841            if [[ $HIGHLIGHT_SUB != 8 ]]; then
1842               HIGHLIGHT_SUB=$(( HIGHLIGHT_SUB + 1 ))
1843            fi
1844         fi
1845
1846     dialog --default-item ${HIGHLIGHT_SUB} --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ConfBseMenuTitle " --menu "$_ConfBseBody" 0 0 8 \
1847         "1" "$_ConfBseFstab" \
1848         "2" "$_ConfBseHost" \
1849         "3" "$_ConfBseSysLoc" \
1850         "4" "$_ConfBseTimeHC" \
1851         "5" "$_ConfUsrRoot" \
1852         "6" "$_ConfUsrNew" \
1853         "7" "$_SecMenuTitle" \
1854         "8" "$_Back" 2>${ANSWER}        
1855         
1856         HIGHLIGHT_SUB=$(cat ${ANSWER})
1857         case $(cat ${ANSWER}) in
1858         "1") generate_fstab 
1859              ;;
1860         "2") set_hostname
1861              ;;
1862         "3") set_locale
1863              ;;        
1864         "4") set_timezone
1865                          set_hw_clock
1866              ;;
1867                 "5") set_root_password 
1868                         ;;
1869                 "6") create_new_user
1870                         ;;
1871                 "7") security_menu
1872                         ;;
1873           *) main_menu
1874                         ;;
1875     esac
1876     
1877     config_base_menu
1878
1879 }
1880
1881 # Edit configs of installed system
1882 edit_configs() {
1883         
1884         # Clear the file variables
1885         FILE=""
1886         user_list=""
1887         
1888         if [[ $SUB_MENU != "edit configs" ]]; then
1889            SUB_MENU="edit configs"
1890            HIGHLIGHT_SUB=1
1891         else
1892            if [[ $HIGHLIGHT_SUB != 12 ]]; then
1893               HIGHLIGHT_SUB=$(( HIGHLIGHT_SUB + 1 ))
1894            fi
1895         fi
1896
1897    dialog --default-item ${HIGHLIGHT_SUB} --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_SeeConfOptTitle " --menu "$_SeeConfOptBody" 0 0 12 \
1898    "1" "/etc/vconsole.conf" \
1899    "2" "/etc/locale.conf" \
1900    "3" "/etc/hostname" \
1901    "4" "/etc/hosts" \
1902    "5" "/etc/sudoers" \
1903    "6" "/etc/mkinitcpio.conf" \
1904    "7" "/etc/fstab" \
1905    "8" "/etc/crypttab" \
1906    "9" "grub/syslinux/systemd-boot" \
1907    "10" "/etc/lxdm/lxdm.conf" \
1908    "11" "/etc/pacman.conf" \
1909    "12" "$_Back" 2>${ANSWER}
1910         
1911         HIGHLIGHT_SUB=$(cat ${ANSWER})
1912         case $(cat ${ANSWER}) in
1913             "1") [[ -e ${MOUNTPOINT}/etc/vconsole.conf ]] && FILE="${MOUNTPOINT}/etc/vconsole.conf"
1914              ;;
1915         "2") [[ -e ${MOUNTPOINT}/etc/locale.conf ]] && FILE="${MOUNTPOINT}/etc/locale.conf" 
1916              ;;
1917         "3") [[ -e ${MOUNTPOINT}/etc/hostname ]] && FILE="${MOUNTPOINT}/etc/hostname"
1918              ;;
1919         "4") [[ -e ${MOUNTPOINT}/etc/hosts ]] && FILE="${MOUNTPOINT}/etc/hosts"
1920              ;;
1921         "5") [[ -e ${MOUNTPOINT}/etc/sudoers ]] && FILE="${MOUNTPOINT}/etc/sudoers"
1922              ;;
1923         "6") [[ -e ${MOUNTPOINT}/etc/mkinitcpio.conf ]] && FILE="${MOUNTPOINT}/etc/mkinitcpio.conf"
1924              ;;
1925         "7") [[ -e ${MOUNTPOINT}/etc/fstab ]] && FILE="${MOUNTPOINT}/etc/fstab"
1926              ;;
1927         "8") [[ -e ${MOUNTPOINT}/etc/crypttab ]] && FILE="${MOUNTPOINT}/etc/crypttab"
1928                          ;;
1929         "9") [[  $BOOTLOADER == "grub" ]] && FILE="${MOUNTPOINT}/etc/default/grub"
1930                          [[  $BOOTLOADER == "syslinux" ]] && FILE="${MOUNTPOINT}/boot/syslinux/syslinux.cfg"
1931                          if [[  $BOOTLOADER == "systemd-boot" ]]; then
1932                                 FILE="${MOUNTPOINT}${UEFI_MOUNT}/loader/loader.conf"     
1933                                 files=$(ls ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/*.conf)
1934                                 for i in ${files}; do
1935                                         FILE="$FILE ${i}"
1936                                 done
1937                          fi
1938             ;;
1939         "10") [[ -e ${MOUNTPOINT}/etc/lxdm/lxdm.conf ]] && FILE="${MOUNTPOINT}/etc/lxdm/lxdm.conf" 
1940             ;;
1941         "11") [[ -e ${MOUNTPOINT}/etc/pacman.conf ]] && FILE="${MOUNTPOINT}/etc/pacman.conf"
1942                         ;;
1943          *) main_menu
1944             ;;
1945      esac
1946      
1947         [[ $FILE != "" ]] && ${TEXTEDITOR} $FILE \
1948         || dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_ErrTitle " --msgbox "$_SeeConfErrBody" 0 0
1949         
1950         edit_configs
1951 }
1952
1953 main_menu() {
1954         
1955         if [[ $HIGHLIGHT != 5 ]]; then
1956            HIGHLIGHT=$(( HIGHLIGHT + 1 ))
1957         fi
1958
1959    dialog --default-item ${HIGHLIGHT} --backtitle "$VERSION - $SYSTEM ($ARCHI)" --title " $_MMTitle " \
1960     --menu "$_MMBody" 0 0 5 \
1961         "1" "$_PrepMenuTitle" \
1962         "2" "$_InstBsMenuTitle" \
1963         "3" "$_ConfBseMenuTitle" \
1964     "4" "$_SeeConfOptTitle" \
1965         "5" "$_Done" 2>${ANSWER}
1966
1967     HIGHLIGHT=$(cat ${ANSWER})
1968     
1969     # Depending on the answer, first check whether partition(s) are mounted and whether base has been installed
1970     if [[ $(cat ${ANSWER}) -eq 2 ]]; then
1971        check_mount
1972     fi
1973
1974     if [[ $(cat ${ANSWER}) -ge 3 ]] && [[ $(cat ${ANSWER}) -le 4 ]]; then
1975        check_mount
1976        check_base
1977     fi
1978     
1979     case $(cat ${ANSWER}) in
1980         "1") prep_menu 
1981              ;;
1982         "2") install_root_menu
1983              ;;
1984         "3") config_base_menu
1985              ;;         
1986         "4") edit_configs
1987              ;;            
1988           *) dialog --backtitle "$VERSION - $SYSTEM ($ARCHI)" --yesno "$_CloseInstBody" 0 0
1989           
1990              if [[ $? -eq 0 ]]; then
1991                 umount_partitions
1992                 clear
1993                 exit 0
1994              else
1995                 main_menu
1996              fi
1997              
1998              ;;
1999     esac
2000     
2001     main_menu 
2002     
2003 }
2004
2005 ######################################################################
2006 ##                                                                                                                                      ##
2007 ##                        Execution                                                             ##
2008 ##                                                                                                                                      ##
2009 ######################################################################
2010 id_system
2011 select_language
2012 check_requirements
2013
2014         while true; do
2015           main_menu      
2016     done