Exit if the chroot directory already exists
[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 # Add chroot config to schroot.conf
52 cat <<EOF >>/etc/schroot/schroot.conf
53 [$chrootdir]
54 description=Debian Buster for $chrootdir
55 directory=/opt/chroot/$chrootdir
56 groups=sbuild-security,$SUDO_USER,users
57 type=directory
58 profile=$chrootdir
59 EOF
60
61 echo "Adding the following $chrootdir section to /etc/schroot/schroot.conf:
62
63 [$chrootdir]
64 description=Debian Buster for $chrootdir
65 directory=/opt/chroot/$chrootdir
66 groups=sbuild-security,$SUDO_USER,users
67 type=directory
68
69 This enables you to chroot into $chrootdir as an unprivileged user by running
70 'schroot -c $chrootdir /bin/bash'
71 "
72
73 # Install apt dependencies in the chroot
74 cd /tmp
75 schroot -c $chrootdir /bin/bash << EOF
76 echo Installing necessary apt dependencies in the chroot
77 apt-get update -y
78 apt-get upgrade -y
79 apt-get install -y automake binutils-dev build-essential \
80 ccache cmake gcc git \
81 libtool \
82 sysvinit-core \
83 sysvinit-utils \
84 sudo \
85 python2.7 \
86 python3 python3-pip \
87 python3-setuptools python3-dev
88
89 # yeah systemd in a chroot? not very funny joke.
90 apt-get remove -y systemd
91
92 # add sudo no password
93 echo '$SUDO_USER ALL=NOPASSWD: ALL' >> /etc/sudoers.d/$chrootdir
94
95 # Create user with same UID in the chroot
96 useradd -m -p `python3 -c 'import crypt; print (crypt.crypt("1234","Fx"))'` \
97 -s /bin/bash $SUDO_USER -k /etc/skel
98 echo -e "
99 Added user ${SUDO_USER} with \e[1;91mpassword: 1234\e[0m"
100
101 # add deb-src to sources
102 echo deb-src http://ftp.debian.org/debian buster main > \
103 /etc/apt/sources.list.d/bustersrc.list
104
105 echo -e "
106 \e[1;91mPlease use command 'passwd ${SUDO_USER}' to change this immediately after this script is run for security purposes.\e[0m
107 "
108
109 # Add convenience variable to chroot user .bash_profile
110 echo -e 'export PATH=/usr/lib/ccache:"\044PATH"\nexport DISPLAY=:0.0\n' > /home/$SUDO_USER/.bash_profile
111 chown $SUDO_USER /home/$SUDO_USER/.bash_profile
112 chgrp $SUDO_USER /home/$SUDO_USER/.bash_profile
113
114 echo -e "Added 'export PATH=/usr/lib/ccache:\"\044PATH\"\nexport DISPLAY=:0.0' to /home/$SUDO_USER/.bash_profile to speed up rebuilds"
115 EOF