use schroot "type=directory" which means no need to do fstab modification
[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 echo "creating chroot '$chrootdir'"
12
13 # Create coriolis chroot dir in /opt
14 mkdir -p /opt/chroot/$chrootdir
15 # Install debootstrap and schroot via apt
16 apt install -y debootstrap schroot
17 # Install debian/buster chroot using debootstrap
18 /usr/sbin/debootstrap buster /opt/chroot/$chrootdir \
19 http://ftp.uk.debian.org/debian
20 echo "$chrootdir" > /opt/chroot/$chrootdir/etc/debian_chroot
21
22 # Install apt dependencies in the chroot
23 chroot /opt/chroot/$chrootdir /bin/bash << EOF
24 echo Installing necessary apt dependencies in the chroot
25 apt-get update -y
26 apt-get upgrade -y
27 apt-get install -y automake binutils-dev build-essential \
28 ccache cmake gcc git \
29 libtool \
30 sysvinit-core \
31 sysvinit-utils \
32 sudo \
33 python2.7 \
34 python3 python3-pip \
35 python3-setuptools python3-dev
36
37 # yeah systemd in a chroot? not very funny joke.
38 apt-get remove -y systemd
39
40 # add sudo no password
41 echo '$SUDO_USER ALL=NOPASSWD: ALL' >> /etc/sudoers.d/$chrootdir
42
43 # Create user with same UID in the chroot
44 useradd -m -p `python3 -c 'import crypt; print (crypt.crypt("1234","Fx"))'` \
45 -s /bin/bash $SUDO_USER -k /etc/skel
46 echo -e "
47 Added user ${SUDO_USER} with \e[1;91mpassword: 1234\e[0m"
48
49 # add deb-src to sources
50 echo deb-src http://ftp.debian.org/debian buster main > \
51 /etc/apt/sources.list.d/bustersrc.list
52
53 echo -e "
54 \e[1;91mPlease use command 'passwd ${SUDO_USER}' to change this immediately after this script is run for security purposes.\e[0m
55 "
56
57 # Add convenience variable to chroot user .bash_profile
58 echo -e 'export PATH=/usr/lib/ccache:"\044PATH"\nexport DISPLAY=:0.0\n' > /home/$SUDO_USER/.bash_profile
59 chown $SUDO_USER /home/$SUDO_USER/.bash_profile
60 chgrp $SUDO_USER /home/$SUDO_USER/.bash_profile
61
62 echo -e "Added 'export PATH=/usr/lib/ccache:\"\044PATH\"\nexport DISPLAY=:0.0' to /home/$SUDO_USER/.bash_profile to speed up rebuilds"
63 EOF
64
65 # Add chroot config to schroot.conf
66 cat <<EOF >>/etc/schroot/schroot.conf
67 [$chrootdir]
68 description=Debian Buster for $chrootdir
69 directory=/opt/chroot/$chrootdir
70 groups=sbuild-security,$SUDO_USER,users
71 type=directory
72 EOF
73
74 echo "Adding the following $chrootdir section to /etc/schroot/schroot.conf:
75
76 [$chrootdir]
77 description=Debian Buster for $chrootdir
78 directory=/opt/chroot/$chrootdir
79 groups=sbuild-security,$SUDO_USER,users
80 type=directory
81
82 This enables you to chroot into $chrootdir as an unprivileged user by running
83 'schroot -c $chrootdir /bin/bash'
84 "
85