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