Fix correct commit hash for release 0.7.0 of openXC7
[dev-env-setup.git] / mk-deb-chroot
1 #!/bin/bash
2 if [ "$EUID" -ne 0 ]
3 then echo "Please run as root"
4 exit
5 fi
6 if [ -z "$1" ]
7 then echo "Need arg $1 for chroot to make"
8 exit
9 fi
10 chrootdir="$1"
11
12 if [ -e /opt/chroot/$chrootdir ]; then
13 echo "/opt/chroot/$chrootdir already exists"
14 exit
15 fi
16
17
18 echo "Creating chroot dir '$chrootdir'"
19
20 # Create coriolis chroot dir in /opt
21 mkdir -p /opt/chroot/$chrootdir
22
23 # argh apt with broken proxies and unreliable ISPs, getting fed up with it
24 if [ -e /tmp/brokenproxy ]; then
25 echo "Temp file containing apt proxy fix configuration already exists, skipping!"
26 else
27 cat <<EOF >/tmp/brokenproxy
28 APT::Acquire::Retries "20";
29 APT::Get::Fix-Broken "true";
30 Acquire::http::Pipeline-Depth "0";
31 Acquire::http::No-Cache=True;
32 Acquire::BrokenProxy=true;
33 EOF
34 fi
35
36 if [ -e /etc/apt/apt.conf.d/80-retries ]; then
37 echo "Host system apt.conf.d already has 80-retries, skipping!"
38 else
39 cp /tmp/brokenproxy /etc/apt/apt.conf.d/80-retries
40 fi
41
42 # Install debootstrap and schroot
43 if [ -e /etc/debian_version ]; then
44 # Debian detected: use apt
45 REQUIRED_PKG="debootstrap"
46 PKG_OK=$(dpkg-query -W --showformat='${Status}\n'\
47 $REQUIRED_PKG|grep "install ok installed")
48 echo Checking for $REQUIRED_PKG: $PKG_OK
49 if [ "" = "$PKG_OK" ]; then
50 apt install -y debootstrap
51 fi
52
53 REQUIRED_PKG="schroot"
54 PKG_OK=$(dpkg-query -W --showformat='${Status}\n'\
55 $REQUIRED_PKG|grep "install ok installed")
56 echo Checking for $REQUIRED_PKG: $PKG_OK
57 if [ "" = "$PKG_OK" ]; then
58 apt install -y schroot
59 fi
60 elif [ -e /etc/arch-release ]; then
61 # Arch Linux detected: use pacman
62 pacman -S --needed debootstrap schroot
63 fi
64
65 # Debian mirror from where to download packages for the chroot
66 CHROOT_DEBIAN_MIRROR=${CHROOT_DEBIAN_MIRROR:-http://ftp.uk.debian.org/debian}
67
68 # Install debian/buster chroot using debootstrap,
69 # any /etc/ config must be done after this point!
70 /usr/sbin/debootstrap buster /opt/chroot/$chrootdir $CHROOT_DEBIAN_MIRROR
71
72 echo "$chrootdir" > /opt/chroot/$chrootdir/etc/debian_chroot
73
74 # make chroot profile
75 mkdir -p /etc/schroot/$chrootdir
76 cp /etc/schroot/default/copyfiles /etc/schroot/$chrootdir
77 touch /etc/schroot/$chrootdir/nssdatabases
78
79 # Copy over brokenproxy apt script to the new chroot environment
80 cp /tmp/brokenproxy /opt/chroot/$chrootdir/etc/apt/apt.conf.d/80-retries
81
82 # create special fstab not bind-mounting /home
83 schroot_fstab="
84 # <file system> <mount point> <type> <options> <dump> <pass>
85 /proc /proc none rw,bind 0 0
86 /sys /sys none rw,bind 0 0
87 /dev /dev none rw,bind 0 0
88 /dev/pts /dev/pts none rw,bind 0 0
89 /tmp /tmp none rw,bind 0 0
90 "
91
92 cat <<EOF >/etc/schroot/$chrootdir/fstab
93 $schroot_fstab
94 EOF
95
96 echo "Adding the following fstab mounts to /etc/schroot/$chrootdir/fstab:
97
98 $schroot_fstab
99
100 These rules are for the virtual filesystems used by linux in the chroot env
101 "
102
103 schroot_profile="
104 [$chrootdir]
105 description=Debian Buster for $chrootdir
106 directory=/opt/chroot/$chrootdir
107 groups=sbuild-security,$SUDO_USER,users
108 type=directory
109 profile=$chrootdir
110 "
111
112 if grep -x "\[$chrootdir\]" /etc/schroot/schroot.conf; then
113 echo "Found [$chrootdir] section in /etc/schroot/schroot.conf"
114 else
115 # Add chroot config to schroot.conf
116 cat <<EOF >>/etc/schroot/schroot.conf
117 $schroot_profile
118 EOF
119
120 echo "Adding the following $chrootdir section to /etc/schroot/schroot.conf:
121
122 $schroot_profile
123
124 This enables you to chroot into $chrootdir as an unprivileged user by running
125 'schroot -c $chrootdir /bin/bash'
126 "
127 fi
128
129 #!!! buster backports is needed for a lot of things
130 cat << EOF > /opt/chroot/$chrootdir/etc/apt/sources.list.d/buster_backports.list
131 deb http://deb.debian.org/debian buster-backports main
132 deb-src http://deb.debian.org/debian buster-backports main
133 EOF
134
135 # oooo annoying, pin preferences for buster-backports
136 # make it low pririty for reproduceability
137 cat << EOF > /opt/chroot/$chrootdir/etc/apt/preferences.d/99buster-backports
138 Package: *
139 Pin: release a=buster-backports
140 Pin-Priority: 100
141
142 Package: libcurl3-gnutls
143 Pin: version 7.64*
144 Pin-Priority: 1001
145 EOF
146
147 # Install apt dependencies in the chroot
148 cd /tmp
149 schroot -c $chrootdir /bin/bash --directory=/tmp << EOF
150 echo Installing necessary apt dependencies in the chroot
151 apt-get update -y
152 apt-get upgrade -y
153 apt-get install -y automake binutils-dev build-essential \
154 ccache cmake gcc git \
155 libtool \
156 sysvinit-core \
157 sysvinit-utils \
158 sudo \
159 python2.7 \
160 python3 python3-pip \
161 python3-setuptools python3-dev \
162 python3-setuptools-scm
163
164 # yeah systemd in a chroot? not very funny joke.
165 apt-get remove -y systemd
166
167 # add sudo no password
168 echo '$SUDO_USER ALL=NOPASSWD: ALL' >> /etc/sudoers.d/$chrootdir
169
170 # Create user with same UID in the chroot
171 useradd -m -p `python3 -c 'import crypt; print (crypt.crypt("1234","Fx"))'` \
172 -s /bin/bash $SUDO_USER -k /etc/skel
173 echo -e "
174 Added user ${SUDO_USER} with \e[1;91mpassword: 1234\e[0m"
175
176 # add deb-src to sources
177 echo deb-src http://ftp.debian.org/debian buster main > \
178 /etc/apt/sources.list.d/bustersrc.list
179
180 echo -e "
181 \e[1;91mPlease use command 'passwd ${SUDO_USER}' to change this immediately after this script is run for security purposes.\e[0m
182 "
183
184 # Add convenience variable to chroot user .bash_profile
185 echo -e 'export PATH=/usr/lib/ccache:"\044PATH"\nexport DISPLAY=:0.0\n' > /home/$SUDO_USER/.bash_profile
186 chown $SUDO_USER /home/$SUDO_USER/.bash_profile
187 chgrp $SUDO_USER /home/$SUDO_USER/.bash_profile
188
189 echo -e "Added 'export PATH=/usr/lib/ccache:\"\044PATH\"\nexport DISPLAY=:0.0' to /home/$SUDO_USER/.bash_profile to speed up rebuilds"
190 EOF