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

# Set globals
4 5
TMPDIR=/var/tmp/manjaro-arm-installer
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 15
# clearing variables
DEVICE=""
EDITION=""
USER=""
PASSWORD=""
ROOTPASSWORD=""
SDCARD=""

Dan Johansen's avatar
Dan Johansen committed
16 17

# Functions
18 19 20 21 22 23 24 25
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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

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

48 49 50 51 52
getarmprofiles () {
    if ls $PROFILES/arm-profiles/* 1> /dev/null 2>&1; then
        cd $PROFILES/arm-profiles
        git pull
    else
53 54
        sudo mkdir -p $PROFILES
        sudo chmod 777 $PROFILES
55 56 57 58 59 60 61 62 63 64 65 66 67 68
        cd $PROFILES
        git clone https://gitlab.com/Strit/arm-profiles.git
    fi

}

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

    msg "Creating install for $DEVICE..."
69 70 71 72
    
    # fetch and extract rootfs
    msg "Downloading latest $ARCH rootfs..."
    cd $TMPDIR
73
    wget https://www.strits.dk/files/Manjaro-ARM-$ARCH-latest.tar.gz 1> /dev/null 2>&1
74 75
    
    msg "Extracting $ARCH rootfs..."
Dan Johansen's avatar
Dan Johansen committed
76
    sudo bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
77 78
    
    msg "Setting up keyrings..."
79 80
    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
81 82 83
    
    msg "Installing packages for $EDITION on $DEVICE..."
    # Install device and editions specific packages
84
    sudo systemd-nspawn -D $TMPDIR/root pacman -S base $PKG_DEVICE $PKG_EDITION lsb-release --needed --noconfirm
Dan Johansen's avatar
Dan Johansen committed
85 86 87
    
    msg "Enabling services..."
    # Enable services
88 89
    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
90 91

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

    msg "Applying overlay for $EDITION..."
96
    sudo cp -ap $PROFILES/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
Dan Johansen's avatar
Dan Johansen committed
97 98 99
    
    msg "Setting up users..."
    #setup users
100 101 102
    echo "$USER" > $TMPDIR/user
    echo "$PASSWORD" >> $TMPDIR/password
    echo "$PASSWORD" >> $TMPDIR/password
103 104 105
    echo "$ROOTPASSWORD" >> $TMPDIR/rootpassword
    echo "$ROOTPASSWORD" >> $TMPDIR/rootpassword
    sudo systemd-nspawn -D $TMPDIR/root passwd root < $TMPDIR/rootpassword 1> /dev/null 2>&1
106 107 108
    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
109 110 111 112 113
    
    msg "Enabling user services..."
    if [[ "$EDITION" = "minimal" ]] || [[ "$EDITION" = "server" ]]; then
        echo "No user services for $EDITION edition"
    else
114
        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
115 116 117 118
    fi

    msg "Setting up system settings..."
    #system setup
119 120
    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
Dan Johansen's avatar
Dan Johansen committed
121 122 123
    
    msg "Doing device specific setups for $DEVICE..."
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
124 125 126 127
        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
128 129 130 131 132
    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
133 134
        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
135 136 137 138
    else
        echo ""
    fi
    
139
    msg "Cleaning install for unwanted files..."
Dan Johansen's avatar
Dan Johansen committed
140
       if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
141
        sudo rm $TMPDIR/root/usr/bin/qemu-arm-static
Dan Johansen's avatar
Dan Johansen committed
142
    else
143
        sudo rm $TMPDIR/root/usr/bin/qemu-aarch64-static
Dan Johansen's avatar
Dan Johansen committed
144
    fi
145 146 147 148
    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
149 150
    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
151

152
    msg "$DEVICE $EDITION install complete"
Dan Johansen's avatar
Dan Johansen committed
153 154 155
}

prepare_card () {
156
    msg "Getting $SDCARD ready for $DEVICE..."
Dan Johansen's avatar
Dan Johansen committed
157 158 159
    # For Raspberry Pi devices
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
        #partition with boot and root
160 161 162
        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
163
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
164
        END_SECTOR=$(expr $START + $SIZE)
165 166 167 168
        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
169 170
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
171 172
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
173 174
        sudo mount ${SDCARD}1 $TMPDIR/boot
        sudo mount ${SDCARD}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
175 176 177 178

    # For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
        #Clear first 8mb
179
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
180 181
	
    #partition with a single root partition
182 183 184 185
        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
186 187
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
188 189
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
190
        sudo mount ${SDCARD}1 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
191 192 193 194 195

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

    #Clear first 8mb
196
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
197 198
	
    #partition with a single root partition
199 200 201 202
        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
203 204
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
205 206
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
207
        sudo mount ${SDCARD}1 $TMPDIR/root
208

Dan Johansen's avatar
Dan Johansen committed
209 210 211 212
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then

    #Clear first 8mb
213
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
214 215
	
    #partition with a single root partition
216 217 218 219
        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
220 221
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
222 223
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
224
        sudo mount ${SDCARD}1 $TMPDIR/root
225 226 227 228 229 230 231 232 233 234 235 236 237 238
    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
239
        sudo partprobe $SDCARD 1> /dev/null 2>&1
240 241 242 243 244
        
# For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
    #flash bootloader
        cd $TMPDIR/root/boot/
245
        sudo ./sd_fusing.sh $SDCARD 1> /dev/null 2>&1
246 247 248 249 250
        cd ~

    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
251
        sudo partprobe $SDCARD 1> /dev/null 2>&1
252 253 254 255
        
    # For pinebook device
    elif [[ "$DEVICE" = "pinebook" ]]; then
    #flash bootloader
256
        sudo dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1 1> /dev/null 2>&1
257 258 259 260
        
    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
261
        sudo partprobe $SDCARD 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
262
        
263 264 265
        
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then
Dan Johansen's avatar
Dan Johansen committed
266
    #flash bootloader
267 268 269
        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
270 271 272 273
        
    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
274
        sudo partprobe $SDCARD 1> /dev/null 2>&1
275 276
        
fi
Dan Johansen's avatar
Dan Johansen committed
277 278
}

279 280

# Using Dialog to ask for user input for variables
281 282
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
        --menu "Choose a device:" 20 51 6 \
283 284
        "rpi3" "Raspberry Pi 3" \
        "oc2" "Odroid C2" \
285 286 287 288
	"pinebook" "PineBook" \
	"rpi2" "Raspberry Pi 2 (not maintained)" \
	"oc1" "Odroid C1/C1+ (not maintained)" \
	"xu4" "Odroid XU4 (not maintained)" 3>&1 1>&2 2>&3 3>&-)
289 290 291 292 293


#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
294 295
EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
	--menu "Choose an edition:" 20 50 5 \
296 297
	"minimal" "Minimal Edition (Just CLI)" \
	"lxqt" "Full LXQT Desktop and apps (lightweight)" \
298 299 300
	"kde" "Full KDE Desktop (Heavy)" \
	"mate" "Full MATE desktop and apps (lightweight, not maintained)" \
	"i3" "Mininal i3 WM with apps (very light)" 3>&1 1>&2 2>&3 3>&-) 
301 302 303 304 305 306 307 308 309

else 
	clear
	exit 1
fi


if [ ! -z "$EDITION" ]
then
310
USER=$(dialog --clear --title "Manjaro ARM Installer" \
311 312 313 314 315 316 317 318 319 320 321 322
	--inputbox "Enter username:" 8 50 \
        3>&1 1>&2 2>&3 3>&-)
else 
	clear
	exit 1
fi




if [ ! -z "$USER" ]
then
323 324
PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
	--passwordbox "Enter Password for $USER:" 8 50 \
325 326 327 328 329 330 331 332 333 334 335
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi



if [ ! -z "$PASSWORD" ]
then
336
ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	--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 "")
357 358 359
done < <( lsblk -adn -o NAME )
SDCARD=$(dialog --title "Manjaro ARM Installer" \
	--menu "Chose your SDCard" 20 50 10 \
360 361 362 363 364 365 366 367 368 369 370 371
       	"${W[@]}" 3>&2 2>&1 1>&3)

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


# List all the responses upon completion (remove this for production)
clear
372 373
echo "Device = $DEVICE"
echo "Edition = $EDITION"
374
echo "User name = $USER"
375
echo "Password for $USER = ********** ($PASSWORD)" #password is here for testing purposes
376 377
echo "Root Password = ********** ($ROOTPASSWORD)" #password is here for testing purposes
echo "SDCard = $SDCARD"
Dan Johansen's avatar
Dan Johansen committed
378 379 380 381 382 383 384

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

385 386
# get the profiles
getarmprofiles
Dan Johansen's avatar
Dan Johansen committed
387 388 389 390 391 392

#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}')

393

Dan Johansen's avatar
Dan Johansen committed
394
# Commands
395
timer_start=$(get_timer)
Dan Johansen's avatar
Dan Johansen committed
396 397

prepare_card
398 399 400 401
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync