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

Angus Ainslie's avatar
Angus Ainslie committed
15

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

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

27

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

30
31
32
33
echo "Configuring ${distro} in ${basedir} for board ${pureos_board}"

case ${pureos_board} in 
  imx6)
Guido Gunther's avatar
Guido Gunther committed
34
    fdt_file=imx6qp-nitrogen6_max_purism.dtb
35
36
37
38
39
40
41
42
    ;;
  imx8)
    echo "board type ${pureos_board} not configured yet"
    ;;
  qemu)
    echo "board type ${pureos_board} not configured yet"
    exit 1
    ;;
43
  *)
44
45
46
47
    echo "unrecognized board type ${pureos_board}"
    exit 1
    ;;
esac
Pedro Vicente's avatar
Pedro Vicente committed
48

Guido Gunther's avatar
Guido Gunther committed
49

50
51
52
# bindmount qemu into the rootfs so we can chroot into it if needed
function setup_qemu()
{
Guido Gunther's avatar
Guido Gunther committed
53
    [ -f "${target_qemu}" ] || cp "${qemu}" "${target_qemu}"
54
55
56
}


Guido Gunther's avatar
Guido Gunther committed
57
58
function setup_kernel()
{
Guido Gunther's avatar
Guido Gunther committed
59
60
61
    local link

    link=$(basename "${basedir}"/boot/vmlinuz-*)
Guido Gunther's avatar
Guido Gunther committed
62
63
    rm -f "${basedir}/boot/zImage"
    ln -s "${link}" "${basedir}/boot/zImage"
64
    cp data/update-zImage-link "${basedir}/etc/kernel/postinst.d/"
Guido Gunther's avatar
Guido Gunther committed
65
66
67

    mkdir -p "${basedir}/boot/dtbs"
    cp "${basedir}"/usr/lib/linux-image-*/${fdt_file} "${basedir}/boot/dtbs/"
68
69
70
    # Make sure we have the kernel available for tests
    mkdir -p tmp/
    cp "${basedir}/boot/zImage" "tmp/"
Guido Gunther's avatar
Guido Gunther committed
71
72
73
}


74
75
function setup_uboot()
{
Guido Gunther's avatar
Guido Gunther committed
76
77
78
    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"
    ln -fs boot.scr "${basedir}/boot/6x_bootscript"
79

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


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

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


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

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

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

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


119
120
121
122
function prepare_chroot()
{
    trap cleanup_chroot EXIT

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


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


140
141
function setup_gui()
{
Guido Gunther's avatar
Guido Gunther committed
142
143
    # Allow purism user to launch weston from the console, etc.
    for group in weston-launch video audio; do
Guido Gunther's avatar
Guido Gunther committed
144
        chroot "${basedir}" adduser purism "${group}"
Guido Gunther's avatar
Guido Gunther committed
145
    done
Guido Gunther's avatar
Guido Gunther committed
146
    # Will move into a debian package once we have build infra
Guido Gunther's avatar
Guido Gunther committed
147
148
    cp data/weston.service "${basedir}/etc/systemd/system/"
    chroot "${basedir}" systemctl enable weston.service
149
150
}

151

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


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


Guido Gunther's avatar
Guido Gunther committed
173
174
175
176
177
178
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
179
180
    chroot "${basedir}" systemctl enable sshd-host-keys.service
    chroot "${basedir}" rm -f /etc/ssh/ssh_host_*
Guido Gunther's avatar
Guido Gunther committed
181
182
183
}


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

194

195
196
function setup_apt()
{
Guido Gunther's avatar
Guido Gunther committed
197
    cat << EOF > "${basedir}/etc/apt/sources.list"
198
199
200
201
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
202
203
204
205
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
EOF

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


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

Pedro Vicente's avatar
Pedro Vicente committed
228
229
230
231
allow-hotplug eth0
iface eth0 inet dhcp
EOF

232
233
234

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

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

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

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

257
setup_network
258
prepare_chroot
259
setup_qemu
260
setup_apt
261
install_pkgs
262
setup_gui
Guido Gunther's avatar
Guido Gunther committed
263
setup_journal
Guido Gunther's avatar
Guido Gunther committed
264
setup_sshd
265
setup_touchscreen
Guido Gunther's avatar
Guido Gunther committed
266
267
268
269
270
# Skip setup until we have hardware,  not needed for qemu
if [ "${pureos_board}" != "imx8" ]; then
  setup_kernel
  setup_uboot
fi
271
create_stamp
272
273

if [ -x files/local.sh ]; then
274
275
276
    echo "Warning: local.sh found, image is tainted"
    echo "tainted: true" >> files/meta.yml
    ./files/local.sh "$@"
277
fi