#! /bin/bash # ***************************** # Version 1.2.17 # ***************************** # Set globals TMPDIR=/var/tmp/manjaro-arm-installer ARCH='aarch64' NSPAWN='systemd-nspawn -q --resolv-conf=copy-host --timezone=off -D' # set colorscheme export DIALOGRC="./dialogrc_gui" # clearing variables DEVICE="" EDITION="" USER="" USERGROUPS="" FULLNAME="" PASSWORD="" CONFIRMPASSWORD="" CONFIRMROOTPASSWORD="" ROOTPASSWORD="" SDCARD="" SDTYP="" SDDEV="" DEV_NAME="" TIMEZONE="" LOCALE="" HOSTNAME="" # check if root if [ "$EUID" -ne 0 ]; then echo "*******************************************************************************************" echo "* *" echo "* This script requires root permissions to run. Please run as root or with sudo! *" echo "* *" echo "*******************************************************************************************" exit fi # Sanity checks for dependencies declare -a DEPENDNECIES=("git" "parted" "systemd-nspawn" "wget" "dialog" "bsdtar" "openssl" "awk" "mkfs.vfat") 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." exit 1 fi done 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 # Functions 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 } 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 } 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)" } getarmprofiles () { info "Getting package lists ready for $DEVICE $EDITION edition..." if [ -d "$TMPDIR/arm-profiles/" ]; then cd $TMPDIR/arm-profiles git fetch origin 1> /dev/null 2>&1 git reset --hard origin/master 1> /dev/null 2>&1 else mkdir -p $TMPDIR chmod 777 $TMPDIR git clone https://gitlab.manjaro.org/manjaro-arm/applications/arm-profiles.git $TMPDIR/arm-profiles/ 1> /dev/null 2>&1 fi } create_install() { msg "Creating install for $DEVICE..." info "Used device is ${SDCARD}${SDDEV}" # fetch and extract rootfs info "Downloading latest $ARCH rootfs..." cd $TMPDIR wget -q --show-progress --progress=bar:force:noscroll https://osdn.net/projects/manjaro-arm/storage/.rootfs/Manjaro-ARM-$ARCH-latest.tar.gz info "Extracting $ARCH rootfs..." bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root info "Setting up keyrings..." $NSPAWN $TMPDIR/root pacman-key --init 1> /dev/null 2>&1 $NSPAWN $TMPDIR/root pacman-key --populate archlinux archlinuxarm manjaro manjaro-arm 1> /dev/null 2>&1 info "Generating mirrorlist..." $NSPAWN $TMPDIR/root pacman-mirrors -f10 1> /dev/null 2>&1 info "Installing packages for $EDITION on $DEVICE..." # Setup cache mount mkdir -p $TMPDIR/pkg-cache mount -o bind $TMPDIR/pkg-cache $TMPDIR/root/var/cache/pacman/pkg # Install device and editions specific packages $NSPAWN $TMPDIR/root pacman -Syyu base manjaro-system manjaro-release systemd systemd-libs $PKG_EDITION $PKG_DEVICE --noconfirm info "Enabling services..." # Enable services $NSPAWN $TMPDIR/root systemctl enable getty.target haveged.service 1> /dev/null 2>&1 $NSPAWN $TMPDIR/root systemctl enable $SRV_EDITION 1> /dev/null 2>&1 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 info "Applying overlay for $EDITION..." cp -ap $TMPDIR/arm-profiles/overlays/$EDITION/* $TMPDIR/root/ info "Setting up users..." #setup users echo "$USER" > $TMPDIR/user echo "$PASSWORD" > $TMPDIR/password echo "$ROOTPASSWORD" > $TMPDIR/rootpassword info "Setting password for root ..." $NSPAWN $TMPDIR/root awk -i inplace -F: "BEGIN {OFS=FS;} \$1 == \"root\" {\$2=\"$(openssl passwd -1 $(cat $TMPDIR/rootpassword))\"} 1" /etc/shadow 1> /dev/null 2>&1 info "Adding user..." $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 $NSPAWN $TMPDIR/root usermod -aG $USERGROUPS $(cat $TMPDIR/user) 1> /dev/null 2>&1 $NSPAWN $TMPDIR/root chfn -f "$FULLNAME" $(cat $TMPDIR/user) 1> /dev/null 2>&1 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 info "Setting up system settings..." #system setup $NSPAWN $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1 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/ $NSPAWN $TMPDIR/root ln -sf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime 1> /dev/null 2>&1 $NSPAWN $TMPDIR/root sed -i s/"#$LOCALE"/"$LOCALE"/g /etc/locale.gen 1> /dev/null 2>&1 echo "LANG=$LOCALE" | tee --append $TMPDIR/root/etc/locale.conf 1> /dev/null 2>&1 $NSPAWN $TMPDIR/root locale-gen echo "KEYMAP=$CLIKEYMAP" | tee --append $TMPDIR/root/etc/vconsole.conf 1> /dev/null 2>&1 if [[ "$EDITION" != "minimal" ]]; then 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 sed -i s/"us"/"$X11KEYMAP"/ $TMPDIR/root/etc/X11/xorg.conf.d/00-keyboard.conf fi echo "$HOSTNAME" | tee --append $TMPDIR/root/etc/hostname 1> /dev/null 2>&1 sed -i s/"enable systemd-resolved.service"/"#enable systemd-resolved.service"/ $TMPDIR/root/usr/lib/systemd/system-preset/90-systemd.preset echo "Correcting permissions from overlay..." chown -R root:root $TMPDIR/root/etc if [[ "$EDITION" != "minimal" && "$EDITION" != "server" ]]; then 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/ fi info "Cleaning install for unwanted files..." umount $TMPDIR/root/var/cache/pacman/pkg rm -rf $TMPDIR/root/usr/bin/qemu-aarch64-static rm -rf $TMPDIR/root/var/cache/pacman/pkg/* rm -rf $TMPDIR/root/var/log/* rm -rf $TMPDIR/root/etc/*.pacnew rm -rf $TMPDIR/root/usr/lib/systemd/system/systemd-firstboot.service rm -rf $TMPDIR/root/etc/machine-id # Remove temp files on host rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz* msg "$DEVICE $EDITION install complete" } prepare_card () { msg "Getting $SDCARD ready for $DEVICE..." # umount SD card umount ${SDCARD}${SDDEV}1 1> /dev/null 2>&1 umount ${SDCARD}${SDDEV}2 1> /dev/null 2>&1 # Create partitions #Clear first 32mb dd if=/dev/zero of=${SDCARD} bs=1M count=32 1> /dev/null 2>&1 #partition with boot and root parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1 parted -s $SDCARD mkpart primary fat32 32M 256M 1> /dev/null 2>&1 START=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/start` SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}${SDDEV}1/size` END_SECTOR=$(expr $START + $SIZE) parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1 partprobe $SDCARD 1> /dev/null 2>&1 mkfs.vfat "${SDCARD}${SDDEV}1" -n BOOT_MNJRO 1> /dev/null 2>&1 mkfs.ext4 -O ^metadata_csum,^64bit "${SDCARD}${SDDEV}2" -L ROOT_MNJRO 1> /dev/null 2>&1 mkdir -p $TMPDIR/root mkdir -p $TMPDIR/boot mount ${SDCARD}${SDDEV}1 $TMPDIR/boot mount ${SDCARD}${SDDEV}2 $TMPDIR/root } cleanup () { msg "Writing bootloader and cleaning up after install..." # 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 ;; on2|oc4) dd if=$TMPDIR/boot/u-boot.bin of=${SDCARD} conv=fsync,notrunc bs=512 seek=1 1> /dev/null 2>&1 ;; 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 ;; pinebook|pine64-lts|pine64|pinephone|pinetab|pine-h64) dd if=$TMPDIR/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1 ;; pbpro|rockpro64|rockpi4) dd if=$TMPDIR/boot/idbloader.img of=${SDCARD} seek=64 conv=notrunc 1> /dev/null 2>&1 dd if=$TMPDIR/boot/u-boot.itb of=${SDCARD} seek=16384 conv=notrunc 1> /dev/null 2>&1 ;; rock64|roc-cc) dd if=$TMPDIR/boot/idbloader.img of=${SDCARD} seek=64 conv=notrunc 1> /dev/null 2>&1 dd if=$TMPDIR/boot/uboot.img of=${SDCARD} seek=16384 conv=notrunc 1> /dev/null 2>&1 dd if=$TMPDIR/boot/trust.img of=${SDCARD} seek=24576 conv=notrunc 1> /dev/null 2>&1 ;; esac #clean up umount $TMPDIR/root umount $TMPDIR/boot rm -r $TMPDIR/root $TMPDIR/boot partprobe $SDCARD 1> /dev/null 2>&1 } # Using Dialog to ask for user input for variables DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \ --menu "Choose a device:" 20 75 10 \ "rpi4" "Raspberry Pi 4" \ "rpi3" "Raspberry Pi 3" \ "pbpro" "Pinebook Pro" \ "rockpro64" "RockPro64" \ "rockpi4" "Rock Pi 4" \ "on2" "Odroid N2" \ "oc4" "Odroid C4" \ "oc2" "Odroid C2" \ "pinebook" "Pinebook" \ "pine64-lts" "Pine64-LTS / Sopine" \ "pine64" "Pine64+" \ "pine-h64" "Pine H64" \ "rock64" "Rock64" \ "roc-cc" "LibreComputer Renegade" \ "vim3" "Khadas Vim 3" \ "vim2" "Khadas Vim 2" \ "vim1" "Khadas Vim 1" \ 3>&1 1>&2 2>&3 3>&-) #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 if [ ! -z "$DEVICE" ]; then EDITION=$(dialog --clear --title "Manjaro ARM Installer" \ --menu "Choose an edition:" 20 75 10 \ "minimal" "Minimal Edition (only CLI)" \ "kde-plasma" "Full KDE/Plasma Desktop (full featured)" \ "xfce" "Full XFCE desktop and apps (full featured)" \ "mate" "Full MATE desktop and apps (lightweight)" \ "lxqt" "Full LXQT Desktop and apps (lightweight)" \ "i3" "Mininal i3 WM with apps (very light)" \ "cubocore" "QT based Desktop (lightweight)" \ "gnome" "Full Gnome desktop and apps (EXPERIMANTAL)" \ 3>&1 1>&2 2>&3 3>&-) else clear exit 1 fi if [ ! -z "$EDITION" ]; then USER=$(dialog --clear --title "Manjaro ARM Installer" \ --inputbox "Enter username: (usernames most be all lowercase)" 8 50 \ 3>&1 1>&2 2>&3 3>&-) if [[ "$USER" =~ [A-Z] ]] || [[ "$USER" == *['!'@#\$%^\&*()_+]* ]]; then clear msg "Configuration aborted! Username contained invalid characters." exit 1 fi else clear exit 1 fi if [ ! -z "$USER" ] then USERGROUPS=$(dialog --clear --title "Manjaro ARM Installer" \ --inputbox "Enter additional groups for $USER in a comma seperated list: (empty if none) (default: wheel,sys,audio,input,video,storage,lp,network,users,power)" 8 90 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [ ! -z "$USER" ] then FULLNAME=$(dialog --clear --title "Manjaro ARM Installer" \ --inputbox "Enter Full Name for $USER:" 8 50 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [ ! -z "$FULLNAME" ]; then PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \ --insecure --passwordbox "Enter Password for $USER:" 8 50 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [ ! -z "$PASSWORD" ]; then CONFIRMPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \ --insecure --passwordbox "Confirm Password for $USER:" 8 50 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [[ "$PASSWORD" != "$CONFIRMPASSWORD" ]]; then clear msg "User passwords do not match! Please restart the installer and try again." exit 1 fi if [ ! -z "$CONFIRMPASSWORD" ]; then ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \ --insecure --passwordbox "Enter Root Password:" 8 50 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [ ! -z "$ROOTPASSWORD" ]; then CONFIRMROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \ --insecure --passwordbox "Confirm Root Password:" 8 50 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [[ "$ROOTPASSWORD" != "$CONFIRMROOTPASSWORD" ]]; then clear msg "Root passwords do not match! Please restart the installer and try again." exit 1 fi if [ ! -z "$CONFIRMROOTPASSWORD" ] 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 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" \ --menu "Choose your SDCard - Be sure the correct drive is selected!" 20 50 10 \ "${W[@]}" 3>&2 2>&1 1>&3) # add /dev/ to the selected option above DEV_NAME=$SDCARD SDCARD=/dev/$SDCARD SDTYP=${SDCARD:5:2} else clear exit 1 fi if [[ "$SDTYP" = "sd" ]]; then SDDEV="" elif [[ "$SDTYP" = "mm" ]]; then SDDEV="p" else clear exit 1 fi 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>&- \ ) else clear exit 1 fi if [ ! -z "$TIMEZONE" ]; then let i=0 W=() while read -r line; do let i=$i+1 W+=($line "") done < <( cat /etc/locale.gen | grep "UTF-8" | tail -n +2 | sed -e 's/^#*//' | awk '{print $1}' ) LOCALE=$(dialog --clear --title "Manjaro ARM Installer" \ --menu "Choose your locale!" 20 50 15 \ "${W[@]}" 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [ ! -z "$LOCALE" ]; then let i=0 W=() while read -r line; do let i=$i+1 W+=($line "") done < <( localectl list-keymaps ) CLIKEYMAP=$(dialog --clear --title "Manjaro ARM Installer" \ --menu "Choose your TTY keyboard layout:" 20 50 15 \ "${W[@]}" 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [[ "$EDITION" != "minimal" ]]; then 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 fi if [ ! -z "$CLIKEYMAP" ]; then HOSTNAME=$(dialog --clear --title "Manjaro ARM Installer" \ --inputbox "Enter desired hostname for this system:" 8 50 \ 3>&1 1>&2 2>&3 3>&- \ ) else clear exit 1 fi if [ ! -z "$HOSTNAME" ]; then dialog --clear --title "Manjaro ARM Installer" \ --yesno "Is the below information correct: Device = $DEVICE Edition = $EDITION Username = $USER Additional usergroups = $USERGROUPS Password for $USER = (password hidden) Password for root = (password hidden) SD Card = $SDCARD Timezone = $TIMEZONE Locale = $LOCALE TTY Keyboard layout = $CLIKEYMAP X11 Keyboard layout = $X11KEYMAP Hostname = $HOSTNAME" 20 70 \ 3>&1 1>&2 2>&3 3>&- else clear exit 1 fi response=$? case $response in 0) clear; msg "Proceeding....";; 1) clear; msg "Installation aborted...."; exit 1;; 255) clear; msg "Installation aborted..."; exit 1;; esac # get the profiles getarmprofiles #Package lists 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}') # Commands timer_start=$(get_timer) prepare_card create_install cleanup show_elapsed_time "${FUNCNAME}" "${timer_start}" sync