manjaro-arm-installer 23.3 KB
Newer Older
Strit's avatar
Strit committed
1 2
#! /bin/bash

3
# *****************************
Strit's avatar
Strit committed
4
#       Version 1.2.3
5 6
# *****************************

Strit's avatar
Strit committed
7
# Set globals
8
TMPDIR=/var/tmp/manjaro-arm-installer
9
ARCH='aarch64'
10
NSPAWN='systemd-nspawn -q --resolv-conf=copy-host --timezone=off -D'
Strit's avatar
Strit committed
11

Strit's avatar
Strit committed
12 13 14
# set colorscheme
export DIALOGRC="./dialogrc_gui"

15 16 17 18
# clearing variables
DEVICE=""
EDITION=""
USER=""
19
USERGROUPS=""
Strit's avatar
Strit committed
20
FULLNAME=""
21
PASSWORD=""
Strit's avatar
Strit committed
22 23
CONFIRMPASSWORD=""
CONFIRMROOTPASSWORD=""
24 25
ROOTPASSWORD=""
SDCARD=""
26 27
SDTYP=""
SDDEV=""
28
DEV_NAME=""
29 30
TIMEZONE=""
LOCALE=""
31

32
# check if root
33 34 35 36 37 38
if [ "$EUID" -ne 0 ]; then
    echo "*******************************************************************************************"
    echo "*                                                                                         *"
    echo "*     This script requires root permissions to run. Please run as root or with sudo!      *"
    echo "*                                                                                         *"
    echo "*******************************************************************************************"
39 40 41
  exit
fi

42
# Sanity checks for dependencies
43
if [ ! -f /usr/bin/git ]; then
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    echo "git command is missing! Please install git!"
    exit 1
    elif [ ! -f /usr/bin/parted ]; then
    echo "parted command is missing! Please install parted!"
    exit 1
    elif [ ! -f /usr/bin/systemd-nspawn ]; then
    echo "systemd-nspawn command is missing! Please install systemd!"
    exit 1
    elif [ ! -f /usr/bin/wget ]; then
    echo "wget command is missing! Please install wget!"
    exit 1
    elif [ ! -f /usr/bin/dialog ]; then
    echo "dialog command is missing! Please install dialog!"
    exit 1
    elif [ ! -f /usr/bin/bsdtar ]; then
    echo "bsdtar command is missing! Please install libarchive!"
    exit 1
61
    elif [ ! -f /usr/bin/mkpasswd ]; then
62
    echo "mkpasswd command is missing! Please install debian-whois-mkpasswd!"
63 64 65 66
    exit 1
    elif [ ! -f /usr/bin/awk ]; then
    echo "awk command is missing! Please install gawk!"
    exit 1
67
fi
68

Strit's avatar
Strit committed
69 70

# Functions
71 72 73 74 75 76 77 78
msg() {
    ALL_OFF="\e[1;0m"
    BOLD="\e[1;1m"
    GREEN="${BOLD}\e[1;32m"
      local mesg=$1; shift
      printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
 }

79 80 81 82 83 84 85
info() {
    ALL_OFF="\e[1;0m"
    BOLD="\e[1;1m"
    BLUE="${BOLD}\e[1;34m"
      local mesg=$1; shift
      printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
 }
Strit's avatar
Strit committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

usage_build_installer() {
    echo "Usage: ${0##*/} [options]"
    echo '    -h                 This help'
    echo ''
    echo ''
    exit $1
}

get_timer(){
    echo $(date +%s)
}

# $1: start timer
elapsed_time(){
    echo $(echo $1 $(get_timer) | awk '{ printf "%0.2f",($2-$1)/60 }')
}

show_elapsed_time(){
    msg "Time %s: %s minutes..." "$1" "$(elapsed_time $2)"
}

108
getarmprofiles () {
109
    info "Getting package lists ready for $DEVICE $EDITION edition..."
110 111 112
    if ls $TMPDIR/arm-profiles/* 1> /dev/null 2>&1; then
        cd $TMPDIR/arm-profiles
        git pull 1> /dev/null 2>&1
113
    else
114 115
        mkdir -p $TMPDIR
        chmod 777 $TMPDIR
116 117
        cd $TMPDIR
        git clone https://gitlab.com/Strit/arm-profiles.git 1> /dev/null 2>&1
118 119 120 121 122 123
    fi

}

create_install() {
    msg "Creating install for $DEVICE..."
124
    msg "Used device is ${SDCARD}${SDDEV}x"
125 126
    
    # fetch and extract rootfs
127
    info "Downloading latest $ARCH rootfs..."
128
    cd $TMPDIR
129
    wget -q --show-progress --progress=bar:force:noscroll https://osdn.net/projects/manjaro-arm/storage/.rootfs/Manjaro-ARM-$ARCH-latest.tar.gz
130
    
131
    info "Extracting $ARCH rootfs..."
132
    bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Strit's avatar
Strit committed
133
    
134
    info "Setting up keyrings..."
Strit's avatar
Strit committed
135 136
    $NSPAWN $TMPDIR/root pacman-key --init 1> /dev/null 2>&1
    $NSPAWN $TMPDIR/root pacman-key --populate archlinuxarm manjaro manjaro-arm 1> /dev/null 2>&1
Strit's avatar
Strit committed
137
    
138
    info "Installing packages for $EDITION on $DEVICE..."
Strit's avatar
Strit committed
139
    # Install device and editions specific packages
140
    $NSPAWN $TMPDIR/root pacman -Syyu base $PKG_DEVICE $PKG_EDITION --noconfirm
Strit's avatar
Strit committed
141
        if [[ "$DEVICE" = "on2" ]] || [[ "$DEVICE" = "rpi4" ]] || [[ "$DEVICE" = "vim3" ]]; then
142
    if [[ "$EDITION" = "kde" ]] || [[ "$EDITION" = "cubocore" ]]; then
143 144
    $NSPAWN $TMPDIR/root pacman -R sddm sddm-kcm matcha-dynamic-theme --noconfirm
    $NSPAWN $TMPDIR/root pacman -S sddm-compat sddm-kcm matcha-dynamic-theme --noconfirm
145
    elif [[ "$EDITION" = "lxqt" ]]; then
146 147
    $NSPAWN $TMPDIR/root pacman -R sddm matcha-dynamic-theme --noconfirm
    $NSPAWN $TMPDIR/root pacman -S sddm-compat matcha-dynamic-theme --noconfirm
148 149
    fi
    fi
Strit's avatar
Strit committed
150
    
151
    info "Enabling services..."
Strit's avatar
Strit committed
152
    # Enable services
153
    $NSPAWN $TMPDIR/root systemctl enable getty.target haveged.service 1> /dev/null 2>&1
Strit's avatar
Strit committed
154
    $NSPAWN $TMPDIR/root systemctl enable $SRV_EDITION 1> /dev/null 2>&1
155 156 157
    if [ -f $TMPDIR/root/usr/bin/xdg-user-dirs-update ]; then
    $NSPAWN $TMPDIR/root systemctl --global enable xdg-user-dirs-update.service 1> /dev/null 2>&1
    fi
Strit's avatar
Strit committed
158

159
    info "Applying overlay for $EDITION..."
160
    cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
Strit's avatar
Strit committed
161
    
162
    info "Setting up users..."
Strit's avatar
Strit committed
163
    #setup users
164
    echo "$USER" > $TMPDIR/user
165 166 167 168 169 170 171
    echo "$PASSWORD" > $TMPDIR/password
    echo "$ROOTPASSWORD" > $TMPDIR/rootpassword

    info "setting password for root ..."
    $NSPAWN $TMPDIR/root awk -i inplace -F: "BEGIN {OFS=FS;} \$1 == \"root\" {\$2=\"$(mkpasswd --hash=SHA-512 $(cat $TMPDIR/rootpassword))\"} 1" /etc/shadow 1> /dev/null 2>&1

    info "Adding user..."
172
    $NSPAWN $TMPDIR/root useradd -m -g users -G wheel,sys,input,video,storage,lp,network,users,power -p $(mkpasswd --hash=SHA-512 $(cat $TMPDIR/password)) -s /bin/bash $(cat $TMPDIR/user) 1> /dev/null 2>&1
173
    $NSPAWN $TMPDIR/root usermod -aG $USERGROUPS $(cat $TMPDIR/user) 1> /dev/null 2>&1
174
    $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1
175

176
    info "Enabling user services..."
Strit's avatar
Strit committed
177
    if [[ "$EDITION" = "minimal" ]] || [[ "$EDITION" = "server" ]]; then
178
        msg "No user services for $EDITION edition"
Strit's avatar
Strit committed
179
    else
Strit's avatar
Strit committed
180
        $NSPAWN $TMPDIR/root --user $(cat $TMPDIR/user) systemctl --user enable pulseaudio.service 1> /dev/null 2>&1
Strit's avatar
Strit committed
181 182
    fi

183
    info "Setting up system settings..."
Strit's avatar
Strit committed
184
    #system setup
Strit's avatar
Strit committed
185
    $NSPAWN $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1
186 187 188 189
    rm -f $TMPDIR/root/etc/ssl/certs/ca-certificates.crt
    rm -f $TMPDIR/root/etc/ca-certificates/extracted/tls-ca-bundle.pem
    cp -a /etc/ssl/certs/ca-certificates.crt $TMPDIR/root/etc/ssl/certs/
    cp -a /etc/ca-certificates/extracted/tls-ca-bundle.pem $TMPDIR/root/etc/ca-certificates/extracted/
190 191
    $NSPAWN $TMPDIR/root timedatectl set-timezone $TIMEZONE 1> /dev/null 2>&1
    $NSPAWN $TMPDIR/root timedatectl set-ntp true 1> /dev/null 2>&1
Strit's avatar
Strit committed
192
    $NSPAWN $TMPDIR/root sed -i s/"#$LOCALE"/"$LOCALE"/g /etc/locale.gen 1> /dev/null 2>&1
193 194 195 196 197 198
    $NSPAWN $TMPDIR/root locale-gen 1> /dev/null 2>&1
    $NSPAWN $TMPDIR/root localectl set-locale $LOCALE 1> /dev/null 2>&1
    if [[ "$SYSTEM" != "Pinebook" ]]; then
    $NSPAWN $TMPDIR/root localectl set-keymap $KEYMAP 1> /dev/null 2>&1
    fi
    $NSPAWN $TMPDIR/root hostnamectl set-hostname $HOSTNAME 1> /dev/null 2>&1
199
    chown -R root:root $TMPDIR/root/etc 1> /dev/null 2>&1
200
    if [[ "$EDITION" != "minimal" && "$EDITION" != "server" ]]; then
201
    chown root:polkitd $TMPDIR/root/etc/polkit-1/rules.d 1> /dev/null 2>&1
202
    fi
Strit's avatar
Strit committed
203
    
204
    info "Doing device specific setups for $DEVICE..."
205
    if [[ "$DEVICE" = "rpi3" ]]; then
Strit's avatar
Strit committed
206 207 208
        echo "dtparam=audio=on" | tee --append $TMPDIR/root/boot/config.txt 1> /dev/null 2>&1
        echo "blacklist vchiq" | tee --append $TMPDIR/root/etc/modprobe.d/blacklist-vchiq.conf 1> /dev/null 2>&1
        echo "blacklist snd_bcm2835" | tee --append $TMPDIR/root/etc/modprobe.d/blacklist-vchiq.conf 1> /dev/null 2>&1
209
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
Strit's avatar
Strit committed
210 211
    elif [[ "$DEVICE" = "rpi4" ]]; then
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
212
        $NSPAWN $TMPDIR/root systemctl enable attach-bluetooth.service 1> /dev/null 2>&1
Strit's avatar
Strit committed
213 214
        # fix wifi
        sed -i s/'boardflags3=0x48200100'/'boardflags3=0x44200100'/ $TMPDIR/root/usr/lib/firmware/updates/brcm/brcmfmac43455-sdio.txt 1> /dev/null 2>&1
215
    elif [[ "$DEVICE" = "oc2" ]]; then
216
        $NSPAWN $TMPDIR/root systemctl enable amlogic.service 1> /dev/null 2>&1
217 218
    elif [[ "$DEVICE" = "on2" ]]; then
        $NSPAWN $TMPDIR/root systemctl disable dhcpcd.service 1> /dev/null 2>&1
219
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
Strit's avatar
Strit committed
220 221
    elif [[ "$DEVICE" = "vim3" ]]; then
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
222
        $NSPAWN $TMPDIR/root systemctl enable bluetooth-khadas.service 1> /dev/null 2>&1
223
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
Strit's avatar
Strit committed
224
        $NSPAWN $TMPDIR/root systemctl enable pinebook-post-install.service 1> /dev/null 2>&1
Strit's avatar
Strit committed
225
    else
Strit's avatar
Strit committed
226
        echo "No device specific setting needed for $DEVICE"
Strit's avatar
Strit committed
227 228
    fi
    
229
    info "Cleaning install for unwanted files..."
230
    rm $TMPDIR/root/usr/bin/qemu-aarch64-static
231 232 233 234
    rm -rf $TMPDIR/root/var/cache/pacman/pkg/*
    rm -rf $TMPDIR/root/var/log/*
    rm -rf $TMPDIR/root/usr/lib/systemd/system/systemd-firstboot.service
    rm -rf $TMPDIR/root/etc/machine-id
235 236
    
    # Remove temp files on host
237 238
    rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword
    rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz*
Strit's avatar
Strit committed
239

240
    msg "$DEVICE $EDITION install complete"
Strit's avatar
Strit committed
241 242 243
}

prepare_card () {
244
    msg "Getting $SDCARD ready for $DEVICE..."
Strit's avatar
Strit committed
245
    # For Raspberry Pi devices
Strit's avatar
Strit committed
246
    if [[ "$DEVICE" = "rpi3" ]] || [[ "$DEVICE" = "rpi4" ]]; then
247 248 249 250
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1

Strit's avatar
Strit committed
251
        #partition with boot and root
252 253
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 0% 100M 1> /dev/null 2>&1
254
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/start` 
Strit's avatar
Strit committed
255
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
256
        END_SECTOR=$(expr $START + $SIZE)
257 258
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
259 260
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
261 262
        
        #Mount SD card
Strit's avatar
Strit committed
263 264
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
265 266
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Strit's avatar
Strit committed
267 268

    # For Odroid devices
269
    elif [[ "$DEVICE" = "oc2" ]]; then
270 271
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
272
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
273

Strit's avatar
Strit committed
274
        #Clear first 8mb
275
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
276

Strit's avatar
Strit committed
277
    #partition with a single root partition
278 279 280
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary ext4 0% 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
281
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
282 283
        
        #Mount SD card
Strit's avatar
Strit committed
284
        mkdir -p $TMPDIR/root
285
        chmod 777 -R $TMPDIR/root
286
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
287 288
        
   elif [[ "$DEVICE" = "on2" ]]; then
Helmut Stult's avatar
Helmut Stult committed
289 290 291 292
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1

293
        #Clear first 8 mb
Strit's avatar
Strit committed
294
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
295 296
        
    #partition with 2 partitions
Strit's avatar
Strit committed
297 298
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 0% 256M 1> /dev/null 2>&1
299 300
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/start`
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
301
        END_SECTOR=$(expr $START + $SIZE)
Strit's avatar
Strit committed
302 303
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
304 305
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
306 307 308 309
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
310 311
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Strit's avatar
Strit committed
312 313 314 315 316 317 318 319 320 321 322 323 324
        
    # For Khadas devices
        elif [[ "$DEVICE" = "vim3" ]]; then
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
        
        #Clear first 8 mb
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
        
    #partition with 2 partitions
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 32M 256M 1> /dev/null 2>&1
325 326
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/start`
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
Strit's avatar
Strit committed
327 328 329
        END_SECTOR=$(expr $START + $SIZE)
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
330 331
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1>/dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
Strit's avatar
Strit committed
332 333 334 335
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
336 337
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Strit's avatar
Strit committed
338
        
Strit's avatar
Strit committed
339

340
    # For pine devices
341
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
342 343
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
344
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Strit's avatar
Strit committed
345 346

    #Clear first 8mb
347
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
348

Strit's avatar
Strit committed
349
    #partition with a single root partition
350 351 352
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary ext4 0% 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
353
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
354 355
        
        # Mount SD card
Strit's avatar
Strit committed
356
        mkdir -p $TMPDIR/root
357
        chmod 777 -R $TMPDIR/root
358
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
359

Strit's avatar
Strit committed
360
    # For rockpro64 device
361
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]; then
362 363
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
364
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Strit's avatar
Strit committed
365 366

    #Clear first 8mb
367
        dd if=/dev/zero of=${SDCARD} bs=1M count=32 1> /dev/null 2>&1
368

Strit's avatar
Strit committed
369
    #partition with a single root partition
370 371 372
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary ext4 32M 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
373
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
374 375
        
        # Mount SD card
Strit's avatar
Strit committed
376
        mkdir -p $TMPDIR/root
377
        chmod 777 -R $TMPDIR/root
378
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
379 380 381 382 383 384
    fi
}

cleanup () {
    msg "Writing bootloader and cleaning up after install..."
   # For Raspberry Pi devices
Strit's avatar
Strit committed
385
    if [[ "$DEVICE" = "rpi3" ]] || [[ "$DEVICE" = "rpi4" ]]; then
386
       #move boot files
387
        mv $TMPDIR/root/boot/* $TMPDIR/boot
388 389
        
        #clean up
390 391 392 393
        umount $TMPDIR/root
        umount $TMPDIR/boot
        rm -r $TMPDIR/root $TMPDIR/boot
        partprobe $SDCARD 1> /dev/null 2>&1
394
        
395
    # For Odroid devices
396
    elif [[ "$DEVICE" = "oc2" ]]; then
397 398
    #flash bootloader
        cd $TMPDIR/root/boot/
399
        ./sd_fusing.sh $SDCARD 1> /dev/null 2>&1
400 401 402
        cd ~

    #clean up
403 404 405
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
406
        
407 408 409 410 411 412 413 414 415 416 417 418
    elif [[ "$DEVICE" = "on2" ]]; then
    
    # move boot files
        mv $TMPDIR/root/boot/* $TMPDIR/boot
    #flash bootloader
        dd if=$TMPDIR/boot/u-boot.bin of=${SDCARD} conv=fsync,notrunc bs=512 seek=1 1> /dev/null 2>&1
        
    #clean up
        umount $TMPDIR/root
        umount $TMPDIR/boot
        losetup -d $LDEV 1> /dev/null 2>&1
        rm -r $TMPDIR/root $TMPDIR/boot
Strit's avatar
Strit committed
419 420 421 422
        partprobe $SDCARD 1> /dev/null 2>&1
        
    # For Khadas devices
    elif [[ "$DEVICE" = "vim3" ]]; then
Strit's avatar
Strit committed
423
   #clean up
Strit's avatar
Strit committed
424 425 426 427 428
        umount $TMPDIR/root
        umount $TMPDIR/boot
        losetup -d $LDEV 1> /dev/null 2>&1
        rm -r $TMPDIR/root $TMPDIR/boot
        partprobe $SDCARD 1> /dev/null 2>&1
429
        
430
    # For pinebook device
431
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
432
    #flash bootloader
433
        dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1
434 435
        
    #clean up
436 437 438
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
Strit's avatar
Strit committed
439
        
440 441
        
    # For rockpro64 device
442
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]; then
Strit's avatar
Strit committed
443
    #flash bootloader
444 445 446
        dd if=$TMPDIR/root/boot/idbloader.img of=${SDCARD} seek=64 conv=notrunc 1> /dev/null 2>&1
        dd if=$TMPDIR/root/boot/uboot.img of=${SDCARD} seek=16384 conv=notrunc 1> /dev/null 2>&1
        dd if=$TMPDIR/root/boot/trust.img of=${SDCARD} seek=24576 conv=notrunc 1> /dev/null 2>&1
Strit's avatar
Strit committed
447 448
        
    #clean up
449 450 451
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
452 453
        
fi
Strit's avatar
Strit committed
454 455
}

456 457

# Using Dialog to ask for user input for variables
458
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
Helmut Stult's avatar
Helmut Stult committed
459
        --menu "Choose a device:" 20 75 10 \
Strit's avatar
Strit committed
460
        "rpi4"      "Rapsberry Pi 4" \
461 462 463 464
		"pinebook"  "Pinebook" \
		"rock64"    "ROCK64" \
		"rockpro64" "ROCKPro64" \
		"sopine"    "SOPINE / PINE A64-LTS" \
465
		"pine64"    "Pine64+" \
466
		"oc2"       "Odroid C2" \
467
		"on2"       "Odroid N2" \
Strit's avatar
Strit committed
468
		"vim3"      "Khadas Vim 3" \
469
		"rockpi4"   "Rock Pi 4" \
Helmut Stult's avatar
Helmut Stult committed
470
		"rpi3"      "Raspberry Pi 3" \
471
		3>&1 1>&2 2>&3 3>&-)
472 473 474


#The if statement makes sure that the user has put in something in the previous prompt. If not (left blank or pressed cancel) the script will end
Helmut Stult's avatar
Helmut Stult committed
475 476 477 478 479
if [ ! -z "$DEVICE" ]; then
	EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose an edition:" 20 75 10 \
		"minimal"   "Minimal Edition            (only CLI)" \
		"kde"       "Full KDE/Plasma Desktop    (full featured)" \
480
		"xfce"      "Full XFCE desktop and apps (full featured)" \
481
		"mate"      "Full MATE desktop and apps (lightweight)" \
Helmut Stult's avatar
Helmut Stult committed
482 483 484 485
		"lxqt"      "Full LXQT Desktop and apps (lightweight)" \
		"i3"        "Mininal i3 WM with apps    (very light)" \
		"cubocore"  "QT based Desktop           (lightweight)" \
		3>&1 1>&2 2>&3 3>&-) 
486 487 488 489 490 491 492

else 
	clear
	exit 1
fi


Helmut Stult's avatar
Helmut Stult committed
493 494
if [ ! -z "$EDITION" ]; then
	USER=$(dialog --clear --title "Manjaro ARM Installer" \
495 496
	--inputbox "Enter username:
(usernames most be all lowercase)" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
497
	3>&1 1>&2 2>&3 3>&-)
Strit's avatar
Strit committed
498 499 500 501 502
    if [[ "$USER" =~ [A-Z] ]] || [[ "$USER" == *['!'@#\$%^\&*()_+]* ]]; then
    clear
    msg "Configuration aborted! Username contained invalid characters."
    exit 1
    fi
503 504 505 506 507
else 
	clear
	exit 1
fi

508 509
if [ ! -z "$USER" ]
then
510 511 512 513 514 515 516 517 518 519 520 521 522
USERGROUPS=$(dialog --clear --title "Manjaro ARM Installer" \
    --inputbox "Enter additional groups for $USER in a comma seperated list: (empty if none)
(default groups: wheel,sys,storage,lp,network,users,power)" 8 50 \
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi

if [ ! -z "$USER" ]
then
FULLNAME=$(dialog --clear --title "Manjaro ARM Installer" \
523 524 525 526 527 528 529
    --inputbox "Enter Full Name for $USER:" 8 50 \
        3>&1 1>&2 2>&3 3>&- \
	)
else
    clear
    exit 1
fi
530 531


532
if [ ! -z "$FULLNAME" ]; then
Helmut Stult's avatar
Helmut Stult committed
533
	PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
534
	--insecure --passwordbox "Enter Password for $USER:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
535
	3>&1 1>&2 2>&3 3>&- \
536 537 538 539 540 541
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
542 543
if [ ! -z "$PASSWORD" ]; then
	CONFIRMPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Strit's avatar
Strit committed
544
	--insecure --passwordbox "Confirm Password for $USER:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
545
	3>&1 1>&2 2>&3 3>&- \
Strit's avatar
Strit committed
546 547 548 549 550
	)
else 
	clear
	exit 1
fi
551

Strit's avatar
Strit committed
552
if [[ "$PASSWORD" != "$CONFIRMPASSWORD" ]]; then
Helmut Stult's avatar
Helmut Stult committed
553 554 555
	clear
	msg "User passwords do not match! Please restart the installer and try again."
	exit 1
Strit's avatar
Strit committed
556
fi
557

Helmut Stult's avatar
Helmut Stult committed
558 559
if [ ! -z "$CONFIRMPASSWORD" ]; then
	ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
560
	--insecure --passwordbox "Enter Root Password:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
561
	3>&1 1>&2 2>&3 3>&- \
562 563 564 565 566 567
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
568 569
if [ ! -z "$ROOTPASSWORD" ]; then
	CONFIRMROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Strit's avatar
Strit committed
570
	--insecure --passwordbox "Confirm Root Password:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
571
	3>&1 1>&2 2>&3 3>&- \
Strit's avatar
Strit committed
572 573 574 575 576
	)
else 
	clear
	exit 1
fi
577

Strit's avatar
Strit committed
578
if [[ "$ROOTPASSWORD" != "$CONFIRMROOTPASSWORD" ]]; then
Helmut Stult's avatar
Helmut Stult committed
579 580 581
	clear
	msg "Root passwords do not match! Please restart the installer and try again."
	exit 1
Strit's avatar
Strit committed
582
fi
583

Strit's avatar
Strit committed
584
if [ ! -z "$CONFIRMROOTPASSWORD" ]
585 586 587
then

# simple command to put the results of lsblk (just the names of the devices) into an array and make that array populate the options	
Helmut Stult's avatar
Helmut Stult committed
588 589 590 591 592 593 594
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( lsblk -dn -o NAME )
	SDCARD=$(dialog --title "Manjaro ARM Installer" \
595
	--menu "Choose your SDCard - Be sure the correct drive is selected!" 20 50 10 \
Helmut Stult's avatar
Helmut Stult committed
596
	"${W[@]}" 3>&2 2>&1 1>&3)
597 598

# add /dev/ to the selected option above
Helmut Stult's avatar
Helmut Stult committed
599 600 601
	DEV_NAME=$SDCARD
	SDCARD=/dev/$SDCARD
	SDTYP=${SDCARD:5:2}
602 603 604
else 
	clear
	exit 1
Helmut Stult's avatar
Helmut Stult committed
605
fi
606

Helmut Stult's avatar
Helmut Stult committed
607 608 609 610 611 612 613
if [[ "$SDTYP" = "sd" ]]; then
	SDDEV=""
elif [[ "$SDTYP" = "mm" ]]; then
	SDDEV="p"
else 
	clear
	exit 1
614 615 616
fi


Helmut Stult's avatar
Helmut Stult committed
617 618 619 620 621 622 623 624 625 626
if [ ! -z "$SDCARD" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( timedatectl list-timezones )
	TIMEZONE=$(dialog --clear --title "Manjaro ARM Installer" \
	--menu "Choose your timezone!" 20 50 15 \
	"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
627 628 629 630 631
	)
else 
	clear
	exit 1
fi
632 633


Helmut Stult's avatar
Helmut Stult committed
634 635 636 637 638 639 640 641 642 643 644
if [ ! -z "$TIMEZONE" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( cat /etc/locale.gen | grep "UTF-8" | tail -n +2 | awk '{print $1}' | sed -e 's/^#*//' )
	LOCALE=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose your locale!" 20 50 15 \
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
		)
645 646 647 648 649
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
650 651 652 653 654 655 656 657 658 659
if [ ! -z "$LOCALE" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( localectl list-keymaps )
	KEYMAP=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose your keyboard layout:" 20 50 15 \
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
660 661 662 663 664 665
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
666 667
if [ ! -z "$KEYMAP" ]; then
	HOSTNAME=$(dialog --clear --title "Manjaro ARM Installer" \
668
	--inputbox "Enter desired hostname for this system:" 8 50 \
669
	3>&1 1>&2 2>&3 3>&- \
670 671 672 673 674 675
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
676 677
if [ ! -z "$HOSTNAME" ]; then
	dialog --clear --title "Manjaro ARM Installer" \
678 679
    --yesno "Is the below information correct:
    Username = $USER
680
    Additional usergroups = $USERGROUPS
681 682
    Password for $USER = $PASSWORD
    Password for root = $ROOTPASSWORD
683
    SD Card = $SDCARD
684
    Timezone = $TIMEZONE
685 686
    Locale = $LOCALE
    Keyboard layout = $KEYMAP
Helmut Stult's avatar
Helmut Stult committed
687
    Hostname = $HOSTNAME" 20 70 \
688 689
    3>&1 1>&2 2>&3 3>&-
else
Helmut Stult's avatar
Helmut Stult committed
690 691
	clear
	exit 1
692 693 694 695 696 697 698 699 700
fi

response=$?
case $response in
   0) clear; msg "Proceeding....";;
   1) clear; msg "Installation aborted...."; exit 1;;
   255) clear; msg "Installation aborted..."; exit 1;;
esac

701

702 703
# get the profiles
getarmprofiles
Strit's avatar
Strit committed
704 705

#Package lists
706 707 708
PKG_DEVICE=$(grep "^[^#;]" $TMPDIR/arm-profiles/devices/$DEVICE | awk '{print $1}')
PKG_EDITION=$(grep "^[^#;]" $TMPDIR/arm-profiles/editions/$EDITION | awk '{print $1}')
SRV_EDITION=$(grep "^[^#;]" $TMPDIR/arm-profiles/services/$EDITION | awk '{print $1}')
Strit's avatar
Strit committed
709

710

Strit's avatar
Strit committed
711
# Commands
712
timer_start=$(get_timer)
Strit's avatar
Strit committed
713 714

prepare_card
715 716 717 718
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync