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

# Set globals
4
#LIBDIR=/usr/share/manjaro-arm-tools/lib
Dan Johansen's avatar
Dan Johansen committed
5
SDCARD=/dev/mmcblk0
6 7
TMPDIR=/var/tmp/manjaro-arm-installer
PROFILES=/var/tmp/manjaro-arm-installer/profiles
8 9 10 11 12
ARCH='aarch64'
DEVICE='rpi3'
EDITION='minimal'
USER='manjaro'
PASSWORD='manjaro'
13
ROOTPASSWORD='root'
14

Dan Johansen's avatar
Dan Johansen committed
15 16 17 18



#imports
19
#source $LIBDIR/functions.sh
Dan Johansen's avatar
Dan Johansen committed
20 21 22


# Functions
23 24 25 26 27 28 29 30
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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

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

53 54 55 56 57
getarmprofiles () {
    if ls $PROFILES/arm-profiles/* 1> /dev/null 2>&1; then
        cd $PROFILES/arm-profiles
        git pull
    else
58 59
        sudo mkdir -p $PROFILES
        sudo chmod 777 $PROFILES
60 61 62 63 64 65 66 67 68 69 70 71 72 73
        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
74 75

    # backup host mirrorlist
76
    #sudo mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist-orig
Dan Johansen's avatar
Dan Johansen committed
77 78

    # Create arm mirrlorlist
79 80
    #echo "Server = http://mirrors.dotsrc.org/manjaro-arm/stable/\$arch/\$repo/" > mirrorlist
    #sudo mv mirrorlist /etc/pacman.d/mirrorlist
81 82
  
    # create chroot on SD card
83
    #    sudo pacstrap -G -c -C $LIBDIR/pacman.conf.$ARCH $TMPDIR/root base manjaro-arm-keyring
Dan Johansen's avatar
Dan Johansen committed
84 85
    
    # Enable cross architecture Chrooting
86 87 88 89 90 91 92 93 94 95 96 97
    #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
    wget https://www.strits.dk/files/Manjaro-ARM-$ARCH-latest.tar.gz
    
    msg "Extracting $ARCH rootfs..."
Dan Johansen's avatar
Dan Johansen committed
98
    sudo bsdtar -xpf $TMPDIR/Manjaro-ARM-$ARCH-latest.tar.gz -C $TMPDIR/root
Dan Johansen's avatar
Dan Johansen committed
99 100
    
    msg "Setting up keyrings..."
101 102
    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
103 104 105
    
    msg "Installing packages for $EDITION on $DEVICE..."
    # Install device and editions specific packages
106
    sudo systemd-nspawn -D $TMPDIR/root pacman -S base $PKG_DEVICE $PKG_EDITION lsb-release --needed --noconfirm
Dan Johansen's avatar
Dan Johansen committed
107 108 109 110 111 112 113

    # restore original mirrorlist to host system
    sudo mv /etc/pacman.d/mirrorlist-orig /etc/pacman.d/mirrorlist
    sudo pacman -Syy
    
    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
Dan Johansen's avatar
Dan Johansen committed
147 148 149
    
    msg "Doing device specific setups for $DEVICE..."
    if [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "rpi3" ]]; then
150 151 152 153
        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
154 155 156 157 158
    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
159 160
        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
161 162 163 164
    else
        echo ""
    fi
    
165
    msg "Cleaning install for unwanted files..."
Dan Johansen's avatar
Dan Johansen committed
166
       if [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "rpi2" ]] || [[ "$DEVICE" = "xu4" ]]; then
167
        sudo rm $TMPDIR/root/usr/bin/qemu-arm-static
Dan Johansen's avatar
Dan Johansen committed
168
    else
169
        sudo rm $TMPDIR/root/usr/bin/qemu-aarch64-static
Dan Johansen's avatar
Dan Johansen committed
170
    fi
171 172 173 174
    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
175 176
    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
177

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

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

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

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

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

Dan Johansen's avatar
Dan Johansen committed
235 236 237 238 239 240 241 242 243 244
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then

    #Clear first 8mb
        sudo dd if=/dev/zero of=${SDCARD} bs=1M count=8
	
    #partition with a single root partition
        sudo parted -s $SDCARD mklabel msdos
        sudo parted -s $SDCARD mkpart primary ext4 0% 100%
        sudo partprobe $SDCARD
245
        sudo mkfs.ext4 -O ^metadata_csum,^64bit ${SDCARD}1
246 247
        
        # Mount SD card
Dan Johansen's avatar
Dan Johansen committed
248 249
        mkdir -p $TMPDIR/root
        sudo chmod 777 -R $TMPDIR/root
250
        sudo mount ${SDCARD}1 $TMPDIR/root
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    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
        sudo partprobe $SDCARD
        
# For Odroid devices
    elif [[ "$DEVICE" = "oc1" ]] || [[ "$DEVICE" = "oc2" ]] || [[ "$DEVICE" = "xu4" ]]; then
    #flash bootloader
        cd $TMPDIR/root/boot/
        sudo ./sd_fusing.sh $SDCARD
        cd ~

    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
        sudo partprobe $SDCARD
        
    # For pinebook device
    elif [[ "$DEVICE" = "pinebook" ]]; then
    #flash bootloader
        sudo dd if=$TMPDIR/root/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${SDCARD} bs=8k seek=1
        
    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
        sudo partprobe $SDCARD
Dan Johansen's avatar
Dan Johansen committed
288
        
289 290 291
        
    # For rockpro64 device
    elif [[ "$DEVICE" = "rockpro64" ]]; then
Dan Johansen's avatar
Dan Johansen committed
292 293 294 295 296 297 298 299 300
    #flash bootloader
        sudo dd if=$TMPDIR/root/boot/idbloader.img of=${SDCARD} seek=64 conv=notrunc
        sudo dd if=$TMPDIR/root/boot/uboot.img of=${SDCARD} seek=16384 conv=notrunc
        sudo dd if=$TMPDIR/root/boot/trust.img of=${SDCARD} seek=24576 conv=notrunc
        
    #clean up
        sudo umount $TMPDIR/root
        sudo rm -r $TMPDIR/root
        sudo partprobe $SDCARD
301 302
        
fi
Dan Johansen's avatar
Dan Johansen committed
303 304
}

305 306 307
# Asking for user input!
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
308
read -p 'What is the path to the SD card? (example: /dev/sdb)? ' SDCARD
309
read -p 'What username do you want? ' USER
310
read -sp "What password should $USER have? " PASSWORD
311 312 313 314
read -sp "
What password should the root user have? " ROOTPASSWORD
echo " 
" #to fix next line from being in the password line
315 316 317 318
echo "Device = $DEVICE"
echo "Edition = $EDITION"
echo "Path to SD Card = $SDCARD"
echo "Username = $USER"
Dan Johansen's avatar
Dan Johansen committed
319 320 321 322 323 324 325 326


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

327 328 329 330 331
#if [[ ! -f $SDCARD ]]; then
 #   echo "Path $SDCARD does not exist! Aborting..."
 #   exit 1
#fi

332 333
# get the profiles
getarmprofiles
Dan Johansen's avatar
Dan Johansen committed
334 335 336 337 338 339

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

340 341 342 343 344 345 346 347 348
# Remove '/dev/' from the name 
DEV_NAME=`echo "$SDCARD" | cut -d "/" -f 3`

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

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

349

Dan Johansen's avatar
Dan Johansen committed
350
# Commands
351
timer_start=$(get_timer)
Dan Johansen's avatar
Dan Johansen committed
352 353

prepare_card
354 355 356 357
create_install
cleanup
show_elapsed_time "${FUNCNAME}" "${timer_start}"
sync