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

Guido Gunther's avatar
Guido Gunther committed
27
mirror=deb.debian.org
Guido Gunther's avatar
Guido Gunther committed
28

29
30
echo "Configuring ${distro} in ${basedir} for board ${pureos_board}"

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


Guido Gunther's avatar
Guido Gunther committed
38
39
function setup_kernel()
{
Guido Gunther's avatar
Guido Gunther committed
40
41
42
    local link

    link=$(basename "${basedir}"/boot/vmlinuz-*)
Guido Gunther's avatar
Guido Gunther committed
43
44
    rm -f "${basedir}/boot/zImage"
    ln -s "${link}" "${basedir}/boot/zImage"
45
    cp data/update-zImage-link "${basedir}/etc/kernel/postinst.d/"
Guido Gunther's avatar
Guido Gunther committed
46
47

    mkdir -p "${basedir}/boot/dtbs"
48
49
50
51
52
53
    case "${pureos_board}" in
        imx6)
            fdt_source="${basedir}"/usr/lib/linux-image-*/"${fdt_file}"
            ;;
        imx8)
            fdt_source="${basedir}"/usr/lib/linux-image-*/freescale/"${fdt_file}"
54
55
	    cp "${basedir}/boot/${link}" "${basedir}/boot/Image.gz"
	    gunzip "${basedir}/boot/Image.gz"
56
            ;;
57
58
59
60
61
        ec-som)
            fdt_source="${basedir}"/usr/lib/linux-image-*/freescale/"${fdt_file}"
            cp "${basedir}/boot/${link}" "${basedir}/boot/Image.gz"
            gunzip "${basedir}/boot/Image.gz"
            ;;
62
63
    esac
    cp ${fdt_source} "${basedir}/boot/dtbs/"
64
65
66
    # Make sure we have the kernel available for tests
    mkdir -p tmp/
    cp "${basedir}/boot/zImage" "tmp/"
Guido Gunther's avatar
Guido Gunther committed
67
68
69
}


70
71
function setup_uboot()
{
Guido Gunther's avatar
Guido Gunther committed
72
73
    sed -e "s/##DTBNAME##/${fdt_file}/" "data/boot-${pureos_board}.txt.in" > "${basedir}/boot/boot.txt"
    mkimage -A arm -T script -O linux -d "${basedir}/boot/boot.txt" "${basedir}/boot/boot.scr"
74
75
76
    if [ "${pureos_board}" == "imx6" ]; then
        ln -fs boot.scr "${basedir}/boot/6x_bootscript"
    fi
77

Guido Gunther's avatar
Guido Gunther committed
78
    [ ! -d "files/uboot-${pureos_board}" ] || cp -a "files/uboot-${pureos_board}/"* "${basedir}/boot/"
79
80
81
}


82
83
function build_tarball()
{
Guido Gunther's avatar
Guido Gunther committed
84
   tarball=$(basename "${image_name}" .img).tar.xz
85

Guido Gunther's avatar
Guido Gunther committed
86
   echo -n "Taring up rootfs to $PWD/${tarball}..."
87
88
89
90
91
   tar --acl \
       --exclude='./run/*' \
       --exclude='./dev/*' \
       --exclude='./sys/*' \
       --exclude='./proc/*' \
Guido Gunther's avatar
Guido Gunther committed
92
       -acf "${PWD}/${tarball}" -C "${basedir}" .
93
94
95
96
   echo "Done."
}


97
function cleanup_chroot()
98
{
Guido Gunther's avatar
Guido Gunther committed
99
    chroot "${basedir}" apt-get clean
100

Guido Gunther's avatar
Guido Gunther committed
101
102
103
    rm "${basedir}/etc/resolv.conf"
    mv "${basedir}/etc/resolv.conf.bak" "${basedir}/etc/resolv.conf"
    rm "${basedir}/usr/sbin/policy-rc.d"
104

Guido Gunther's avatar
Guido Gunther committed
105
106
107
108
    umount "${basedir}/dev/pts"
    umount "${basedir}/dev"
    umount "${basedir}/sys"
    umount "${basedir}/proc"
109
110

    # existence of stamp indicates success
Guido Gunther's avatar
Guido Gunther committed
111
    if [ -f "${stamp}" ] && [ -n "${build_tarball}" ]; then
112
113
      build_tarball
    fi
114
115
116
}


117
118
119
120
function prepare_chroot()
{
    trap cleanup_chroot EXIT

Guido Gunther's avatar
Guido Gunther committed
121
122
123
124
125
126
127
128
    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"
129
130
131
}


132
133
function create_stamp()
{
Guido Gunther's avatar
Guido Gunther committed
134
    git log --format=format:%H -1 > "${stamp}"
135
136
137
}


138
139
function setup_gui()
{
Guido Gunther's avatar
Guido Gunther committed
140
    # Allow purism user to launch weston from the console, etc.
Guido Gunther's avatar
Guido Gunther committed
141
    for group in video audio; do
Guido Gunther's avatar
Guido Gunther committed
142
        chroot "${basedir}" adduser purism "${group}"
Guido Gunther's avatar
Guido Gunther committed
143
    done
Guido Gunther's avatar
Guido Gunther committed
144
    cp data/{phosh}.service "${basedir}/etc/systemd/system/"
Guido Gunther's avatar
Guido Gunther committed
145
    chroot "${basedir}" systemctl enable phosh.service
146
147
}

148

149
150
151
152
function setup_touchscreen()
{
    # this needs to be manually loaded
    echo blacklist rmi-i2c > /lib/modprobe.d/rmi-blacklist.conf
153
154
    cp data/rmi-ts.service "${basedir}/etc/systemd/system/"
    chroot "${basedir}" systemctl enable rmi-ts
155
156
157
}


Guido Gunther's avatar
Guido Gunther committed
158
159
160
function setup_journal()
{
    # Enable systemd journal
Guido Gunther's avatar
Guido Gunther committed
161
162
163
    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
164
    for l in daemon messages syslog kern mail user; do
Guido Gunther's avatar
Guido Gunther committed
165
        chroot "${basedir}" rm -f /var/log/${l}.*
Guido Gunther's avatar
Guido Gunther committed
166
167
168
169
    done
}


Guido Gunther's avatar
Guido Gunther committed
170
171
172
173
174
175
function setup_sshd()
{
    [[ "$packages" =~ "openssh-server" ]] || return
    # Generate new host keys on first boot
    cp data/gen-sshd-host-keys "${basedir}/usr/local/bin/"
    cp data/sshd-host-keys.service "${basedir}/etc/systemd/system/"
Guido Gunther's avatar
Guido Gunther committed
176
177
    chroot "${basedir}" systemctl enable sshd-host-keys.service
    chroot "${basedir}" rm -f /etc/ssh/ssh_host_*
Guido Gunther's avatar
Guido Gunther committed
178
179
180
}


181
182
# 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
183
184
185
# other packages are better installed late to not confuse deboostrap about
# systemd and systemd-shim (e.g. libpam-systemd)
function install_pkgs()
186
{
187
    chroot "${basedir}" apt-get -y update
Guido Gunther's avatar
Guido Gunther committed
188
    chroot "${basedir}" apt-get -y install ${packages}
189
190
}

191

192
193
function setup_apt()
{
Guido Gunther's avatar
Guido Gunther committed
194
    cat << EOF > "${basedir}/etc/apt/sources.list"
195
196
197
198
deb http://$mirror/debian $distro main contrib non-free
deb-src http://$mirror/debian $distro main contrib non-free
deb http://$mirror/debian $distro-updates main contrib non-free
deb-src http://$mirror/debian $distro-updates main contrib non-free
Pedro Vicente's avatar
Pedro Vicente committed
199
200
deb http://security.debian.org/debian-security $distro/updates main contrib non-free
deb-src http://security.debian.org/debian-security $distro/updates main contrib non-free
Guido Gunther's avatar
Guido Gunther committed
201
202
203
204
EOF

    cat << EOF > "${basedir}/etc/apt/sources.list.d/ci.list"
deb http://ci.puri.sm/ scratch librem5
Pedro Vicente's avatar
Pedro Vicente committed
205
206
EOF

Guido Gunther's avatar
Guido Gunther committed
207
    cat << EOF > "${basedir}/etc/apt/apt.conf.d/71-no-recommends"
Pedro Vicente's avatar
Pedro Vicente committed
208
209
	APT::Install-Recommends "0";
	APT::Install-Suggests "0";
210
EOF
Guido Gunther's avatar
Guido Gunther committed
211

Guido Gunther's avatar
Guido Gunther committed
212
    chroot ${basedir} apt-get install -y gnupg
Guido Gunther's avatar
Guido Gunther committed
213
    cat data/ci-repo.key | chroot ${basedir} apt-key add -
214
215
216
}


217
218
function setup_network()
{
Guido Gunther's avatar
Guido Gunther committed
219
    cat << EOF > "${basedir}/etc/hosts"
Pedro Vicente's avatar
Pedro Vicente committed
220
221
222
223
224
225
226
227
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
228
    cat << EOF > "${basedir}/etc/network/interfaces"
Pedro Vicente's avatar
Pedro Vicente committed
229
230
auto lo
iface lo inet loopback
231

Pedro Vicente's avatar
Pedro Vicente committed
232
233
234
235
allow-hotplug eth0
iface eth0 inet dhcp
EOF

236
237
238

# Don't setup resolv.conf if it's a symlink
# DHCP will later handle this just fine
Guido Gunther's avatar
Guido Gunther committed
239
240
    if [ -f "${basedir}/etc/resolv.conf" ]; then
        cat <<EOF > "${basedir}/etc/resolv.conf"
241
242
    nameserver 208.67.220.220
    nameserver 208.67.222.222
Pedro Vicente's avatar
Pedro Vicente committed
243
EOF
244
245
246
247
    fi
}

# create the directories so that this script can be tested without the generated filesystem
Guido Gunther's avatar
Guido Gunther committed
248
249
mkdir -p "${basedir}/etc/apt/apt.conf.d/"
mkdir -p "${basedir}/etc/network"
Pedro Vicente's avatar
Pedro Vicente committed
250
251
252
253

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

Guido Gunther's avatar
Guido Gunther committed
256
cat << EOF > "${basedir}/debconf.set"
Pedro Vicente's avatar
Pedro Vicente committed
257
258
259
console-common console-data/keymap/policy select Select keymap from full list
console-common console-data/keymap/full select en-latin1-nodeadkeys
EOF
260

261
setup_network
262
prepare_chroot
263
setup_qemu
264
setup_apt
265
install_pkgs
266
setup_gui
Guido Gunther's avatar
Guido Gunther committed
267
setup_journal
Guido Gunther's avatar
Guido Gunther committed
268
setup_sshd
269
setup_touchscreen
270

Guido Gunther's avatar
Guido Gunther committed
271
# Skip setup until we have hardware,  not needed for qemu
272
case "${pureos_board}" in
273
    imx6|imx8|ec-som)
274
275
276
277
278
279
280
281
        setup_kernel
        setup_uboot
        ;;
    ?)
        echo "No kernel or u-boot setup for ${pureos_board}"
        ;;
esac

282
create_stamp
283
284

if [ -x files/local.sh ]; then
285
286
287
    echo "Warning: local.sh found, image is tainted"
    echo "tainted: true" >> files/meta.yml
    ./files/local.sh "$@"
288
fi