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

# Set globals
4
TMPDIR=/var/tmp/manjaro-arm-installer
5
#PROFILES=/var/tmp/manjaro-arm-installer/profiles
6
ARCH='aarch64'
Dan Johansen's avatar
Dan Johansen committed
7

8 9 10 11 12 13 14
# clearing variables
DEVICE=""
EDITION=""
USER=""
PASSWORD=""
ROOTPASSWORD=""
SDCARD=""
15 16
#TIMEZONE=""
#LOCALE=""
17

18
# Sanity checks for dependencies
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
if [ ! -f /usr/bin/sudo ]; then
    echo "sudo command is missing! Please install sudo!"
    exit 1
    elif [ ! -f /usr/bin/git ]; then
    echo "git command is missing! Please install git!"
    exit 1
    elif [ ! -f /usr/bin/parted ]; then
    echo "parted command is missing! Please install parted!"
    exit 1
    elif [ ! -f /usr/bin/systemd-nspawn ]; then
    echo "systemd-nspawn command is missing! Please install systemd!"
    exit 1
    elif [ ! -f /usr/bin/wget ]; then
    echo "wget command is missing! Please install wget!"
    exit 1
    elif [ ! -f /usr/bin/dialog ]; then
    echo "dialog command is missing! Please install dialog!"
    exit 1
    elif [ ! -f /usr/bin/bsdtar ]; then
    echo "bsdtar command is missing! Please install libarchive!"
    exit 1
fi
41

Dan Johansen's avatar
Dan Johansen committed
42 43

# Functions
44 45 46 47 48 49 50 51
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
 }

Dan Johansen's avatar
Dan Johansen committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

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

74
getarmprofiles () {
75 76 77
    if ls $TMPDIR/arm-profiles/* 1> /dev/null 2>&1; then
        cd $TMPDIR/arm-profiles
        git pull 1> /dev/null 2>&1
78
    else
79 80 81 82
        sudo mkdir -p $TMPDIR
        sudo chmod 777 $TMPDIR
        cd $TMPDIR
        git clone https://gitlab.com/Strit/arm-profiles.git 1> /dev/null 2>&1
83 84 85 86 87 88 89 90 91 92 93 94
    fi

}

create_install() {
 if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
        ARCH='armv7h'
    else
        ARCH='aarch64'
    fi

    msg "Creating install for $DEVICE..."
95 96 97 98
    
    # fetch and extract rootfs
    msg "Downloading latest $ARCH rootfs..."
    cd $TMPDIR
99
    wget https://www.strits.dk/files/Manjaro-ARM-$ARCH-latest.tar.gz 1> /dev/null 2>&1
100 101
    
    msg "Extracting $ARCH rootfs..."
Dan Johansen's avatar
Dan Johansen committed
102
    sudo bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
103 104
    
    msg "Setting up keyrings..."
105 106
    sudo systemd-nspawn -D $TMPDIR/root pacman-key --init 1> /dev/null 2>&1
    sudo systemd-nspawn -D $TMPDIR/root pacman-key --populate archlinuxarm manjaro manjaro-arm 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
107 108 109
    
    msg "Installing packages for $EDITION on $DEVICE..."
    # Install device and editions specific packages
110
    sudo systemd-nspawn -D $TMPDIR/root pacman -Syy base $PKG_DEVICE $PKG_EDITION --needed --noconfirm
Dan Johansen's avatar
Dan Johansen committed
111 112 113
    
    msg "Enabling services..."
    # Enable services
114 115
    sudo systemd-nspawn -D $TMPDIR/root systemctl enable systemd-networkd.service getty.target haveged.service dhcpcd.service 1> /dev/null 2>&1
    sudo systemd-nspawn -D $TMPDIR/root systemctl enable $SRV_EDITION 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
116 117

    if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]]; then
118
        sudo systemd-nspawn -D $TMPDIR/root systemctl enable amlogic.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
119 120 121
    fi

    msg "Applying overlay for $EDITION..."
122
    sudo cp -ap $PROFILES/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
Dan Johansen's avatar
Dan Johansen committed
123 124 125
    
    msg "Setting up users..."
    #setup users
126 127 128
    echo "$USER" > $TMPDIR/user
    echo "$PASSWORD" >> $TMPDIR/password
    echo "$PASSWORD" >> $TMPDIR/password
129 130 131
    echo "$ROOTPASSWORD" >> $TMPDIR/rootpassword
    echo "$ROOTPASSWORD" >> $TMPDIR/rootpassword
    sudo systemd-nspawn -D $TMPDIR/root passwd root < $TMPDIR/rootpassword 1> /dev/null 2>&1
132 133 134
    sudo systemd-nspawn -D $TMPDIR/root useradd -m -g users -G wheel,storage,network,power -s /bin/bash $(cat $TMPDIR/user) 1> /dev/null 2>&1
    sudo systemd-nspawn -D $TMPDIR/root passwd $(cat $TMPDIR/user) < $TMPDIR/password 1> /dev/null 2>&1

Dan Johansen's avatar
Dan Johansen committed
135 136 137 138 139
    
    msg "Enabling user services..."
    if [[ "$EDITION" = "minimal" ]] || [[ "$EDITION" = "server" ]]; then
        echo "No user services for $EDITION edition"
    else
140
        sudo systemd-nspawn -D $TMPDIR/root --user $(cat $TMPDIR/user) systemctl --user enable pulseaudio.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
141 142 143 144
    fi

    msg "Setting up system settings..."
    #system setup
145 146
    sudo systemd-nspawn -D $TMPDIR/root chmod u+s /usr/bin/ping 1> /dev/null 2>&1
    sudo systemd-nspawn -D $TMPDIR/root update-ca-trust 1> /dev/null 2>&1
147 148
#    sudo systemd-nspawn -D $TMPDIR/root timedatectl set-timezone "$TIMEZONE"
#    sudo systemd-nspawn -D $TMPDIR/root localectl set-locale "$LOCALE"
Dan Johansen's avatar
Dan Johansen committed
149 150 151
    
    msg "Doing device specific setups for $DEVICE..."
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
152 153 154 155
        echo "dtparam=audio=on" | sudo tee --append $TMPDIR/root/boot/config.txt
        echo "hdmi_drive=2" | sudo tee --append $TMPDIR/root/boot/config.txt
        echo "audio_pwm_mode=2" | sudo tee --append $TMPDIR/root/boot/config.txt
        echo "/dev/mmcblk0p1  /boot   vfat    defaults        0       0" | sudo tee --append $TMPDIR/root/etc/fstab
Dan Johansen's avatar
Dan Johansen committed
156 157 158 159 160
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]]; then
        echo "No device setups for $DEVICE..."
    elif [[ "$DEVICE" = "rock64" ]] || [[ "$DEVICE" = "rockpro64" ]]; then
        echo "No device setups for $DEVICE..."
    elif [[ "$DEVICE" = "pinebook" ]]; then
161 162
        sudo systemd-nspawn -D $TMPDIR/root systemctl enable pinebook-post-install.service 1> /dev/null 2>&1
        sudo systemd-nspawn -D $TMPDIR/root --user $(cat $TMPDIR/user) systemctl --user enable pinebook-user.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
163 164 165 166
    else
        echo ""
    fi
    
167
    msg "Cleaning install for unwanted files..."
Dan Johansen's avatar
Dan Johansen committed
168
       if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
169
        sudo rm $TMPDIR/root/usr/bin/qemu-arm-static
Dan Johansen's avatar
Dan Johansen committed
170
    else
171
        sudo rm $TMPDIR/root/usr/bin/qemu-aarch64-static
Dan Johansen's avatar
Dan Johansen committed
172
    fi
173 174 175 176
    sudo rm -rf $TMPDIR/root/var/cache/pacman/pkg/*
    sudo rm -rf $TMPDIR/root/var/log/*
    
    # Remove temp files on host
Dan Johansen's avatar
Dan Johansen committed
177 178
    sudo rm -rf $TMPDIR/user $TMPDIR/password $TMPDIR/rootpassword
    sudo rm -rf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz
Dan Johansen's avatar
Dan Johansen committed
179

180
    msg "$DEVICE $EDITION install complete"
Dan Johansen's avatar
Dan Johansen committed
181 182 183
}

prepare_card () {
184
    msg "Getting $SDCARD ready for $DEVICE..."
Dan Johansen's avatar
Dan Johansen committed
185 186 187
    # For Raspberry Pi devices
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
        #partition with boot and root
188 189 190
        sudo parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        sudo parted -s $SDCARD mkpart primary fat32 0% 100M 1> /dev/null 2>&1
        START=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/start` 
Dan Johansen's avatar
Dan Johansen committed
191
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
192
        END_SECTOR=$(expr $START + $SIZE)
193 194 195 196
        sudo parted -s $SDCARD mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
        sudo partprobe $SDCARD 1> /dev/null 2>&1
        sudo mkfs.vfat "${SDCARD}1" 1> /dev/null 2>&1
        sudo mkfs.ext4 "${SDCARD}2" 1> /dev/null 2>&1
197 198
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
199 200
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
201 202
        sudo mount ${SDCARD}1 $TMPDIR/boot
        sudo mount ${SDCARD}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
203 204 205 206

    # For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
        #Clear first 8mb
207
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
208 209
	
    #partition with a single root partition
210 211 212 213
        sudo parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        sudo parted -s $SDCARD mkpart primary ext4 0% 100% 1> /dev/null 2>&1
        sudo partprobe $SDCARD 1> /dev/null 2>&1
        sudo mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}1 1> /dev/null 2>&1
214 215
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
216 217
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
218
        sudo mount ${SDCARD}1 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
219 220 221 222 223

    # For pinebook device
    elif [[ "$DEVICE" = "pinebook" ]]; then

    #Clear first 8mb
224
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
225 226
	
    #partition with a single root partition
227 228 229 230
        sudo parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        sudo parted -s $SDCARD mkpart primary ext4 0% 100% 1> /dev/null 2>&1
        sudo partprobe $SDCARD 1> /dev/null 2>&1
        sudo mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}1 1> /dev/null 2>&1
231 232
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
233 234
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
235
        sudo mount ${SDCARD}1 $TMPDIR/root
236

Dan Johansen's avatar
Dan Johansen committed
237 238 239 240
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then

    #Clear first 8mb
241
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
242 243
	
    #partition with a single root partition
244 245 246 247
        sudo parted -s $SDCARD mklabel msdos 1> /dev/null 2>&1
        sudo parted -s $SDCARD mkpart primary ext4 0% 100% 1> /dev/null 2>&1
        sudo partprobe $SDCARD 1> /dev/null 2>&1
        sudo mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}1 1> /dev/null 2>&1
248 249
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
250 251
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
252
        sudo mount ${SDCARD}1 $TMPDIR/root
253 254 255 256 257 258 259 260 261 262 263 264 265 266
    fi
}

cleanup () {
    msg "Writing bootloader and cleaning up after install..."
   # For Raspberry Pi devices
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
       #copy rootfs contents over to the FS
        sudo mv $TMPDIR/root/boot/* $TMPDIR/boot
        
        #clean up
        sudo umount $TMPDIR/root
        sudo umount $TMPDIR/boot
        sudo rm -r $TMPDIR/root $TMPDIR/boot
267
        sudo partprobe $SDCARD 1> /dev/null 2>&1
268 269 270 271 272
        
# For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
    #flash bootloader
        cd $TMPDIR/root/boot/
273
        sudo ./sd_fusing.sh $SDCARD 1> /dev/null 2>&1
274 275 276 277 278
        cd ~

    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
279
        sudo partprobe $SDCARD 1> /dev/null 2>&1
280 281 282 283
        
    # For pinebook device
    elif [[ "$DEVICE" = "pinebook" ]]; then
    #flash bootloader
284
        sudo dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1
285 286 287 288
        
    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
289
        sudo partprobe $SDCARD 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
290
        
291 292 293
        
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then
Dan Johansen's avatar
Dan Johansen committed
294
    #flash bootloader
295 296 297
        sudo dd if=$TMPDIR/root/boot/idbloader.img of=${SDCARD} seek=64 conv=notrunc 1> /dev/null 2>&1
        sudo dd if=$TMPDIR/root/boot/uboot.img of=${SDCARD} seek=16384 conv=notrunc 1> /dev/null 2>&1
        sudo 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
298 299 300 301
        
    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
302
        sudo partprobe $SDCARD 1> /dev/null 2>&1
303 304
        
fi
Dan Johansen's avatar
Dan Johansen committed
305 306
}

307 308

# Using Dialog to ask for user input for variables
309 310
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
        --menu "Choose a device:" 20 51 6 \
311 312
        "rpi3" "Raspberry Pi 3" \
        "oc2" "Odroid C2" \
313
	"pinebook" "PineBook" \
314 315 316
	"rpi2" "Raspberry Pi 2 (unmaintained)" \
	"oc1" "Odroid C1/C1+ (unmaintained)" \
	"xu4" "Odroid XU4 (unmaintained)" 3>&1 1>&2 2>&3 3>&-)
317 318 319 320 321


#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
322 323
EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
	--menu "Choose an edition:" 20 50 5 \
324 325
	"minimal" "Minimal Edition (Just CLI)" \
	"lxqt" "Full LXQT Desktop and apps (lightweight)" \
326
	"kde" "Full KDE/Plasma Desktop (Heavy)" \
327
	"mate" "Full MATE desktop and apps (lightweight, unmaintained)" \
328
	"i3" "Mininal i3 WM with apps (very light)" 3>&1 1>&2 2>&3 3>&-) 
329 330 331 332 333 334 335 336 337

else 
	clear
	exit 1
fi


if [ ! -z "$EDITION" ]
then
338
USER=$(dialog --clear --title "Manjaro ARM Installer" \
339 340 341 342 343 344 345 346 347 348 349 350
	--inputbox "Enter username:" 8 50 \
        3>&1 1>&2 2>&3 3>&-)
else 
	clear
	exit 1
fi




if [ ! -z "$USER" ]
then
351 352
PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
	--passwordbox "Enter Password for $USER:" 8 50 \
353 354 355 356 357 358 359 360 361 362 363
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi



if [ ! -z "$PASSWORD" ]
then
364
ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	--passwordbox "Enter Root Password:" 8 50 \
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi




if [ ! -z "$ROOTPASSWORD" ]
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 "")
385 386
done < <( lsblk -adn -o NAME )
SDCARD=$(dialog --title "Manjaro ARM Installer" \
387
	--menu "Chose your SDCard - Be sure the correct drive is selected!" 20 50 15 \
388 389 390 391 392 393 394 395 396 397
       	"${W[@]}" 3>&2 2>&1 1>&3)

# add /dev/ to the selected option above
SDCARD=/dev/$SDCARD
else 
	clear
	exit 1
fi


398 399 400 401 402 403 404 405 406 407
#if [ ! -z "$SDCARD" ]
#then
#TIMEZONE=$(dialog --clear --title "Manjaro ARM Installer" \
#	--inputbox "Enter timezone (example Europe/London):" 8 50 \
#        3>&1 1>&2 2>&3 3>&- \
#	)
#else 
#	clear
#	exit 1
#fi
408 409


410 411 412 413 414 415 416 417 418 419
#if [ ! -z "$TIMEZONE" ]
#then
#LOCALE=$(dialog --clear --title "Manjaro ARM Installer" \
#	--inputbox "Enter locale (example en_US.UTF-8):" 8 50 \
#        3>&1 1>&2 2>&3 3>&- \
#	)
#else 
#	clear
#	exit 1
#fi
420

421
# List all the responses upon completion to be verified
422
clear
423 424
echo "Device = $DEVICE"
echo "Edition = $EDITION"
425
echo "User name = $USER"
426 427
echo "Password for $USER = ($PASSWORD)"
echo "Root Password = ($ROOTPASSWORD)"
428
echo "SDCard = $SDCARD"
429 430
#echo "Timezone = $TIMEZONE"
#echo "Locale = $LOCALE"
Dan Johansen's avatar
Dan Johansen committed
431

432 433 434 435 436 437 438 439
echo "Are the above settings correct?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) echo "Continuing..."; break;;
        No ) exit 1;;
    esac
done

Dan Johansen's avatar
Dan Johansen committed
440 441 442 443 444 445
if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
    ARCH='armv7h'
else
    ARCH='aarch64'
fi

446 447
# get the profiles
getarmprofiles
Dan Johansen's avatar
Dan Johansen committed
448 449 450 451 452 453

#Package lists
PKG_DEVICE=$(grep "^[^#;]" $PROFILES/arm-profiles/devices/$DEVICE | awk '{print $1}')
PKG_EDITION=$(grep "^[^#;]" $PROFILES/arm-profiles/editions/$EDITION | awk '{print $1}')
SRV_EDITION=$(grep "^[^#;]" $PROFILES/arm-profiles/services/$EDITION | awk '{print $1}')

454

Dan Johansen's avatar
Dan Johansen committed
455
# Commands
456
timer_start=$(get_timer)
Dan Johansen's avatar
Dan Johansen committed
457 458

prepare_card
459 460 461 462
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync