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

3
# *****************************
Helmut Stult's avatar
Helmut Stult committed
4
#       Version 1.2.1
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

12 13 14 15
# clearing variables
DEVICE=""
EDITION=""
USER=""
Strit's avatar
Strit committed
16
FULLNAME=""
17
PASSWORD=""
Strit's avatar
Strit committed
18 19
CONFIRMPASSWORD=""
CONFIRMROOTPASSWORD=""
20 21
ROOTPASSWORD=""
SDCARD=""
22 23
SDTYP=""
SDDEV=""
Helmut Stult's avatar
Helmut Stult committed
24
DEV_NAME=""
25 26
TIMEZONE=""
LOCALE=""
27

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

38
# Sanity checks for dependencies
39
if [ ! -f /usr/bin/git ]; then
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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
57
    elif [ ! -f /usr/bin/mkpasswd ]; then
58
    echo "mkpasswd command is missing! Please install debian-whois-mkpasswd!"
59 60 61 62
    exit 1
    elif [ ! -f /usr/bin/awk ]; then
    echo "awk command is missing! Please install gawk!"
    exit 1
63
fi
64

Strit's avatar
Strit committed
65 66

# Functions
67 68 69 70 71 72 73 74
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
 }

75 76 77 78 79 80 81
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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

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)"
}

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

}

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

155
    info "Applying overlay for $EDITION..."
156
    cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
Strit's avatar
Strit committed
157
    
158
    info "Setting up users..."
Strit's avatar
Strit committed
159
    #setup users
160
    echo "$USER" > $TMPDIR/user
161 162 163 164 165 166 167 168
    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..."
    $NSPAWN $TMPDIR/root useradd -m -g users -G wheel,storage,network,power -p $(mkpasswd --hash=SHA-512 $(cat $TMPDIR/password)) -s /bin/bash $(cat $TMPDIR/user) 1> /dev/null 2>&1
169
    $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1
170

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

178
    info "Setting up system settings..."
Strit's avatar
Strit committed
179
    #system setup
Strit's avatar
Strit committed
180
    $NSPAWN $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1
181 182 183 184
    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/
185 186
    $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
187
    $NSPAWN $TMPDIR/root sed -i s/"#$LOCALE"/"$LOCALE"/g /etc/locale.gen 1> /dev/null 2>&1
188 189 190 191 192 193
    $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
194
    chown -R root:root $TMPDIR/root/etc 1> /dev/null 2>&1
195
    if [[ "$EDITION" != "minimal" && "$EDITION" != "server" ]]; then
196
    chown root:polkitd $TMPDIR/root/etc/polkit-1/rules.d 1> /dev/null 2>&1
197
    fi
Strit's avatar
Strit committed
198
    
199
    info "Doing device specific setups for $DEVICE..."
200
    if [[ "$DEVICE" = "rpi3" ]]; then
Strit's avatar
Strit committed
201 202 203
        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
204
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
Strit's avatar
Strit committed
205 206 207 208 209 210
    elif [[ "$DEVICE" = "rpi4" ]]; then
    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
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
        # fix wifi
        sed -i s/'boardflags3=0x48200100'/'boardflags3=0x44200100'/ $TMPDIR/root/usr/lib/firmware/updates/brcm/brcmfmac43455-sdio.txt 1> /dev/null 2>&1
211
    elif [[ "$DEVICE" = "oc2" ]]; then
212
        $NSPAWN $TMPDIR/root systemctl enable amlogic.service 1> /dev/null 2>&1
213 214
    elif [[ "$DEVICE" = "on2" ]]; then
        $NSPAWN $TMPDIR/root systemctl disable dhcpcd.service 1> /dev/null 2>&1
215
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
Strit's avatar
Strit committed
216 217
    elif [[ "$DEVICE" = "vim3" ]]; then
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
Strit's avatar
Strit committed
218
    elif [[ "$DEVICE" = "pinebook" ]]; then
Strit's avatar
Strit committed
219
        $NSPAWN $TMPDIR/root systemctl enable pinebook-post-install.service 1> /dev/null 2>&1
Strit's avatar
Strit committed
220
    else
Strit's avatar
Strit committed
221
        echo "No device specific setting needed for $DEVICE"
Strit's avatar
Strit committed
222 223
    fi
    
224
    info "Cleaning install for unwanted files..."
225
    rm $TMPDIR/root/usr/bin/qemu-aarch64-static
226 227 228 229
    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
230 231
    
    # Remove temp files on host
232 233
    rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword
    rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz*
Strit's avatar
Strit committed
234

235
    msg "$DEVICE $EDITION install complete"
Strit's avatar
Strit committed
236 237 238
}

prepare_card () {
239
    msg "Getting $SDCARD ready for $DEVICE..."
Strit's avatar
Strit committed
240
    # For Raspberry Pi devices
Strit's avatar
Strit committed
241
    if [[ "$DEVICE" = "rpi3" ]] || [[ "$DEVICE" = "rpi4" ]]; then
242 243 244 245
        # 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
246
        #partition with boot and root
247 248
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 0% 100M 1> /dev/null 2>&1
249
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/start` 
Strit's avatar
Strit committed
250
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
251
        END_SECTOR=$(expr $START + $SIZE)
252 253
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
254 255
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
256 257
        
        #Mount SD card
Strit's avatar
Strit committed
258 259
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
260 261
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Strit's avatar
Strit committed
262 263

    # For Odroid devices
264
    elif [[ "$DEVICE" = "oc2" ]]; then
265 266
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
Helmut Stult's avatar
Helmut Stult committed
267
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
268

Strit's avatar
Strit committed
269
        #Clear first 8mb
270
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Helmut Stult's avatar
Helmut Stult committed
271

Strit's avatar
Strit committed
272
    #partition with a single root partition
273 274 275
        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
276
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
277 278
        
        #Mount SD card
Strit's avatar
Strit committed
279
        mkdir -p $TMPDIR/root
280
        chmod 777 -R $TMPDIR/root
281
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
282 283
        
   elif [[ "$DEVICE" = "on2" ]]; then
Helmut Stult's avatar
Helmut Stult committed
284 285 286 287
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1

288
        #Clear first 8 mb
Strit's avatar
Strit committed
289
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
290 291
        
    #partition with 2 partitions
Strit's avatar
Strit committed
292 293 294 295
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 0% 256M 1> /dev/null 2>&1
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}p1/start`
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}p1/size`
296
        END_SECTOR=$(expr $START + $SIZE)
Strit's avatar
Strit committed
297 298 299 300
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
        mkfs.vfat "${SDCARD}${SDDEV}p1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}p2" -L ROOT 1> /dev/null 2>&1
301 302 303 304
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
Strit's avatar
Strit committed
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
        mount ${SDCARD}${SDDEV}p1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}p2 $TMPDIR/root
        
    # 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
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}p1/start`
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}p1/size`
        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
        mkfs.vfat "${SDCARD}${SDDEV}p1" -n BOOT 1>/dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}p2" -L ROOT 1> /dev/null 2>&1
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
        mount ${SDCARD}${SDDEV}p1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}p2 $TMPDIR/root
        
Strit's avatar
Strit committed
334

335
    # For pine devices
336 337 338
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]]; then
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
Helmut Stult's avatar
Helmut Stult committed
339
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Strit's avatar
Strit committed
340 341

    #Clear first 8mb
342
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Helmut Stult's avatar
Helmut Stult committed
343

Strit's avatar
Strit committed
344
    #partition with a single root partition
345 346 347
        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
348
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
349 350
        
        # Mount SD card
Strit's avatar
Strit committed
351
        mkdir -p $TMPDIR/root
352
        chmod 777 -R $TMPDIR/root
353
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
354

Strit's avatar
Strit committed
355
    # For rockpro64 device
356
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]; then
357 358
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
Helmut Stult's avatar
Helmut Stult committed
359
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Strit's avatar
Strit committed
360 361

    #Clear first 8mb
362
        dd if=/dev/zero of=${SDCARD} bs=1M count=32 1> /dev/null 2>&1
Helmut Stult's avatar
Helmut Stult committed
363

Strit's avatar
Strit committed
364
    #partition with a single root partition
365 366 367
        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
368
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
369 370
        
        # Mount SD card
Strit's avatar
Strit committed
371
        mkdir -p $TMPDIR/root
372
        chmod 777 -R $TMPDIR/root
373
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
374 375 376 377 378 379
    fi
}

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

    #clean up
398 399 400
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
401
        
402 403 404 405 406 407 408 409 410 411 412 413
    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
414 415 416 417 418 419 420 421 422 423 424 425 426 427
        partprobe $SDCARD 1> /dev/null 2>&1
        
    # For Khadas devices
    elif [[ "$DEVICE" = "vim3" ]]; then
    #flash bootloader
        dd if=$TMPDIR/boot/u-boot.bin.sd.bin of=$SDCARD bs=1 count=444 1> /dev/null 2>&1
        dd if=$TMPDIR/boot/u-boot.bin.sd.bin of=$SDCARD bs=512 skip=1 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
        partprobe $SDCARD 1> /dev/null 2>&1
428
        
429
    # For pinebook device
430
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]]; then
431
    #flash bootloader
432
        dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1
433 434
        
    #clean up
435 436 437
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
Strit's avatar
Strit committed
438
        
439 440
        
    # For rockpro64 device
441
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]]; then
Strit's avatar
Strit committed
442
    #flash bootloader
443 444 445
        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
446 447
        
    #clean up
448 449 450
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
451 452
        
fi
Strit's avatar
Strit committed
453 454
}

455 456

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


#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
473 474 475 476 477
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)" \
478
		"mate"      "Full MATE desktop and apps (lightweight)" \
Helmut Stult's avatar
Helmut Stult committed
479 480 481 482
		"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>&-) 
483 484 485 486 487 488 489

else 
	clear
	exit 1
fi


Helmut Stult's avatar
Helmut Stult committed
490 491
if [ ! -z "$EDITION" ]; then
	USER=$(dialog --clear --title "Manjaro ARM Installer" \
492
	--inputbox "Enter username:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
493
	3>&1 1>&2 2>&3 3>&-)
494 495 496 497 498
else 
	clear
	exit 1
fi

499 500 501 502 503 504 505 506 507 508
if [ ! -z "$USER" ]
then
FULLNAME=$(dialog --clear --title "Finish Manjaro ARM Install" \
    --inputbox "Enter Full Name for $USER:" 8 50 \
        3>&1 1>&2 2>&3 3>&- \
	)
else
    clear
    exit 1
fi
509 510


511
if [ ! -z "$FULLNAME" ]; then
Helmut Stult's avatar
Helmut Stult committed
512
	PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
513
	--insecure --passwordbox "Enter Password for $USER:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
514
	3>&1 1>&2 2>&3 3>&- \
515 516 517 518 519 520
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
521 522
if [ ! -z "$PASSWORD" ]; then
	CONFIRMPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Strit's avatar
Strit committed
523
	--insecure --passwordbox "Confirm Password for $USER:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
524
	3>&1 1>&2 2>&3 3>&- \
Strit's avatar
Strit committed
525 526 527 528 529
	)
else 
	clear
	exit 1
fi
530

Strit's avatar
Strit committed
531
if [[ "$PASSWORD" != "$CONFIRMPASSWORD" ]]; then
Helmut Stult's avatar
Helmut Stult committed
532 533 534
	clear
	msg "User passwords do not match! Please restart the installer and try again."
	exit 1
Strit's avatar
Strit committed
535
fi
536

Helmut Stult's avatar
Helmut Stult committed
537 538
if [ ! -z "$CONFIRMPASSWORD" ]; then
	ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
539
	--insecure --passwordbox "Enter Root Password:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
540
	3>&1 1>&2 2>&3 3>&- \
541 542 543 544 545 546
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
547 548
if [ ! -z "$ROOTPASSWORD" ]; then
	CONFIRMROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Strit's avatar
Strit committed
549
	--insecure --passwordbox "Confirm Root Password:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
550
	3>&1 1>&2 2>&3 3>&- \
Strit's avatar
Strit committed
551 552 553 554 555
	)
else 
	clear
	exit 1
fi
556

Strit's avatar
Strit committed
557
if [[ "$ROOTPASSWORD" != "$CONFIRMROOTPASSWORD" ]]; then
Helmut Stult's avatar
Helmut Stult committed
558 559 560
	clear
	msg "Root passwords do not match! Please restart the installer and try again."
	exit 1
Strit's avatar
Strit committed
561
fi
562

Strit's avatar
Strit committed
563
if [ ! -z "$CONFIRMROOTPASSWORD" ]
564 565 566
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
567 568 569 570 571 572 573
	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" \
Helmut Stult's avatar
Helmut Stult committed
574
	--menu "Choose your SDCard - Be sure the correct drive is selected!" 20 50 10 \
Helmut Stult's avatar
Helmut Stult committed
575
	"${W[@]}" 3>&2 2>&1 1>&3)
576 577

# add /dev/ to the selected option above
Helmut Stult's avatar
Helmut Stult committed
578 579 580
	DEV_NAME=$SDCARD
	SDCARD=/dev/$SDCARD
	SDTYP=${SDCARD:5:2}
581 582 583
else 
	clear
	exit 1
Helmut Stult's avatar
Helmut Stult committed
584
fi
585

Helmut Stult's avatar
Helmut Stult committed
586 587 588 589 590 591 592
if [[ "$SDTYP" = "sd" ]]; then
	SDDEV=""
elif [[ "$SDTYP" = "mm" ]]; then
	SDDEV="p"
else 
	clear
	exit 1
593 594 595
fi


Helmut Stult's avatar
Helmut Stult committed
596 597 598 599 600 601 602 603 604 605
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>&- \
606 607 608 609 610
	)
else 
	clear
	exit 1
fi
611 612


Helmut Stult's avatar
Helmut Stult committed
613 614 615 616 617 618 619 620 621 622 623
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>&- \
		)
624 625 626 627 628
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
629 630 631 632 633 634 635 636 637 638
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>&- \
639 640 641 642 643 644
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
645 646
if [ ! -z "$KEYMAP" ]; then
	HOSTNAME=$(dialog --clear --title "Manjaro ARM Installer" \
647
	--inputbox "Enter desired hostname for this system:" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
648
	3>&1 1>&2 2>&3 3>&- \
649 650 651 652 653 654
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
655 656
if [ ! -z "$HOSTNAME" ]; then
	dialog --clear --title "Manjaro ARM Installer" \
657 658 659 660
    --yesno "Is the below information correct:
    Username = $USER
    Password for $USER = $PASSWORD
    Password for root = $ROOTPASSWORD
661
    SD Card = $SDCARD
662
    Timezone = $TIMEZONE
663 664
    Locale = $LOCALE
    Keyboard layout = $KEYMAP
Helmut Stult's avatar
Helmut Stult committed
665
    Hostname = $HOSTNAME" 20 70 \
666 667
    3>&1 1>&2 2>&3 3>&-
else
Helmut Stult's avatar
Helmut Stult committed
668 669
	clear
	exit 1
670 671 672 673 674 675 676 677 678
fi

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

679

680 681
# get the profiles
getarmprofiles
Strit's avatar
Strit committed
682 683

#Package lists
684 685 686
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
687

688

Strit's avatar
Strit committed
689
# Commands
690
timer_start=$(get_timer)
Strit's avatar
Strit committed
691 692

prepare_card
693 694 695 696
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync