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