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

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

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

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

Strit's avatar
Strit committed
66 67

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

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

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

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

}

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

156
    info "Applying overlay for $EDITION..."
157
    cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
Strit's avatar
Strit committed
158
    
159
    info "Setting up users..."
Strit's avatar
Strit committed
160
    #setup users
161
    echo "$USER" > $TMPDIR/user
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..."
169
    $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
170
    $NSPAWN $TMPDIR/root usermod -aG $USERGROUPS $(cat $TMPDIR/user) 1> /dev/null 2>&1
171
    $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1
172

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

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

237
    msg "$DEVICE $EDITION install complete"
Strit's avatar
Strit committed
238 239 240
}

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

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

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

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

290
        #Clear first 8 mb
Strit's avatar
Strit committed
291
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
292 293
        
    #partition with 2 partitions
Strit's avatar
Strit committed
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
296 297
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/start`
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
298
        END_SECTOR=$(expr $START + $SIZE)
Strit's avatar
Strit committed
299 300
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
301 302
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
303 304 305 306
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
307 308
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Strit's avatar
Strit committed
309 310 311 312 313 314 315 316 317 318 319 320 321
        
    # 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
322 323
        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
324 325 326
        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
327 328
        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
329 330 331 332
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
333 334
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Strit's avatar
Strit committed
335
        
Strit's avatar
Strit committed
336

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

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

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

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

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

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

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

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

453 454

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


#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
472 473 474 475 476
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)" \
477
		"xfce"      "Full XFCE desktop and apps (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 493
	--inputbox "Enter username:
(usernames most be all lowercase)" 8 50 \
Helmut Stult's avatar
Helmut Stult committed
494
	3>&1 1>&2 2>&3 3>&-)
Strit's avatar
Strit committed
495 496 497 498 499
    if [[ "$USER" =~ [A-Z] ]] || [[ "$USER" == *['!'@#\$%^\&*()_+]* ]]; then
    clear
    msg "Configuration aborted! Username contained invalid characters."
    exit 1
    fi
500 501 502 503 504
else 
	clear
	exit 1
fi

505 506
if [ ! -z "$USER" ]
then
507 508 509 510 511 512 513 514 515 516 517 518 519
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" \
520 521 522 523 524 525 526
    --inputbox "Enter Full Name for $USER:" 8 50 \
        3>&1 1>&2 2>&3 3>&- \
	)
else
    clear
    exit 1
fi
527 528


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

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

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

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

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

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

Strit's avatar
Strit committed
581
if [ ! -z "$CONFIRMROOTPASSWORD" ]
582 583 584
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
585 586 587 588 589 590 591
	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" \
592
	--menu "Choose your SDCard - Be sure the correct drive is selected!" 20 50 10 \
Helmut Stult's avatar
Helmut Stult committed
593
	"${W[@]}" 3>&2 2>&1 1>&3)
594 595

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

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


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


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

Helmut Stult's avatar
Helmut Stult committed
647 648 649 650 651 652 653 654 655 656
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>&- \
657 658 659 660 661 662
	)
else 
	clear
	exit 1
fi

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

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

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

698

699 700
# get the profiles
getarmprofiles
Strit's avatar
Strit committed
701 702

#Package lists
703 704 705
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
706

707

Strit's avatar
Strit committed
708
# Commands
709
timer_start=$(get_timer)
Strit's avatar
Strit committed
710 711

prepare_card
712 713 714 715
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync