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