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 apt install -y debootstrap schroot
46 elif [ -e /etc/arch-release ]; then
47 # Arch Linux detected: use pacman
48 pacman -S --needed debootstrap schroot
49 fi
50
51 # Install debian/buster chroot using debootstrap,
52 # any /etc/ config must be done after this point!
53 /usr/sbin/debootstrap buster /opt/chroot/$chrootdir \
54 http://ftp.uk.debian.org/debian
55 echo "$chrootdir" > /opt/chroot/$chrootdir/etc/debian_chroot
56
57 # make chroot profile
58 mkdir -p /etc/schroot/$chrootdir
59 cp /etc/schroot/default/copyfiles /etc/schroot/$chrootdir
60 touch /etc/schroot/$chrootdir/nssdatabases
61
62 # Copy over brokenproxy apt script to the new chroot environment
63 cp /tmp/brokenproxy /opt/chroot/$chrootdir/etc/apt/apt.conf.d/80-retries
64
65 # create special fstab not bind-mounting /home
66 schroot_fstab="
67 # <file system> <mount point> <type> <options> <dump> <pass>
68 /proc /proc none rw,bind 0 0
69 /sys /sys none rw,bind 0 0
70 /dev /dev none rw,bind 0 0
71 /dev/pts /dev/pts none rw,bind 0 0
72 /tmp /tmp none rw,bind 0 0
73 "
74
75 cat <<EOF >/etc/schroot/$chrootdir/fstab
76 $schroot_fstab
77 EOF
78
79 echo "Adding the following fstab mounts to /etc/schroot/$chrootdir/fstab:
80
81 $schroot_fstab
82
83 These rules are for the virtual filesystems used by linux in the chroot env
84 "
85
86 schroot_profile="
87 [$chrootdir]
88 description=Debian Buster for $chrootdir
89 directory=/opt/chroot/$chrootdir
90 groups=sbuild-security,$SUDO_USER,users
91 type=directory
92 profile=$chrootdir
93 "
94
95 if grep -x "\[$chrootdir\]" /etc/schroot/schroot.conf; then
96 echo "Found [$chrootdir] section in /etc/schroot/schroot.conf"
97 else
98 # Add chroot config to schroot.conf
99 cat <<EOF >>/etc/schroot/schroot.conf
100 $schroot_profile
101 EOF
102
103 echo "Adding the following $chrootdir section to /etc/schroot/schroot.conf:
104
105 $schroot_profile
106
107 This enables you to chroot into $chrootdir as an unprivileged user by running
108 'schroot -c $chrootdir /bin/bash'
109 "
110 fi
111
112 # Install apt dependencies in the chroot
113 cd /tmp
114 schroot -c $chrootdir /bin/bash --directory=/tmp << EOF
115 echo Installing necessary apt dependencies in the chroot
116 apt-get update -y
117 apt-get upgrade -y
118 apt-get install -y automake binutils-dev build-essential \
119 ccache cmake gcc git \
120 libtool \
121 sysvinit-core \
122 sysvinit-utils \
123 sudo \
124 python2.7 \
125 python3 python3-pip \
126 python3-setuptools python3-dev
127
128 # yeah systemd in a chroot? not very funny joke.
129 apt-get remove -y systemd
130
131 # add sudo no password
132 echo '$SUDO_USER ALL=NOPASSWD: ALL' >> /etc/sudoers.d/$chrootdir
133
134 # Create user with same UID in the chroot
135 useradd -m -p `python3 -c 'import crypt; print (crypt.crypt("1234","Fx"))'` \
136 -s /bin/bash $SUDO_USER -k /etc/skel
137 echo -e "
138 Added user ${SUDO_USER} with \e[1;91mpassword: 1234\e[0m"
139
140 # add deb-src to sources
141 echo deb-src http://ftp.debian.org/debian buster main > \
142 /etc/apt/sources.list.d/bustersrc.list
143
144 echo -e "
145 \e[1;91mPlease use command 'passwd ${SUDO_USER}' to change this immediately after this script is run for security purposes.\e[0m
146 "
147
148 # Add convenience variable to chroot user .bash_profile
149 echo -e 'export PATH=/usr/lib/ccache:"\044PATH"\nexport DISPLAY=:0.0\n' > /home/$SUDO_USER/.bash_profile
150 chown $SUDO_USER /home/$SUDO_USER/.bash_profile
151 chgrp $SUDO_USER /home/$SUDO_USER/.bash_profile
152
153 echo -e "Added 'export PATH=/usr/lib/ccache:\"\044PATH\"\nexport DISPLAY=:0.0' to /home/$SUDO_USER/.bash_profile to speed up rebuilds"
154 EOF