root.sh 5.31 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
10
11
# distro and basedir set by calling script 
basedir=$1
image_name=$2
Angus Ainslie's avatar
Angus Ainslie committed
12
cwd=`pwd`
13
qemu="${basedir}/usr/bin/qemu-arm-static"
Angus Ainslie's avatar
Angus Ainslie committed
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
24

25

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

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

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

Guido Gunther's avatar
Guido Gunther committed
47

48
49
50
51
52
53
54
# bindmount qemu into the rootfs so we can chroot into it if needed
function setup_qemu()
{
    [ -f ${qemu} ] || cp /usr/bin/qemu-arm-static "${qemu}"
}


Guido Gunther's avatar
Guido Gunther committed
55
56
57
58
59
60
61
62
63
64
65
function setup_kernel()
{
    local link="$(basename $basedir/boot/vmlinuz-*)"
    rm -f "${basedir}/boot/zImage"
    ln -s "${link}" "${basedir}/boot/zImage"

    mkdir -p "${basedir}/boot/dtbs"
    cp "${basedir}"/usr/lib/linux-image-*/${fdt_file} "${basedir}/boot/dtbs/"
}


66
67
function setup_uboot()
{
Guido Gunther's avatar
Guido Gunther committed
68
    sed -e "s/##DTBNAME##/${fdt_file}/" data/boot-${pureos_board}.txt.in > $basedir/boot/boot.txt
Guido Gunther's avatar
Guido Gunther committed
69
70
    mkimage -A arm -T script -O linux -d $basedir/boot/boot.txt $basedir/boot/boot.scr
    ln -fs boot.scr $basedir/boot/6x_bootscript
71

Guido Gunther's avatar
Guido Gunther committed
72
    [ ! -d files/uboot-${pureos_board} ] || cp -a files/uboot-${pureos_board}/* $basedir/boot/
73
74
75
}


76
function cleanup_chroot()
77
{
78
79
80
81
    rm $basedir/etc/resolv.conf
    mv $basedir/etc/resolv.conf.bak $basedir/etc/resolv.conf
    rm $basedir/usr/sbin/policy-rc.d

82
83
84
85
86
87
88
    umount $basedir/dev/pts
    umount $basedir/dev
    umount $basedir/sys
    umount $basedir/proc
}


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function prepare_chroot()
{
    trap cleanup_chroot EXIT

    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
}


104
105
function setup_gui()
{
Guido Gunther's avatar
Guido Gunther committed
106
107
108
109
    # Allow purism user to launch weston from the console, etc.
    for group in weston-launch video audio; do
        chroot $basedir adduser purism $group
    done
Guido Gunther's avatar
Guido Gunther committed
110
    # Will move into a debian package once we have build infra
Guido Gunther's avatar
Guido Gunther committed
111
    cp data/weston.service $basedir/etc/systemd/system/
Guido Gunther's avatar
Guido Gunther committed
112
    chroot $basedir systemctl enable weston.service
113
114
}

115

Guido Gunther's avatar
Guido Gunther committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function build_tarball()
{
   tarball="$(basename ${image_name} .img)".tar.xz
   pwd=$PWD

   echo -n "Taring up rootfs to $PWD/$tarball..."
   tar --acl \
       --exclude='./run/*' \
       --exclude='./dev/*' \
       --exclude='./sys/*' \
       --exclude='./proc/*' \
       -acf $PWD/$tarball -C $basedir .
   echo "Done."
}


132
133
# 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
134
135
136
# other packages are better installed late to not confuse deboostrap about
# systemd and systemd-shim (e.g. libpam-systemd)
function install_pkgs()
137
138
139
140
141
142
143
144
145
146
{
    # Inspired by https://linux-sunxi.org/Mainline_Debian_HowTo
    (
        export DEBIAN_FRONTEND=noninteractive
        export DEBCONF_NONINTERACTIVE_SEEN=true
        export LC_ALL=C
        export LANGUAGE=C
        export LANG=C

        chroot $basedir apt-get -y update
147
        chroot $basedir apt-get -y install $packages
148
149
150
    )
}

151

152

153
154
155
156
# create the directories so that this script can be tested without the generated filesystem
mkdir -p $basedir/etc/apt/apt.conf.d/
mkdir -p $basedir/etc/network

Pedro Vicente's avatar
Pedro Vicente committed
157
cat << EOF > $basedir/etc/apt/sources.list
158
159
160
161
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
162
163
164
165
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

Angus Ainslie's avatar
Angus Ainslie committed
166
cat << EOT > $basedir/etc/apt/apt.conf.d/71-no-recommends
Pedro Vicente's avatar
Pedro Vicente committed
167
168
169
170
	APT::Install-Recommends "0";
	APT::Install-Suggests "0";
EOT

171
echo "pureos" > $basedir/etc/hostname
Pedro Vicente's avatar
Pedro Vicente committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185


cat << EOF > $basedir/etc/hosts
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

cat << EOF > $basedir/etc/network/interfaces
auto lo
iface lo inet loopback
186

Pedro Vicente's avatar
Pedro Vicente committed
187
188
189
190
allow-hotplug eth0
iface eth0 inet dhcp
EOF

191
192
193
194
195
196
197

# 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
Pedro Vicente's avatar
Pedro Vicente committed
198
EOF
199
fi
Pedro Vicente's avatar
Pedro Vicente committed
200
201
202
203
204
205
206
207
208

export MALLOC_CHECK_=0 # workaround for LP: #520465
export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive

cat << EOF > $basedir/debconf.set
console-common console-data/keymap/policy select Select keymap from full list
console-common console-data/keymap/full select en-latin1-nodeadkeys
EOF
209

210
prepare_chroot
211
setup_qemu
212
install_pkgs
Guido Gunther's avatar
Guido Gunther committed
213
setup_kernel
214
setup_uboot
215
setup_gui
Guido Gunther's avatar
Guido Gunther committed
216
build_tarball