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