Debian Codegraph @

source/util/server/install-ubuntu-chroot

raw
1 #!/bin/bash
2 #---help---
3 # Usage: install-ubuntu-chroot -d destination-directory
4 #
5 # This script downloads and installs a recent version of Ubuntu to a chroot.
6 #---help---
7
8 set -euo pipefail
9
10 die() {
11 printf '\033[1;31mERROR:\033[0m %s\n' "$@" >&2 # bold red
12 exit 1
13 }
14
15 einfo() {
16 printf '\n\033[1;36m> %s\033[0m\n' "$@" >&2 # bold cyan
17 }
18
19 ewarn() {
20 printf '\033[1;33m> %s\033[0m\n' "$@" >&2 # bold yellow
21 }
22
23 usage() {
24 sed -En '/^#---help---/,/^#---help---/p' "$0" | sed -E 's/^# ?//; 1d;$d;'
25 }
26
27 download_file() (
28 local url="$1"
29 local sha256="$2"
30 local dest="${3:-.}"
31 local file="${url##*/}"
32
33 mkdir -p "$dest" && cd "$dest"
34
35 if [ -f "$file" ]; then
36 einfo "$file exists; checking sha256sum..."
37 echo "$sha256 $file" | sha256sum -c && exit 0
38 fi
39
40 einfo "Downloading $file..."
41
42 # On Wheezy, we may have outdated certificates and be unable to update.
43 # Skipping the check is safe because we immediately verify the sha256.
44 rm -f "$file" \
45 && wget --no-check-certificate -T 10 "$url" \
46 && echo "$sha256 $file" | sha256sum -c
47 )
48
49 gen_chroot_script() {
50 cat <<-EOF
51 #!/bin/sh
52 set -e
53 ENV_FILTER_REGEX='(TERM)'
54 EOF
55 cat <<-'EOF'
56 user='root'
57 if [ $# -ge 2 ] && [ "$1" = '-u' ]; then
58 user="$2"; shift 2
59 fi
60 oldpwd="$(pwd)"
61 [ "$(id -u)" -eq 0 ] || _sudo='sudo'
62 tmpfile="$(mktemp)"
63 chmod 644 "$tmpfile"
64 export | sed -En "s/^([^=]+ ${ENV_FILTER_REGEX}=)('.*'|\".*\")$/\1\3/p" > "$tmpfile" || true
65 cd "$(dirname "$0")"
66 $_sudo mv "$tmpfile" env.sh
67 $_sudo chroot . /usr/bin/env -i su -l "$user" \
68 sh -c ". /etc/profile; . /env.sh; cd '$oldpwd' 2>/dev/null; \"\$@\"" \
69 -- "${@:-bash}"
70 EOF
71 }
72
73 ################################################################################
74
75 while getopts 'd:h' OPTION; do
76 case "$OPTION" in
77 d) CHROOT_DIR="$OPTARG";;
78 h) usage; exit 0;;
79 *) exit 1;;
80 esac
81 done
82
83 if [ "$(id -u)" -ne 0 ]; then
84 die "This script must be run as root!"
85 fi
86
87 [ -z "${CHROOT_DIR:-""}" ] && die "You must specify a chroot directory with -d";
88 [ -e "${CHROOT_DIR:-""}" ] && die "Something already exists at $CHROOT_DIR";
89
90 ################################################################################
91
92 IMAGE_URL="https://cloud-images.ubuntu.com/minimal/releases/eoan/release-20200129/ubuntu-19.10-minimal-cloudimg-amd64-root.tar.xz"
93 IMAGE_SHA="69d8e83fd17c6c8c5e014937aaeb19a35e3b30363f9daffa7decbe707bb8cfdf"
94 TEMP_DIR="/tmp/install-ubuntu-chroot"
95
96 download_file "$IMAGE_URL" "$IMAGE_SHA" "$TEMP_DIR"
97
98 mkdir -p "$CHROOT_DIR"
99
100 cd "$CHROOT_DIR"
101
102 tar -xvf "$TEMP_DIR/${IMAGE_URL##*/}"
103
104 einfo "Replacing chroot's /etc/resolv.conf"
105
106 rm -v -f etc/resolv.conf
107 cp -v /etc/resolv.conf etc/resolv.conf
108
109 einfo "Generating enter-chroot"
110
111 gen_chroot_script > enter-chroot
112 chmod +x enter-chroot
113
114 einfo "Mounting /proc and /dev/pts"
115
116 mount -v --bind /dev/pts dev/pts
117 mount -v --bind /proc proc
118
119 einfo 'Updating chroot system'
120
121 ./enter-chroot <<-EOF
122 apt -y update && apt -y upgrade
123 EOF
124
125 einfo "Unmounting /proc and /dev/pts"
126
127 umount -v proc
128 umount -v dev/pts
129
130 einfo 'Done!'