root.sh 8.58 KB
Newer Older
Guido Gunther's avatar
Guido Gunther committed
1
#!/bin/bash
2
3
4
5
6
#
# Copyright (C) 2017 Purism SPC
#
# SPDX-License-Identifier: GPL-3.0+
#
Pedro Vicente's avatar
Pedro Vicente committed
7

Guido Gunther's avatar
Guido Gunther committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Parmeters passed in via image builder:
# Mandatory
: "${apt_sources:?'apt_sources' not set}"
: "${distro:?'distro' not set}"
: "${packages:?'packages' not set}"
: "${pureos_board:?'pureos_board' not set}"

# Optional
: "${build_tarball:-}"
: "${ci_pkgs:-}"
: "${fdt_file:-}"
: "${kernel_deb:-}"
: "${patched:-}"
: "${qemu:-}"
22

Angus Ainslie's avatar
Angus Ainslie committed
23
# distro and basedir set by calling script 
Guido Gunther's avatar
Guido Gunther committed
24
25
basedir="$1"
image_name="$2"
Guido Gunther's avatar
Guido Gunther committed
26
27
target_qemu="${basedir}/${qemu}"
stamp="${basedir}/var/log/gitrev.log"
28

29
30
31
32
33
34
for env_var in distro pureos_board packages; do
    if [ -z "${!env_var}" ]; then
	echo "Must pass in \$${env_var} via the environment" 1>&2
	exit 1
    fi
done
35

Pedro Vicente's avatar
Pedro Vicente committed
36
set -e
Guido Gunther's avatar
Guido Gunther committed
37
set -u
Guido Gunther's avatar
Guido Gunther committed
38
set -x
39

40

41
42
echo "Configuring ${distro} in ${basedir} for board ${pureos_board}"

43
44
45
# bindmount qemu into the rootfs so we can chroot into it if needed
function setup_qemu()
{
Guido Gunther's avatar
Guido Gunther committed
46
    [ -f "${target_qemu}" ] || cp "${qemu}" "${target_qemu}"
47
48
49
}


Guido Gunther's avatar
Guido Gunther committed
50
51
function setup_kernel()
{
Guido Gunther's avatar
Guido Gunther committed
52
    local link
53
54
    local fdt_source
    fdt_source=""
Guido Gunther's avatar
Guido Gunther committed
55

56
    mkdir -p tmp/
Guido Gunther's avatar
Guido Gunther committed
57
58

    mkdir -p "${basedir}/boot/dtbs"
59
60
61
    case "${pureos_board}" in
        imx6)
            fdt_source="${basedir}"/usr/lib/linux-image-*/"${fdt_file}"
62
63
64
65
66
            link=$(basename "${basedir}"/boot/vmlinuz-*)
            rm -f "${basedir}/boot/zImage"
            ln -s "${link}" "${basedir}/boot/zImage"
            cp data/update-zImage-link "${basedir}/etc/kernel/postinst.d/"
            cp "${basedir}/boot/zImage" "tmp/"
67
            ;;
68
        ec-som|imx8)
69
70
71
72
            fdt_source="${basedir}"/usr/lib/linux-image-*/freescale/"${fdt_file}"
            cp "${basedir}/boot/${link}" "${basedir}/boot/Image.gz"
            gunzip "${basedir}/boot/Image.gz"
            ;;
73
74
75
76
        devkit*)
            # Reconfigure kernel package to trigger kernel hooks
	    chroot "${basedir}" dpkg-reconfigure $(basename ${kernel_deb%%_*})
            ;;
77
    esac
78
    [ -z "${fdt_source}" ] || cp ${fdt_source} "${basedir}/boot/dtbs/"
Guido Gunther's avatar
Guido Gunther committed
79
80
81
}


82
83
function setup_uboot()
{
84
85
86
87
88
89
    sed -e "s/##DTBNAME##/${fdt_file}/" "data/boot-${pureos_board}.txt.in" > "${basedir}/boot/boot_emmc.txt"
    sed -e "s/##RFSPART##/0/" -i "${basedir}/boot/boot_emmc.txt"
    mkimage -A arm -T script -O linux -d "${basedir}/boot/boot_emmc.txt" "${basedir}/boot/boot_emmc.scr"
    sed -e "s/##DTBNAME##/${fdt_file}/" "data/boot-${pureos_board}.txt.in" > "${basedir}/boot/boot_sd.txt"
    sed -e "s/##RFSPART##/1/" -i "${basedir}/boot/boot_sd.txt"
    mkimage -A arm -T script -O linux -d "${basedir}/boot/boot_sd.txt" "${basedir}/boot/boot_sd.scr"
90
    if [ "${pureos_board}" == "imx6" ]; then
91
        ln -fs boot_sd.scr "${basedir}/boot/6x_bootscript"
92
93
    elif [ "${pureos_board}" == "devkit" ]; then
        ln -fs boot_emmc.scr "${basedir}/boot/boot.scr"
94
    else
95
        ln -fs boot_sd.scr "${basedir}/boot/boot.scr"
96
    fi
97

Guido Gunther's avatar
Guido Gunther committed
98
    [ ! -d "files/uboot-${pureos_board}" ] || cp -a "files/uboot-${pureos_board}/"* "${basedir}/boot/"
99
100
101
}


102
103
function build_tarball()
{
Guido Gunther's avatar
Guido Gunther committed
104
   tarball=$(basename "${image_name}" .img).tar.xz
105

Guido Gunther's avatar
Guido Gunther committed
106
   echo -n "Taring up rootfs to $PWD/${tarball}..."
107
108
109
110
111
   tar --acl \
       --exclude='./run/*' \
       --exclude='./dev/*' \
       --exclude='./sys/*' \
       --exclude='./proc/*' \
Guido Gunther's avatar
Guido Gunther committed
112
       -acf "${PWD}/${tarball}" -C "${basedir}" .
113
114
115
116
   echo "Done."
}


117
118
119
120
121
122
123
function check_patched()
{
    if [ -z "${patched}" ]; then
        return
    fi

    for pkg in ${patched}; do
124
125
126
	# Not all architectures have all packages, so check this first
	if chroot "${basedir}" dpkg -s ${pkg}; then
          if ! chroot "${basedir}" dpkg -s ${pkg} | grep -qs "^Version: .*pureos"; then
127
128
            echo "${pkg} is not a patched version."
            exit 1
129
          fi
130
131
132
133
134
        fi
    done
}


135
function cleanup_chroot()
136
{
Guido Gunther's avatar
Guido Gunther committed
137
    chroot "${basedir}" apt-get clean
138

Guido Gunther's avatar
Guido Gunther committed
139
140
141
    rm "${basedir}/etc/resolv.conf"
    mv "${basedir}/etc/resolv.conf.bak" "${basedir}/etc/resolv.conf"
    rm "${basedir}/usr/sbin/policy-rc.d"
142

Guido Gunther's avatar
Guido Gunther committed
143
144
145
146
    umount "${basedir}/dev/pts"
    umount "${basedir}/dev"
    umount "${basedir}/sys"
    umount "${basedir}/proc"
147
148

    # existence of stamp indicates success
149
150
151
152
    if [ -f "${stamp}" ]; then
      [ -z "${build_tarball}" ] || build_tarball
    else
      exit 1
153
    fi
154
155
156
}


157
158
159
160
function prepare_chroot()
{
    trap cleanup_chroot EXIT

Guido Gunther's avatar
Guido Gunther committed
161
162
163
164
165
166
167
168
    mount -t proc chproc "${basedir}/proc"
    mount -t sysfs chsys "${basedir}/sys"
    mount -t devtmpfs chdev "${basedir}/dev" || mount --bind /dev "${basedir}/dev"
    mount -t devpts chpts "${basedir}/dev/pts"
    echo -e '#!/bin/sh\nexit 101' > "${basedir}/usr/sbin/policy-rc.d"
    chmod 755 "${basedir}/usr/sbin/policy-rc.d"
    mv "${basedir}/etc/resolv.conf" "${basedir}/etc/resolv.conf.bak"
    cp /etc/resolv.conf "${basedir}/etc"
169
170
171
}


172
173
function create_stamp()
{
Guido Gunther's avatar
Guido Gunther committed
174
    git log --format=format:%H -1 > "${stamp}"
175
176
177
}


178
179
function setup_gui()
{
Guido Gunther's avatar
Guido Gunther committed
180
    chroot "${basedir}" systemctl enable phosh.service
181
182
}

183

Guido Gunther's avatar
Guido Gunther committed
184
185
186
function setup_journal()
{
    # Enable systemd journal
Guido Gunther's avatar
Guido Gunther committed
187
188
189
    chroot "${basedir}" mkdir -p /var/log/journal
    chroot "${basedir}" systemd-tmpfiles --create --prefix /var/log/journal
    chroot "${basedir}" apt-get -y remove rsyslog
Guido Gunther's avatar
Guido Gunther committed
190
    for l in daemon messages syslog kern mail user; do
Guido Gunther's avatar
Guido Gunther committed
191
        chroot "${basedir}" rm -f /var/log/${l}.*
Guido Gunther's avatar
Guido Gunther committed
192
193
194
195
    done
}


Guido Gunther's avatar
Guido Gunther committed
196
197
function setup_sshd()
{
198
    [ -x "${basedir}/usr/sbin/sshd" ] || return
199
    # Make sure unique host keys are generated on first boot
Guido Gunther's avatar
Guido Gunther committed
200
    chroot "${basedir}" rm -f /etc/ssh/ssh_host_*
Guido Gunther's avatar
Guido Gunther committed
201
202
203
}


204
205
206
207
208
209
210
function setup_locale()
{
    echo "en_US.UTF-8 UTF-8" > "${basedir}/etc/locale.gen"
    chroot "${basedir}" dpkg-reconfigure locales
}


211
212
# We have to do this because task packages don't work with debootstrap
# (and won't).  See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=841649
213
214
215
# other packages are better installed late to not confuse deboostrap about
# systemd and systemd-shim (e.g. libpam-systemd)
function install_pkgs()
216
{
217
    chroot "${basedir}" apt-get -y update
Guido Gunther's avatar
Guido Gunther committed
218
219
    # Need to split package list
    # shellcheck disable=SC2086
220
221
    if ! chroot "${basedir}" apt -y install ${packages}; then
	echo "Package installation failed, this might help to diagnose the problem:"
Guido Gunther's avatar
Guido Gunther committed
222
	# shellcheck disable=SC2086
223
224
	chroot "${basedir}" apt -y -o Debug::pkgProblemResolver=yes install -y --simulate ${packages}
    fi
225
226
}

227

228
229
function setup_apt()
{
Guido Gunther's avatar
Guido Gunther committed
230
    # shellcheck disable=SC2001
231
232
233
    echo "${apt_sources}" | sed 's/|/\n/g' > "${basedir}/etc/apt/sources.list"
    echo "Set sources list to:"
    cat "${basedir}/etc/apt/sources.list"
Guido Gunther's avatar
Guido Gunther committed
234

Guido Gunther's avatar
Guido Gunther committed
235
    cat << EOF > "${basedir}/etc/apt/apt.conf.d/71-no-recommends"
Pedro Vicente's avatar
Pedro Vicente committed
236
237
	APT::Install-Recommends "0";
	APT::Install-Suggests "0";
238
EOF
Guido Gunther's avatar
Guido Gunther committed
239
    chroot "${basedir}" apt-get install -y gnupg
240
241
242
243
244
245
246

    if [ "${ci_pkgs}" -gt 0 ]; then
      cat << EOF > "${basedir}/etc/apt/sources.list.d/ci.list"
deb http://ci.puri.sm/ scratch librem5
EOF
      cat data/ci-repo.key | chroot ${basedir} apt-key add -
    fi
247
248
249
}


250
251
function setup_network()
{
Guido Gunther's avatar
Guido Gunther committed
252
    cat << EOF > "${basedir}/etc/hosts"
Pedro Vicente's avatar
Pedro Vicente committed
253
254
255
256
257
258
259
260
127.0.0.1       pureos   localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
EOF

Guido Gunther's avatar
Guido Gunther committed
261
    cat << EOF > "${basedir}/etc/network/interfaces"
Pedro Vicente's avatar
Pedro Vicente committed
262
263
auto lo
iface lo inet loopback
264

265
source-directory interfaces.d
Pedro Vicente's avatar
Pedro Vicente committed
266
267
EOF

268
269
270
271
272
273
274
275
276
277
278
# Don't setup resolv.conf if it's a symlink
# DHCP will later handle this just fine
    if [ -f "${basedir}/etc/resolv.conf" ]; then
        cat <<EOF > "${basedir}/etc/resolv.conf"
    nameserver 208.67.220.220
    nameserver 208.67.222.222
EOF
    fi
}


Guido Gunther's avatar
Guido Gunther committed
279
280
281
282
function create_report()
{
	echo "Installed packages:"
	chroot "${basedir}" dpkg -l
Guido Gunther's avatar
Guido Gunther committed
283
	echo -e "\\nInstalled apt keys:"
Guido Gunther's avatar
Guido Gunther committed
284
	chroot "${basedir}" apt-key list
Guido Gunther's avatar
Guido Gunther committed
285
	echo -e "\\nKernels:"
Guido Gunther's avatar
Guido Gunther committed
286
287
288
	ls -l "${basedir}/boot"
}

289

290
# create the directories so that this script can be tested without the generated filesystem
Guido Gunther's avatar
Guido Gunther committed
291
292
mkdir -p "${basedir}/etc/apt/apt.conf.d/"
mkdir -p "${basedir}/etc/network"
Pedro Vicente's avatar
Pedro Vicente committed
293
294
295
296

export MALLOC_CHECK_=0 # workaround for LP: #520465
export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive
297
export DEBCONF_NONINTERACTIVE_SEEN=true
Pedro Vicente's avatar
Pedro Vicente committed
298

Guido Gunther's avatar
Guido Gunther committed
299
cat << EOF > "${basedir}/debconf.set"
Pedro Vicente's avatar
Pedro Vicente committed
300
301
302
console-common console-data/keymap/policy select Select keymap from full list
console-common console-data/keymap/full select en-latin1-nodeadkeys
EOF
303

304
setup_network
305
prepare_chroot
306
setup_qemu
307
setup_apt
308
install_pkgs
309
setup_locale
310
setup_gui
Guido Gunther's avatar
Guido Gunther committed
311
setup_journal
Guido Gunther's avatar
Guido Gunther committed
312
setup_sshd
313

Guido Gunther's avatar
Guido Gunther committed
314
# Skip setup until we have hardware,  not needed for qemu
315
case "${pureos_board}" in
316
    imx6|imx8|ec-som|devkit)
317
318
319
320
321
322
323
324
        setup_kernel
        setup_uboot
        ;;
    ?)
        echo "No kernel or u-boot setup for ${pureos_board}"
        ;;
esac

325
check_patched
Guido Gunther's avatar
Guido Gunther committed
326
create_report > "./tmp/${pureos_board}-report.log"
327
create_stamp
328
329

if [ -x files/local.sh ]; then
330
331
332
    echo "Warning: local.sh found, image is tainted"
    echo "tainted: true" >> files/meta.yml
    ./files/local.sh "$@"
333
fi