add mk-deb-chroot based on coriolis2-chroot
[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 http://ftp.debian.org/debian
27 # Mount all chroot mount points from fstab
28 mount /opt/chroot/$chrootdir/dev
29 mount /opt/chroot/$chrootdir/dev/pts
30 mount /opt/chroot/$chrootdir/proc
31 mount /opt/chroot/$chrootdir/sys
32 mount /opt/chroot/$chrootdir/tmp
33 echo "$chrootdir" > /opt/chroot/$chrootdir/etc/debian_chroot
34 # Install apt dependencies in the chroot
35 chroot /opt/chroot/$chrootdir /bin/bash << EOF
36 echo Installing necessary apt dependencies in the chroot
37 apt-get update -y
38 apt-get upgrade -y
39 apt-get install -y automake binutils-dev build-essential \
40 ccache cmake gcc git \
41 libtool \
42 python3 python3-pip \
43 python3-setuptools python-dev
44
45 # Create user with same UID in the chroot
46 useradd -m -p `python -c 'import crypt; print crypt.crypt("1234","Fx")'` -s /bin/bash $SUDO_USER -k /etc/skel
47 echo -e "
48 Added user ${SUDO_USER} with \e[1;91mpassword: 1234\e[0m"
49
50 echo -e "
51 \e[1;91mPlease use command 'passwd ${SUDO_USER}' to change this immediately after this script is run for security purposes.\e[0m
52 "
53
54 # Add convenience variable to chroot user .bash_profile
55 echo -e 'export PATH=/usr/lib/ccache:"\044PATH"\nexport DISPLAY=:0.0\n' > /home/$SUDO_USER/.bash_profile
56 chown $SUDO_USER /home/$SUDO_USER/.bash_profile
57 chgrp $SUDO_USER /home/$SUDO_USER/.bash_profile
58
59 echo -e "Added 'export PATH=/usr/lib/ccache:\"\044PATH\"\nexport DISPLAY=:0.0' to /home/$SUDO_USER/.bash_profile to speed up rebuilds"
60 EOF
61
62 # Add chroot config to schroot.conf
63 cat <<EOF >>/etc/schroot/schroot.conf
64 [$chrootdir]
65 description=Debian Buster for $chrootdir
66 directory=/opt/chroot/$chrootdir
67 groups=sbuild-security,$SUDO_USER,users
68 EOF
69
70 echo "Adding the following $chrootdir section to /etc/schroot/schroot.conf:
71
72 [$chrootdir]
73 description=Debian Buster for $chrootdir
74 directory=/opt/chroot/$chrootdir
75 groups=sbuild-security,$SUDO_USER,users
76
77 This enables you to chroot into $chrootdir as an unprivileged user by running
78 'schroot -c $chrootdir /bin/bash'
79 "
80