util-base.sh 31.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# !/bin/bash
#
# Architect Installation Framework (2016-2017)
#
# Written by Carl Duff and @mandog for Archlinux
# Heavily modified and re-written by @Chrysostomus to install Manjaro instead
# Contributors: @papajoker, @oberon and the Manjaro-Community.
#
# This program is free software, provided under the GNU General Public License
# as published by the Free Software Foundation. So feel free to copy, distribute,
# or modify it as you wish.

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
setup_profiles() {
    # setup profiles with either git or package 
    if [[ -e /tmp/.git_profiles ]]; then 
        PROFILES="$DATADIR/profiles"
        clear
        # install git if not already installed
        inst_needed git
        # download manjaro-tools.-isoprofiles git repo
        if [[ -e $PROFILES ]]; then
            git -C $PROFILES pull 2>$ERR
            check_for_error "pull profiles repo" $?
        else
            git clone --depth 1 https://github.com/manjaro/iso-profiles.git $PROFILES 2>$ERR
            check_for_error "clone profiles repo" $?
        fi
    else
        PROFILES="/usr/share/manjaro-tools/iso-profiles"
        # Only show this information box once
        clear
        pacman -Sy --noconfirm $p manjaro-iso-profiles-{base,official,community} 2>$ERR
        check_for_error "update profiles pkgs" $?
    fi
}

enable_services() {
        # Enable services in the chosen profile
        echo "Enabling services"
        if [[ -e /mnt/.openrc ]]; then
            eval $(grep -e "enable_openrc=" $profile | sed 's/# //g')
            echo "${enable_openrc[@]}" | xargs -n1 > /tmp/.services
            echo /mnt/etc/init.d/* | xargs -n1 | cut -d/ -f5 > /tmp/.available_services
            grep -f /tmp/.available_services /tmp/.services > /tmp/.fix && mv /tmp/.fix /tmp/.services
            for service in $(cat /tmp/.services) ; do
                arch_chroot "rc-update add $service default" 2>$ERR
                check_for_error "enable $service" $?
            done

            # enable display manager for openrc
            if [[ "$(cat /tmp/.display-manager)" == sddm ]]; then
                sed -i "s/$(grep "DISPLAYMANAGER=" /mnt/etc/conf.d/xdm)/DISPLAYMANAGER=\"sddm\"/g" /mnt/etc/conf.d/xdm
                arch_chroot "rc-update add xdm default" 2>$ERR
                check_for_error "add xdm default: sddm" "$?"
                set_sddm_ck
            elif [[ "$(cat /tmp/.display-manager)" == lightdm ]]; then
                set_lightdm_greeter
                sed -i "s/$(grep "DISPLAYMANAGER=" /mnt/etc/conf.d/xdm)/DISPLAYMANAGER=\"lightdm\"/g" /mnt/etc/conf.d/xdm
                arch_chroot "rc-update add xdm default" 2>$ERR
                check_for_error "add xdm default: lightdm" "$?"

            else
                check_for_error "no DM installed."
                echo "no display manager was installed."
                sleep 2
            fi
        else
            eval $(grep -e "enable_systemd=" $profile | sed 's/# //g')
            echo "${enable_systemd[@]}" | xargs -n1 > /tmp/.services
            echo /mnt/usr/lib/systemd/system/* | xargs -n1 | cut -d/ -f7 | sed 's/.service//g' > /tmp/.available_services
            grep -f /tmp/.available_services /tmp/.services > /tmp/.fix && mv /tmp/.fix /tmp/.services
            arch_chroot "systemctl enable $(cat /tmp/.services)" 2>$ERR
73
            check_for_error "enable $(cat /tmp/.services | tr '\n' ' ')" $?
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            arch_chroot "systemctl disable pacman-init" 2>$ERR
            check_for_error "disable pacman-init" $?

            # enable display manager for systemd
            if [[ "$(cat /tmp/.display-manager)" == lightdm ]]; then
                set_lightdm_greeter
                arch_chroot "systemctl enable lightdm" 2>$ERR
                check_for_error "enable lightdm" "$?"
            elif [[ "$(cat /tmp/.display-manager)" == sddm ]]; then
                arch_chroot "systemctl enable sddm" 2>$ERR
                check_for_error "enable sddm" "$?"
            elif [[ "$(cat /tmp/.display-manager)" == gdm ]]; then
                arch_chroot "systemctl enable gdm" 2>$ERR
                check_for_error "enable gdm" "$?"
            else
                check_for_error "no DM installed."
                echo "no display manager was installed"
                sleep 2
            fi
        fi
}

install_extra() {
    # Offer to install various "common" packages.
98 99 100 101
    local options=() nb=0
    cpkgs="manjaro-settings-manager pamac octopi pacli pacui fish fisherman zsh zsh-completions \
      manjaro-zsh-config mhwd-chroot bmenu clonezilla snapper snap-pac manjaro-tools-iso manjaro-tools-base manjaro-tools-pkg"
    for p in ${cpkgs}; do
102
        ! grep "$p" /mnt/.desktop && options+=("$p" "" off)
103
    done
104
    nb="$((${#options[@]}/3))"; (( nb>20 )) && nb=20 # if list too long limit
Bernhard Landauer's avatar
Bernhard Landauer committed
105
    DIALOG " $_InstComTitle " --checklist "\n$_InstComBody\n\n$_UseSpaceBar\n  " 0 50 $nb "${options[@]}" 2>${PACKAGES}
106 107 108 109 110 111 112 113 114 115

    # If at least one package, install.
    if [[ $(cat ${PACKAGES}) != "" ]]; then
        clear
        basestrap -i ${MOUNTPOINT} $(cat ${PACKAGES}) 2>$ERR
        check_for_error "basestrap -i ${MOUNTPOINT} $(cat ${PACKAGES})" "$?"
    fi
}

filter_packages() {
116
        DIALOG " $_PkgList " --infobox "\n$_PlsWaitBody\n " 0 0
117
        # Parse package list based on user input and remove parts that don't belong to pacman
118
        cat "$pkgs_src" >> $pkgs_target 2>$ERR
119
        check_for_error "$FUNCNAME" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
120
        if [[ -e /mnt/.openrc ]]; then
121
            # Remove any packages tagged with >systemd and remove >openrc tags
122 123
            sed -i '/>systemd/d' $pkgs_target
            sed -i 's/>openrc //g' $pkgs_target
124 125
        else
            # Remove any packages tagged with >openrc and remove >systemd tags
126 127
            sed -i '/>openrc/d' $pkgs_target
            sed -i 's/>systemd //g' $pkgs_target
128 129 130 131
        fi

        if [[ "$(uname -m)" == "x86_64" ]]; then
            # Remove any packages tagged with >i686 and remove >x86_64 tags
132 133 134
            sed -i '/>i686/d' $pkgs_target
            sed -i '/>nonfree_i686/d' $pkgs_target
            sed -i 's/>x86_64 //g' $pkgs_target
135 136
        else
            # Remove any packages tagged with >x86_64 and remove >i686 tags
137 138 139
            sed -i '/>x86_64/d' $pkgs_target
            sed -i '/>nonfree_x86_64/d' $pkgs_target
            sed -i 's/>i686 //g' $pkgs_target
140 141 142
        fi

        # If multilib repo is enabled, install multilib packages
143
        if grep -q "^[multilib]" /etc/pacman.conf; then
144
            # Remove >multilib tags
145 146
            sed -i 's/>multilib //g' $pkgs_target
            sed -i 's/>nonfree_multilib //g' $pkgs_target
147 148
        else
            # Remove lines with >multilib tag
149 150
            sed -i '/>multilib/d' $pkgs_target
            sed -i '/>nonfree_multilib/d' $pkgs_target
151 152
        fi

153
        if grep -q ">extra" $pkgs_target; then
154
            # User to select base|extra profile
Bernhard Landauer's avatar
Bernhard Landauer committed
155
            DIALOG "$_ExtraTitle" --no-cancel --menu "\n$_ExtraBody\n " 0 0 2 \
156 157 158 159 160 161 162 163 164 165 166 167 168 169
              "1" "full" \
              "2" "minimal" 2>/tmp/.version

            if [[ $(cat /tmp/.version) -eq 2 ]]; then
                check_for_error "selected 'minimal' profile"
                touch /tmp/.minimal
            else
                check_for_error "selected 'full' profile"
                [[ -e /tmp/.minimal ]] && rm /tmp/.minimal
            fi
        fi

        if [[ -e /tmp/.minimal ]]; then
            # Remove >extra tags
170 171
            sed -i 's/>basic //g' $pkgs_target
            sed -i '/>extra/d' $pkgs_target
172 173
        else
            # Remove >basic tags
174 175
            sed -i 's/>extra //g' $pkgs_target
            sed -i '/>basic/d' $pkgs_target
176 177
        fi
        # remove >manjaro flags and >sonar flags+pkgs until we support it properly
178 179
        sed -i '/>sonar/d' $pkgs_target
        sed -i 's/>manjaro //g' $pkgs_target
180 181
        # Remove commented lines
        # remove everything except the first word of every lines
182
        sed -i 's/\s.*$//' $pkgs_target
183
        # Remove lines with #
184
        sed -i '/#/d' $pkgs_target
185
        # remove KERNEL variable
186
        sed -i '/KERNEL/d' $pkgs_target
187
        # Remove empty lines
188
        sed -i '/^\s*$/d' $pkgs_target
189
        # remove zsh
190
        sed -i '/^zsh$/d' $pkgs_target
191 192 193

        # Remove packages that have been dropped from repos
        pacman -Ssq > /tmp/.available_packages
194 195
        grep -f /tmp/.available_packages $pkgs_target > /tmp/.tmp
        mv /tmp/.tmp $pkgs_target
196 197
}

198
install_base() {
199
    if [[ -e /mnt/.base_installed ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
200
        DIALOG " $_InstBseTitle " --yesno "\n$_WarnInstBase\n " 0 0 && rm /mnt/.base_installed || return 0
201
    fi
202
    # Prep variables
203
    setup_profiles
204 205
    pkgs_src=$PROFILES/shared/Packages-Root
    pkgs_target=/mnt/.base
206 207
    BTRF_CHECK=$(echo "btrfs-progs" "" off)
    F2FS_CHECK=$(echo "f2fs-tools" "" off)
208 209 210 211
    mhwd-kernel -l | awk '/linux/ {print $2}' > /tmp/.available_kernels
    kernels=$(cat /tmp/.available_kernels)

    # User to select initsystem
212
    DIALOG " $_ChsInit " --menu "\n$_Note\n$_WarnOrc\n$(evaluate_profiles)\n " 0 0 2 \
213 214
      "1" "systemd" \
      "2" "openrc" 2>${INIT}
Bernhard Landauer's avatar
Bernhard Landauer committed
215 216 217 218 219 220 221 222 223

    if [[ $(cat ${INIT}) != "" ]]; then
        if [[ $(cat ${INIT}) -eq 2 ]]; then
            check_for_error "init openrc"
            touch /mnt/.openrc
        else
            check_for_error "init systemd"
            [[ -e /mnt/.openrc ]] && rm /mnt/.openrc
        fi
224
    else
Bernhard Landauer's avatar
Bernhard Landauer committed
225
        return 0
226
    fi
227
    # Create the base list of packages
228
    echo "" > /mnt/.base
Bernhard Landauer's avatar
Bernhard Landauer committed
229

Bernhard Landauer's avatar
Bernhard Landauer committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    declare -i loopmenu=1
    while ((loopmenu)); do
        # Choose kernel and possibly base-devel
        DIALOG " $_InstBseTitle " --checklist "\n$_InstStandBseBody$_UseSpaceBar\n " 0 0 20 \
          "yaourt + base-devel" "-" off \
          $(cat /tmp/.available_kernels | awk '$0=$0" - off"') 2>${PACKAGES} || { loopmenu=0; return 0; }
        if [[ ! $(grep "linux" ${PACKAGES}) ]]; then
            # Check if a kernel is already installed
            ls ${MOUNTPOINT}/boot/*.img >/dev/null 2>&1
            if [[ $? == 0 ]]; then
                DIALOG " Check Kernel " --msgbox "\nlinux-$(ls ${MOUNTPOINT}/boot/*.img | cut -d'-' -f2 | grep -v ucode.img | sort -u) detected \n " 0 0
                check_for_error "linux-$(ls ${MOUNTPOINT}/boot/*.img | cut -d'-' -f2) already installed"
                loopmenu=0
            else
                DIALOG " $_ErrTitle " --msgbox "\n$_ErrNoKernel\n " 0 0
            fi
246
        else
Bernhard Landauer's avatar
Bernhard Landauer committed
247
            cat ${PACKAGES} | sed 's/+ \|\"//g' | tr ' ' '\n' >> /mnt/.base
248
            echo " " >> /mnt/.base
Bernhard Landauer's avatar
Bernhard Landauer committed
249 250
            check_for_error "selected: $(cat ${PACKAGES})"
            loopmenu=0
251
        fi
Bernhard Landauer's avatar
Bernhard Landauer committed
252
    done
253

Bernhard Landauer's avatar
Bernhard Landauer committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
    # Choose wanted kernel modules
    DIALOG " $_ChsAddPkgs " --checklist "\n$_UseSpaceBar\n " 0 0 12 \
      "KERNEL-headers" "-" off \
      "KERNEL-acpi_call" "-" on \
      "KERNEL-ndiswrapper" "-" on \
      "KERNEL-broadcom-wl" "-" off \
      "KERNEL-r8168" "-" off \
      "KERNEL-rt3562sta" "-" off \
      "KERNEL-tp_smapi" "-" off \
      "KERNEL-vhba-module" "-" off \
      "KERNEL-virtualbox-guest-modules" "-" off \
      "KERNEL-virtualbox-host-modules" "-" off \
      "KERNEL-spl" "-" off \
      "KERNEL-zfs" "-" off 2>/tmp/.modules || return 0
268

Bernhard Landauer's avatar
Bernhard Landauer committed
269
    if [[ $(cat /tmp/.modules) != "" ]]; then
270
        check_for_error "modules: $(cat /tmp/.modules)"
Bernhard Landauer's avatar
Bernhard Landauer committed
271
        for kernel in $(cat ${PACKAGES} | grep -vE '(yaourt|base-devel)'); do
Matti Hyttinen's avatar
Matti Hyttinen committed
272
            cat /tmp/.modules | sed "s/KERNEL/\n$kernel/g" >> /mnt/.base
273
        done
Matti Hyttinen's avatar
Matti Hyttinen committed
274
        echo " " >> /mnt/.base
Bernhard Landauer's avatar
Bernhard Landauer committed
275 276
    fi
    filter_packages
277 278 279 280
    # remove grub
    sed -i '/grub/d' /mnt/.base
    echo "nilfs-utils" >> /mnt/.base
    check_for_error "packages to install: $(cat /mnt/.base | sort | tr '\n' ' ')"
281
    clear
Bernhard Landauer's avatar
Bernhard Landauer committed
282
    basestrap ${MOUNTPOINT} $(cat /mnt/.base) 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
283
    check_for_error "install basepkgs" $? || { DIALOG " $_InstBseTitle " --msgbox "\n$_InstFail\n " 0 0; HIGHLIGHT_SUB=2; return 1; }
284

Bernhard Landauer's avatar
Bernhard Landauer committed
285 286 287
    # If root is on btrfs volume, amend mkinitcpio.conf
    [[ $(lsblk -lno FSTYPE,MOUNTPOINT | awk '/ \/mnt$/ {print $1}') == btrfs ]] && sed -e '/^HOOKS=/s/\ fsck//g' -i ${MOUNTPOINT}/etc/mkinitcpio.conf && \
      check_for_error "root on btrfs volume. amend mkinitcpio."
288

Bernhard Landauer's avatar
Bernhard Landauer committed
289 290 291
    # If root is on nilfs2 volume, amend mkinitcpio.conf
    [[ $(lsblk -lno FSTYPE,MOUNTPOINT | awk '/ \/mnt$/ {print $1}') == nilfs2 ]] && sed -e '/^HOOKS=/s/\ fsck//g' -i ${MOUNTPOINT}/etc/mkinitcpio.conf && \
      check_for_error "root on nilfs2 volume. amend mkinitcpio."
292

Bernhard Landauer's avatar
Bernhard Landauer committed
293 294 295
    # Use mhwd to install selected kernels with right kernel modules
    # This is as of yet untested
    # arch_chroot "mhwd-kernel -i $(cat ${PACKAGES} | xargs -n1 | grep -f /tmp/.available_kernels | xargs)"
296

297 298 299 300 301 302 303 304 305 306 307
    # copy keymap and consolefont settings to target
    if [[ -e /mnt/.openrc ]]; then
        echo -e "keymap=\"$(ini linux.keymap)\"" > ${MOUNTPOINT}/etc/conf.d/keymaps
        arch_chroot "rc-update add keymaps boot" 2>$ERR
        check_for_error "configure keymaps" $?
        echo -e "consolefont=\"$(ini linux.font)\"" > ${MOUNTPOINT}/etc/conf.d/consolefont
        arch_chroot "rc-update add consolefont boot" 2>$ERR
        check_for_error "configure consolefont" $?
    else
        echo -e "KEYMAP=$(ini linux.keymap)\nFONT=$(ini linux.font) > ${MOUNTPOINT}/etc/vconsole.conf"
        check_for_error "configure vconsole"
308 309
    fi

Bernhard Landauer's avatar
Bernhard Landauer committed
310 311 312
    # If specified, copy over the pacman.conf file to the installation
    if [[ $COPY_PACCONF -eq 1 ]]; then
        cp -f /etc/pacman.conf ${MOUNTPOINT}/etc/pacman.conf
313
        check_for_error "copy pacman.conf"
314
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
315 316 317 318 319 320 321 322

    # if branch was chosen, use that also in installed system. If not, use the system setting
    [[ -z $(ini branch) ]] && ini branch $(ini system.branch)
    sed -i "s/Branch =.*/Branch = $(ini branch)/;s/# //" ${MOUNTPOINT}/etc/pacman-mirrors.conf

    touch /mnt/.base_installed
    check_for_error "base installed succesfully."
    install_network_drivers
323 324
}

325
install_bootloader() {
326 327
    check_base
    if [[ $? -eq 0 ]]; then
328
        if [[ $SYSTEM == "BIOS" ]]; then
329
            bios_bootloader
330
        else
331
            uefi_bootloader
332
        fi
333 334
    else
        HIGHLIGHT_SUB=2
Bernhard Landauer's avatar
Bernhard Landauer committed
335
    fi
336 337
}

338 339 340
uefi_bootloader() {
    #Ensure again that efivarfs is mounted
    [[ -z $(mount | grep /sys/firmware/efi/efivars) ]] && mount -t efivarfs efivarfs /sys/firmware/efi/efivars
341

Bernhard Landauer's avatar
Bernhard Landauer committed
342
    DIALOG " $_InstUefiBtTitle " --yesno "\n$_InstUefiBtBody\n " 0 0 || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
343 344
    clear
    basestrap ${MOUNTPOINT} grub efibootmgr dosfstools 2>$ERR
345
    check_for_error "$FUNCNAME grub" $? || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
346

Bernhard Landauer's avatar
Bernhard Landauer committed
347
    DIALOG " $_InstGrub " --infobox "\n$_PlsWaitBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
348 349 350 351 352 353 354 355 356 357 358 359 360
    # if root is encrypted, amend /etc/default/grub
    boot_encrypted_setting
    #install grub
    arch_chroot "grub-install --target=x86_64-efi --efi-directory=${UEFI_MOUNT} --bootloader-id=manjaro_grub --recheck" 2>$ERR
    check_for_error "grub-install --target=x86_64-efi" $?

    # If encryption used amend grub
    [[ $LUKS_DEV != "" ]] && sed -i "s~GRUB_CMDLINE_LINUX=.*~GRUB_CMDLINE_LINUX=\"$LUKS_DEV\"~g" ${MOUNTPOINT}/etc/default/grub

    # If root is on btrfs volume, amend grub
    [[ $(lsblk -lno FSTYPE,MOUNTPOINT | awk '/ \/mnt$/ {print $1}') == btrfs ]] && \
      sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i ${MOUNTPOINT}/etc/default/grub

361
    grub_mkconfig
Bernhard Landauer's avatar
Bernhard Landauer committed
362 363

    # Ask if user wishes to set Grub as the default bootloader and act accordingly
Bernhard Landauer's avatar
Bernhard Landauer committed
364
    DIALOG " $_InstUefiBtTitle " --yesno "\n$_SetBootDefBody ${UEFI_MOUNT}/EFI/boot $_SetBootDefBody2\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
365 366 367 368
    if [[ $? -eq 0 ]]; then
        arch_chroot "mkdir ${UEFI_MOUNT}/EFI/boot" 2>$ERR
        arch_chroot "cp -r ${UEFI_MOUNT}/EFI/manjaro_grub/grubx64.efi ${UEFI_MOUNT}/EFI/boot/bootx64.efi" 2>$ERR
        check_for_error "Install GRUB" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
369
        DIALOG " $_InstUefiBtTitle " --infobox "\nGrub $_SetDefDoneBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
370
        sleep 2
371
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
372

Bernhard Landauer's avatar
Bernhard Landauer committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
<<DISABLED_FOR_NOW
    "systemd-boot")
        arch_chroot "bootctl --path=${UEFI_MOUNT} install" 2>$ERR
        check_for_error "systemd-boot" $?

        # Deal with LVM Root
        [[ $(echo $ROOT_PART | grep "/dev/mapper/") != "" ]] && bl_root=$ROOT_PART \
          || bl_root=$"PARTUUID="$(blkid -s PARTUUID ${ROOT_PART} | sed 's/.*=//g' | sed 's/"//g')

        # Create default config files. First the loader
        echo -e "default  arch\ntimeout  10" > ${MOUNTPOINT}${UEFI_MOUNT}/loader/loader.conf 2>$ERR

        # Second, the kernel conf files
        [[ -e ${MOUNTPOINT}/boot/initramfs-linux.img ]] && \
          echo -e "title\tManjaro Linux\nlinux\t/vmlinuz-linux\ninitrd\t/initramfs-linux.img\noptions\troot=${bl_root} rw" \
          > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/arch.conf
        [[ -e ${MOUNTPOINT}/boot/initramfs-linux-lts.img ]] && \
          echo -e "title\tManjaro Linux LTS\nlinux\t/vmlinuz-linux-lts\ninitrd\t/initramfs-linux-lts.img\noptions\troot=${bl_root} rw" \
          > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/arch-lts.conf
        [[ -e ${MOUNTPOINT}/boot/initramfs-linux-grsec.img ]] && \
          echo -e "title\tManjaro Linux Grsec\nlinux\t/vmlinuz-linux-grsec\ninitrd\t/initramfs-linux-grsec.img\noptions\troot=${bl_root} rw" \
          > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/arch-grsec.conf
        [[ -e ${MOUNTPOINT}/boot/initramfs-linux-zen.img ]] && \
          echo -e "title\tManjaro Linux Zen\nlinux\t/vmlinuz-linux-zen\ninitrd\t/initramfs-linux-zen.img\noptions\troot=${bl_root} rw" \
          > ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/arch-zen.conf

        # Finally, amend kernel conf files for LUKS and BTRFS
        sysdconf=$(ls ${MOUNTPOINT}${UEFI_MOUNT}/loader/entries/arch*.conf)
        for i in ${sysdconf}; do
            [[ $LUKS_DEV != "" ]] && sed -i "s~rw~$LUKS_DEV rw~g" ${i}
        done
DISABLED_FOR_NOW
405 406 407 408
}

# Grub auto-detects installed kernels, etc. Syslinux does not, hence the extra code for it.
bios_bootloader() {
409
    DIALOG " $_InstBiosBtTitle " --menu "\n$_InstGrubBody\n " 0 0 2 \
410 411
      "grub" "" \
      "grub + os-prober" "" 2>${PACKAGES} || return 0
412 413 414 415
    clear

    # If something has been selected, act
    if [[ $(cat ${PACKAGES}) != "" ]]; then
416
        sed -i 's/+ \|\"//g' ${PACKAGES}
417
        basestrap ${MOUNTPOINT} $(cat ${PACKAGES}) 2>$ERR
418
        check_for_error "$FUNCNAME" $? || return 1
419 420 421 422

        # If Grub, select device
        if [[ $(cat ${PACKAGES} | grep "grub") != "" ]]; then
            select_device
423 424
            # if root is encrypted, amend /etc/default/grub
            boot_encrypted_setting
425 426
            # If a device has been selected, configure
            if [[ $DEVICE != "" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
427
                DIALOG " $_InstGrub " --infobox "\n$_PlsWaitBody\n " 0 0
428
                arch_chroot "grub-install --target=i386-pc --recheck $DEVICE" 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
429
                check_for_error "grub-install --target=i386-pc" $?
430 431 432 433 434 435 436 437 438 439 440 441 442

                # if /boot is LVM (whether using a seperate /boot mount or not), amend grub
                if ( [[ $LVM -eq 1 ]] && [[ $LVM_SEP_BOOT -eq 0 ]] ) || [[ $LVM_SEP_BOOT -eq 2 ]]; then
                    sed -i "s/GRUB_PRELOAD_MODULES=\"\"/GRUB_PRELOAD_MODULES=\"lvm\"/g" ${MOUNTPOINT}/etc/default/grub
                fi

                # If encryption used amend grub
                [[ $LUKS_DEV != "" ]] && sed -i "s~GRUB_CMDLINE_LINUX=.*~GRUB_CMDLINE_LINUX=\"$LUKS_DEV\"~g" ${MOUNTPOINT}/etc/default/grub

                # If root is on btrfs volume, amend grub
                [[ $(lsblk -lno FSTYPE,MOUNTPOINT | awk '/ \/mnt$/ {print $1}') == btrfs ]] && \
                  sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i ${MOUNTPOINT}/etc/default/grub

443
                grub_mkconfig
444 445 446
            fi
        else
            # Syslinux
Bernhard Landauer's avatar
Bernhard Landauer committed
447
            DIALOG " $_InstSysTitle " --menu "\n$_InstSysBody\n " 0 0 2 \
448 449 450 451 452
              "syslinux-install_update -iam" "[MBR]" "syslinux-install_update -i" "[/]" 2>${PACKAGES}

            # If an installation method has been chosen, run it
            if [[ $(cat ${PACKAGES}) != "" ]]; then
                arch_chroot "$(cat ${PACKAGES})" 2>$ERR
patrick's avatar
patrick committed
453
                check_for_error "syslinux-install" $?
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

                # Amend configuration file. First remove all existing entries, then input new ones.
                sed -i '/^LABEL.*$/,$d' ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                #echo -e "\n" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg

                # First the "main" entries
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux\n\tLINUX \
                  ../vmlinuz-linux\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux-lts.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux realtime LTS\n\tLINUX \
                  ../vmlinuz-linux-lts\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-lts.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux-grsec.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux realtime\n\tLINUX \
                  ../vmlinuz-linux-grsec\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-grsec.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux-zen.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux release candidate\n\tLINUX \
                  ../vmlinuz-linux-zen\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-zen.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg

                # Second the "fallback" entries
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux Fallback\n\tLINUX \
                  ../vmlinuz-linux\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-fallback.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux-lts.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux Fallback realtime LTS\n\tLINUX \
                  ../vmlinuz-linux-lts\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-lts-fallback.img" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux-grsec.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro Linux Fallback realtime\n\tLINUX \
                  ../vmlinuz-linux-grsec\n\tAPPEND root=${ROOT_PART} rw\n\tINITRD ../initramfs-linux-grsec-fallback.img" \
                  >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                [[ -e ${MOUNTPOINT}/boot/initramfs-linux-zen.img ]] && echo -e "\n\nLABEL arch\n\tMENU LABEL Manjaro 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

                # Third, amend for LUKS
                [[ $LUKS_DEV != "" ]] && sed -i "s~rw~$LUKS_DEV rw~g" ${MOUNTPOINT}/boot/syslinux/syslinux.cfg

                # Finally, re-add the "default" entries
                echo -e "\n\nLABEL hdt\n\tMENU LABEL HDT (Hardware Detection Tool)\n\tCOM32 hdt.c32" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                echo -e "\n\nLABEL reboot\n\tMENU LABEL Reboot\n\tCOM32 reboot.c32" >> ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
                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
                echo -e "\n\nLABEL poweroff\n\tMENU LABEL Poweroff\n\tCOM32 poweroff.c32" ${MOUNTPOINT}/boot/syslinux/syslinux.cfg
            fi
490
        fi
491 492
    fi
}
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507
boot_encrypted_setting() {
    # Check if there is separate encrypted /boot partition 
    if $(lsblk | grep '/mnt/boot' | grep -q 'crypt' ); then
        echo "GRUB_ENABLE_CRYPTODISK=y" >> /mnt/etc/default/grub
    # Check if root is encrypted and there is no separate /boot
    elif $(lsblk | grep "/mnt$" | grep -q 'crypt' ) && [[ $(lsblk | grep "/mnt/boot$") == "" ]]; then
        echo "GRUB_ENABLE_CRYPTODISK=y" >> /mnt/etc/default/grub
    else
        true
    fi
}

# Function will not allow incorrect UUID type for installed system.
generate_fstab() {
Bernhard Landauer's avatar
Bernhard Landauer committed
508
    DIALOG " $_ConfBseFstab " --menu "\n$_FstabBody\n " 0 0 4 \
509 510 511 512 513 514 515
      "fstabgen -p" "$_FstabDevName" \
      "fstabgen -L -p" "$_FstabDevLabel" \
      "fstabgen -U -p" "$_FstabDevUUID" \
      "fstabgen -t PARTUUID -p" "$_FstabDevPtUUID" 2>${ANSWER}

    if [[ $(cat ${ANSWER}) != "" ]]; then
        if [[ $SYSTEM == "BIOS" ]] && [[ $(cat ${ANSWER}) == "fstabgen -t PARTUUID -p" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
516
            DIALOG " $_ErrTitle " --msgbox "\n$_FstabErr\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
517
        else
518 519 520
            $(cat ${ANSWER}) ${MOUNTPOINT} > ${MOUNTPOINT}/etc/fstab 2>$ERR
            check_for_error "$FUNCNAME" $?
            [[ -f ${MOUNTPOINT}/swapfile ]] && sed -i "s/\\${MOUNTPOINT}//" ${MOUNTPOINT}/etc/fstab
Bernhard Landauer's avatar
Bernhard Landauer committed
521
        fi
522
    fi
523 524
}

525 526 527 528 529 530 531 532 533 534
run_mkinitcpio() {
    clear
    # If LVM and/or LUKS used, add the relevant hook(s)
    ([[ $LVM -eq 1 ]] && [[ $LUKS -eq 0 ]]) && { sed -i 's/block filesystems/block lvm2 filesystems/g' ${MOUNTPOINT}/etc/mkinitcpio.conf 2>$ERR || check_for_error "lVM2 hooks" $?; }
    ([[ $LVM -eq 1 ]] && [[ $LUKS -eq 1 ]]) && { sed -i 's/block filesystems/block encrypt lvm2 filesystems/g' ${MOUNTPOINT}/etc/mkinitcpio.conf 2>$ERR || check_for_error "lVM/LUKS hooks" $?; }
    ([[ $LVM -eq 0 ]] && [[ $LUKS -eq 1 ]]) && { sed -i 's/block filesystems/block encrypt filesystems/g' ${MOUNTPOINT}/etc/mkinitcpio.conf 2>$ERR || check_for_error "LUKS hooks" $?; }
    
    arch_chroot "mkinitcpio -P" 2>$ERR
    check_for_error "$FUNCNAME" "$?"
}
535

536 537 538 539 540
# locale array generation code adapted from the Manjaro 0.8 installer
set_locale() {
    LOCALES=""
    for i in $(cat /etc/locale.gen | grep -v "#  " | sed 's/#//g' | sed 's/ UTF-8//g' | grep .UTF-8); do
        LOCALES="${LOCALES} ${i} -"
Matti Hyttinen's avatar
Matti Hyttinen committed
541
    done
542

Bernhard Landauer's avatar
Bernhard Landauer committed
543
    DIALOG " $_ConfBseSysLoc " --menu "\n$_localeBody\n " 0 0 12 ${LOCALES} 2>${ANSWER} || return 0
544 545 546 547 548 549 550

    LOCALE=$(cat ${ANSWER})

    echo "LANG=\"${LOCALE}\"" > ${MOUNTPOINT}/etc/locale.conf
    sed -i "s/#${LOCALE}/${LOCALE}/" ${MOUNTPOINT}/etc/locale.gen 2>$ERR
    arch_chroot "locale-gen" >/dev/null 2>$ERR
    check_for_error "$FUNCNAME" "$?"
patrick's avatar
patrick committed
551
    ini linux.locale "$LOCALE"
Matti Hyttinen's avatar
Matti Hyttinen committed
552 553 554 555
    if [[ -e /mnt/.openrc ]]; then 
        mkdir ${MOUNTPOINT}/etc/env.d
        echo "LANG=\"${LOCALE}\"" > ${MOUNTPOINT}/etc/env.d/02locale
    fi
556 557
}

558 559 560 561 562 563
# Set Zone and Sub-Zone
set_timezone() {
    ZONE=""
    for i in $(cat /usr/share/zoneinfo/zone.tab | awk '{print $3}' | grep "/" | sed "s/\/.*//g" | sort -ud); do
        ZONE="$ZONE ${i} -"
    done
564

Bernhard Landauer's avatar
Bernhard Landauer committed
565
    DIALOG " $_ConfBseTimeHC " --menu "\n$_TimeZBody\n " 0 0 10 ${ZONE} 2>${ANSWER} || return 1
566 567 568 569 570 571 572
    ZONE=$(cat ${ANSWER})

    SUBZONE=""
    for i in $(cat /usr/share/zoneinfo/zone.tab | awk '{print $3}' | grep "${ZONE}/" | sed "s/${ZONE}\///g" | sort -ud); do
        SUBZONE="$SUBZONE ${i} -"
    done

Bernhard Landauer's avatar
Bernhard Landauer committed
573
    DIALOG " $_ConfBseTimeHC " --menu "\n$_TimeSubZBody\n " 0 0 11 ${SUBZONE} 2>${ANSWER} || return 1
574 575
    SUBZONE=$(cat ${ANSWER})

Bernhard Landauer's avatar
Bernhard Landauer committed
576
    DIALOG " $_ConfBseTimeHC " --yesno "\n$_TimeZQ ${ZONE}/${SUBZONE}?\n " 0 0
577 578 579
    if (( $? == 0 )); then
        arch_chroot "ln -sf /usr/share/zoneinfo/${ZONE}/${SUBZONE} /etc/localtime" 2>$ERR
        check_for_error "$FUNCNAME ${ZONE}/${SUBZONE}" $?
patrick's avatar
patrick committed
580
        ini linux.zone "${ZONE}/${SUBZONE}"
Bernhard Landauer's avatar
Bernhard Landauer committed
581 582
    else
        return 1
583
    fi
584
}
585

586
set_hw_clock() {
Bernhard Landauer's avatar
Bernhard Landauer committed
587
    DIALOG " $_ConfBseTimeHC " --menu "\n$_HwCBody\n " 0 0 2 \
588 589 590 591 592 593
    "utc" "-" \
    "localtime" "-" 2>${ANSWER}

    if [[ $(cat ${ANSWER}) != "" ]]; then
        arch_chroot "hwclock --systohc --$(cat ${ANSWER})"  2>$ERR
        check_for_error "$FUNCNAME" "$?"
patrick's avatar
patrick committed
594
        ini linux.time "$ANSWER"
595 596 597
    fi
}

598
set_hostname() {
Bernhard Landauer's avatar
Bernhard Landauer committed
599
    DIALOG " $_ConfBseHost " --inputbox "\n$_HostNameBody\n " 0 0 "manjaro" 2>${ANSWER} || return 0
600 601 602 603 604

    echo "$(cat ${ANSWER})" > ${MOUNTPOINT}/etc/hostname 2>$ERR
    echo -e "#<ip-address>\t<hostname.domain.org>\t<hostname>\n127.0.0.1\tlocalhost.localdomain\tlocalhost\t$(cat \
      ${ANSWER})\n::1\tlocalhost.localdomain\tlocalhost\t$(cat ${ANSWER})" > ${MOUNTPOINT}/etc/hosts 2>$ERR
    check_for_error "$FUNCNAME"
patrick's avatar
patrick committed
605
    ini linux.hostname "$ANSWER"
606 607
}

608 609
# Adapted and simplified from the Manjaro 0.8 and Antergos 2.0 installers
set_root_password() {
Bernhard Landauer's avatar
Bernhard Landauer committed
610
    DIALOG " $_ConfUsrRoot " --clear --insecure --passwordbox "\n$_PassRtBody\n " 0 0 \
611 612
      2> ${ANSWER} || return 0
    PASSWD=$(cat ${ANSWER})
613

Bernhard Landauer's avatar
Bernhard Landauer committed
614
    DIALOG " $_ConfUsrRoot " --clear --insecure --passwordbox "\n$_PassReEntBody\n " 0 0 \
615 616
      2> ${ANSWER} || return 0
    PASSWD2=$(cat ${ANSWER})
617

618 619 620
    if [[ $PASSWD == $PASSWD2 ]]; then
        echo -e "${PASSWD}\n${PASSWD}" > /tmp/.passwd
        arch_chroot "passwd root" < /tmp/.passwd >/dev/null 2>$ERR
patrick's avatar
patrick committed
621
        check_for_error "$FUNCNAME" $?
622 623
        rm /tmp/.passwd
    else
Bernhard Landauer's avatar
Bernhard Landauer committed
624
        DIALOG " $_ErrTitle " --msgbox "\n$_PassErrBody\n " 0 0
625
        set_root_password
626
    fi
627 628 629 630
}

# Originally adapted from the Antergos 2.0 installer
create_new_user() {
Bernhard Landauer's avatar
Bernhard Landauer committed
631
    DIALOG " $_NUsrTitle " --inputbox "\n$_NUsrBody\n " 0 0 "" 2>${ANSWER} || return 0
632 633 634 635 636 637 638 639
    USER=$(cat ${ANSWER})

    # Loop while user name is blank, has spaces, or has capital letters in it.
    while [[ ${#USER} -eq 0 ]] || [[ $USER =~ \ |\' ]] || [[ $USER =~ [^a-z0-9\ ] ]]; do
        DIALOG " $_NUsrTitle " --inputbox "$_NUsrErrBody" 0 0 "" 2>${ANSWER} || return 0
        USER=$(cat ${ANSWER})
    done

640
    shell=""
Bernhard Landauer's avatar
Bernhard Landauer committed
641
    DIALOG " $_NUsrTitle " --radiolist "\n$_DefShell\n$_UseSpaceBar\n " 0 0 3 \
642 643 644
      "zsh" "-" on \
      "bash" "-" off \
      "fish" "-" off 2>/tmp/.shell
Matti Hyttinen's avatar
Matti Hyttinen committed
645
    shell_choice=$(cat /tmp/.shell)
646

Matti Hyttinen's avatar
Matti Hyttinen committed
647
    case ${shell_choice} in
648
        "zsh") [[ ! -e /mnt/etc/skel/.zshrc ]] && basestrap ${MOUNTPOINT} manjaro-zsh-config
649
            shell=/usr/bin/zsh
650
            ;;
651 652
        "fish") [[ ! -e /mnt/usr/bin/fish ]] && basestrap ${MOUNTPOINT} fish
            shell=/usr/bin/fish
653
            ;;
654
        "bash") shell=/bin/bash
655 656 657 658
            ;;
    esac
    check_for_error "default shell: [${shell}]"

659
    # Enter password. This step will only be reached where the loop has been skipped or broken.
Bernhard Landauer's avatar
Bernhard Landauer committed
660
    DIALOG " $_ConfUsrNew " --clear --insecure --passwordbox "\n$_PassNUsrBody $USER\n " 0 0 \
661 662 663
      2> ${ANSWER} || return 0
    PASSWD=$(cat ${ANSWER})

Bernhard Landauer's avatar
Bernhard Landauer committed
664
    DIALOG " $_ConfUsrNew " --clear --insecure --passwordbox "\n$_PassReEntBody\n " 0 0 \
665 666 667 668 669
      2> ${ANSWER} || return 0
    PASSWD2=$(cat ${ANSWER})

    # loop while passwords entered do not match.
    while [[ $PASSWD != $PASSWD2 ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
670
        DIALOG " $_ErrTitle " --msgbox "\n$_PassErrBody\n " 0 0
671

Bernhard Landauer's avatar
Bernhard Landauer committed
672
        DIALOG " $_ConfUsrNew " --clear --insecure --passwordbox "\n$_PassNUsrBody $USER\n " 0 0 \
673 674 675
          2> ${ANSWER} || return 0
        PASSWD=$(cat ${ANSWER})

Bernhard Landauer's avatar
Bernhard Landauer committed
676
        DIALOG " $_ConfUsrNew " --clear --insecure --passwordbox "\n_PassReEntBody\n " 0 0 \
677 678
          2> ${ANSWER} || return 0
        PASSWD2=$(cat ${ANSWER})
679 680
    done

681
    # create new user. This step will only be reached where the password loop has been skipped or broken.
Bernhard Landauer's avatar
Bernhard Landauer committed
682
    DIALOG " $_ConfUsrNew " --infobox "\n$_NUsrSetBody\n " 0 0
683 684
    sleep 2

patrick's avatar
patrick committed
685 686 687 688 689 690 691
    local list=$(ini linux.users)
    [[ -n "$list" ]] && list="${list};"
    ini linux.users "${list}${USER}"
    list=$(ini linux.shells)
    [[ -n "$list" ]] && list="${list};"
    ini linux.shells "${list}${shell}"

692 693
    # Create the user, set password, then remove temporary password file
    arch_chroot "groupadd ${USER}"
694
    arch_chroot "useradd ${USER} -m -g ${USER} -G wheel,storage,power,network,video,audio,lp -s $shell" 2>$ERR
695 696 697 698 699 700 701 702 703 704
    check_for_error "add user to groups" $?
    echo -e "${PASSWD}\n${PASSWD}" > /tmp/.passwd
    arch_chroot "passwd ${USER}" < /tmp/.passwd >/dev/null 2>$ERR
    check_for_error "create user pwd" $?
    rm /tmp/.passwd

    # Set up basic configuration files and permissions for user
    #arch_chroot "cp /etc/skel/.bashrc /home/${USER}"
    arch_chroot "chown -R ${USER}:${USER} /home/${USER}"
    [[ -e ${MOUNTPOINT}/etc/sudoers ]] && sed -i '/%wheel ALL=(ALL) ALL/s/^#//' ${MOUNTPOINT}/etc/sudoers
705
}