manjaro-arm-installer 24.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.8
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

Dan Johansen's avatar
Dan Johansen committed
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=""
Dan Johansen's avatar
Dan Johansen committed
22 23
CONFIRMPASSWORD=""
CONFIRMROOTPASSWORD=""
24 25
ROOTPASSWORD=""
SDCARD=""
26 27
SDTYP=""
SDDEV=""
Helmut Stult's avatar
 
Helmut Stult committed
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

53

Dan Johansen's avatar
Dan Johansen committed
54 55

# Functions
56 57 58 59 60 61 62 63
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
 }

64 65 66 67 68 69 70
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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

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

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

}

create_install() {
    msg "Creating install for $DEVICE..."
109
    info "Used device is ${SDCARD}${SDDEV}"
110 111
    
    # fetch and extract rootfs
112
    info "Downloading latest $ARCH rootfs..."
113
    cd $TMPDIR
114
    wget -q --show-progress --progress=bar:force:noscroll https://osdn.net/projects/manjaro-arm/storage/.rootfs/Manjaro-ARM-$ARCH-latest.tar.gz
115
    
116
    info "Extracting $ARCH rootfs..."
117
    bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
118
    
119
    info "Setting up keyrings..."
Dan Johansen's avatar
Dan Johansen committed
120
    $NSPAWN $TMPDIR/root pacman-key --init 1> /dev/null 2>&1
121
    $NSPAWN $TMPDIR/root pacman-key --populate archlinux archlinuxarm manjaro manjaro-arm 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
122
    
123
    info "Installing packages for $EDITION on $DEVICE..."
Dan Johansen's avatar
Dan Johansen committed
124
    # Install device and editions specific packages
125
    $NSPAWN $TMPDIR/root pacman -Syyu base manjaro-system manjaro-release systemd systemd-libs $PKG_DEVICE $PKG_EDITION --noconfirm
126 127 128 129 130 131 132 133 134
    #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
135
    
136
    info "Enabling services..."
Dan Johansen's avatar
Dan Johansen committed
137
    # Enable services
138
    $NSPAWN $TMPDIR/root systemctl enable getty.target haveged.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
139
    $NSPAWN $TMPDIR/root systemctl enable $SRV_EDITION 1> /dev/null 2>&1
140 141 142
    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
143

144
    info "Applying overlay for $EDITION..."
145
    cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
146

147
    info "Setting up users..."
Dan Johansen's avatar
Dan Johansen committed
148
    #setup users
149
    echo "$USER" > $TMPDIR/user
150 151 152
    echo "$PASSWORD" > $TMPDIR/password
    echo "$ROOTPASSWORD" > $TMPDIR/rootpassword

153
    info "Setting password for root ..."
Dan Johansen's avatar
Dan Johansen committed
154
    $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
155 156

    info "Adding user..."
Dan Johansen's avatar
Dan Johansen committed
157
    $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
158
    $NSPAWN $TMPDIR/root usermod -aG $USERGROUPS $(cat $TMPDIR/user) 1> /dev/null 2>&1
159
    $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1
160
    
Dan Johansen's avatar
fix DNS  
Dan Johansen committed
161 162 163 164 165 166 167
    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
    
168
    info "Setting up system settings..."
Dan Johansen's avatar
Dan Johansen committed
169
    #system setup
Dan Johansen's avatar
Dan Johansen committed
170
    $NSPAWN $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1
171 172 173 174
    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/
175
    $NSPAWN $TMPDIR/root ln -sf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
176
    $NSPAWN $TMPDIR/root sed -i s/"#$LOCALE"/"$LOCALE"/g /etc/locale.gen 1> /dev/null 2>&1
177 178
    echo "LANG=$LOCALE" | tee --append $TMPDIR/root/etc/locale.conf 1> /dev/null 2>&1
    $NSPAWN $TMPDIR/root locale-gen
179
    echo "KEYMAP=$CLIKEYMAP" | tee --append $TMPDIR/root/etc/vconsole.conf 1> /dev/null 2>&1
180 181 182 183
    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
184
    sed -i s/"us"/"$X11KEYMAP"/ $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
185
    echo "$HOSTNAME" | tee --append $TMPDIR/root/etc/hostname 1> /dev/null 2>&1
186
    sed -i s/"enable systemd-resolved.service"/"#enable systemd-resolved.service"/ $TMPDIR/root/usr/lib/systemd/system-preset/90-systemd.preset
187

188 189
    echo "Correcting permissions from overlay..."
    chown -R root:root $TMPDIR/root/etc
190
    if [[ "$EDITION" != "minimal" && "$EDITION" != "server" ]]; then
191 192 193
        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/
194
    fi
195

Dan Johansen's avatar
Dan Johansen committed
196
    
197
    info "Doing device specific setups for $DEVICE..."
198
    if [[ "$DEVICE" = "rpi3" ]]; then
Dan Johansen's avatar
Dan Johansen committed
199 200 201
        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
202
        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
203 204
    elif [[ "$DEVICE" = "rpi4" ]]; then
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
205
        $NSPAWN $TMPDIR/root systemctl enable attach-bluetooth.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
206 207
        # fix wifi
        sed -i s/'boardflags3=0x48200100'/'boardflags3=0x44200100'/ $TMPDIR/root/usr/lib/firmware/updates/brcm/brcmfmac43455-sdio.txt 1> /dev/null 2>&1
208
    elif [[ "$DEVICE" = "oc2" ]]; then
209
        $NSPAWN $TMPDIR/root systemctl enable amlogic.service 1> /dev/null 2>&1
210
    elif [[ "$DEVICE" = "on2" ]]; then
211
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
212
        sed -i s/'meson64_odroidn2.dtb'/'meson-g12b-odroid-n2.dtb'/ $TMPDIR/boot/boot.ini 1> /dev/null 2>&1
213
    elif [[ "$DEVICE" = "vim1" ]] || [[ "$DEVICE" = "vim2" ]] || [[ "$DEVICE" = "vim3" ]]; then
Dan Johansen's avatar
Dan Johansen committed
214
        echo "LABEL=BOOT  /boot   vfat    defaults        0       0" | tee --append $TMPDIR/root/etc/fstab 1> /dev/null 2>&1
215
        $NSPAWN $TMPDIR/root systemctl enable bluetooth-khadas.service khadas-utils.service 1> /dev/null 2>&1
216
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
Dan Johansen's avatar
Dan Johansen committed
217
        $NSPAWN $TMPDIR/root systemctl enable pinebook-post-install.service 1> /dev/null 2>&1
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        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
233
    else
Dan Johansen's avatar
Dan Johansen committed
234
        echo "No device specific setting needed for $DEVICE"
Dan Johansen's avatar
Dan Johansen committed
235 236
    fi
    
237
    info "Cleaning install for unwanted files..."
238
    rm -rf $TMPDIR/root/usr/bin/qemu-aarch64-static
239 240
    rm -rf $TMPDIR/root/var/cache/pacman/pkg/*
    rm -rf $TMPDIR/root/var/log/*
241
    rm -rf $TMPDIR/root/etc/*.pacnew
242 243
    rm -rf $TMPDIR/root/usr/lib/systemd/system/systemd-firstboot.service
    rm -rf $TMPDIR/root/etc/machine-id
244 245
    
    # Remove temp files on host
246 247
    rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword
    rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz*
Dan Johansen's avatar
Dan Johansen committed
248

249
    msg "$DEVICE $EDITION install complete"
Dan Johansen's avatar
Dan Johansen committed
250 251 252
}

prepare_card () {
253
    msg "Getting $SDCARD ready for $DEVICE..."
Dan Johansen's avatar
Dan Johansen committed
254
    # For Raspberry Pi devices
Dan Johansen's avatar
Dan Johansen committed
255
    if [[ "$DEVICE" = "rpi3" ]] || [[ "$DEVICE" = "rpi4" ]]; then
256 257 258 259
        # 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
260
        #partition with boot and root
261 262
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 0% 100M 1> /dev/null 2>&1
263 264
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/start` 
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/size`
265
        END_SECTOR=$(expr $START + $SIZE)
266 267
        parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        partprobe $SDCARD 1> /dev/null 2>&1
268 269
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
270 271
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
272 273
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
274 275
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
276 277

    # For Odroid devices
278
    elif [[ "$DEVICE" = "oc2" ]]; then
279 280
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
Helmut Stult's avatar
 
Helmut Stult committed
281
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
282

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

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

302
        #Clear first 8 mb
Dan Johansen's avatar
Dan Johansen committed
303
        dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
304
        
Dan Johansen's avatar
Dan Johansen committed
305 306 307
    #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
308 309
        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
310 311 312
        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
313
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT 1> /dev/null 2>&1
314
        mkfs.ext4 "${SDCARD}${SDDEV}2" -L ROOT 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
315 316 317 318
        
    #copy rootfs contents over to the FS
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
319 320
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
        mount ${SDCARD}${SDDEV}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
321
        
Dan Johansen's avatar
Dan Johansen committed
322

323
    # For pine devices
324
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
325 326
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
Helmut Stult's avatar
 
Helmut Stult committed
327
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
328 329

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

Dan Johansen's avatar
Dan Johansen committed
332
    #partition with a single root partition
333 334 335
        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
336
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
337 338
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
339
        mkdir -p $TMPDIR/root
340
        chmod 777 -R $TMPDIR/root
341
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
342

Dan Johansen's avatar
Dan Johansen committed
343
    # For rockpro64 device
Dan Johansen's avatar
Dan Johansen committed
344
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]  || [[ "$DEVICE" = "pbpro" ]]; then
345 346
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
Helmut Stult's avatar
 
Helmut Stult committed
347
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
348 349

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

Dan Johansen's avatar
Dan Johansen committed
352
    #partition with a single root partition
353 354 355
        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
356
        mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}${SDDEV}1 -L ROOT 1> /dev/null 2>&1
357 358
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
359
        mkdir -p $TMPDIR/root
360
        chmod 777 -R $TMPDIR/root
361
        mount ${SDCARD}${SDDEV}1 $TMPDIR/root
362 363 364 365 366 367
    fi
}

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

    #clean up
386 387 388
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
389
        
390
    elif [[ "$DEVICE" = "on2" ]] || [[ "$DEVICE" = "vim1" ]] || [[ "$DEVICE" = "vim2" ]] || [[ "$DEVICE" = "vim3" ]]; then
391 392
    # move boot files
        mv $TMPDIR/root/boot/* $TMPDIR/boot
393
    if [[ "$DEVICE" = "on2" ]]; then
Dan Johansen's avatar
fix DNS  
Dan Johansen committed
394
    #flash bootloader
395 396
        dd if=$TMPDIR/boot/u-boot.bin of=${LDEV} conv=fsync,notrunc bs=512 seek=1 1> /dev/null 2>&1
    fi
397 398 399 400 401 402
        
    #clean up
        umount $TMPDIR/root
        umount $TMPDIR/boot
        losetup -d $LDEV 1> /dev/null 2>&1
        rm -r $TMPDIR/root $TMPDIR/boot
Dan Johansen's avatar
Dan Johansen committed
403 404
        partprobe $SDCARD 1> /dev/null 2>&1
        
405
    # For pine device
406
    elif [[ "$DEVICE" = "pinebook" ]] || [[ "$DEVICE" = "sopine" ]] || [[ "$DEVICE" = "pine64" ]]; then
407
    #flash bootloader
408
        dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1
409 410
        
    #clean up
411 412 413
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
414
        
415
        
416
    # For rockchip devices
Dan Johansen's avatar
Dan Johansen committed
417
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]] || [[ "$DEVICE" = "rockpi4" ]]  || [[ "$DEVICE" = "pbpro" ]]; then
Dan Johansen's avatar
Dan Johansen committed
418
    #flash bootloader
419 420 421
        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
Dan Johansen's avatar
Dan Johansen committed
422 423
        
    #clean up
424 425 426
        umount $TMPDIR/root
        rm -r $TMPDIR/root
        partprobe $SDCARD 1> /dev/null 2>&1
427 428
        
fi
Dan Johansen's avatar
Dan Johansen committed
429 430
}

431 432

# Using Dialog to ask for user input for variables
433
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
Helmut Stult's avatar
Helmut Stult committed
434
        --menu "Choose a device:" 20 75 10 \
435
        "rpi4"      "Raspberry Pi 4" \
Dan Johansen's avatar
Dan Johansen committed
436
        "pbpro"     "Pinebook Pro" \
Helmut Stult's avatar
 
Helmut Stult committed
437
		"pinebook"  "Pinebook" \
438 439 440
		"rock64"    "Rock64" \
		"rockpro64" "RockPro64" \
		"sopine"    "Sopine / Pine64-LTS" \
441
		"pine64"    "Pine64+" \
Helmut Stult's avatar
 
Helmut Stult committed
442
		"oc2"       "Odroid C2" \
443
		"on2"       "Odroid N2" \
Dan Johansen's avatar
Dan Johansen committed
444
		"vim3"      "Khadas Vim 3" \
445
		"rockpi4"   "Rock Pi 4" \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
446
		"rpi3"      "Raspberry Pi 3" \
Dan Johansen's avatar
Dan Johansen committed
447
		"vim1"      "Khadas Vim 1" \
Helmut Stult's avatar
 
Helmut Stult committed
448
		3>&1 1>&2 2>&3 3>&-)
449 450 451


#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
v1.1.0  
Helmut Stult committed
452 453 454 455
if [ ! -z "$DEVICE" ]; then
	EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose an edition:" 20 75 10 \
		"minimal"   "Minimal Edition            (only CLI)" \
456
		"kde-plasma" "Full KDE/Plasma Desktop    (full featured)" \
457
		"xfce"      "Full XFCE desktop and apps (full featured)" \
458
		"mate"      "Full MATE desktop and apps (lightweight)" \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
459 460 461
		"lxqt"      "Full LXQT Desktop and apps (lightweight)" \
		"i3"        "Mininal i3 WM with apps    (very light)" \
		"cubocore"  "QT based Desktop           (lightweight)" \
462
		"gnome"     "Full Gnome desktop and apps (EXPERIMANTAL)" \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
463
		3>&1 1>&2 2>&3 3>&-) 
464 465 466 467 468 469 470

else 
	clear
	exit 1
fi


Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
471 472
if [ ! -z "$EDITION" ]; then
	USER=$(dialog --clear --title "Manjaro ARM Installer" \
473 474
	--inputbox "Enter username:
(usernames most be all lowercase)" 8 50 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
475
	3>&1 1>&2 2>&3 3>&-)
Dan Johansen's avatar
Dan Johansen committed
476 477 478 479 480
    if [[ "$USER" =~ [A-Z] ]] || [[ "$USER" == *['!'@#\$%^\&*()_+]* ]]; then
    clear
    msg "Configuration aborted! Username contained invalid characters."
    exit 1
    fi
481 482 483 484 485
else 
	clear
	exit 1
fi

486 487
if [ ! -z "$USER" ]
then
488 489
USERGROUPS=$(dialog --clear --title "Manjaro ARM Installer" \
    --inputbox "Enter additional groups for $USER in a comma seperated list: (empty if none)
490
(default: wheel,sys,input,video,storage,lp,network,users,power)" 8 90 \
491 492 493 494 495 496 497 498 499 500
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi

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


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

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

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

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

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

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

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

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

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


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


Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
612 613 614 615 616 617
if [ ! -z "$TIMEZONE" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
618
	done < <( cat /etc/locale.gen | grep "UTF-8" | tail -n +2 | sed -e 's/^#*//' | awk '{print $1}' )
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
619 620 621 622
	LOCALE=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose your locale!" 20 50 15 \
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
		)
623 624 625 626 627
else 
	clear
	exit 1
fi

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
628 629 630 631 632 633 634
if [ ! -z "$LOCALE" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( localectl list-keymaps )
635 636
	CLIKEYMAP=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose your TTY keyboard layout:" 20 50 15 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
637
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
638 639 640 641 642 643
	)
else 
	clear
	exit 1
fi

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
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
v1.1.0  
Helmut Stult committed
661
	HOSTNAME=$(dialog --clear --title "Manjaro ARM Installer" \
662
	--inputbox "Enter desired hostname for this system:" 8 50 \
Helmut Stult's avatar
 
Helmut Stult committed
663
	3>&1 1>&2 2>&3 3>&- \
664 665 666 667 668 669
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
670 671
if [ ! -z "$HOSTNAME" ]; then
	dialog --clear --title "Manjaro ARM Installer" \
672
    --yesno "Is the below information correct:
673 674
    Device = $DEVICE
    Edition = $EDITION
675
    Username = $USER
676
    Additional usergroups = $USERGROUPS
677 678
    Password for $USER = (password hidden)
    Password for root = (password hidden)
679
    SD Card = $SDCARD
680
    Timezone = $TIMEZONE
681
    Locale = $LOCALE
682 683
    TTY Keyboard layout = $CLIKEYMAP
    X11 Keyboard layout = $X11KEYMAP
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
v1.1.0  
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
Dan Johansen's avatar
Dan Johansen 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}')
Dan Johansen's avatar
Dan Johansen committed
706

707

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

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