More work in progress.
[distorted-chroot] / bin / mkbuildchroot
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
29 ## Convert the PROXY configuration setting into something that will affect
30 ## `debootstrap'.
31 case $PROXY in nil) ;; *) http_proxy=$PROXY; export PROXY ;; esac
32
33 ## Parse the command-line.
34 badp=nil forcep=nil
35 while getopts "f" opt; do
36 case $opt in
37 f) forcep=t ;;
38 *) badp=t ;;
39 esac
40 done
41 shift $(( $OPTIND - 1 ))
42 case $# in 2) ;; *) badp=t ;; esac
43 case $badp in t) echo >&2 "usage: $0 [-f] DIST ARCH"; exit 2 ;; esac
44 d=$1 a=$2
45
46 case $d-$a in *-*-*) echo >&2 "$0: bad chroot name \`$arg'"; exit 2 ;; esac
47 if [ ! -d /dev/$VG/ ]; then echo >&2 "$0: no volume group \`$VG'"; exit 2; fi
48
49 ## Decide whether we need to do special things for installing a foreign
50 ## architecture.
51 qemup=nil dbsopts=
52 for fa in $FOREIGN_ARCHS; do
53 case $fa in
54 "$a")
55 qemup=t dbsopts=--foreign
56 eval qarch=\$${a}_QEMUARCH qhost=\$${a}_QEMUHOST
57 break
58 ;;
59 esac
60 done
61
62 ## Construct the logical volume and lay a filesystem onto it.
63 lv=$LVPREFIX$d-$a
64 mnt=$HERE/mnt/$lv
65 mkdir -p $mnt
66 if mountpoint -q $mnt; then umount $mnt; fi
67 if [ -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
72 fi
73 lvcreate --yes $LVSZ -n$lv $VG
74 mkfs -j -L$d-$a /dev/$VG/$lv
75 mount -orelatime,data=writeback,commit=3600,barrier=0 /dev/$VG/$lv $mnt/
76 mkdir -m755 $mnt/fs/
77 chmod 750 $mnt/
78
79 ## Install the base system.
80 want=$BASE_PACKAGES
81 case $qemup in
82 t) want="$want $FOREIGN_BASE_PACKAGES" ;;
83 nil) want="$want $NATIVE_BASE_PACKAGES" ;;
84 esac
85 pkgs=; for p in $want; do pkgs=${pkgs:+$pkgs,}$p; done
86 eatmydata debootstrap $dbsopts --arch=$a --variant=minbase \
87 --include=$pkgs $d $mnt/fs/ $DEBMIRROR
88
89 ## If this is a cross-installation, then install the necessary `qemu' and
90 ## complete the installation.
91 case $qemup in
92 t)
93 install $LOCAL/cross/$d-$qhost/QEMU/qemu-$qarch-static \
94 $mnt/fs/usr/bin/
95 chroot $mnt/fs/ /debootstrap/debootstrap --second-stage
96 ln -sf /usr/local.schroot/cross/$d-$qhost/QEMU/qemu-$qarch-static \
97 $mnt/fs/usr/bin/
98 ;;
99 esac
100
101 ## Set up `/usr/local'.
102 rm -rf $mnt/fs/usr/local/; ln -s local.schroot/$a $mnt/fs/usr/local
103
104 ## Install the `apt' configuration.
105 rm -rf $mnt/fs/etc/apt/apt.conf $mnt/fs/etc/apt/sources.list
106 for c in $LOCAL/etc/apt/apt.conf.d/*; do
107 ln -s /usr/local.schroot/${c#$LOCAL/} $mnt/fs/etc/apt/apt.conf.d/
108 done
109 ln -s /usr/local.schroot/etc/apt/sources.$d $mnt/fs/etc/apt/sources.list
110
111 cat >$mnt/fs/etc/apt/apt.conf.d/20arch <<EOF
112 ### -*-conf-*-
113
114 APT {
115 Architecture "$a";
116 };
117 EOF
118
119 ## Set up the locale and time zone from the host system.
120 cp /etc/locale.gen /etc/timezone $mnt/fs/etc/
121 tz=$(cat /etc/timezone); ln -sf /usr/share/zoneinfo/$tz $mnt/fs/etc/localtime
122 ln -sf /proc/mounts $mnt/fs/etc/mtab
123
124 cp /etc/default/locale $mnt/fs/etc/default/
125
126 ## Prevent daemons from starting within the chroot.
127 cat >$mnt/fs/usr/sbin/policy-rc.d <<EOF
128 #! /bin/sh
129 echo >&2 "policy-rc.d: Services disabled by policy."
130 exit 101
131 EOF
132 chmod +x $mnt/fs/usr/sbin/policy-rc.d
133
134 ## Hack the dynamic linker to prefer libraries in `/usr' over `/usr/local'.
135 cat >$mnt/fs/etc/ld.so.conf.d/libc.conf <<EOF
136 # libc default configuration
137 EOF
138 cat >$mnt/fs/etc/ld.so.conf.d/zzz-local.conf <<EOF
139 ### -*-conf-*-
140 ### Local hack to make /usr/local/ late.
141 /usr/local/lib
142 EOF
143
144 ## We're done setting the chroot environment up.
145 umount $mnt/
146
147 ## If this is a foreign architecture then we need to set it up.
148 case $qemup in
149 t)
150 ## Keep the chroot's native Qemu out of our way: otherwise we'll stop
151 ## being able to run programs in the chroot. There's a hack here because
152 ## the `--no-rename' option was required in the same version in which is
153 ## was introduced, so there's no single incantation that will work across
154 ## the boundary.
155 schroot -uroot -csource:$LVPREFIX$d-$a -- eatmydata sh -e -c "
156 if dpkg-divert >/dev/null 2>&1 --no-rename --help
157 then no_rename=--no-rename
158 else no_rename=
159 fi
160
161 dpkg-divert --package install-cross-tools \$no_rename \
162 --divert /usr/bin/qemu-$qarch-static.$a --add /usr/bin/qemu-$qarch-static"
163
164 ## Install faster native tools.
165 $STATE/bin/install-cross-tools $d $a
166
167 ## Install `build-essential', which had been delayed from earlier.
168 schroot -uroot -csource:$LVPREFIX$d-$a -- \
169 eatmydata apt-get -y install build-essential
170 ;;
171 esac
172
173 ## Set the chroot's package state up properly.
174 schroot -uroot -csource:$LVPREFIX$d-$a -- eatmydata sh -e -c '
175 apt-get update
176 apt-get -y upgrade
177 locale-gen
178 ldconfig
179 apt-get -y autoremove
180 apt-get clean'
181
182 ###----- That's all, folks --------------------------------------------------