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