manjaro-arm-installer 14.3 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..."
Dan Johansen's avatar
Dan Johansen committed
69 70

    # backup host mirrorlist
71
    #sudo mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist-orig
Dan Johansen's avatar
Dan Johansen committed
72 73

    # Create arm mirrlorlist
74 75
    #echo "Server = http://mirrors.dotsrc.org/manjaro-arm/stable/\$arch/\$repo/" > mirrorlist
    #sudo mv mirrorlist /etc/pacman.d/mirrorlist
76 77
  
    # create chroot on SD card
78
    #    sudo pacstrap -G -c -C $LIBDIR/pacman.conf.$ARCH $TMPDIR/root base manjaro-arm-keyring
Dan Johansen's avatar
Dan Johansen committed
79 80
    
    # Enable cross architecture Chrooting
81 82 83 84 85 86 87 88 89
    #if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
    #    sudo cp /usr/bin/qemu-arm-static $TMPDIR/root/usr/bin/
    #else
    #    sudo cp /usr/bin/qemu-aarch64-static $TMPDIR/root/usr/bin/
    #fi
    
    # fetch and extract rootfs
    msg "Downloading latest $ARCH rootfs..."
    cd $TMPDIR
90
    wget https://www.strits.dk/files/Manjaro-ARM-$ARCH-latest.tar.gz 1> /dev/null 2>&1
91 92
    
    msg "Extracting $ARCH rootfs..."
Dan Johansen's avatar
Dan Johansen committed
93
    sudo bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
94 95
    
    msg "Setting up keyrings..."
96 97
    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
98 99 100
    
    msg "Installing packages for $EDITION on $DEVICE..."
    # Install device and editions specific packages
101
    sudo systemd-nspawn -D $TMPDIR/root pacman -S base $PKG_DEVICE $PKG_EDITION lsb-release --needed --noconfirm
Dan Johansen's avatar
Dan Johansen committed
102 103

    # restore original mirrorlist to host system
104 105
    #sudo mv /etc/pacman.d/mirrorlist-orig /etc/pacman.d/mirrorlist
    #sudo pacman -Syy
Dan Johansen's avatar
Dan Johansen committed
106 107 108
    
    msg "Enabling services..."
    # Enable services
109 110
    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
111 112

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

    msg "Applying overlay for $EDITION..."
117
    sudo cp -ap $PROFILES/arm-profiles/overlays/$EDITION/* $TMPDIR/root/
Dan Johansen's avatar
Dan Johansen committed
118 119 120
    
    msg "Setting up users..."
    #setup users
121 122 123
    echo "$USER" > $TMPDIR/user
    echo "$PASSWORD" >> $TMPDIR/password
    echo "$PASSWORD" >> $TMPDIR/password
124 125 126
    echo "$ROOTPASSWORD" >> $TMPDIR/rootpassword
    echo "$ROOTPASSWORD" >> $TMPDIR/rootpassword
    sudo systemd-nspawn -D $TMPDIR/root passwd root < $TMPDIR/rootpassword 1> /dev/null 2>&1
127 128 129
    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
130 131 132 133 134
    
    msg "Enabling user services..."
    if [[ "$EDITION" = "minimal" ]] || [[ "$EDITION" = "server" ]]; then
        echo "No user services for $EDITION edition"
    else
135
        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
136 137 138 139
    fi

    msg "Setting up system settings..."
    #system setup
140 141
    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
142 143 144
    
    msg "Doing device specific setups for $DEVICE..."
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
145 146 147 148
        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
149 150 151 152 153
    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
154 155
        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
156 157 158 159
    else
        echo ""
    fi
    
160
    msg "Cleaning install for unwanted files..."
Dan Johansen's avatar
Dan Johansen committed
161
       if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
162
        sudo rm $TMPDIR/root/usr/bin/qemu-arm-static
Dan Johansen's avatar
Dan Johansen committed
163
    else
164
        sudo rm $TMPDIR/root/usr/bin/qemu-aarch64-static
Dan Johansen's avatar
Dan Johansen committed
165
    fi
166 167 168 169
    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
170 171
    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
172

173
    msg "$DEVICE $EDITION install complete"
Dan Johansen's avatar
Dan Johansen committed
174 175 176
}

prepare_card () {
177
    msg "Getting $SDCARD ready for $DEVICE..."
Dan Johansen's avatar
Dan Johansen committed
178 179 180
    # For Raspberry Pi devices
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
        #partition with boot and root
181 182 183
        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
184
        SIZE=`cat /sys/block/$DEV_NAME/${DEV_NAME}1/size`
185
        END_SECTOR=$(expr $START + $SIZE)
186 187 188 189
        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
190 191
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
192 193
        mkdir -p $TMPDIR/root
        mkdir -p $TMPDIR/boot
194 195
        sudo mount ${SDCARD}1 $TMPDIR/boot
        sudo mount ${SDCARD}2 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
196 197 198 199

    # For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
        #Clear first 8mb
200
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
201 202
	
    #partition with a single root partition
203 204 205 206
        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
207 208
        
        #Mount SD card
Dan Johansen's avatar
Dan Johansen committed
209 210
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
211
        sudo mount ${SDCARD}1 $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
212 213 214 215 216

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

    #Clear first 8mb
217
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
218 219
	
    #partition with a single root partition
220 221 222 223
        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
224 225
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
226 227
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
228
        sudo mount ${SDCARD}1 $TMPDIR/root
229

Dan Johansen's avatar
Dan Johansen committed
230 231 232 233
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then

    #Clear first 8mb
234
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
235 236
	
    #partition with a single root partition
237 238 239 240
        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
241 242
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
243 244
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
245
        sudo mount ${SDCARD}1 $TMPDIR/root
246 247 248 249 250 251 252 253 254 255 256 257 258 259
    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
260
        sudo partprobe $SDCARD 1> /dev/null 2>&1
261 262 263 264 265
        
# For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
    #flash bootloader
        cd $TMPDIR/root/boot/
266
        sudo ./sd_fusing.sh $SDCARD 1> /dev/null 2>&1
267 268 269 270 271
        cd ~

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

300
# Asking for user input!
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
#read -p 'What device is this for? [Options: rpi3, oc2 and pinebook] ' DEVICE
#read -p 'What edition do you want? [Options: minimal, lxqt and kde] ' EDITION
#read -p 'What is the path to the SD card? (example: /dev/sdb)? ' SDCARD
#read -p 'What username do you want? ' USER
#read -sp "What password should $USER have? " PASSWORD
#read -sp "
#What password should the root user have? " ROOTPASSWORD
#echo " 
#" #to fix next line from being in the password line
#echo "Device = $DEVICE"
#echo "Edition = $EDITION"
#echo "Path to SD Card = $SDCARD"
#echo "Username = $USER"


# Using ncurses to ask for user input for variables
317 318
DEVICE=$(dialog --clear --title "Manjaro ARM Installer" \
        --menu "Choose a device:" 20 51 6 \
319 320
        "rpi3" "Raspberry Pi 3" \
        "oc2" "Odroid C2" \
321 322 323 324
	"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>&-)
325 326 327 328 329


#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
330 331
EDITION=$(dialog --clear --title "Manjaro ARM Installer" \
	--menu "Choose an edition:" 20 50 5 \
332 333
	"minimal" "Minimal Edition (Just CLI)" \
	"lxqt" "Full LXQT Desktop and apps (lightweight)" \
334 335 336
	"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>&-) 
337 338 339 340 341 342 343 344 345

else 
	clear
	exit 1
fi


if [ ! -z "$EDITION" ]
then
346
USER=$(dialog --clear --title "Manjaro ARM Installer" \
347 348 349 350 351 352 353 354 355 356 357 358
	--inputbox "Enter username:" 8 50 \
        3>&1 1>&2 2>&3 3>&-)
else 
	clear
	exit 1
fi




if [ ! -z "$USER" ]
then
359 360
PASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
	--passwordbox "Enter Password for $USER:" 8 50 \
361 362 363 364 365 366 367 368 369 370 371
        3>&1 1>&2 2>&3 3>&- \
	)
else 
	clear
	exit 1
fi



if [ ! -z "$PASSWORD" ]
then
372
ROOTPASSWORD=$(dialog --clear --title "Manjaro ARM Installer" \
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	--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 "")
393 394 395
done < <( lsblk -adn -o NAME )
SDCARD=$(dialog --title "Manjaro ARM Installer" \
	--menu "Chose your SDCard" 20 50 10 \
396 397 398 399 400 401 402 403 404 405 406 407
       	"${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
408 409
echo "Device = $DEVICE"
echo "Edition = $EDITION"
410
echo "User name = $USER"
411
echo "Password for $USER = ********** ($PASSWORD)" #password is here for testing purposes
412 413
echo "Root Password = ********** ($ROOTPASSWORD)" #password is here for testing purposes
echo "SDCard = $SDCARD"
Dan Johansen's avatar
Dan Johansen committed
414 415 416 417 418 419 420

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

421 422
# get the profiles
getarmprofiles
Dan Johansen's avatar
Dan Johansen committed
423 424 425 426 427 428

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

429
# Remove '/dev/' from the name 
430
#DEV_NAME=`echo "$SDCARD" | cut -d "/" -f 3`
431 432

# Get size of SDCard (final sector)
433
#SECTOR_SIZE=`cat /sys/block/${DEV_NAME}/size`
434 435

# Set the ending sector that the partition should be resized too
436
#END_SECTOR=`expr $SECTOR_SIZE - 1`
437

438

Dan Johansen's avatar
Dan Johansen committed
439
# Commands
440
timer_start=$(get_timer)
Dan Johansen's avatar
Dan Johansen committed
441 442

prepare_card
443 444 445 446
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync