ARGH getting really fed up with ISP transparent proxies that break
[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 /opt/chroot/$chrootdir
22
23 # argh apt with broken proxies and unreliable ISPs, getting fed up with it
24 cat <<EOF >/tmp/brokenproxy
25 APT::Acquire::Retries "20";
26 APT::Get::Fix-Broken "true";
27 Acquire::http::Pipeline-Depth "0";
28 Acquire::http::No-Cache=True;
29 Acquire::BrokenProxy=true;
30 EOF
31
32 cp /tmp/brokenproxy /etc/apt/apt.conf.d/80-retries
33 cp /tmp/brokenproxy /opt/chroot/coriolis/etc/apt/apt.conf.d/80-retries
34
35 # Install debootstrap and schroot
36 if [ -e /etc/debian_version ]; then
37 # Debian detected: use apt
38 apt install -y debootstrap schroot
39 elif [ -e /etc/arch-release ]; then
40 # Arch Linux detected: use pacman
41 pacman -S --needed debootstrap schroot
42 fi
43
44 # Install debian/buster chroot using debootstrap
45 /usr/sbin/debootstrap buster /opt/chroot/$chrootdir \
46 http://ftp.uk.debian.org/debian
47 echo "$chrootdir" > /opt/chroot/$chrootdir/etc/debian_chroot
48
49 # make chroot profile
50 mkdir -p /etc/schroot/$chrootdir
51 cp /etc/schroot/default/copyfiles /etc/schroot/$chrootdir
52 touch /etc/schroot/$chrootdir/nssdatabases
53
54 # create special fstab not bind-mounting /home
55 cat <<EOF >/etc/schroot/$chrootdir/fstab
56 # <file system> <mount point> <type> <options> <dump> <pass>
57 /proc /proc none rw,bind 0 0
58 /sys /sys none rw,bind 0 0
59 /dev /dev none rw,bind 0 0
60 /dev/pts /dev/pts none rw,bind 0 0
61 /tmp /tmp none rw,bind 0 0
62 EOF
63
64 if grep -x "\[$chrootdir\]" /etc/schroot/schroot.conf; then
65 echo "Found [$chrootdir] section in /etc/schroot/schroot.conf"
66 else
67 # Add chroot config to schroot.conf
68 cat <<EOF >>/etc/schroot/schroot.conf
69 [$chrootdir]
70 description=Debian Buster for $chrootdir
71 directory=/opt/chroot/$chrootdir
72 groups=sbuild-security,$SUDO_USER,users
73 type=directory
74 profile=$chrootdir
75 EOF
76
77 echo "Adding the following $chrootdir section to /etc/schroot/schroot.conf:
78
79 [$chrootdir]
80 description=Debian Buster for $chrootdir
81 directory=/opt/chroot/$chrootdir
82 groups=sbuild-security,$SUDO_USER,users
83 type=directory
84
85 This enables you to chroot into $chrootdir as an unprivileged user by running
86 'schroot -c $chrootdir /bin/bash'
87 "
88 fi
89
90 # Install apt dependencies in the chroot
91 cd /tmp
92 schroot -c $chrootdir /bin/bash << EOF
93 echo Installing necessary apt dependencies in the chroot
94 apt-get update -y
95 apt-get upgrade -y
96 apt-get install -y automake binutils-dev build-essential \
97 ccache cmake gcc git \
98 libtool \
99 sysvinit-core \
100 sysvinit-utils \
101 sudo \
102 python2.7 \
103 python3 python3-pip \
104 python3-setuptools python3-dev
105
106 # yeah systemd in a chroot? not very funny joke.
107 apt-get remove -y systemd
108
109 # add sudo no password
110 echo '$SUDO_USER ALL=NOPASSWD: ALL' >> /etc/sudoers.d/$chrootdir
111
112 # Create user with same UID in the chroot
113 useradd -m -p `python3 -c 'import crypt; print (crypt.crypt("1234","Fx"))'` \
114 -s /bin/bash $SUDO_USER -k /etc/skel
115 echo -e "
116 Added user ${SUDO_USER} with \e[1;91mpassword: 1234\e[0m"
117
118 # add deb-src to sources
119 echo deb-src http://ftp.debian.org/debian buster main > \
120 /etc/apt/sources.list.d/bustersrc.list
121
122 echo -e "
123 \e[1;91mPlease use command 'passwd ${SUDO_USER}' to change this immediately after this script is run for security purposes.\e[0m
124 "
125
126 # Add convenience variable to chroot user .bash_profile
127 echo -e 'export PATH=/usr/lib/ccache:"\044PATH"\nexport DISPLAY=:0.0\n' > /home/$SUDO_USER/.bash_profile
128 chown $SUDO_USER /home/$SUDO_USER/.bash_profile
129 chgrp $SUDO_USER /home/$SUDO_USER/.bash_profile
130
131 echo -e "Added 'export PATH=/usr/lib/ccache:\"\044PATH\"\nexport DISPLAY=:0.0' to /home/$SUDO_USER/.bash_profile to speed up rebuilds"
132 EOF