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

3
# *****************************
4
#       Version 1.3.4
5 6
# *****************************

Dan Johansen's avatar
Dan Johansen committed
7
# Set globals
8
TMPDIR=/var/tmp/manjaro-arm-installer
9
ARCH='aarch64'
10
CARCH=$(uname -m)
11
NSPAWN='systemd-nspawn -q --resolv-conf=copy-host --timezone=off -D'
Dan Johansen's avatar
Dan Johansen committed
12

Dan Johansen's avatar
Dan Johansen committed
13
# set colorscheme
14
if [[ -f "./dialogrc_gui" ]]; then
Dan Johansen's avatar
Dan Johansen committed
15
export DIALOGRC="./dialogrc_gui"
16 17
else
export DIALOGRC="/etc/manjaro-arm-installer/dialogrc_gui"
Dan Johansen's avatar
Dan Johansen committed
18
fi
Dan Johansen's avatar
Dan Johansen committed
19

20 21 22 23
# clearing variables
DEVICE=""
EDITION=""
USER=""
24
USERGROUPS=""
Dan Johansen's avatar
Dan Johansen committed
25
FULLNAME=""
26
PASSWORD=""
Dan Johansen's avatar
Dan Johansen committed
27 28
CONFIRMPASSWORD=""
CONFIRMROOTPASSWORD=""
29 30
ROOTPASSWORD=""
SDCARD=""
31 32
SDTYP=""
SDDEV=""
Helmut Stult's avatar
 
Helmut Stult committed
33
DEV_NAME=""
34 35
TIMEZONE=""
LOCALE=""
36
HOSTNAME=""
37

38
# check if root
39 40 41 42 43 44
if [ "$EUID" -ne 0 ]; then
    echo "*******************************************************************************************"
    echo "*                                                                                         *"
    echo "*     This script requires root permissions to run. Please run as root or with sudo!      *"
    echo "*                                                                                         *"
    echo "*******************************************************************************************"
45 46 47
  exit
fi

48
# Sanity checks for dependencies
49
declare -a DEPENDNECIES=("git" "parted" "systemd-nspawn" "wget" "dialog" "bsdtar" "openssl" "awk" "mkfs.vfat")
50 51 52 53

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."
54
    exit 1
55 56 57
  fi
done

58
if [[ "$CARCH" != "aarch64" ]]; then
Dan Johansen's avatar
Dan Johansen committed
59 60 61 62
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
63
fi
64

Dan Johansen's avatar
Dan Johansen committed
65 66

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

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

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

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

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

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

104
getarmprofiles () {
105
    info "Getting package lists ready for $DEVICE $EDITION edition..."
106 107 108
    
    if [ -d "$TMPDIR/arm-profiles/" ];
    then
109
        cd $TMPDIR/arm-profiles
110 111
        git fetch origin 1> /dev/null 2>&1
	git reset --hard origin/master 1> /dev/null 2>&1
112
    else
113 114
        mkdir -p $TMPDIR
        chmod 777 $TMPDIR
115
        git clone https://gitlab.manjaro.org/manjaro-arm/applications/arm-profiles.git $TMPDIR/arm-profiles/ 1> /dev/null 2>&1
116 117 118 119 120 121
    fi

}

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

154
    info "Applying overlay for $EDITION..."
155
    cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
156

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

163
    info "Setting password for root ..."
Dan Johansen's avatar
Dan Johansen committed
164
    $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
165 166

    info "Adding user..."
167
    $NSPAWN $TMPDIR/root useradd -m -G wheel,sys,audio,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
168
    $NSPAWN $TMPDIR/root usermod -aG $USERGROUPS $(cat $TMPDIR/user) 1> /dev/null 2>&1
169
    $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1
170
    
Dan Johansen's avatar
fix DNS  
Dan Johansen committed
171 172 173 174 175 176 177
    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
    
178
    info "Setting up system settings..."
Dan Johansen's avatar
Dan Johansen committed
179
    #system setup
Dan Johansen's avatar
Dan Johansen committed
180
    $NSPAWN $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1
181 182 183 184
    rm -f $TMPDIR/root/etc/ssl/certs/ca-certificates.crt
    rm -f $TMPDIR/root/etc/ca-certificates/extracted/tls-ca-bundle.pem
    cp -a /etc/ssl/certs/ca-certificates.crt $TMPDIR/root/etc/ssl/certs/
    cp -a /etc/ca-certificates/extracted/tls-ca-bundle.pem $TMPDIR/root/etc/ca-certificates/extracted/
185
    $NSPAWN $TMPDIR/root ln -sf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
186
    $NSPAWN $TMPDIR/root sed -i s/"#$LOCALE"/"$LOCALE"/g /etc/locale.gen 1> /dev/null 2>&1
187 188
    echo "LANG=$LOCALE" | tee --append $TMPDIR/root/etc/locale.conf 1> /dev/null 2>&1
    $NSPAWN $TMPDIR/root locale-gen
189
    echo "KEYMAP=$CLIKEYMAP" | tee --append $TMPDIR/root/etc/vconsole.conf 1> /dev/null 2>&1
190
    if [[ "$EDITION" != "minimal" ]]; then
191 192 193 194
    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
195
    sed -i s/"us"/"$X11KEYMAP"/ $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf
196
    fi
197 198 199
    if [[ "$EDITION" = "sway" ]]; then
    sed -i s/"us"/"$X11KEYMAP"/ $TMPDIR/root/etc/sway/inputs/default-keyboard 1> /dev/null 2>&1
    fi
200
    echo "$HOSTNAME" | tee --append $TMPDIR/root/etc/hostname 1> /dev/null 2>&1
201
    sed -i s/"enable systemd-resolved.service"/"#enable systemd-resolved.service"/ $TMPDIR/root/usr/lib/systemd/system-preset/90-systemd.preset
202

203 204
    echo "Correcting permissions from overlay..."
    chown -R root:root $TMPDIR/root/etc
205
    if [[ "$EDITION" != "minimal" && "$EDITION" != "server" ]]; then
206 207 208
        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/
209
    fi
Dan Johansen's avatar
Dan Johansen committed
210
    
Dan Johansen's avatar
Dan Johansen committed
211 212
    [ ! -z "$CRYPT" ] && tweakinitrd_crypt
    
213
    info "Cleaning install for unwanted files..."
214
    umount $TMPDIR/root/var/cache/pacman/pkg
215
    rm -rf $TMPDIR/root/usr/bin/qemu-aarch64-static
216 217
    rm -rf $TMPDIR/root/var/cache/pacman/pkg/*
    rm -rf $TMPDIR/root/var/log/*
218
    rm -rf $TMPDIR/root/etc/*.pacnew
219 220
    rm -rf $TMPDIR/root/usr/lib/systemd/system/systemd-firstboot.service
    rm -rf $TMPDIR/root/etc/machine-id
221 222
    
    # Remove temp files on host
223 224
    rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword
    rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz*
Dan Johansen's avatar
Dan Johansen committed
225

226
    msg "$DEVICE $EDITION install complete"
Dan Johansen's avatar
Dan Johansen committed
227 228 229
}

prepare_card () {
230
    msg "Getting $SDCARD ready for $DEVICE..."
231 232 233
        # umount SD card
        umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1
        umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1
234 235 236
    # Create partitions
        #Clear first 32mb
        dd if=/dev/zero of=${SDCARD} bs=1M count=32 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
237
        #partition with boot and root
Dan Johansen's avatar
Dan Johansen committed
238 239
        parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        parted -s $SDCARD mkpart primary fat32 32M 256M 1> /dev/null 2>&1
240 241
        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
242 243 244
        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
245
        mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT_MNJRO 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
246 247 248 249 250 251 252 253

        if [ -z "$CRYPT" ]; then
          mkfs.ext4 -O ^metadata_csum,^64bit "${SDCARD}${SDDEV}2" -L ROOT_MNJRO 1> /dev/null 2>&1
        else
          cryptsetup luksFormat -q "${SDCARD}${SDDEV}2"
          cryptsetup open "${SDCARD}${SDDEV}2" ROOT_MNJRO
          mkfs.ext4 -O ^metadata_csum,^64bit /dev/mapper/ROOT_MNJRO 1> /dev/null 2>&1
        fi
254

Dan Johansen's avatar
Dan Johansen committed
255 256
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
257
        mount ${SDCARD}${SDDEV}1 $TMPDIR/boot
Dan Johansen's avatar
Dan Johansen committed
258 259 260 261 262 263
        if [ -z "$CRYPT" ]; then
          mount ${SDCARD}${SDDEV}2 $TMPDIR/root
        else
          [ ! -e /dev/mapper/ROOT_MNJRO ] && cryptsetup open "${SDCARD}${SDDEV}2" ROOT_MNJRO
          mount /dev/mapper/ROOT_MNJRO $TMPDIR/root
        fi
264 265 266 267
}

cleanup () {
    msg "Writing bootloader and cleaning up after install..."
268 269 270 271 272 273 274 275 276
    # Move boot files
    mv $TMPDIR/root/boot/* $TMPDIR/boot
    # Flash bootloader
    case "$DEVICE" in
    oc2)
        dd if=$TMPDIR/boot/bl1.bin.hardkernel of=${SDCARD} conv=fsync bs=1 count=442 1> /dev/null 2>&1
        dd if=$TMPDIR/boot/bl1.bin.hardkernel of=${SDCARD} conv=fsync bs=512 skip=1 seek=1 1> /dev/null 2>&1
        dd if=$TMPDIR/boot/u-boot.gxbb of=${SDCARD} conv=fsync bs=512 seek=97 1> /dev/null 2>&1
        ;;
277
    on2|oc4)
278
        dd if=$TMPDIR/boot/u-boot.bin of=${SDCARD} conv=fsync,notrunc bs=512 seek=1 1> /dev/null 2>&1
279 280 281 282 283
        ;;
    vim1|vim2|vim3)
        dd if=$TMPDIR/boot/u-boot.bin of=${SDCARD} conv=fsync bs=1 count=442 1> /dev/null 2>&1
        dd if=$TMPDIR/boot/u-boot.bin of=${SDCARD} conv=fsync bs=512 skip=1 seek=1 1> /dev/null 2>&1
        ;;
284
    pinebook|pine64-lts|pine64|pinephone|pinetab|pine-h64)
285
        dd if=$TMPDIR/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} conv=fsync bs=8k seek=1 1> /dev/null 2>&1
286
        ;;
287
    pbpro|rockpro64|rockpi4|nanopc-t4|rock64|roc-cc)
288 289
        dd if=$TMPDIR/boot/idbloader.img of=${SDCARD} seek=64 conv=notrunc,fsync 1> /dev/null 2>&1
        dd if=$TMPDIR/boot/u-boot.itb of=${SDCARD} seek=16384 conv=notrunc,fsync 1> /dev/null 2>&1
290 291
        ;;
    esac
Dan Johansen's avatar
Dan Johansen committed
292 293 294

	[ ! -z "$CRYPT" ] && post_crypt
    
295 296 297
    #clean up
        umount $TMPDIR/root
        umount $TMPDIR/boot
Dan Johansen's avatar
Dan Johansen committed
298 299 300 301
		if [ ! -z "$CRYPT" ]; then
			cryptsetup close /dev/mapper/ROOT_MNJRO
		fi
		partprobe $SDCARD 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
302 303
}

Dan Johansen's avatar
Dan Johansen committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
tweakinitrd_crypt () {
    case "$DEVICE" in
    pbpro)
      # Use the proper mkinitcpio.
      # NOTE: I've tried to modify only the HOOKS but it seems some kernel modules are required for the display to show stuff
      cat << EOF > ${TMPDIR}/root/etc/mkinitcpio.conf
MODULES=(panfrost rockchipdrm drm_kms_helper hantro_vpu analogix_dp rockchip_rga panel_simple arc_uart cw2015_battery i2c-hid icp iscsi_boot_sysfs jsm pwm_bl spl uhid)
BINARIES=()
FILES=()
HOOKS=(base udev keyboard autodetect keymap modconf block encrypt lvm2 filesystems fsck)
COMPRESSION="cat"
EOF

      # Install lvm2, this will trigger the cpio rebuild
      $NSPAWN $TMPDIR/root pacman -Syyu lvm2 --noconfirm
      ;;
    esac
}

post_crypt () {
    # Get the UUID
    UUID=$(blkid -s UUID -o value "${SDCARD}${SDDEV}2")

    # Modify the /boot/extlinux/extlinux.conf to match our needs
    case "$DEVICE" in
    pbpro)
      # NOTE: I've tried to only modify the cryptdevice and root parameters but bootsplash and console=ttyS2 prevents to show the password prompt
      sed -i -e "s!APPEND.*!APPEND initrd=/initramfs-linux.img console=tty1 cryptdevice=UUID=${UUID}:ROOT_MNJRO root=/dev/mapper/ROOT_MNJRO rw rootwait video=eDP-1:1920x1080@60 video=HDMI-A-1:1920x1080@60!g" ${TMPDIR}/boot/extlinux/extlinux.conf
      ;;
    esac

    # Generate the /etc/crypttab file
    echo "ROOT_MNJRO   UUID=${UUID}    none            luks,discard" > ${TMPDIR}/root/etc/crypttab
}
338 339

# Using Dialog to ask for user input for variables
340
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
Helmut Stult's avatar
Helmut Stult committed
341
        --menu "Choose a device:" 20 75 10 \
342
        "rpi4"      "Raspberry Pi 4 (and 3)" \
Dan Johansen's avatar
Dan Johansen committed
343
        "pbpro"     "Pinebook Pro" \
344
		"rockpro64" "RockPro64" \
345 346 347 348 349
		"rockpi4"   "Rock Pi 4" \
		"on2"       "Odroid N2" \
		"oc4"		"Odroid C4" \
		"oc2"       "Odroid C2" \
		"pinebook"  "Pinebook" \
350
		"pine64-lts"    "Pine64-LTS / Sopine" \
351
		"pine64"    "Pine64+" \
352 353 354
		"pine-h64"	"Pine H64" \
		"rock64"    "Rock64" \
		"roc-cc"	"LibreComputer Renegade" \
355
		"nanopc-t4"	"NanoPC T4" \
Dan Johansen's avatar
Dan Johansen committed
356
		"vim3"      "Khadas Vim 3" \
357
		"vim2"		"Khadas Vim 2" \
Dan Johansen's avatar
Dan Johansen committed
358
		"vim1"      "Khadas Vim 1" \
Helmut Stult's avatar
 
Helmut Stult committed
359
		3>&1 1>&2 2>&3 3>&-)
360 361 362


#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
363 364 365 366
if [ ! -z "$DEVICE" ]; then
	EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose an edition:" 20 75 10 \
		"minimal"   "Minimal Edition            (only CLI)" \
367
		"kde-plasma" "Full KDE/Plasma Desktop    (full featured)" \
368
		"xfce"      "Full XFCE desktop and apps (full featured)" \
369
		"mate"      "Full MATE desktop and apps (lightweight)" \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
370 371
		"lxqt"      "Full LXQT Desktop and apps (lightweight)" \
		"i3"        "Mininal i3 WM with apps    (very light)" \
Dan Johansen's avatar
Dan Johansen committed
372
		"sway"		"Minimal Sway WM with apps  (very light)" \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
373
		"cubocore"  "QT based Desktop           (lightweight)" \
374
		"gnome"     "Full Gnome desktop and apps (EXPERIMANTAL)" \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
375
		3>&1 1>&2 2>&3 3>&-) 
376 377 378 379 380 381 382

else 
	clear
	exit 1
fi


Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
383 384
if [ ! -z "$EDITION" ]; then
	USER=$(dialog --clear --title "Manjaro ARM Installer" \
Dan Johansen's avatar
Dan Johansen committed
385
	--inputbox "Enter the username you want:
386
(usernames most be all lowercase)" 8 50 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
387
	3>&1 1>&2 2>&3 3>&-)
Dan Johansen's avatar
Dan Johansen committed
388 389 390 391 392
    if [[ "$USER" =~ [A-Z] ]] || [[ "$USER" == *['!'@#\$%^\&*()_+]* ]]; then
    clear
    msg "Configuration aborted! Username contained invalid characters."
    exit 1
    fi
393 394 395 396 397
else 
	clear
	exit 1
fi

398 399
if [ ! -z "$USER" ]
then
400 401
USERGROUPS=$(dialog --clear --title "Manjaro ARM Installer" \
    --inputbox "Enter additional groups for $USER in a comma seperated list: (empty if none)
402
(default: wheel,sys,audio,input,video,storage,lp,network,users,power)" 8 90 \
403 404 405 406 407 408 409 410 411 412
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi

if [ ! -z "$USER" ]
then
FULLNAME=$(dialog --clear --title "Manjaro ARM Installer" \
Dan Johansen's avatar
Dan Johansen committed
413
    --inputbox "Enter desired Full Name for $USER:" 8 50 \
414 415 416 417 418 419
        3>&1 1>&2 2>&3 3>&- \
	)
else
    clear
    exit 1
fi
420 421


422
if [ ! -z "$FULLNAME" ]; then
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
423
	PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Dan Johansen's avatar
Dan Johansen committed
424
	--insecure --passwordbox "Enter new Password for $USER:" 8 50 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
425
	3>&1 1>&2 2>&3 3>&- \
426 427 428 429 430 431
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
432 433
if [ ! -z "$PASSWORD" ]; then
	CONFIRMPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Dan Johansen's avatar
Dan Johansen committed
434
	--insecure --passwordbox "Confirm new Password for $USER:" 8 50 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
435
	3>&1 1>&2 2>&3 3>&- \
Dan Johansen's avatar
Dan Johansen committed
436 437 438 439 440
	)
else 
	clear
	exit 1
fi
441

Dan Johansen's avatar
Dan Johansen committed
442
if [[ "$PASSWORD" != "$CONFIRMPASSWORD" ]]; then
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
443 444 445
	clear
	msg "User passwords do not match! Please restart the installer and try again."
	exit 1
Dan Johansen's avatar
Dan Johansen committed
446
fi
447

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
448 449
if [ ! -z "$CONFIRMPASSWORD" ]; then
	ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Dan Johansen's avatar
Dan Johansen committed
450
	--insecure --passwordbox "Enter new Root Password:" 8 50 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
451
	3>&1 1>&2 2>&3 3>&- \
452 453 454 455 456 457
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
458 459
if [ ! -z "$ROOTPASSWORD" ]; then
	CONFIRMROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
Dan Johansen's avatar
Dan Johansen committed
460
	--insecure --passwordbox "Confirm new Root Password:" 8 50 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
461
	3>&1 1>&2 2>&3 3>&- \
Dan Johansen's avatar
Dan Johansen committed
462 463 464 465 466
	)
else 
	clear
	exit 1
fi
467

Dan Johansen's avatar
Dan Johansen committed
468
if [[ "$ROOTPASSWORD" != "$CONFIRMROOTPASSWORD" ]]; then
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
469 470 471
	clear
	msg "Root passwords do not match! Please restart the installer and try again."
	exit 1
Dan Johansen's avatar
Dan Johansen committed
472
fi
473

Dan Johansen's avatar
Dan Johansen committed
474
if [ ! -z "$CONFIRMROOTPASSWORD" ]
475 476 477
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
478 479 480 481 482 483 484
	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" \
Dan Johansen's avatar
Dan Johansen committed
485 486
	--menu "Choose your SDCard/eMMC/USB - Be sure the correct drive is selected! 
WARNING! This WILL destroy the data on it!" 20 50 10 \
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
487
	"${W[@]}" 3>&2 2>&1 1>&3)
488 489

# add /dev/ to the selected option above
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
490 491 492
	DEV_NAME=$SDCARD
	SDCARD=/dev/$SDCARD
	SDTYP=${SDCARD:5:2}
493 494 495
else 
	clear
	exit 1
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
496
fi
497

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
498 499 500 501 502 503 504
if [[ "$SDTYP" = "sd" ]]; then
	SDDEV=""
elif [[ "$SDTYP" = "mm" ]]; then
	SDDEV="p"
else 
	clear
	exit 1
505 506 507
fi


Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
508 509 510 511 512 513 514 515 516 517
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>&- \
518 519 520 521 522
	)
else 
	clear
	exit 1
fi
523 524


Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
525 526 527 528 529 530
if [ ! -z "$TIMEZONE" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
531
	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
532 533 534 535
	LOCALE=$(dialog --clear --title "Manjaro ARM Installer" \
		--menu "Choose your locale!" 20 50 15 \
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
		)
536 537 538 539 540
else 
	clear
	exit 1
fi

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
541 542 543 544 545 546 547
if [ ! -z "$LOCALE" ]; then
	let i=0
	W=()
	while read -r line; do
		let i=$i+1
		W+=($line "")
	done < <( localectl list-keymaps )
548 549
	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
550
		"${W[@]}" 3>&1 1>&2 2>&3 3>&- \
551 552 553 554 555 556
	)
else 
	clear
	exit 1
fi

557
if [[ "$EDITION" != "minimal" ]]; then
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
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
573
fi
574

575
if [ ! -z "$CLIKEYMAP" ]; then
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
576
	HOSTNAME=$(dialog --clear --title "Manjaro ARM Installer" \
577
	--inputbox "Enter desired hostname for this system:" 8 50 \
Helmut Stult's avatar
 
Helmut Stult committed
578
	3>&1 1>&2 2>&3 3>&- \
579 580 581 582 583 584
	)
else 
	clear
	exit 1
fi

Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
585 586
if [ ! -z "$HOSTNAME" ]; then
	dialog --clear --title "Manjaro ARM Installer" \
587
    --yesno "Is the below information correct:
588 589
    Device = $DEVICE
    Edition = $EDITION
590
    Username = $USER
591
    Additional usergroups = $USERGROUPS
592 593
    Password for $USER = (password hidden)
    Password for root = (password hidden)
Dan Johansen's avatar
Dan Johansen committed
594
    SDCard/eMMC/USB = $SDCARD
595
    Timezone = $TIMEZONE
596
    Locale = $LOCALE
597 598
    TTY Keyboard layout = $CLIKEYMAP
    X11 Keyboard layout = $X11KEYMAP
Helmut Stult's avatar
Helmut Stult committed
599
    Hostname = $HOSTNAME" 20 70 \
600 601
    3>&1 1>&2 2>&3 3>&-
else
Helmut Stult's avatar
v1.1.0  
Helmut Stult committed
602 603
	clear
	exit 1
604 605 606 607 608 609 610 611 612
fi

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

613

614 615
# get the profiles
getarmprofiles
Dan Johansen's avatar
Dan Johansen committed
616 617

#Package lists
618 619 620
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
621

622

Dan Johansen's avatar
Dan Johansen committed
623
# Commands
624
timer_start=$(get_timer)
Dan Johansen's avatar
Dan Johansen committed
625 626

prepare_card
627 628 629 630
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync