summaryrefslogtreecommitdiff
path: root/iso/root/installer.sh
blob: d2654aca5efc36295730e226dfa88f423cc715ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/bin/sh

. /usr/lib/xitui.sh
. /usr/lib/glyphs.sh
. /usr/lib/colors.sh

DEBUG_MODE=false

logfile="installer.log"
default_packages="base xipkg dracut grub doas neofetch vim tzdata mksh"
additional_services="networkmanager xorg iwd"
cmdline=""

OPT_KERNEL="linux-libre"
OPT_BOOTSYSTEM="efi"
OPT_ENCRYPTION="none"

welcome_splash () {
    t_radio "Welcome to the xilinux installer" "install xilinux" "network configuration" "exit to shell"

    case "$T_RESULT" in
        "network configuration")
            t_clean
            nmtui
            t_init
            welcome_splash
            ;;
        "install xilinux")
            return 0
            ;;
        *)
            t_clean
            exit 0
            ;;
    esac
}


list_disks () {
    lsblk -r | while read -r line; do
        set - $line
        [ "$6" = "disk" ] && {
            printf '"/dev/%s (%s)" ' $1 $4
        }
    done
}

list_partitions () {
    lsblk $1 -nlp -o NAME
    echo "none"
}

partition_disk () {
    t_msg "Partitioning $1..."

    # if is efi, create an efi partition
    efi=
    [ "$OPT_BOOTSYSTEM" = "efi" ] && {
        export EFI_PART=$11
        export SYS_PART=$12
        efi="type=ef, start=2048, size=210000"
    } || {
        export SYS_PART=$11
    }

    export SWAP_PART=none

    echo "
    unit: sectors
    sector-size: 512

    $efi
    type=83
    " | sfdisk $1 >>$logfile &&
    t_msg "Partitioned $1!"
}

detect_efi () {
    [ -d "/sys/firmware/efi" ] \
        && OPT_BOOTSYSTEM="efi" \
        || OPT_BOOTSYSTEM="bios"

    t_yesno "Detected boot system as '$OPT_BOOTSYSTEM', is this correct?" || {
        [ $OPT_BOOTSYSTEM = "efi" ] \
            && OPT_BOOTSYSTEM="bios" \
            || OPT_BOOTSYSTEM="efi"
    }
    return 0
}

encrypt_disk () {
    #t_yesno "${LIGHT_RED}Warning this will ${RED}irreversibly ${LIGHT_RED} erase all data.\n${BLUE}continue?" || {
        #partition_disks
        #return 1
    #}
    #t_msg "Wiping disk $ROOT_DISK..."
    #printf "YES\n" | cryptsetup open --type plain -d /dev/urandom $ROOT_DISK to_be_wiped >> $logfile
    #dd if=/dev/zero of=/dev/mapper/to_be_wiped >> $logfile
    #cryptsetup close to_be_wiped >> $logfile

    default_packages="$default_packages crypsetup lvm2"

    partition_disk $ROOT_DISK
    enter_password "disk encryption password"
    t_msg "Encrypting $ROOT_DISK.."
    printf "$passwd" | cryptsetup luksFormat $SYS_PART -d ->> $logfile

    printf "$passwd" | cryptsetup luksOpen $SYS_PART cryptlvm -d - >> $logfile

    pvcreate /dev/mapper/cryptlvm >> $logfile
    vgcreate xilinux /dev/mapper/cryptlvm >> $logfile
    
    lvcreate -l 100%FREE xilinux -n root >> $logfile

    uuid="$(blkid --probe --output value --match-tag UUID $SYS_PART)"

    cmdline="rd.luks.uuid=luks-$uuid rd.lvm.lv=xilinux/root  root=/dev/xilinux/root rootflags=rw,relatime"
    default_packages="$default_packages lvm2 cryptsetup"

    export SYS_PART=/dev/xilinux/root
    return 0
}

partition_disks () {
    # need to use eval to work with spaces in line names
    eval "t_radio 'Select install disk' $(list_disks)"
    local selected=$(echo $T_RESULT | cut -d' ' -f1)
    export ROOT_DISK=$selected

    t_yesno "${BLUE}Auto-partition $selected disk?\n${RED}(Warning: existing data will be overwritten)" &&  {
        t_yesno "${BLUE}Use full disk encryption?" && {
            export OPT_ENCRYPTION="lvm-luks"
            encrypt_disk $ROOT_DISK
            return 0
        } || {
            partition_disk $selected || return 1
        }
    } || {
        cfdisk $selected && {
            t_radio 'Select primary system partition' $(list_partitions $selected)
            export SYS_PART=$T_RESULT

            [ $OPT_BOOTSYSTEM = "efi" ] && {
                t_radio 'Select efi system partition' $(list_partitions $selected)
                export EFI_PART=$T_RESULT
            }

            t_radio 'Select swap partition' $(list_partitions $selected)
            export SWAP_PART=$T_RESULT
        }
    }
}

format_disks () {
    t_msg "Formatting partitions...
${TABCHAR}System Partition
${TABCHAR}EFI Partition
"


    [ -b "$SYS_PART" ] && mkfs.ext4 $SYS_PART >> $logfile
    t_msg "Formatting partitions...
${GREEN}${TABCHAR}System partition ${CHECKMARK} (ext4)
${TABCHAR}EFI Partition
"

    [ -b "$EFI_PART" ] && mkfs.fat -F 32 $EFI_PART >> $logfile
    t_msg "Formatting partitions...
${GREEN}${TABCHAR}System partition ${CHECKMARK} (ext4)
${GREEN}${TABCHAR}EFI Partition ${CHECKMARK} (fat32)
"

    [ -b "$SWAP_PART" ] && mkswap $SWAP_PART >> $logfile
    return 0
}

mount_disks () {
    t_msg "Mounting disks..."
    export sysroot=/xilinux.mnt
    export efi_mntpoint=/xilinux.mnt/boot

    [ ! -f "$sysroot" ] && mkdir -p $sysroot

    [ -b "$SYS_PART" ] && {
        mount $SYS_PART $sysroot
        true
    } ||  {
        t_prompt "${RED}No system partition is available!"
        return 1
    }

    echo "[efi] mounting $EFI_PART to ${efi_mntpoint}" >> $logfile
    [ -b "$EFI_PART" ] && {
        mkdir -p $efi_mntpoint
        mount $EFI_PART $efi_mntpoint
        true
    }

    [ -b "$SWAP_PART" ] && swapon $SWAP_PART
    return 0
}

prompt_kernel () {
    t_radio "Select which kernel you would like to use:" "linux-libre" "linux"
    OPT_KERNEL=$T_RESULT
}

bootstrap_system () {
    t_msg "Creating directories..."
    xi -qvy -r $sysroot bootstrap >> $logfile
}

install_base () {
    {
        t_msg "Syncing repos..."
        xi -qvy -r $sysroot sync >> $logfile 
        for pkg in $default_packages $OPT_KERNEL; do 
            t_msg "${BLUE}Installing packages...${RESET}$pkg"
            xi -qlvy -r $sysroot install $pkg >> $logfile
        done
    } 
}

copy_resolvconf () {
    [ ! -d $sysroot/etc ] && mkdir $sysroot/etc
    cp /etc/resolv.conf $sysroot/etc/resolv.conf
}

sync_system () {
    t_msg "Syncing system..."
    xichroot $sysroot xi sync >> $logfile
    return 0
}

generate_fstab () {
    t_msg "Generating fstab..."
    xichroot $sysroot genfstab -U / > $sysroot/etc/fstab
}

build_initramfs () {

    mkdir -p $sysroot/var/tmp
    for kernel_version in $(ls $sysroot/usr/lib/modules); do 
        t_msg "Building initramfs for $kernel_version..."
        xichroot $sysroot dracut --kver $kernel_version 2>> $logfile >> $logfile
    done
}

install_grub () {
    t_yesno "Install grub?" && {
        [ "$OPT_BOOTSYSTEM" = "efi" ] && {
                target="x86_64-efi"
                opts="--target=$target --efi-directory=${efi_mntpoint#$sysroot}" 
                t_yesno "Install as removable system?" && opts="--removable $opts " || true
            } || {
                target="i386-pc"
                opts="--target=$target $ROOT_DISK"
            }

        t_msg "Installing grub for target $target..."
        echo "[grub] installing with $opts" >> $logfile
        xichroot $sysroot grub-install $opts >> $logfile

        t_msg "Creating grub configuration..."
        sed -i "s@GRUB_CMDLINE_LINUX_DEFAULT=\".*\"@GRUB_CMDLINE_LINUX_DEFAULT=\"$cmdline\"@g" $sysroot/etc/default/grub 
        xichroot $sysroot grub-mkconfig -o /boot/grub/grub.cfg

        t_prompt "Installed grub for $target"
    } || return 0
}

configure_hostname () {
    t_input "Enter hostname:"
    local hostname=$T_RESULT
    echo $hostname > $sysroot/etc/hostname

    cat > $sysroot/etc/hosts << EOF
127.0.0.1   localhost
::1         localhost
127.0.1.1   $hostname.local $hostname
EOF
}

enter_password () {
    local label=${1:-password}
    export passwd=""
    t_input_hidden "Enter $label:"
    passwd=$T_RESULT
    t_input_hidden "Confirm $label"
    local cpasswd=$T_RESULT

    [ "$passwd" = "$cpasswd" ] || {
        t_prompt "Passwords do not match!"
        enter_password $*
    }

}

configure_users () {
    t_input "Enter username:"
    local username=$T_RESULT
    enter_password

    t_msg "Creating user..."
    xichroot $sysroot useradd -s /bin/mksh -m $username
    printf "$passwd\n$passwd\n" | xichroot $sysroot passwd $username

    t_yesno "Allow this user to use doas?" && {
        mkdir -p $sysroot/etc/doas.d/
        echo "permit persist david" >> $sysroot/etc/doas.d/doas.conf
    }

    t_yesno "Set a password for the root user?" && {
        enter_password
        printf "$passwd\n$passwd\n" | xichroot $sysroot passwd
    }

    return 0
}

fix_permissions () {
    xichroot $sysroot chmod 755 /
    xichroot $sysroot chmod 755 /usr
    xichroot $sysroot chmod 755 /usr/bin
    xichroot $sysroot chmod 755 /usr/lib
}

set_timezone () {
    zoneinfo="$sysroot/usr/share/zoneinfo"
    cp  $zoneinfo/$1 $sysroot/etc/localtime
    echo "$1" > /etc/timezone

    t_cls_ptrn
    t_prompt "Successfully set timezone!"
}

select_timezone () {
    t_cls_ptrn
    zoneinfo="$sysroot/usr/share/zoneinfo"
    selection=$1
    t_paged_radio "Select your timezone: $selection" $(ls "$zoneinfo/$selection") "more..."

    selection="$selection/$T_RESULT"

    [ -f "$zoneinfo/$selection" ] && {

        t_yesno "Use $selection as your system timezone? " && {
            set_timezone $selection
        } || {
            select_timezone
        }
        return 0
    }

    [ -d "$zoneinfo/$selection" ] && {
        select_timezone $selection
    } || {
        t_prompt "The timezone you entered does not exist!"
        select_timezone
    }
}

install_additional () {
    t_check "Install and configure additional services: " $additional_services
    local services="$T_RESULT"

    for service in $services; do
        service_$service
    done
}

service_networkmanager () {
    t_msg "Installing NetworkManager..."
    {
        xi -qvly -r $sysroot install networkmanager 
        xichroot $sysroot rc-update add networkmanager
    } >> $logfile
}

service_iwd () {
    t_msg "Installing iwd..."
    {
        xi -qvly -r $sysroot install iwd 
        xichroot $sysroot rc-update add iwd
    } >> $logfile
}

service_xorg () {
    t_msg "Installing xorg..."
    xi -qvly -r $sysroot install base-xorg base-fonts >> $logfile
    t_check "Select video drivers:" $(xi search xf86-video- | cut -f2 -d/)
    t_msg "Installing $T_RESULT..."
    [ "${#T_RESULT}" != "0" ] && xi -r $sysroot install $T_RESULT >> $logfile
    t_prompt "Installed basic xorg functionality
TODO: preconfigured window managers, for now you need to configure them yourself"
}

prompt_shell () {
    t_yesno "Would you like to drop into the shell of your new system before rebooting?" && {
        t_clean
        xichroot $sysroot /bin/mksh -l
        t_init
        t_no_cur
        t_cls_ptrn
    }
    return 0
}

umount_disks () {
    t_msg "Unmounting disks..."
    umount -R $sysroot
    [ -b "$SWAP_PART" ] && swapoff $SWAP_PART
    [ "$OPT_ENCRYPTION" = "lvm-luks" ] && cryptsetup close cryptlvm
    return 0
}

prompt_reboot () {
    t_yesno "Install complete, reboot?" && {
        t_clean
        reboot & exit 0
    }
}

t_init
t_no_cur
checkroot 

steps="welcome_splash
detect_efi
partition_disks
format_disks
mount_disks
prompt_kernel
bootstrap_system
install_base
build_initramfs
install_grub
configure_hostname
select_timezone
copy_resolvconf
generate_fstab
configure_users
fix_permissions
install_additional
prompt_shell
umount_disks
prompt_reboot
"

$DEBUG_MODE && {
    echo > $logfile

    {
        tail -f $logfile | while read -r line; do
            t_tail $logfile 
        done 
    } &
}

for step in $steps; do
    t_cls_ptrn
    echo "[installer] doing step $step" >> $logfile
    $step 2>> $logfile || {
        t_prompt "${RED}An error occurred!"
        t_clean
        exit 1
    }
done
t_prompt "Completed install!"
t_clean