functions.sh 40.5 KB
Newer Older
Dan Johansen's avatar
Dan Johansen committed
1
2
3
#! /bin/bash

#variables
4
BRANCH='stable'
5
6
LIBDIR=/usr/share/manjaro-arm-tools/lib
BUILDDIR=/var/lib/manjaro-arm-tools/pkg
7
BUILDSERVER=https://repo.manjaro.org/repo
8
9
10
11
PACKAGER=$(cat /etc/makepkg.conf | grep PACKAGER)
PKGDIR=/var/cache/manjaro-arm-tools/pkg
ROOTFS_IMG=/var/lib/manjaro-arm-tools/img
TMPDIR=/var/lib/manjaro-arm-tools/tmp
12
IMGDIR=/var/cache/manjaro-arm-tools/img
Dan Johansen's avatar
Dan Johansen committed
13
IMGNAME=Manjaro-ARM-$EDITION-$DEVICE-$VERSION
14
PROFILES=/usr/share/manjaro-arm-tools/profiles
15
NSPAWN='systemd-nspawn -q --resolv-conf=copy-host --timezone=off -D'
16
OSDN='storage.osdn.net:/storage/groups/m/ma/manjaro-arm'
17
STORAGE_USER=$(whoami)
Dan Johansen's avatar
Dan Johansen committed
18
VERSION=$(date +'%y'.'%m')
Dan Johansen's avatar
Dan Johansen committed
19
FLASHVERSION=$(date +'%y'.'%m')
20
ARCH='aarch64'
21
DEVICE='rpi4'
22
EDITION='minimal'
23
USER='manjaro'
24
HOSTNAME='manjaro-arm'
25
PASSWORD='manjaro'
26
CARCH=$(uname -m)
27
COLORS=true
28
FILESYSTEM='ext4'
29
srv_list=/tmp/services_list
30

31
#import conf file
32
source /etc/manjaro-arm-tools/manjaro-arm-tools.conf 
33

Dan Johansen's avatar
Dan Johansen committed
34

35
36
usage_deploy_img() {
    echo "Usage: ${0##*/} [options]"
37
    echo "    -i <image>         Image to upload. Should be a .xz file."
38
39
    echo "    -d <device>        Device the image is for. [Default = rpi4. Options = $(ls -m --width=0 "$PROFILES/arm-profiles/devices/")]"
    echo "    -e <edition>       Edition of the image. [Default = minimal. Options = $(ls -m --width=0 "$PROFILES/arm-profiles/editions/")]"
Dan Johansen's avatar
Dan Johansen committed
40
    echo "    -v <version>       Version of the image. [Default = Current YY.MM]"
41
    echo "    -k <gpg key ID>    Email address associated with the GPG key to use for signing"
42
    echo "    -u <username>      Username of your OSDN user account with access to upload [Default = currently logged in local user]"
43
    echo "    -t                 Create a torrent of the image"
44
45
46
47
48
49
    echo '    -h                 This help'
    echo ''
    echo ''
    exit $1
}

50
51
usage_build_pkg() {
    echo "Usage: ${0##*/} [options]"
52
    echo "    -a <arch>          Architecture. [Default = aarch64. Options = any or aarch64]"
Dan Johansen's avatar
Dan Johansen committed
53
    echo "    -p <pkg>           Package to build"
54
    echo "    -k                 Keep the previous rootfs for this build"
55
    echo "    -b <branch>        Set the branch used for the build. [Default = stable. Options = stable, testing or unstable]"
56
    echo "    -n                 Install built package into rootfs"
57
    echo "    -i <package>       Install local package into rootfs."
58
    echo '    -h                 This help'
Dan Johansen's avatar
Dan Johansen committed
59
60
61
62
63
    echo ''
    echo ''
    exit $1
}

64
usage_build_img() {
65
    echo "Usage: ${0##*/} [options]"
66
67
    echo "    -d <device>        Device the image is for. [Default = rpi4. Options = $(ls -m --width=0 "$PROFILES/arm-profiles/devices/")]"
    echo "    -e <edition>       Edition of the image. [Default = minimal. Options = $(ls -m --width=0 "$PROFILES/arm-profiles/editions/")]"
68
    echo "    -v <version>       Define the version the resulting image should be named. [Default is current YY.MM]"
69
    echo "    -k <repo>          Add overlay repo [Options = kde-unstable, mobile] or url https://server/path/custom_repo.db"
70
    echo "    -i <package>       Install local package into image rootfs."
71
    echo "    -b <branch>        Set the branch used in the image. [Default = stable. Options = stable, testing or unstable]"
Bernhard Landauer's avatar
Bernhard Landauer committed
72
    echo "    -m                 Create bmap. ('bmap-tools' need to be installed.)"
73
    echo "    -n                 Force download of new rootfs."
74
    echo "    -s <hostname>      Use custom hostname"
75
    echo "    -x                 Don't compress the image."
Bernhard Landauer's avatar
Bernhard Landauer committed
76
    echo "    -c                 Disable colors."
77
    echo "    -f                 Create an image with factory settings."
78
    echo "    -p <filesystem>    Filesystem to be used for the root partition. [Default = ext4. Options = ext4 or btrfs]"
79
80
81
82
83
84
    echo '    -h                 This help'
    echo ''
    echo ''
    exit $1
}

85
usage_build_emmcflasher() {
86
    echo "Usage: ${0##*/} [options]"
87
88
    echo "    -d <device>        Device the image is for. [Default = rpi4. Options = $(ls -m --width=0 "$PROFILES/arm-profiles/devices/")]"
    echo "    -e <edition>       Edition of the image to download. [Default = minimal. Options = $(ls -m --width=0 "$PROFILES/arm-profiles/editions/")]"
89
90
91
92
93
94
95
96
97
98
99
    echo "    -v <version>       Define the version of the release to download. [Default is current YY.MM]"
    echo "    -f <flash version> Version of the eMMC flasher image it self. [Default is current YY.MM]"
    echo "    -i <package>       Install local package into image rootfs."
    echo "    -n                 Force download of new rootfs."
    echo "    -x                 Don't compress the image."
    echo '    -h                 This help'
    echo ''
    echo ''
    exit $1
}

100
101
102
usage_getarmprofiles() {
    echo "Usage: ${0##*/} [options]"
    echo '    -f                 Force download of current profiles from the git repository'
103
    echo '    -p                 Use profiles from pp-factory branch'
104
105
106
107
108
109
    echo '    -h                 This help'
    echo ''
    echo ''
    exit $1
}

110
enable_colors() {
Dan Johansen's avatar
Dan Johansen committed
111
112
113
    ALL_OFF="\e[1;0m"
    BOLD="\e[1;1m"
    GREEN="${BOLD}\e[1;32m"
114
115
116
117
    BLUE="${BOLD}\e[1;34m"
}

msg() {
118
119
    local mesg=$1; shift
    printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
Dan Johansen's avatar
Dan Johansen committed
120
121
 }
 
122
info() {
123
124
    local mesg=$1; shift
    printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
125
 }
Bernhard Landauer's avatar
Bernhard Landauer committed
126
127
128
129
130
131
132

error() {
    local mesg=$1; shift
    printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

cleanup() {
Bernhard Landauer's avatar
Bernhard Landauer committed
133
    umount $ROOTFS_IMG/rootfs_$ARCH/var/cache/pacman/pkg
Bernhard Landauer's avatar
Bernhard Landauer committed
134
135
136
137
138
139
140
    exit ${1:-0}
}

abort() {
    error 'Aborting...'
    cleanup 255
}
141
142
143

prune_cache(){
    info "Prune and unmount pkg-cache..."
144
145
    $NSPAWN $CHROOTDIR paccache -r
    umount $PKG_CACHE
146
}
Dan Johansen's avatar
Dan Johansen committed
147

148
149
load_vars() {
    local var
Dan Johansen's avatar
Dan Johansen committed
150

151
152
153
154
155
    [[ -f $1 ]] || return 1

    for var in {SRC,SRCPKG,PKG,LOG}DEST MAKEFLAGS PACKAGER CARCH GPGKEY; do
        [[ -z ${!var} ]] && eval $(grep -a "^${var}=" "$1")
    done
Dan Johansen's avatar
Dan Johansen committed
156

157
    return 0
Dan Johansen's avatar
Dan Johansen committed
158
159
}

Dan Johansen's avatar
Dan Johansen committed
160
161
162
163
164
165
166
167
168
169
170
171
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)"
}
Dan Johansen's avatar
Dan Johansen committed
172

173
create_torrent() {
174
    info "Creating torrent of $IMAGE..."
175
    cd $IMGDIR/
176
    mktorrent -v -a udp://tracker.opentrackr.org:1337 -w https://osdn.net/dl/manjaro-arm/$IMAGE -o $IMAGE.torrent $IMAGE
177
178
}

179
180
checksum_img() {
    # Create checksums for the image
181
    info "Creating checksums for [$IMAGE]..."
182
    cd $IMGDIR/
Dan Johansen's avatar
Dan Johansen committed
183
184
    sha1sum $IMAGE > $IMAGE.sha1
    sha256sum $IMAGE > $IMAGE.sha256
185
186
    info "Creating signature for [$IMAGE]..."
    gpg --detach-sign -u $GPGMAIL "$IMAGE"
187
    if [ ! -f "$IMAGE.sig" ]; then
188
189
        echo "Image not signed. Aborting..."
        exit 1
190
    fi
191
192
193
194
195
}

img_upload() {
    # Upload image + checksums to image server
    msg "Uploading image and checksums to server..."
196
    info "Please use your server login details..."
197
198
    img_name=${IMAGE%%.*}
    rsync -raP $img_name* $STORAGE_USER@$OSDN/$DEVICE/$EDITION/$VERSION/
199
200
}

201
create_rootfs_pkg() {
202
    msg "Building $PACKAGE for $ARCH..."
203
    # Remove old rootfs if it exists
204
    if [ -d $CHROOTDIR ]; then
205
        info "Removing old rootfs..."
206
        rm -rf $CHROOTDIR
207
    fi
208
    msg "Creating rootfs..."
209
    # cd to rootfs
210
    mkdir -p $CHROOTDIR
211
    # basescrap the rootfs filesystem
212
    info "Switching branch to $BRANCH..."
213
    sed -i s/"arm-stable"/"arm-$BRANCH"/g $LIBDIR/pacman.conf.$ARCH
214
    $LIBDIR/pacstrap -G -M -C $LIBDIR/pacman.conf.$ARCH $CHROOTDIR fakeroot-qemu base-devel
215
216
    echo "Server = $BUILDSERVER/arm-$BRANCH/\$repo/\$arch" > $CHROOTDIR/etc/pacman.d/mirrorlist
    sed -i s/"arm-$BRANCH"/"arm-stable"/g $LIBDIR/pacman.conf.$ARCH
217
    if [[ $CARCH != "aarch64" ]]; then
218
219
        # Enable cross architecture Chrooting
        cp /usr/bin/qemu-aarch64-static $CHROOTDIR/usr/bin/
220
    fi
221
222
223
224
225
226

    msg "Configuring rootfs for building..."
    $NSPAWN $CHROOTDIR pacman-key --init 1> /dev/null 2>&1
    $NSPAWN $CHROOTDIR pacman-key --populate archlinuxarm manjaro manjaro-arm 1> /dev/null 2>&1
    cp $LIBDIR/makepkg $CHROOTDIR/usr/bin/
    $NSPAWN $CHROOTDIR chmod +x /usr/bin/makepkg 1> /dev/null 2>&1
227
    $NSPAWN $CHROOTDIR update-ca-trust
228
229
230
231
    sed -i s/'#PACKAGER="John Doe <john@doe.com>"'/"$PACKAGER"/ $CHROOTDIR/etc/makepkg.conf
    sed -i s/'#MAKEFLAGS="-j2"'/'MAKEFLAGS="-j$(nproc)"'/ $CHROOTDIR/etc/makepkg.conf
    sed -i s/'COMPRESSXZ=(xz -c -z -)'/'COMPRESSXZ=(xz -c -z - --threads=0)'/ $CHROOTDIR/etc/makepkg.conf
    $NSPAWN $CHROOTDIR pacman -Syy
232
}
233

234
create_rootfs_img() {
235
236
    #Check if device file exists
    if [ ! -f "$PROFILES/arm-profiles/devices/$DEVICE" ]; then 
237
238
239
        echo 'Invalid device '$DEVICE', please choose one of the following'
        echo "$(ls $PROFILES/arm-profiles/devices/)"
        exit 1
240
241
242
    fi
    #check if edition file exists
    if [ ! -f "$PROFILES/arm-profiles/editions/$EDITION" ]; then 
243
244
245
        echo 'Invalid edition '$EDITION', please choose one of the following'
        echo "$(ls $PROFILES/arm-profiles/editions/)"
        exit 1
246
    fi
247
    msg "Creating image of $EDITION for $DEVICE..."
248
249
    # Remove old rootfs if it exists
    if [ -d $ROOTFS_IMG/rootfs_$ARCH ]; then
250
251
        info "Removing old rootfs..."
        rm -rf $ROOTFS_IMG/rootfs_$ARCH
252
    fi
253
    mkdir -p $ROOTFS_IMG/rootfs_$ARCH
254
    if [[ "$KEEPROOTFS" = "false" ]]; then
255
256
257
258
        rm -rf $ROOTFS_IMG/Manjaro-ARM-$ARCH-latest.tar.gz*
        # fetch and extract rootfs
        info "Downloading latest $ARCH rootfs..."
        cd $ROOTFS_IMG
259
        wget -q --show-progress --progress=bar:force:noscroll https://github.com/manjaro-arm/rootfs/releases/latest/download/Manjaro-ARM-$ARCH-latest.tar.gz
260
261
262
    fi
    #also fetch it, if it does not exist
    if [ ! -f "$ROOTFS_IMG/Manjaro-ARM-$ARCH-latest.tar.gz" ]; then
263
        cd $ROOTFS_IMG
264
        wget -q --show-progress --progress=bar:force:noscroll https://github.com/manjaro-arm/rootfs/releases/latest/download/Manjaro-ARM-$ARCH-latest.tar.gz
265
    fi
266
    
267
    info "Extracting $ARCH rootfs..."
268
    bsdtar -xpf $ROOTFS_IMG/Manjaro-ARM-$ARCH-latest.tar.gz -C $ROOTFS_IMG/rootfs_$ARCH
269
    
270
    info "Setting up keyrings..."
271
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman-key --init 1>/dev/null || abort
272
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman-key --populate archlinuxarm manjaro manjaro-arm 1>/dev/null || abort
273
    
274
275
    if [[ ! -z ${CUSTOM_REPO} ]]; then
        info "Adding repo [$CUSTOM_REPO] to rootfs"
276
277
278
279
280

        if [[ "$CUSTOM_REPO" =~ ^https?://.*db ]]; then
            CUSTOM_REPO_NAME="${CUSTOM_REPO##*/}" # remove everyting before last slash
            CUSTOM_REPO_NAME="${CUSTOM_REPO_NAME%.*}" # remove everything after last dot
            CUSTOM_REPO_URL="${CUSTOM_REPO%/*}" # remove everything after last slash
281
            sed -i "s/^\[core\]/\[$CUSTOM_REPO_NAME\]\nSigLevel = Optional TrustAll\nServer = ${CUSTOM_REPO_URL//\//\\/}\n\n\[core\]/" $ROOTFS_IMG/rootfs_$ARCH/etc/pacman.conf
282
283
284
        else
            sed -i "s/^\[core\]/\[$CUSTOM_REPO\]\nInclude = \/etc\/pacman.d\/mirrorlist\n\n\[core\]/" $ROOTFS_IMG/rootfs_$ARCH/etc/pacman.conf
        fi
Bernhard Landauer's avatar
Bernhard Landauer committed
285
286
    fi

287
    info "Setting branch to $BRANCH..."
288
    echo "Server = $BUILDSERVER/arm-$BRANCH/\$repo/\$arch" > $ROOTFS_IMG/rootfs_$ARCH/etc/pacman.d/mirrorlist
289
    
290
291
    msg "Installing packages for $EDITION edition on $DEVICE..."
    # Install device and editions specific packages
292
    mount -o bind $PKGDIR/pkg-cache $PKG_CACHE
293
294
295
296
    case "$EDITION" in
        cubocore|phosh|plasma-mobile|plasma-mobile-dev)
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman -Syyu base systemd systemd-libs manjaro-system manjaro-release $PKG_EDITION $PKG_DEVICE --noconfirm || abort
            ;;
297
        minimal|server)
298
299
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman -Syyu base systemd systemd-libs dialog manjaro-arm-oem-install manjaro-system manjaro-release $PKG_EDITION $PKG_DEVICE --noconfirm || abort
            ;;
300
301
302
        *)
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman -Syyu base systemd systemd-libs calamares-arm-oem manjaro-system manjaro-release $PKG_EDITION $PKG_DEVICE --noconfirm || abort
            ;;
303
    esac
304

305
    if [[ ! -z "$ADD_PACKAGE" ]]; then
306
        info "Installing local package {$ADD_PACKAGE} to rootfs..."
307
        cp -ap $ADD_PACKAGE $PKG_CACHE/
308
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman -U /var/cache/pacman/pkg/$ADD_PACKAGE --noconfirm || abort
309
    fi
310
    info "Generating mirrorlist..."
311
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman-mirrors --protocols https --method random --api --set-branch $BRANCH 1> /dev/null 2>&1
312
    
313
    info "Enabling services..."
314
    # Enable services
315
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH systemctl enable getty.target haveged.service pacman-init.service 1>/dev/null
316
317
318
319
    if [[ "$CUSTOM_REPO" = "kde-unstable" ]]; then
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH systemctl enable sshd.service 1>/dev/null
    fi

320
321
322

    while read service; do
        if [ -e $ROOTFS_IMG/rootfs_$ARCH/usr/lib/systemd/system/$service ]; then
323
            echo "Enabling $service ..."
324
325
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH systemctl enable $service 1>/dev/null
        else
Bernhard Landauer's avatar
Bernhard Landauer committed
326
            echo "$service not found in rootfs. Skipping."
327
328
        fi
    done < $srv_list
329

330
    info "Applying overlay for $EDITION edition..."
331
    cp -ap $PROFILES/arm-profiles/overlays/$EDITION/* $ROOTFS_IMG/rootfs_$ARCH/
332
333

    info "Setting up system settings..."
334
    #system setup
335
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH update-ca-trust
336
    echo "$HOSTNAME" | tee --append $ROOTFS_IMG/rootfs_$ARCH/etc/hostname 1> /dev/null 2>&1
337
    case "$EDITION" in
338
        cubocore|plasma-mobile|plasma-mobile-dev)
339
340
            echo "No OEM setup!"
            ;;
341
        phosh|lomiri|nemomobile)
342
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH groupadd -r autologin
343
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH gpasswd -a "$USER" autologin
344
            ;;
345
        minimal|server)
346
347
348
349
350
351
352
353
            echo "Enabling SSH login for root user for headless setup..."
            sed -i s/"#PermitRootLogin prohibit-password"/"PermitRootLogin yes"/g $ROOTFS_IMG/rootfs_$ARCH/etc/ssh/sshd_config
            sed -i s/"#PermitEmptyPasswords no"/"PermitEmptyPasswords yes"/g $ROOTFS_IMG/rootfs_$ARCH/etc/ssh/sshd_config
            echo "Enabling autologin for first setup..."
            mv $ROOTFS_IMG/rootfs_$ARCH/usr/lib/systemd/system/getty\@.service $ROOTFS_IMG/rootfs_$ARCH/usr/lib/systemd/system/getty\@.service.bak
            cp $LIBDIR/getty\@.service $ROOTFS_IMG/rootfs_$ARCH/usr/lib/systemd/system/getty\@.service
            ;;
    esac
354
    
355
    # Create OEM user
356
    if [ -d $ROOTFS_IMG/rootfs_$ARCH/usr/share/calamares ]; then
357
358
        echo "Creating OEM user..."
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH groupadd -r autologin
359
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH useradd -m -g users -u 984 -G wheel,sys,audio,input,video,storage,lp,network,users,power,autologin -p $(openssl passwd -6 oem) -s /bin/bash oem
360
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH echo "oem ALL=(ALL) NOPASSWD: ALL" > $ROOTFS_IMG/rootfs_$ARCH/etc/sudoers.d/g_oem
361
        SESSION=$(ls $ROOTFS_IMG/rootfs_$ARCH/usr/share/xsessions/ | head -1)
362
363
364
365
366
367
368
369
370
371
372
        # For sddm based systems
        if [ -f $ROOTFS_IMG/rootfs_$ARCH/usr/bin/sddm ]; then
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH mkdir -p /etc/sddm.conf.d
            echo "# Created by Manjaro ARM OEM Setup

[Autologin]
User=oem
Session=$SESSION" > $ROOTFS_IMG/rootfs_$ARCH/etc/sddm.conf.d/90-autologin.conf
        fi
        # For lightdm based systems
        if [ -f $ROOTFS_IMG/rootfs_$ARCH/usr/bin/lightdm ]; then
373
374
375
            SESSION=$(echo ${SESSION%.*})
            sed -i s/"#autologin-user="/"autologin-user=oem"/g $ROOTFS_IMG/rootfs_$ARCH/etc/lightdm/lightdm.conf
            sed -i s/"#autologin-user-timeout=0"/"autologin-user-timeout=0"/g $ROOTFS_IMG/rootfs_$ARCH/etc/lightdm/lightdm.conf
376
377
            if [[ "$EDITION" = "lxqt" ]]; then
                sed -i s/"#autologin-session="/"autologin-session=lxqt"/g $ROOTFS_IMG/rootfs_$ARCH/etc/lightdm/lightdm.conf
378
379
380
381
            elif [[ "$EDITION" = "i3" ]]; then
                echo "autologin-user=oem
autologin-user-timeout=0
autologin-session=i3" >> $ROOTFS_IMG/rootfs_$ARCH/etc/lightdm/lightdm.conf
382
                sed -i s/"# Autostart applications"/"# Autostart applications\nexec --no-startup-id sudo -E calamares"/g $ROOTFS_IMG/rootfs_$ARCH/home/oem/.i3/config
383
384
385
            else
                sed -i s/"#autologin-session="/"autologin-session=$SESSION"/g $ROOTFS_IMG/rootfs_$ARCH/etc/lightdm/lightdm.conf
            fi
386
387
388
389
        fi
        # For greetd based Sway edition
        if [ -f $ROOTFS_IMG/rootfs_$ARCH/usr/bin/sway ]; then
            echo '[initial_session]
390
command = "sway --config /etc/greetd/oem-setup"
391
392
393
user = "oem"' >> $ROOTFS_IMG/rootfs_$ARCH/etc/greetd/config.toml
        fi
        # For Gnome edition
394
395
        if [ -f $ROOTFS_IMG/rootfs_$ARCH/usr/bin/gdm ]; then
            sed -i s/"\[daemon\]"/"\[daemon\]\nAutomaticLogin=oem\nAutomaticLoginEnable=True"/g $ROOTFS_IMG/rootfs_$ARCH/etc/gdm/custom.conf
396
397
398
        fi
    fi
    
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    # Lomiri services Temporary in function until it is moved to an individual package.
    if [[ "$EDITION" = "lomiri" ]]; then
        echo "Fix indicators"
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH mkdir -pv /usr/lib/systemd/user/ayatana-indicators.target.wants
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/ayatana-indicator-datetime.service /usr/lib/systemd/user/ayatana-indicators.target.wants/ayatana-indicator-datetime.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/ayatana-indicator-display.service /usr/lib/systemd/user/ayatana-indicators.target.wants/ayatana-indicator-display.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/ayatana-indicator-messages.service /usr/lib/systemd/user/ayatana-indicators.target.wants/ayatana-indicator-messages.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/ayatana-indicator-power.service /usr/lib/systemd/user/ayatana-indicators.target.wants/ayatana-indicator-power.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/ayatana-indicator-session.service /usr/lib/systemd/user/ayatana-indicators.target.wants/ayatana-indicator-session.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/ayatana-indicator-sound.service /usr/lib/systemd/user/ayatana-indicators.target.wants/ayatana-indicator-sound.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/indicator-network.service /usr/lib/systemd/user/ayatana-indicators.target.wants/indicator-network.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/indicator-transfer.service /usr/lib/systemd/user/ayatana-indicators.target.wants/indicator-transfer.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/indicator-bluetooth.service /usr/lib/systemd/user/ayatana-indicators.target.wants/indicator-bluetooth.service
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/indicator-location.service /usr/lib/systemd/user/ayatana-indicators.target.wants/indicator-location.service
        
        echo "Fix background"
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH mkdir -pv /usr/share/backgrounds
        #$NSPAWN $ROOTFS_IMG/rootfs_$ARCH convert -verbose /usr/share/wallpapers/manjaro.jpg /usr/share/wallpapers/manjaro.png
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/share/wallpapers/manjaro.png /usr/share/backgrounds/warty-final-ubuntu.png
        
        echo "Fix Maliit"
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH mkdir -pv /usr/lib/systemd/user/graphical-session.target.wants
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH ln -sfv /usr/lib/systemd/user/maliit-server.service /usr/lib/systemd/user/graphical-session.target.wants/maliit-server.service
    fi
    ### Lomiri Temporary service ends here 

425
    echo "Correcting permissions from overlay..."
Dan Johansen's avatar
Dan Johansen committed
426
427
    chown -R 0:0 $ROOTFS_IMG/rootfs_$ARCH/etc
    chown -R 0:0 $ROOTFS_IMG/rootfs_$ARCH/usr/{local,share}
428
    if [[ -d $ROOTFS_IMG/rootfs_$ARCH/etc/polkit-1/rules.d ]]; then
Dan Johansen's avatar
Dan Johansen committed
429
        chown 0:102 $ROOTFS_IMG/rootfs_$ARCH/etc/polkit-1/rules.d
430
431
    fi
    if [[ -d $ROOTFS_IMG/rootfs_$ARCH/usr/share/polkit-1/rules.d ]]; then
Dan Johansen's avatar
Dan Johansen committed
432
        chown 0:102 $ROOTFS_IMG/rootfs_$ARCH/usr/share/polkit-1/rules.d
433
    fi
434
    
435
436
    if [[ "$FILESYSTEM" = "btrfs" ]]; then
        info "Adding btrfs support to system..."
437
438
439
440
441
442
443
        if [ -f $ROOTFS_IMG/rootfs_$ARCH/boot/extlinux/extlinux.conf ]; then
            sed -i 's/APPEND/& rootflags=subvol=@/' $ROOTFS_IMG/rootfs_$ARCH/boot/extlinux/extlinux.conf
        elif [ -f $ROOTFS_IMG/rootfs_$ARCH/boot/boot.ini ]; then
            sed -i 's/setenv bootargs "/&rootflags=subvol=@ /' $ROOTFS_IMG/rootfs_$ARCH/boot/boot.ini
        elif [ -f $ROOTFS_IMG/rootfs_$ARCH/boot/uEnv.ini ]; then
            sed -i 's/setenv bootargs "/&rootflags=subvol=@ /' $ROOTFS_IMG/rootfs_$ARCH/boot/uEnv.ini
        elif [ -f $ROOTFS_IMG/rootfs_$ARCH/boot/cmdline.txt ]; then
444
            sed -i 's/^/rootflags=subvol=@ rootfstype=btrfs /' $ROOTFS_IMG/rootfs_$ARCH/boot/cmdline.txt
445
446
447
448
        elif [ -f $ROOTFS_IMG/rootfs_$ARCH/boot/boot.txt ]; then
            sed -i 's/setenv bootargs/& rootflags=subvol=@/' $ROOTFS_IMG/rootfs_$ARCH/boot/boot.txt
            $NSPAWN $ROOTFS_IMG/rootfs_$ARCH mkimage -A arm -O linux -T script -C none -n "U-Boot boot script" -d /boot/boot.txt /boot/boot.scr
        fi
449
450
451
        echo "LABEL=ROOT_MNJRO / btrfs  subvol=@,compress=zstd,defaults,noatime  0  0" >> $ROOTFS_IMG/rootfs_$ARCH/etc/fstab
        echo "LABEL=ROOT_MNJRO /home btrfs  subvol=@home,compress=zstd,defaults,noatime  0  0" >> $ROOTFS_IMG/rootfs_$ARCH/etc/fstab
        sed -i '/^MODULES/{s/)/ btrfs)/}' $ROOTFS_IMG/rootfs_$ARCH/etc/mkinitcpio.conf
452
        $NSPAWN $ROOTFS_IMG/rootfs_$ARCH mkinitcpio -P 1> /dev/null 2>&1
453
454
    fi
    
455
456
	if [[ "$FACTORY" = "true" ]]; then
	info "Making settings for factory specific image..."
457
        case "$EDITION" in
458
459
460
461
462
463
464
465
466
            kde-plasma)
                sed -i s/"manjaro-arm.png"/"manjaro-pine64-2b.png"/g $ROOTFS_IMG/rootfs_$ARCH/etc/skel/.config/plasma-org.kde.plasma.desktop-appletsrc
                echo "$EDITION - $(date +'%y'.'%m'.'%d')" | tee --append $ROOTFS_IMG/rootfs_$ARCH/etc/factory-version 1> /dev/null 2>&1
                ;;
            xfce)
                sed -i s/"manjaro-arm.png"/"manjaro-pine64-2b.png"/g $ROOTFS_IMG/rootfs_$ARCH/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml
                sed -i s/"manjaro-arm.png"/"manjaro-pine64-2b.png"/g $ROOTFS_IMG/rootfs_$ARCH/etc/lightdm/lightdm-gtk-greeter.conf
                echo "$EDITION - $(date +'%y'.'%m'.'%d')" | tee --append $ROOTFS_IMG/rootfs_$ARCH/etc/factory-version 1> /dev/null 2>&1
                ;;
467
        esac
468
469
    else
        echo "$DEVICE - $EDITION - $VERSION" | tee --append $ROOTFS_IMG/rootfs_$ARCH/etc/manjaro-arm-version 1> /dev/null 2>&1
470
    fi
471
    
472
473
474
475
476
477
    msg "Creating package list: [$IMGDIR/$IMGNAME-pkgs.txt]"
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman -Qr / > $ROOTFS_IMG/rootfs_$ARCH/var/tmp/pkglist.txt 2>/dev/null
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH sed -i '1s/^[^l]*l//' /var/tmp/pkglist.txt
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH sed -i '$d' /var/tmp/pkglist.txt
    mv $ROOTFS_IMG/rootfs_$ARCH/var/tmp/pkglist.txt "$IMGDIR/$IMGNAME-pkgs.txt"
    
478
479
480
481
482
483
484
    info "Cleaning rootfs for unwanted files..."
    prune_cache
    rm $ROOTFS_IMG/rootfs_$ARCH/usr/bin/qemu-aarch64-static
    rm -rf $ROOTFS_IMG/rootfs_$ARCH/var/log/*
    rm -rf $ROOTFS_IMG/rootfs_$ARCH/etc/*.pacnew
    rm -rf $ROOTFS_IMG/rootfs_$ARCH/usr/lib/systemd/system/systemd-firstboot.service
    rm -rf $ROOTFS_IMG/rootfs_$ARCH/etc/machine-id
485
    rm -rf $ROOTFS_IMG/rootfs_$ARCH/etc/pacman.d/gnupg
Bernhard Landauer's avatar
Bernhard Landauer committed
486

487
    msg "$DEVICE $EDITION rootfs complete"
488
489
}

Dan Johansen's avatar
Dan Johansen committed
490
491
492
create_emmc_install() {
    msg "Creating eMMC install image of $EDITION for $DEVICE..."
    # Remove old rootfs if it exists
493
    if [ -d $CHROOTDIR ]; then
494
        info "Removing old rootfs..."
495
        rm -rf $CHROOTDIR
Dan Johansen's avatar
Dan Johansen committed
496
    fi
497
    mkdir -p $CHROOTDIR
Dan Johansen's avatar
Dan Johansen committed
498
    if [[ "$KEEPROOTFS" = "false" ]]; then
499
500
501
502
        rm -rf $ROOTFS_IMG/Manjaro-ARM-$ARCH-latest.tar.gz*
        # fetch and extract rootfs
        info "Downloading latest $ARCH rootfs..."
        cd $ROOTFS_IMG
503
        wget -q --show-progress --progress=bar:force:noscroll https://github.com/manjaro-arm/rootfs/releases/latest/download/Manjaro-ARM-$ARCH-latest.tar.gz
Dan Johansen's avatar
Dan Johansen committed
504
505
506
    fi
    #also fetch it, if it does not exist
    if [ ! -f "$ROOTFS_IMG/Manjaro-ARM-$ARCH-latest.tar.gz" ]; then
507
        cd $ROOTFS_IMG
508
        wget -q --show-progress --progress=bar:force:noscroll https://github.com/manjaro-arm/rootfs/releases/latest/download/Manjaro-ARM-$ARCH-latest.tar.gz
Dan Johansen's avatar
Dan Johansen committed
509
510
511
    fi
    
    info "Extracting $ARCH rootfs..."
512
    bsdtar -xpf $ROOTFS_IMG/Manjaro-ARM-$ARCH-latest.tar.gz -C $CHROOTDIR
Dan Johansen's avatar
Dan Johansen committed
513
514
    
    info "Setting up keyrings..."
515
516
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman-key --init || abort
    $NSPAWN $ROOTFS_IMG/rootfs_$ARCH pacman-key --populate archlinuxarm manjaro manjaro-arm || abort
Dan Johansen's avatar
Dan Johansen committed
517
518
519
    
    msg "Installing packages for eMMC installer edition of $EDITION on $DEVICE..."
    # Install device and editions specific packages
520
    echo "Server = $BUILDSERVER/arm-$BRANCH/\$repo/\$arch" > $CHROOTDIR/etc/pacman.d/mirrorlist
521
    mount -o bind $PKGDIR/pkg-cache $PKG_CACHE
522
    $NSPAWN $CHROOTDIR pacman -Syyu base manjaro-system manjaro-release manjaro-arm-emmc-flasher $PKG_EDITION $PKG_DEVICE --noconfirm
Dan Johansen's avatar
Dan Johansen committed
523
524
525

    info "Enabling services..."
    # Enable services
526
    $NSPAWN $CHROOTDIR systemctl enable getty.target haveged.service 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
527
    
528
    info "Setting up system settings..."
529
    # setting hostname
530
    echo "$HOSTNAME" | tee --append $ROOTFS_IMG/rootfs_$ARCH/etc/hostname 1> /dev/null 2>&1
531
    # enable autologin
532
533
    mv $CHROOTDIR/usr/lib/systemd/system/getty\@.service $CHROOTDIR/usr/lib/systemd/system/getty\@.service.bak
    cp $LIBDIR/getty\@.service $CHROOTDIR/usr/lib/systemd/system/getty\@.service
534
    
535
    if [ -f $IMGDIR/Manjaro-ARM-$EDITION-$DEVICE-$VERSION.img.xz ]; then
536
        info "Copying local $DEVICE $EDITION image..."
537
        cp $IMGDIR/Manjaro-ARM-$EDITION-$DEVICE-$VERSION.img.xz $CHROOTDIR/var/tmp/Manjaro-ARM.img.xz
538
        sync
539
    else
540
        info "Downloading $DEVICE $EDITION image..."
541
        cd $CHROOTDIR/var/tmp/
542
        wget -q --show-progress --progress=bar:force:noscroll -O Manjaro-ARM.img.xz https://github.com/manjaro-arm/$DEVICE-images/releases/download/$VERSION/Manjaro-ARM-$EDITION-$DEVICE-$VERSION.img.xz
543
    fi
544
545
    
    info "Cleaning rootfs for unwanted files..."
546
    prune_cache
547
548
549
550
551
    rm $CHROOTDIR/usr/bin/qemu-aarch64-static
    rm -rf $CHROOTDIR/var/log/*
    rm -rf $CHROOTDIR/etc/*.pacnew
    rm -rf $CHROOTDIR/usr/lib/systemd/system/systemd-firstboot.service
    rm -rf $CHROOTDIR/etc/machine-id
Dan Johansen's avatar
Dan Johansen committed
552
553
}

554
create_img() {
555
    msg "Finishing image for $DEVICE $EDITION edition..."
556
    info "Creating partitions..."
557

558
    ARCH='aarch64'
559
    
560
    SIZE=$(du -s --block-size=MB $CHROOTDIR | awk '{print $1}' | sed -e 's/MB//g')
561
    EXTRA_SIZE=600
562
563
    REAL_SIZE=`echo "$(($SIZE+$EXTRA_SIZE))"`
    
564
    #making blank .img to be used
565
    dd if=/dev/zero of=$IMGDIR/$IMGNAME.img bs=1M count=$REAL_SIZE 1> /dev/null 2>&1
566
567

    #probing loop into the kernel
568
    modprobe loop 1> /dev/null 2>&1
569
570

    #set up loop device
571
    LDEV=`losetup -f`
572
573
574
    DEV=`echo $LDEV | cut -d "/" -f 3`

    #mount image to loop device
575
    losetup $LDEV $IMGDIR/$IMGNAME.img 1> /dev/null 2>&1
576

577
578
579
580
581
582
    case "$FILESYSTEM" in
        btrfs)
            # Create partitions
            #Clear first 32mb
            dd if=/dev/zero of=${LDEV} bs=1M count=32 1> /dev/null 2>&1
            #partition with boot and root
583
            case "$DEVICE" in
584
                oc2|on2|on2-plus|oc4|ohc4|vim1|vim2|vim3|radxa-zero|gtking-pro|gsking-x|edgev|rpi3|rpi4|pinephone)
585
586
587
588
589
                    parted -s $LDEV mklabel msdos 1> /dev/null 2>&1
                    ;;
                quartz64-bsp)
                    parted -s $LDEV mklabel gpt 1> /dev/null 2>&1
                    parted -s $LDEV mkpart uboot fat32 8MiB 16MiB 1> /dev/null 2>&1
590
                    parted -s $LDEV mkpart primary fat32 32M 256M 1> /dev/null 2>&1
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
                    START=`cat /sys/block/$DEV/${DEV}p2/start`
                    SIZE=`cat /sys/block/$DEV/${DEV}p2/size`
                    END_SECTOR=$(expr $START + $SIZE)
                    parted -s $LDEV mkpart primary btrfs "${END_SECTOR}s" 100% 1> /dev/null 2>&1
                    parted -s $LDEV set 2 esp on
                    partprobe $LDEV 1> /dev/null 2>&1
                    mkfs.vfat "${LDEV}p2" -n BOOT_MNJRO 1> /dev/null 2>&1
                    mkfs.btrfs -m single -L ROOT_MNJRO "${LDEV}p3" 1> /dev/null 2>&1
                
                    #copy rootfs contents over to the FS
                    info "Creating subvolumes..."
                    mkdir -p $TMPDIR/root
                    mkdir -p $TMPDIR/boot
                    mount ${LDEV}p2 $TMPDIR/boot
                    # Do subvolumes
                    mount -o compress=zstd "${LDEV}p3" $TMPDIR/root
                    btrfs su cr $TMPDIR/root/@ 1> /dev/null 2>&1
                    btrfs su cr $TMPDIR/root/@home 1> /dev/null 2>&1
                    umount $TMPDIR/root
                    mount -o compress=zstd,subvol=@ "${LDEV}p3" $TMPDIR/root
                    mkdir -p $TMPDIR/root/home
                    mount -o compress=zstd,subvol=@home "${LDEV}p3" $TMPDIR/root/home
                    info "Copying files to image..."
                    cp -ra $ROOTFS_IMG/rootfs_$ARCH/* $TMPDIR/root/
                    mv $TMPDIR/root/boot/* $TMPDIR/boot/
                    ;;
617
                *)
618
619
                    parted -s $LDEV mklabel gpt 1> /dev/null 2>&1
                    ;;
620
            esac
621
622
623
624
625
626
            if [[ "$DEVICE" != "quartz64-bsp" ]]; then
                parted -s $LDEV mkpart primary fat32 32M 256M 1> /dev/null 2>&1
                START=`cat /sys/block/$DEV/${DEV}p1/start`
                SIZE=`cat /sys/block/$DEV/${DEV}p1/size`
                END_SECTOR=$(expr $START + $SIZE)
                parted -s $LDEV mkpart primary btrfs "${END_SECTOR}s" 100% 1> /dev/null 2>&1
627
628
629
                if [[ "$DEVICE" = "jetson-nano" ]]; then
                    parted -s $LDEV set 1 esp on
                fi
630
631
632
                partprobe $LDEV 1> /dev/null 2>&1
                mkfs.vfat "${LDEV}p1" -n BOOT_MNJRO 1> /dev/null 2>&1
                mkfs.btrfs -m single -L ROOT_MNJRO "${LDEV}p2" 1> /dev/null 2>&1
633
    
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
                #copy rootfs contents over to the FS
                info "Creating subvolumes..."
                mkdir -p $TMPDIR/root
                mkdir -p $TMPDIR/boot
                mount ${LDEV}p1 $TMPDIR/boot
                # Do subvolumes
                mount -o compress=zstd "${LDEV}p2" $TMPDIR/root
                btrfs su cr $TMPDIR/root/@ 1> /dev/null 2>&1
                btrfs su cr $TMPDIR/root/@home 1> /dev/null 2>&1
                umount $TMPDIR/root
                mount -o compress=zstd,subvol=@ "${LDEV}p2" $TMPDIR/root
                mkdir -p $TMPDIR/root/home
                mount -o compress=zstd,subvol=@home "${LDEV}p2" $TMPDIR/root/home
                info "Copying files to image..."
                cp -ra $ROOTFS_IMG/rootfs_$ARCH/* $TMPDIR/root/
                mv $TMPDIR/root/boot/* $TMPDIR/boot
            fi
651
652
653
654
655
656
            ;;
        *)
            # Create partitions
            #Clear first 32mb
            dd if=/dev/zero of=${LDEV} bs=1M count=32 1> /dev/null 2>&1
            #partition with boot and root
657
            case "$DEVICE" in
658
                oc2|on2|on2-plus|oc4|ohc4|vim1|vim2|vim3|radxa-zero|gtking-pro|gsking-x|edgev|rpi3|rpi4|pinephone)
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
                    parted -s $LDEV mklabel msdos 1> /dev/null 2>&1
                    ;;
                quartz64-bsp)
                    parted -s $LDEV mklabel gpt 1> /dev/null 2>&1
                    parted -s $LDEV mkpart uboot fat32 8M 16M 1> /dev/null 2>&1
                    parted -s $LDEV mkpart primary fat32 32M 256M 1> /dev/null 2>&1
                    START=`cat /sys/block/$DEV/${DEV}p2/start`
                    SIZE=`cat /sys/block/$DEV/${DEV}p2/size`
                    END_SECTOR=$(expr $START + $SIZE)
                    parted -s $LDEV mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
                    parted -s $LDEV set 2 esp on
                    partprobe $LDEV 1> /dev/null 2>&1
                    mkfs.vfat "${LDEV}p2" -n BOOT_MNJRO 1> /dev/null 2>&1
                    mkfs.ext4 -O ^metadata_csum,^64bit "${LDEV}p3" -L ROOT_MNJRO 1> /dev/null 2>&1
                    info "Copying files to image..."
                    mkdir -p $TMPDIR/root
                    mkdir -p $TMPDIR/boot
                    mount ${LDEV}p2 $TMPDIR/boot
                    mount ${LDEV}p3 $TMPDIR/root
                    cp -ra $ROOTFS_IMG/rootfs_$ARCH/* $TMPDIR/root/
                    mv $TMPDIR/root/boot/* $TMPDIR/boot/
                    ;;
681
                *)
682
683
                    parted -s $LDEV mklabel gpt 1> /dev/null 2>&1
                    ;;
684
            esac
685
686
687
688
689
690
                if [[ "$DEVICE" != "quartz64-bsp" ]]; then
                    parted -s $LDEV mkpart primary fat32 32M 256M 1> /dev/null 2>&1
                    START=`cat /sys/block/$DEV/${DEV}p1/start`
                    SIZE=`cat /sys/block/$DEV/${DEV}p1/size`
                    END_SECTOR=$(expr $START + $SIZE)
                    parted -s $LDEV mkpart primary ext4 "${END_SECTOR}s" 100% 1> /dev/null 2>&1
691
692
693
                    if [[ "$DEVICE" = "jetson-nano" ]]; then
                        parted -s $LDEV set 1 esp on
                    fi
694
695
696
697
698
699
700
701
702
703
704
705
706
                    partprobe $LDEV 1> /dev/null 2>&1
                    mkfs.vfat "${LDEV}p1" -n BOOT_MNJRO 1> /dev/null 2>&1
                    mkfs.ext4 -O ^metadata_csum,^64bit "${LDEV}p2" -L ROOT_MNJRO 1> /dev/null 2>&1

                    #copy rootfs contents over to the FS
                    info "Copying files to image..."
                    mkdir -p $TMPDIR/root
                    mkdir -p $TMPDIR/boot
                    mount ${LDEV}p1 $TMPDIR/boot
                    mount ${LDEV}p2 $TMPDIR/root
                    cp -ra $ROOTFS_IMG/rootfs_$ARCH/* $TMPDIR/root/
                    mv $TMPDIR/root/boot/* $TMPDIR/boot
                fi
707
            ;;
708
    esac
709
        
710
711
712
    # Flash bootloader
    info "Flashing bootloader..."
    case "$DEVICE" in
713
    # AMLogic uboots
714
715
716
717
718
        oc2)
            dd if=$TMPDIR/boot/bl1.bin.hardkernel of=${LDEV} conv=fsync bs=1 count=442 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/bl1.bin.hardkernel of=${LDEV} conv=fsync bs=512 skip=1 seek=1 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/u-boot.gxbb of=${LDEV} conv=fsync bs=512 seek=97 1> /dev/null 2>&1
            ;;
719
        on2|on2-plus|oc4|ohc4)
720
721
            dd if=$TMPDIR/boot/u-boot.bin of=${LDEV} conv=fsync,notrunc bs=512 seek=1 1> /dev/null 2>&1
            ;;
722
        vim1|vim2|vim3|radxa-zero|gtking-pro|gsking-x|edgev)
723
724
            dd if=$TMPDIR/boot/u-boot.bin of=${LDEV} conv=fsync,notrunc bs=442 count=1 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/u-boot.bin of=${LDEV} conv=fsync,notrunc bs=512 skip=1 seek=1 1> /dev/null 2>&1
725
            ;;
726
        # Allwinner uboots
727
728
729
730
        pinebook|pine64-lts|pine64|pinetab|pine-h64)
            dd if=$TMPDIR/boot/u-boot-sunxi-with-spl-$DEVICE.bin of=${LDEV} conv=fsync bs=128k seek=1 1> /dev/null 2>&1
            ;;
        pinephone)
731
            dd if=$TMPDIR/boot/u-boot-sunxi-with-spl-$DEVICE-552.bin of=${LDEV} conv=fsync bs=8k seek=1 1> /dev/null 2>&1
Dan Johansen's avatar
Dan Johansen committed
732
            ;;
733
        # Rockchip RK33XX uboots
734
        pbpro|rockpro64|rockpi4b|rockpi4c|nanopc-t4|rock64|roc-cc|stationp1)
735
736
737
            dd if=$TMPDIR/boot/idbloader.img of=${LDEV} seek=64 conv=notrunc,fsync 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/u-boot.itb of=${LDEV} seek=16384 conv=notrunc,fsync 1> /dev/null 2>&1
            ;;
738
        pbpro-bsp)
739
740
741
            dd if=$TMPDIR/boot/idbloader.img of=${LDEV} seek=64 conv=notrunc,fsync 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/uboot.img of=${LDEV} seek=16384 conv=notrunc,fsync 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/trust.img of=${LDEV} seek=24576 conv=notrunc,fsync 1> /dev/null 2>&1
742
            ;;
743
744
745
746
747
        # Rockchip RK35XX uboots
        quartz64-bsp)
            dd if=$TMPDIR/boot/idblock.bin of=${LDEV} seek=64 conv=notrunc,fsync 1> /dev/null 2>&1
            dd if=$TMPDIR/boot/uboot.img of=${LDEV}p1 conv=notrunc,fsync 1> /dev/null 2>&1
            ;;
748
    esac
749
    
750
    info "Writing PARTUUIDs..."
751
752
753
754
755
756
757
    if [[ "$DEVICE" = "quartz64-bsp" ]]; then
        BOOT_PART=$(lsblk -p -o NAME,PARTUUID | grep "${LDEV}p2" | awk '{print $2}')
        ROOT_PART=$(lsblk -p -o NAME,PARTUUID | grep "${LDEV}p3" | awk '{print $2}')
    else
        BOOT_PART=$(lsblk -p -o NAME,PARTUUID | grep "${LDEV}p1" | awk '{print $2}')
        ROOT_PART=$(lsblk -p -o NAME,PARTUUID | grep "${LDEV}p2" | awk '{print $2}')
    fi
758
759
760
    echo "Boot PARTUUID is $BOOT_PART..."
    sed -i "s/LABEL=BOOT_MNJRO/PARTUUID=$BOOT_PART/g" $TMPDIR/root/etc/fstab
    echo "Root PARTUUID is $ROOT_PART..."
761
762
    if [ -f $TMPDIR/boot/extlinux/extlinux.conf ]; then
        sed -i "s/LABEL=ROOT_MNJRO/PARTUUID=$ROOT_PART/g" $TMPDIR/boot/extlinux/extlinux.conf
763
764
        elif [ -f $TMPDIR/boot/efi/extlinux/extlinux.conf ]; then
            sed -i "s/LABEL=ROOT_MNJRO/PARTUUID=$ROOT_PART/g" $TMPDIR/boot/efi/extlinux/extlinux.conf
765
        elif [ -f $TMPDIR/boot/boot.ini ]; then
766
            sed -i "s/LABEL=ROOT_MNJRO/PARTUUID=$ROOT_PART/g" $TMPDIR/boot/boot.ini
767
        elif [ -f $TMPDIR/boot/uEnv.ini ]; then
768
            sed -i "s/LABEL=ROOT_MNJRO/PARTUUID=$ROOT_PART/g" $TMPDIR/boot/uEnv.ini
769
        elif [ -f $TMPDIR/boot/cmdline.txt ]; then
770
            sed -i "s/PARTUUID=/PARTUUID=$ROOT_PART/g" $TMPDIR/boot/cmdline.txt
771
772
773
774
775
        #elif [ -f $TMPDIR/boot/boot.txt ]; then
        #   sed -i "s/LABEL=ROOT_MNJRO/PARTUUID=$ROOT_PART/g" $TMPDIR/boot/boot.txt
        #   cd $TMPDIR/boot
        #   ./mkscr
        #   cd $HOME
776
777
    fi
    if [[ "$FILESYSTEM" = "btrfs" ]]; then
778
        sed -i "s/LABEL=ROOT_MNJRO/PARTUUID=$ROOT_PART/g" $TMPDIR/root/etc/fstab
779
780
    else
        echo "PARTUUID=$ROOT_PART   /   $FILESYSTEM     defaults    0   1" >> $TMPDIR/root/etc/fstab
781
782
783
    fi
    
    
784
785
    # Clean up
    info "Cleaning up image..."
786
787
788
    if [[ "$FILESYSTEM" = "btrfs" ]]; then
        umount $TMPDIR/root/home
    fi
789
    umount $TMPDIR/root
790
    umount $TMPDIR/boot
791
792
793
    losetup -d $LDEV 1> /dev/null 2>&1
    rm -r $TMPDIR/root $TMPDIR/boot
    partprobe $LDEV 1> /dev/null 2>&1
794
    chmod 666 $IMGDIR/$IMGNAME.img
795
796
}

Bernhard Landauer's avatar
Bernhard Landauer committed
797
798
799
800
801
802
803
804
805
806
807
create_bmap() {
    if [ ! -e /usr/bin/bmaptool ]; then
        echo "'bmap-tools' are not installed. Skipping."
    else
        info "Creating bmap."
        cd ${IMGDIR}
        rm ${IMGNAME}.img.bmap 2>/dev/null
        bmaptool create -o ${IMGNAME}.img.bmap ${IMGNAME}.img
    fi
}

Dan Johansen's avatar
Dan Johansen committed
808
compress() {
809
    if [ -f $IMGDIR/$IMGNAME.img.xz ]; then
810
811
        info "Removing existing compressed image file {$IMGNAME.img.xz}..."
        rm -rf $IMGDIR/$IMGNAME.img.xz
812
    fi
813
    info "Compressing $IMGNAME.img..."
Dan Johansen's avatar
Dan Johansen committed
814
    #compress img
815
    cd $IMGDIR
816
    xz -zv --threads=0 $IMGNAME.img
817
    chmod 666 $IMGDIR/$IMGNAME.img.xz
818

819
    info "Removing rootfs_$ARCH"
820
    rm -rf $CHROOTDIR
821
822
}

823
build_pkg() {
824
825
    # Install local package to rootfs before building
    if [[ ! -z "$ADD_PACKAGE" ]]; then
826
        info "Installing local package {$ADD_PACKAGE} to rootfs..."
827
828
        cp -ap $ADD_PACKAGE $PKG_CACHE
        $NSPAWN $CHROOTDIR pacman -U /var/cache/pacman/pkg/$ADD_PACKAGE --noconfirm
829
    fi
830
    # Build the actual package
Dan Johansen's avatar
Dan Johansen committed
831
    msg "Copying build directory {$PACKAGE} to rootfs..."
832
833
    $NSPAWN $CHROOTDIR mkdir build 1> /dev/null 2>&1
    mount -o bind "$PACKAGE" $CHROOTDIR/build
Dan Johansen's avatar
Dan Johansen committed
834
    msg "Building {$PACKAGE}..."
835
    mount -o bind $PKGDIR/pkg-cache $PKG_CACHE
836
    $NSPAWN $CHROOTDIR pacman -Syu 1> /dev/null 2>&1
837
    if [[ $INSTALL_NEW = true ]]; then
838
        $NSPAWN $CHROOTDIR --chdir=/build/ makepkg -Asci --noconfirm
839
    else
840
        $NSPAWN $CHROOTDIR --chdir=/build/ makepkg -Asc --noconfirm
841
    fi
842
843
844
}

export_and_clean() {
845
    if ls $CHROOTDIR/build/*.pkg.tar.* 1> /dev/null 2>&1; then
846
        #pull package out of rootfs
847
        msg "Package Succeeded..."
848
        info "Extracting finished package out of rootfs..."
Dan Johansen's avatar
Dan Johansen committed
849
        mkdir -p $PKGDIR/$ARCH
850
        cp $CHROOTDIR/build/*.pkg.tar.* $PKGDIR/$ARCH/
851
        chown -R $SUDO_USER $PKGDIR
852
        msg "Package saved as {$PACKAGE} in {$PKGDIR/$ARCH}..."
853
        umount $CHROOTDIR/build
854
855

        #clean up rootfs
856
        info "Cleaning build files from rootfs"
857
        rm -rf $CHROOTDIR/build/
858
    else
859
        msg "!!!!! Package failed to build !!!!!"
860
        umount $CHROOTDIR/build
861
        prune_cache
862
        rm -rf $CHROOTDIR/build/
863
864
865
        exit 1
    fi
}
866

867
868
869
870
871
clone_profiles() {
    cd $PROFILES
    git clone --branch $1 https://gitlab.manjaro.org/manjaro-arm/applications/arm-profiles.git
}

872
get_profiles() {
873
    local branch=master
Bernhard Landauer's avatar
Bernhard Landauer committed
874
    [[ "$FACTORY" = "true" ]] && branch=pp-factory
875
    if ls $PROFILES/arm-profiles/* 1> /dev/null 2>&1; then
876
877
878
879
880
881
882
        if [[ $(grep branch $PROFILES/arm-profiles/.git/config | cut -d\" -f2) = "$branch" ]]; then
            cd $PROFILES/arm-profiles
            git pull
        else
            rm -rf $PROFILES/arm-profiles/
            clone_profiles $branch
        fi