manjaro-arm-installer 25.2 KB
Newer Older
Dan Johansen's avatar
Dan Johansen committed
1 2
#! /bin/bash

3
# *****************************
Dan Johansen's avatar
Dan Johansen committed
4
#       Version 1.2.12
5 6
# *****************************

Dan Johansen's avatar
Dan Johansen 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'
Dan Johansen's avatar
Dan Johansen committed
11

12 13 14
# set colorscheme
export DIALOGRC="./dialogrc_gui"

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

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

43
# Sanity checks for dependencies
44 45 46 47 48
declare -a DEPENDNECIES=("git" "parted" "systemd-nspawn" "wget" "dialog" "bsdtar" "openssl" "awk")

for i in "${DEPENDNECIES[@]}"; do
  if ! [[ -f "/bin/$i" || -f "/sbin/$i" || -f "/usr/bin/$i" || -f "/usr/sbin/$i" ]] ; then
    echo "$i command is missing! Please install the relevant package."
49
    exit 1
50 51 52
  fi
done

Dan Johansen's avatar
Dan Johansen committed
53 54 55 56
if [ ! -f "/usr/lib/binfmt.d/qemu-static.conf" ]; then
    echo "qemu-static.conf file is missing. Please install the relevant package."
    exit 1
fi
57

Dan Johansen's avatar
Dan Johansen committed
58 59

# Functions
60 61 62 63 64 65 66 67
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
 }

68 69 70 71 72 73 74
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
 }
Dan Johansen's avatar
Dan Johansen committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

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

97
getarmprofiles () {
98
    info "Getting package lists ready for $DEVICE $EDITION edition..."
99 100 101
    if ls $TMPDIR/arm-profiles/* 1> /dev/null 2>&1; then
        cd $TMPDIR/arm-profiles
        git pull 1> /dev/null 2>&1
102
    else
103 104
        mkdir -p $TMPDIR
        chmod 777 $TMPDIR
105
        cd $TMPDIR
106
        git clone https://gitlab.manjaro.org/manjaro-arm/applications/arm-profiles.git 1> /dev/null 2>&1
107 108 109 110 111 112
    fi

}

create_install() {
    msg "Creating install for $DEVICE..."
113
    info "Used device is ${SDCARD}${SDDEV}"
114 115
    
    # fetch and extract rootfs
116
    info "Downloading latest $ARCH rootfs..."
117
    cd $TMPDIR
118
    wget -q --show-progress --progress=bar:force:noscroll https://osdn.net/projects/manjaro-arm/storage/.rootfs/Manjaro-ARM-$ARCH-latest.tar.gz
119
    
120
    info "Extracting $ARCH rootfs..."
121
    bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
122
    
123
    info "Setting up keyrings..."
Dan Johansen's avatar
Dan Johansen committed
124
    $NSPAWN $TMPDIR/root pacman-key --init 1> /dev/null 2>&1
125
    $NSPAWN $TMPDIR/root pacman-key --populate archlinux archlinuxarm manjaro manjaro-arm 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
126
    
127
    info "Installing packages for $EDITION on $DEVICE..."
128 129 130
    # Setup cache mount
    mkdir -p $TMPDIR/pkg-cache
    mount -o bind $TMPDIR/pkg-cache $TMPDIR/root/var/cache/pacman/pkg
Dan Johansen's avatar
Dan Johansen committed
131
    # Install device and editions specific packages
132
    $NSPAWN $TMPDIR/root pacman -Syyu base manjaro-system manjaro-release systemd systemd-libs $PKG_DEVICE $PKG_EDITION --noconfirm
133 134 135 136 137 138 139 140 141
    #if [[ "$DEVICE" = "???" ]]; then
    #if [[ "$EDITION" = "kde-plasma" ]] || [[ "$EDITION" = "cubocore" ]]; then
    #$NSPAWN $TMPDIR/root pacman -R sddm sddm-kcm matcha-dynamic-sddm --noconfirm
    #$NSPAWN $TMPDIR/root pacman -S sddm-compat sddm-kcm matcha-dynamic-sddm --noconfirm
    #elif [[ "$EDITION" = "lxqt" ]]; then
    #$NSPAWN $TMPDIR/root pacman -R sddm matcha-dynamic-sddm --noconfirm
    #$NSPAWN $TMPDIR/root pacman -S sddm-compat matcha-dynamic-sddm --noconfirm
    #fi
    #fi
Dan Johansen's avatar
Dan Johansen committed
142
    
143
    info "Enabling services..."
Dan Johansen's avatar
Dan Johansen committed
144
    # Enable services
145
    $NSPAWN $TMPDIR/root systemctl enable getty.target haveged.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
146
    $NSPAWN $TMPDIR/root systemctl enable $SRV_EDITION 1> /dev/null 2>&1
147 148 149
    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
Dan Johansen's avatar
Dan Johansen committed
150

151
    info "Applying overlay for $EDITION..."
152
    cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
153

154
    info "Setting up users..."
Dan Johansen's avatar
Dan Johansen committed
155
    #setup users
156
    echo "$USER" > $TMPDIR/user
157 158 159
    echo "$PASSWORD" > $TMPDIR/password
    echo "$ROOTPASSWORD" > $TMPDIR/rootpassword

160
    info "Setting password for root ..."
Dan Johansen's avatar
Dan Johansen committed
161
    $NSPAWN $TMPDIR/root awk -i inplace -F: "BEGIN {OFS=FS;} \$1 == \"root\" {\$2=\"$(openssl passwd -1 $(cat $TMPDIR/rootpassword))\"} 1" /etc/shadow 1> /dev/null 2>&1
162 163

    info "Adding user..."
Dan Johansen's avatar
Dan Johansen committed
164
    $NSPAWN $TMPDIR/root useradd -m -G wheel,sys,input,video,storage,lp,network,users,power -p $(openssl passwd -1 $(cat $TMPDIR/password)) -s /bin/bash $(cat $TMPDIR/user) 1> /dev/null 2>&1
165
    $NSPAWN $TMPDIR/root usermod -aG $USERGROUPS $(cat $TMPDIR/user) 1> /dev/null 2>&1
166
    $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1
167
    
Dan Johansen's avatar
Dan Johansen committed
168 169 170 171 172 173 174
    info "Enabling user services..."
    if [[ "$EDITION" = "minimal" ]] || [[ "$EDITION" = "server" ]]; then
        echo "No user services for $EDITION edition"
    else
        $NSPAWN $TMPDIR/root --user $(cat $TMPDIR/user) systemctl --user enable pulseaudio.service 1> /dev/null 2>&1
    fi
    
175
    info "Setting up system settings..."
Dan Johansen's avatar
Dan Johansen committed
176
    #system setup
Dan Johansen's avatar
Dan Johansen committed
177
    $NSPAWN $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1
178 179 180 181
    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/
182
    $NSPAWN $TMPDIR/root ln -sf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
183
    $NSPAWN $TMPDIR/root sed -i s/"#$LOCALE"/"$LOCALE"/g /etc/locale.gen 1> /dev/null 2>&1
184 185
    echo "LANG=$LOCALE" | tee --append $TMPDIR/root/etc/locale.conf 1> /dev/null 2>&1
    $NSPAWN $TMPDIR/root locale-gen
186
    echo "KEYMAP=$CLIKEYMAP" | tee --append $TMPDIR/root/etc/vconsole.conf 1> /dev/null 2>&1
187 188 189 190
    echo 'Section "InputClass"' > $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
    echo 'Identifier "system-keyboard"' >> $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
    echo 'Option "XkbLayout" "us"' >> $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
    echo 'EndSection' >> $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
191
    sed -i s/"us"/"$X11KEYMAP"/ $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
192
    echo "$HOSTNAME" | tee --append $TMPDIR/root/etc/hostname 1> /dev/null 2>&1
193
    sed -i s/"enable systemd-resolved.service"/"#enable systemd-resolved.service"/ $TMPDIR/root/usr/lib/systemd/system-preset/90-systemd.preset
194

195 196
    echo "Correcting permissions from overlay..."
    chown -R root:root $TMPDIR/root/etc
197
    if [[ "$EDITION" != "minimal" && "$EDITION" != "server" ]]; then
198 199 200
        chown root:polkitd $TMPDIR/root/etc/polkit-1/rules.d
    elif [[ "$EDITION" = "cubocore" ]]; then
        cp $TMPDIR/root/usr/share/applications/corestuff.desktop $TMPDIR/root/etc/xdg/autostart/
201
    fi
202

Dan Johansen's avatar
Dan Johansen committed
203
    
204
    info "Doing device specific setups for $DEVICE..."
205
    if [[ "$DEVICE" = "rpi3" ]]; then
Dan Johansen's avatar
Dan Johansen 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
Dan Johansen's avatar
Dan Johansen 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
Dan Johansen's avatar
Dan Johansen 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
    elif [[ "$DEVICE" = "on2" ]]; then
218
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
219
        sed -i s/'meson64_odroidn2.dtb'/'meson-g12b-odroid-n2.dtb'/ $TMPDIR/root/boot/boot.ini 1> /dev/null 2>&1
220
    elif [[ "$DEVICE" = "vim1" ]] || [[ "$DEVICE" = "vim2" ]] || [[ "$DEVICE" = "vim3" ]]; then
Dan Johansen's avatar
Dan Johansen committed
221
        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 khadas-utils.service 1> /dev/null 2>&1
223
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
Dan Johansen's avatar
Dan Johansen committed
224
        $NSPAWN $TMPDIR/root systemctl enable pinebook-post-install.service 1> /dev/null 2>&1
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        sed -i s/"HOOKS=(base udev autodetect modconf block filesystems keyboard fsck)"/"HOOKS=(base udev autodetect modconf block filesystems keyboard fsck bootsplash-manjaro)"/g $TMPDIR/root/etc/mkinitcpio.conf
        $NSPAWN $TMPDIR/root mkinitcpio -P 1> /dev/null 2>&1
    elif [[ "$DEVICE" = "pinephone" ]] || [[ "$DEVICE" = "pinetab" ]]; then
        $NSPAWN $TMPDIR/root systemctl enable pinebook-post-install.service 1> /dev/null 2>&1
        sed -i s/"HOOKS=(base udev autodetect modconf block filesystems keyboard fsck)"/"HOOKS=(base udev autodetect modconf block filesystems keyboard fsck bootsplash-manjaro)"/g $TMPDIR/root/etc/mkinitcpio.conf
        $NSPAWN $TMPDIR/root mkinitcpio -P 1> /dev/null 2>&1
        if [[ "$EDITION" = "kde" ]] || [[ "$EDITION" = "cubocore" ]]; then
            sed -i '0,/Session=/s//Session=plasma.desktop/' $TMPDIR/root/etc/sddm.conf
        elif [[ "$EDITION" = "lxqt" ]]; then
            sed -i '0,/Session=/s//Session=/Session=lxqt.desktop/' $TMPDIR/root/etc/sddm.conf
        fi
        if [[ "$EDITION" != "plasma-mobile" ]]; then
        sed -i '0,/User=/s//User=manjaro/' $TMPDIR/rootfs/etc/sddm.conf
        fi

Dan Johansen's avatar
Dan Johansen committed
240
    else
Dan Johansen's avatar
Dan Johansen committed
241
        echo "No device specific setting needed for $DEVICE"
Dan Johansen's avatar
Dan Johansen committed
242 243
    fi
    
244
    info "Cleaning install for unwanted files..."
245
    umount $TMPDIR/root/var/cache/pacman/pkg
246
    rm -rf $TMPDIR/root/usr/bin/qemu-aarch64-static
247 248
    rm -rf $TMPDIR/root/var/cache/pacman/pkg/*
    rm -rf $TMPDIR/root/var/log/*
249
    rm -rf $TMPDIR/root/etc/*.pacnew
250 251
    rm -rf $TMPDIR/root/usr/lib/systemd/system/systemd-firstboot.service
    rm -rf $TMPDIR/root/etc/machine-id
252 253
    
    # Remove temp files on host
254 255
    rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword
    rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz*
Dan Johansen's avatar
Dan Johansen committed
256

257
    msg "$DEVICE $EDITION install complete"
Dan Johansen's avatar
Dan Johansen committed
258 259 260
}

prepare_card () {
261
    msg "Getting $SDCARD ready for $DEVICE..."
Dan Johansen's avatar
Dan Johansen committed
262
    # For Raspberry Pi devices
Dan Johansen's avatar
Dan Johansen committed
263
    if [[ "$DEVICE" = "rpi3" ]] || [[ "$DEVICE" = "rpi4" ]]; then
264 265 266 267
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1

Dan Johansen's avatar
Dan Johansen committed
268
        #partition with boot and root
269 270
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 0% 100M 1> /dev/null 2>&1
271 272
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/start` 
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/size`
273
        END_SECTOR=$(expr $START + $SIZE)
274 275
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
276 277
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
278 279
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
280 281
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
282 283
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
284 285

    # For Odroid devices
286
    elif [[ "$DEVICE" = "oc2" ]]; then
287 288
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
289
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
290

Dan Johansen's avatar
Dan Johansen committed
291
        #Clear first 8mb
292
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
293

Dan Johansen's avatar
Dan Johansen committed
294
    #partition with a single root partition
295 296 297
        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
298
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
299 300
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
301
        mkdir -p $TMPDIR/root
302
        chmod 777 -R $TMPDIR/root
303
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
304
        
305
   elif [[ "$DEVICE" = "on2" ]] || [[ "$DEVICE" = "vim1" ]] || [[ "$DEVICE" = "vim2" ]] || [[ "$DEVICE" = "vim3" ]]; then
Helmut Stult's avatar
Helmut Stult committed
306 307 308 309
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1

310
        #Clear first 8 mb
Dan Johansen's avatar
Dan Johansen committed
311
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
312
        
Dan Johansen's avatar
Dan Johansen committed
313 314 315
    #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
316 317
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/start`
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/size`
Dan Johansen's avatar
Dan Johansen committed
318 319 320
        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
321
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
322
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
323 324 325 326
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
327 328
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
329
        
Dan Johansen's avatar
Dan Johansen committed
330

331
    # For pine devices
332
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
333 334
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
335
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
336 337

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

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

Dan Johansen's avatar
Dan Johansen committed
351
    # For rockpro64 device
Dan Johansen's avatar
Dan Johansen committed
352
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]  || [[ "$DEVICE" = "pbpro" ]]; then
353 354
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
355
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
356 357

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

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

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

    #clean up
394 395 396
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
397
        
398
    elif [[ "$DEVICE" = "on2" ]] || [[ "$DEVICE" = "vim1" ]] || [[ "$DEVICE" = "vim2" ]] || [[ "$DEVICE" = "vim3" ]]; then
399 400
    # move boot files
        mv $TMPDIR/root/boot/* $TMPDIR/boot
401
    if [[ "$DEVICE" = "on2" ]]; then
Dan Johansen's avatar
Dan Johansen committed
402
    #flash bootloader
403
        dd if=$TMPDIR/boot/u-boot.bin of=${SDCARD} conv=fsync,notrunc bs=512 seek=1 1> /dev/null 2>&1
404
    fi
405 406 407 408 409
        
    #clean up
        umount $TMPDIR/root
        umount $TMPDIR/boot
        rm -r $TMPDIR/root $TMPDIR/boot
Dan Johansen's avatar
Dan Johansen committed
410 411
        partprobe $SDCARD 1> /dev/null 2>&1
        
412
    # For pine device
413
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
414
    #flash bootloader
415
        dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1
416 417
        
    #clean up
418 419 420
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
421
        
422
        
423
    # For rockchip devices
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]  || [[ "$DEVICE" = "pbpro" ]]; then        
        # Flash bootloader
        if [[ "$DEVICE" = "pbpro" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]; then
        # Flash bootloader with ATF
        dd if=$TMPDIR/root/boot/idbloader.img of=${LDEV} seek=64 conv=notrunc 1> /dev/null 2>&1
        dd if=$TMPDIR/root/boot/u-boot.itb of=${LDEV} seek=16384 conv=notrunc 1> /dev/null 2>&1
        else
        #echo '' 
        dd if=$TMPDIR/root/boot/idbloader.img of=${LDEV} seek=64 conv=notrunc 1> /dev/null 2>&1
        dd if=$TMPDIR/root/boot/uboot.img of=${LDEV} seek=16384 conv=notrunc 1> /dev/null 2>&1
        dd if=$TMPDIR/root/boot/trust.img of=${LDEV} seek=24576 conv=notrunc 1> /dev/null 2>&1
        #fi
        
        # Below section is for testing uboot with ATF
        #if [[ "$DEVICE" = "rock64" ]]; then
        #flash bootloader
        #dd if=$TMPDIR/root/boot/idbloader.img of=${LDEV} seek=64 conv=notrunc
        #dd if=$TMPDIR/root/boot/u-boot.itb of=${LDEV} seek=16384 conv=notrunc
        fi
Dan Johansen's avatar
Dan Johansen committed
443 444
        
    #clean up
445 446 447
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
448 449
        
fi
Dan Johansen's avatar
Dan Johansen committed
450 451
}

452 453

# Using Dialog to ask for user input for variables
454
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
Helmut Stult's avatar
Helmut Stult committed
455
        --menu "Choose a device:" 20 75 10 \
456
        "rpi4"      "Raspberry Pi 4" \
Dan Johansen's avatar
Dan Johansen committed
457
        "pbpro"     "Pinebook Pro" \
458
		"pinebook"  "Pinebook" \
459 460 461
		"rock64"    "Rock64" \
		"rockpro64" "RockPro64" \
		"sopine"    "Sopine / Pine64-LTS" \
462
		"pine64"    "Pine64+" \
463
		"oc2"       "Odroid C2" \
464
		"on2"       "Odroid N2" \
Dan Johansen's avatar
Dan Johansen committed
465
		"vim3"      "Khadas Vim 3" \
466
		"rockpi4"   "Rock Pi 4" \
Helmut Stult's avatar
Helmut Stult committed
467
		"rpi3"      "Raspberry Pi 3" \
Dan Johansen's avatar
Dan Johansen committed
468
		"vim1"      "Khadas Vim 1" \
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
if [ ! -z "$DEVICE" ]; then
	EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose an edition:" 20 75 10 \
		"minimal"   "Minimal Edition            (only CLI)" \
477
		"kde-plasma" "Full KDE/Plasma Desktop    (full featured)" \
478
		"xfce"      "Full XFCE desktop and apps (full featured)" \
479
		"mate"      "Full MATE desktop and apps (lightweight)" \
Helmut Stult's avatar
Helmut Stult committed
480 481 482
		"lxqt"      "Full LXQT Desktop and apps (lightweight)" \
		"i3"        "Mininal i3 WM with apps    (very light)" \
		"cubocore"  "QT based Desktop           (lightweight)" \
483
		"gnome"     "Full Gnome desktop and apps (EXPERIMANTAL)" \
Helmut Stult's avatar
Helmut Stult committed
484
		3>&1 1>&2 2>&3 3>&-) 
485 486 487 488 489 490 491

else 
	clear
	exit 1
fi


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

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

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


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

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

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

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

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

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

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

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

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


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


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

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

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
if [ ! -z "$CLIKEYMAP" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( localectl list-x11-keymap-layouts )
	X11KEYMAP=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose your X11 keyboard layout:" 20 50 15 \
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi

if [ ! -z "$X11KEYMAP" ]; then
Helmut Stult's avatar
Helmut Stult committed
682
	HOSTNAME=$(dialog --clear --title "Manjaro ARM Installer" \
683
	--inputbox "Enter desired hostname for this system:" 8 50 \
684
	3>&1 1>&2 2>&3 3>&- \
685 686 687 688 689 690
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
Helmut Stult committed
691 692
if [ ! -z "$HOSTNAME" ]; then
	dialog --clear --title "Manjaro ARM Installer" \
693
    --yesno "Is the below information correct:
694 695
    Device = $DEVICE
    Edition = $EDITION
696
    Username = $USER
697
    Additional usergroups = $USERGROUPS
698 699
    Password for $USER = (password hidden)
    Password for root = (password hidden)
700
    SD Card = $SDCARD
701
    Timezone = $TIMEZONE
702
    Locale = $LOCALE
703 704
    TTY Keyboard layout = $CLIKEYMAP
    X11 Keyboard layout = $X11KEYMAP
Helmut Stult's avatar
Helmut Stult committed
705
    Hostname = $HOSTNAME" 20 70 \
706 707
    3>&1 1>&2 2>&3 3>&-
else
Helmut Stult's avatar
Helmut Stult committed
708 709
	clear
	exit 1
710 711 712 713 714 715 716 717 718
fi

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

719

720 721
# get the profiles
getarmprofiles
Dan Johansen's avatar
Dan Johansen committed
722 723

#Package lists
724 725 726
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}')
Dan Johansen's avatar
Dan Johansen committed
727

728

Dan Johansen's avatar
Dan Johansen committed
729
# Commands
730
timer_start=$(get_timer)
Dan Johansen's avatar
Dan Johansen committed
731 732

prepare_card
733 734 735 736
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync