Makefile: Abolish `$(HERE)' as a bad idea.
[distorted-chroot] / bin / mkbuildchroot
CommitLineData
e36b4f25
MW
1#! /bin/sh -e
2###
3### Construct a fresh build-chroot base
4###
5### (c) 2018 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the distorted.org.uk chroot maintenance tools.
11###
12### distorted-chroot is free software: you can redistribute it and/or
13### modify it under the terms of the GNU General Public License as
14### published by the Free Software Foundation; either version 2 of the
15### License, or (at your option) any later version.
16###
17### distorted-chroot is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20### General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with distorted-chroot. If not, write to the Free Software
24### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25### USA.
26
27. state/config.sh # @@@config@@@
28
3e5b03e2
MW
29## Convert the PROXY configuration setting into something that will affect
30## `debootstrap'.
e36b4f25
MW
31case $PROXY in nil) ;; *) http_proxy=$PROXY; export PROXY ;; esac
32
3e5b03e2 33## Parse the command-line.
e36b4f25
MW
34badp=nil forcep=nil
35while getopts "f" opt; do
36 case $opt in
37 f) forcep=t ;;
38 *) badp=t ;;
39 esac
40done
41shift $(( $OPTIND - 1 ))
42case $# in 2) ;; *) badp=t ;; esac
43case $badp in t) echo >&2 "usage: $0 [-f] DIST ARCH"; exit 2 ;; esac
44d=$1 a=$2
45
46case $d-$a in *-*-*) echo >&2 "$0: bad chroot name \`$arg'"; exit 2 ;; esac
47if [ ! -d /dev/$VG/ ]; then echo >&2 "$0: no volume group \`$VG'"; exit 2; fi
48
3e5b03e2
MW
49## Decide whether we need to do special things for installing a foreign
50## architecture.
e36b4f25
MW
51qemup=nil dbsopts=
52for fa in $FOREIGN_ARCHS; do
53 case $fa in
54 "$a")
3e5b03e2
MW
55 qemup=t dbsopts=--foreign
56 eval qarch=\$${a}_QEMUARCH qhost=\$${a}_QEMUHOST
e36b4f25
MW
57 break
58 ;;
59 esac
60done
61
3e5b03e2 62## Construct the logical volume and lay a filesystem onto it.
e36b4f25 63lv=$LVPREFIX$d-$a
3e5b03e2 64mnt=$HERE/mnt/$lv
e36b4f25
MW
65mkdir -p $mnt
66if mountpoint -q $mnt; then umount $mnt; fi
67if [ -b /dev/$VG/$lv ]; then
68 case $forcep in
69 nil) echo >&2 "$0: volume \`$lv' already exists"; exit 2 ;;
70 t) lvremove -f $VG/$lv ;;
71 esac
72fi
73lvcreate --yes $LVSZ -n$lv $VG
74mkfs -j -L$d-$a /dev/$VG/$lv
75mount -orelatime,data=writeback,commit=3600,barrier=0 /dev/$VG/$lv $mnt/
76mkdir -m755 $mnt/fs/
77chmod 750 $mnt/
3e5b03e2
MW
78
79## Install the base system.
80want=$BASE_PACKAGES
3e5b03e2 81pkgs=; for p in $want; do pkgs=${pkgs:+$pkgs,}$p; done
e36b4f25
MW
82eatmydata debootstrap $dbsopts --arch=$a --variant=minbase \
83 --include=$pkgs $d $mnt/fs/ $DEBMIRROR
84
3e5b03e2
MW
85## If this is a cross-installation, then install the necessary `qemu' and
86## complete the installation.
e36b4f25
MW
87case $qemup in
88 t)
89 install $LOCAL/cross/$d-$qhost/QEMU/qemu-$qarch-static \
90 $mnt/fs/usr/bin/
91 chroot $mnt/fs/ /debootstrap/debootstrap --second-stage
92 ln -sf /usr/local.schroot/cross/$d-$qhost/QEMU/qemu-$qarch-static \
93 $mnt/fs/usr/bin/
94 ;;
95esac
96
3e5b03e2
MW
97## Set up `/usr/local'.
98rm -rf $mnt/fs/usr/local/; ln -s local.schroot/$a $mnt/fs/usr/local
e36b4f25 99
3e5b03e2
MW
100## Install the `apt' configuration.
101rm -rf $mnt/fs/etc/apt/apt.conf $mnt/fs/etc/apt/sources.list
102for c in $LOCAL/etc/apt/apt.conf.d/*; do
103 ln -s /usr/local.schroot/${c#$LOCAL/} $mnt/fs/etc/apt/apt.conf.d/
104done
105ln -s /usr/local.schroot/etc/apt/sources.$d $mnt/fs/etc/apt/sources.list
e36b4f25 106
3e5b03e2 107cat >$mnt/fs/etc/apt/apt.conf.d/20arch <<EOF
e36b4f25
MW
108### -*-conf-*-
109
110APT {
111 Architecture "$a";
112};
113EOF
114
3e5b03e2
MW
115## Set up the locale and time zone from the host system.
116cp /etc/locale.gen /etc/timezone $mnt/fs/etc/
117tz=$(cat /etc/timezone); ln -sf /usr/share/zoneinfo/$tz $mnt/fs/etc/localtime
118ln -sf /proc/mounts $mnt/fs/etc/mtab
e36b4f25 119
3e5b03e2 120cp /etc/default/locale $mnt/fs/etc/default/
e36b4f25 121
3e5b03e2
MW
122## Prevent daemons from starting within the chroot.
123cat >$mnt/fs/usr/sbin/policy-rc.d <<EOF
e36b4f25
MW
124#! /bin/sh
125echo >&2 "policy-rc.d: Services disabled by policy."
126exit 101
127EOF
3e5b03e2 128chmod +x $mnt/fs/usr/sbin/policy-rc.d
e36b4f25 129
3e5b03e2
MW
130## Hack the dynamic linker to prefer libraries in `/usr' over `/usr/local'.
131cat >$mnt/fs/etc/ld.so.conf.d/libc.conf <<EOF
e36b4f25
MW
132# libc default configuration
133EOF
3e5b03e2 134cat >$mnt/fs/etc/ld.so.conf.d/zzz-local.conf <<EOF
e36b4f25
MW
135### -*-conf-*-
136### Local hack to make /usr/local/ late.
137/usr/local/lib
138EOF
139
3e5b03e2 140## We're done setting the chroot environment up.
e36b4f25
MW
141umount $mnt/
142
3e5b03e2 143## If this is a foreign architecture then we need to set it up.
e36b4f25
MW
144case $qemup in
145 t)
3e5b03e2
MW
146 ## Keep the chroot's native Qemu out of our way: otherwise we'll stop
147 ## being able to run programs in the chroot. There's a hack here because
148 ## the `--no-rename' option was required in the same version in which is
149 ## was introduced, so there's no single incantation that will work across
150 ## the boundary.
944cdc82 151 schroot -uroot -csource:$lv -- eatmydata sh -e -c "
e36b4f25
MW
152 if dpkg-divert >/dev/null 2>&1 --no-rename --help
153 then no_rename=--no-rename
154 else no_rename=
155 fi
156
157 dpkg-divert --package install-cross-tools \$no_rename \
3e5b03e2
MW
158 --divert /usr/bin/qemu-$qarch-static.$a --add /usr/bin/qemu-$qarch-static"
159
160 ## Install faster native tools.
e36b4f25 161 $STATE/bin/install-cross-tools $d $a
e36b4f25
MW
162esac
163
a98c9dba
MW
164## Install extra packages now that everything should go fairly quickly.
165want=$EXTRA_PACKAGES
166pkgs=; for p in $want; do pkgs=${pkgs:+$pkgs,}$p; done
167schroot -uroot -csource:$lv -- eatmydata apt-get -y install $pkgs
168
3e5b03e2 169## Set the chroot's package state up properly.
944cdc82 170schroot -uroot -csource:$lv -- eatmydata sh -e -c '
e36b4f25
MW
171 apt-get update
172 apt-get -y upgrade
173 locale-gen
174 ldconfig
175 apt-get -y autoremove
176 apt-get clean'
177
178###----- That's all, folks --------------------------------------------------