root.sh 8.14 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

8

Angus Ainslie's avatar
Angus Ainslie committed
9
# distro and basedir set by calling script 
Guido Gunther's avatar
Guido Gunther committed
10
11
basedir="$1"
image_name="$2"
Guido Gunther's avatar
Guido Gunther committed
12
13
target_qemu="${basedir}/${qemu}"
stamp="${basedir}/var/log/gitrev.log"
14

15
16
17
18
19
20
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
21

Pedro Vicente's avatar
Pedro Vicente committed
22
set -e
Guido Gunther's avatar
Guido Gunther committed
23
set -u
Guido Gunther's avatar
Guido Gunther committed
24
set -x
25

26

27
28
echo "Configuring ${distro} in ${basedir} for board ${pureos_board}"

29
30
31
# bindmount qemu into the rootfs so we can chroot into it if needed
function setup_qemu()
{
Guido Gunther's avatar
Guido Gunther committed
32
    [ -f "${target_qemu}" ] || cp "${qemu}" "${target_qemu}"
33
34
35
}


Guido Gunther's avatar
Guido Gunther committed
36
37
function setup_kernel()
{
Guido Gunther's avatar
Guido Gunther committed
38
    local link
39
40
    local fdt_source
    fdt_source=""
Guido Gunther's avatar
Guido Gunther committed
41

42
    mkdir -p tmp/
Guido Gunther's avatar
Guido Gunther committed
43
44

    mkdir -p "${basedir}/boot/dtbs"
45
46
47
    case "${pureos_board}" in
        imx6)
            fdt_source="${basedir}"/usr/lib/linux-image-*/"${fdt_file}"
48
49
50
51
52
            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/"
53
            ;;
54
        ec-som|imx8)
55
56
57
58
            fdt_source="${basedir}"/usr/lib/linux-image-*/freescale/"${fdt_file}"
            cp "${basedir}/boot/${link}" "${basedir}/boot/Image.gz"
            gunzip "${basedir}/boot/Image.gz"
            ;;
59
60
61
62
        devkit*)
            # Reconfigure kernel package to trigger kernel hooks
	    chroot "${basedir}" dpkg-reconfigure $(basename ${kernel_deb%%_*})
            ;;
63
    esac
64
    [ -z "${fdt_source}" ] || cp ${fdt_source} "${basedir}/boot/dtbs/"
Guido Gunther's avatar
Guido Gunther committed
65
66
67
}


68
69
function setup_uboot()
{
70
71
72
73
74
75
    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"
76
    if [ "${pureos_board}" == "imx6" ]; then
77
        ln -fs boot_sd.scr "${basedir}/boot/6x_bootscript"
78
79
    elif [ "${pureos_board}" == "devkit" ]; then
        ln -fs boot_emmc.scr "${basedir}/boot/boot.scr"
80
    else
81
        ln -fs boot_sd.scr "${basedir}/boot/boot.scr"
82
    fi
83

Guido Gunther's avatar
Guido Gunther committed
84
    [ ! -d "files/uboot-${pureos_board}" ] || cp -a "files/uboot-${pureos_board}/"* "${basedir}/boot/"
85
86
87
}


88
89
function build_tarball()
{
Guido Gunther's avatar
Guido Gunther committed
90
   tarball=$(basename "${image_name}" .img).tar.xz
91

Guido Gunther's avatar
Guido Gunther committed
92
   echo -n "Taring up rootfs to $PWD/${tarball}..."
93
94
95
96
97
   tar --acl \
       --exclude='./run/*' \
       --exclude='./dev/*' \
       --exclude='./sys/*' \
       --exclude='./proc/*' \
Guido Gunther's avatar
Guido Gunther committed
98
       -acf "${PWD}/${tarball}" -C "${basedir}" .
99
100
101
102
   echo "Done."
}


103
104
105
106
107
108
109
function check_patched()
{
    if [ -z "${patched}" ]; then
        return
    fi

    for pkg in ${patched}; do
110
111
112
	# 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
113
114
            echo "${pkg} is not a patched version."
            exit 1
115
          fi
116
117
118
119
120
        fi
    done
}


121
function cleanup_chroot()
122
{
Guido Gunther's avatar
Guido Gunther committed
123
    chroot "${basedir}" apt-get clean
124

Guido Gunther's avatar
Guido Gunther committed
125
126
127
    rm "${basedir}/etc/resolv.conf"
    mv "${basedir}/etc/resolv.conf.bak" "${basedir}/etc/resolv.conf"
    rm "${basedir}/usr/sbin/policy-rc.d"
128

Guido Gunther's avatar
Guido Gunther committed
129
130
131
132
    umount "${basedir}/dev/pts"
    umount "${basedir}/dev"
    umount "${basedir}/sys"
    umount "${basedir}/proc"
133
134

    # existence of stamp indicates success
135
136
137
138
    if [ -f "${stamp}" ]; then
      [ -z "${build_tarball}" ] || build_tarball
    else
      exit 1
139
    fi
140
141
142
}


143
144
145
146
function prepare_chroot()
{
    trap cleanup_chroot EXIT

Guido Gunther's avatar
Guido Gunther committed
147
148
149
150
151
152
153
154
    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"
155
156
157
}


158
159
function create_stamp()
{
Guido Gunther's avatar
Guido Gunther committed
160
    git log --format=format:%H -1 > "${stamp}"
161
162
163
}


164
165
function setup_gui()
{
Guido Gunther's avatar
Guido Gunther committed
166
    chroot "${basedir}" systemctl enable phosh.service
167
168
}

169

Guido Gunther's avatar
Guido Gunther committed
170
171
172
function setup_journal()
{
    # Enable systemd journal
Guido Gunther's avatar
Guido Gunther committed
173
174
175
    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
176
    for l in daemon messages syslog kern mail user; do
Guido Gunther's avatar
Guido Gunther committed
177
        chroot "${basedir}" rm -f /var/log/${l}.*
Guido Gunther's avatar
Guido Gunther committed
178
179
180
181
    done
}


Guido Gunther's avatar
Guido Gunther committed
182
183
function setup_sshd()
{
184
    [ -x "${basedir}/usr/sbin/sshd" ] || return
185
    # Make sure unique host keys are generated on first boot
Guido Gunther's avatar
Guido Gunther committed
186
    chroot "${basedir}" rm -f /etc/ssh/ssh_host_*
Guido Gunther's avatar
Guido Gunther committed
187
188
189
}


190
191
192
193
194
195
196
function setup_locale()
{
    echo "en_US.UTF-8 UTF-8" > "${basedir}/etc/locale.gen"
    chroot "${basedir}" dpkg-reconfigure locales
}


197
198
# 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
199
200
201
# other packages are better installed late to not confuse deboostrap about
# systemd and systemd-shim (e.g. libpam-systemd)
function install_pkgs()
202
{
203
    chroot "${basedir}" apt-get -y update
204
205
206
207
    if ! chroot "${basedir}" apt -y install ${packages}; then
	echo "Package installation failed, this might help to diagnose the problem:"
	chroot "${basedir}" apt -y -o Debug::pkgProblemResolver=yes install -y --simulate ${packages}
    fi
208
209
}

210

211
212
function setup_apt()
{
213
214
215
    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
216

Guido Gunther's avatar
Guido Gunther committed
217
    cat << EOF > "${basedir}/etc/apt/apt.conf.d/71-no-recommends"
Pedro Vicente's avatar
Pedro Vicente committed
218
219
	APT::Install-Recommends "0";
	APT::Install-Suggests "0";
220
EOF
Guido Gunther's avatar
Guido Gunther committed
221
    chroot "${basedir}" apt-get install -y gnupg
222
223
224
225
226
227
228

    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
229
230
231
}


232
233
function setup_network()
{
Guido Gunther's avatar
Guido Gunther committed
234
    cat << EOF > "${basedir}/etc/hosts"
Pedro Vicente's avatar
Pedro Vicente committed
235
236
237
238
239
240
241
242
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
243
    cat << EOF > "${basedir}/etc/network/interfaces"
Pedro Vicente's avatar
Pedro Vicente committed
244
245
auto lo
iface lo inet loopback
246

247
source-directory interfaces.d
Pedro Vicente's avatar
Pedro Vicente committed
248
249
EOF

250
251
252
253
254
255
256
257
258
259
260
# 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
261
262
263
264
function create_report()
{
	echo "Installed packages:"
	chroot "${basedir}" dpkg -l
Guido Gunther's avatar
Guido Gunther committed
265
	echo -e "\\nInstalled apt keys:"
Guido Gunther's avatar
Guido Gunther committed
266
	chroot "${basedir}" apt-key list
Guido Gunther's avatar
Guido Gunther committed
267
	echo -e "\\nKernels:"
Guido Gunther's avatar
Guido Gunther committed
268
269
270
	ls -l "${basedir}/boot"
}

271

272
# create the directories so that this script can be tested without the generated filesystem
Guido Gunther's avatar
Guido Gunther committed
273
274
mkdir -p "${basedir}/etc/apt/apt.conf.d/"
mkdir -p "${basedir}/etc/network"
Pedro Vicente's avatar
Pedro Vicente committed
275
276
277
278

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

Guido Gunther's avatar
Guido Gunther committed
281
cat << EOF > "${basedir}/debconf.set"
Pedro Vicente's avatar
Pedro Vicente committed
282
283
284
console-common console-data/keymap/policy select Select keymap from full list
console-common console-data/keymap/full select en-latin1-nodeadkeys
EOF
285

286
setup_network
287
prepare_chroot
288
setup_qemu
289
setup_apt
290
install_pkgs
291
setup_locale
292
setup_gui
Guido Gunther's avatar
Guido Gunther committed
293
setup_journal
Guido Gunther's avatar
Guido Gunther committed
294
setup_sshd
295

Guido Gunther's avatar
Guido Gunther committed
296
# Skip setup until we have hardware,  not needed for qemu
297
case "${pureos_board}" in
298
    imx6|imx8|ec-som|devkit)
299
300
301
302
303
304
305
306
        setup_kernel
        setup_uboot
        ;;
    ?)
        echo "No kernel or u-boot setup for ${pureos_board}"
        ;;
esac

307
check_patched
Guido Gunther's avatar
Guido Gunther committed
308
create_report > "./tmp/${pureos_board}-report.log"
309
create_stamp
310
311

if [ -x files/local.sh ]; then
312
313
314
    echo "Warning: local.sh found, image is tainted"
    echo "tainted: true" >> files/meta.yml
    ./files/local.sh "$@"
315
fi