40f925bacd13474a9097b99afe919032d60d8488
[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 case $PROXY in nil) ;; *) http_proxy=$PROXY; export PROXY ;; esac
30
31 badp=nil forcep=nil
32 while getopts "f" opt; do
33 case $opt in
34 f) forcep=t ;;
35 *) badp=t ;;
36 esac
37 done
38 shift $(( $OPTIND - 1 ))
39 case $# in 2) ;; *) badp=t ;; esac
40 case $badp in t) echo >&2 "usage: $0 [-f] DIST ARCH"; exit 2 ;; esac
41 d=$1 a=$2
42
43 case $d-$a in *-*-*) echo >&2 "$0: bad chroot name \`$arg'"; exit 2 ;; esac
44 if [ ! -d /dev/$VG/ ]; then echo >&2 "$0: no volume group \`$VG'"; exit 2; fi
45
46 qemup=nil dbsopts=
47 for fa in $FOREIGN_ARCHS; do
48 case $fa in
49 "$a")
50 qemup=t qbsopts=--foreign
51 eval qhost=\$${a}_QEMUARCH qhost=\$${a}_QEMUHOST
52 break
53 ;;
54 esac
55 done
56
57 lv=$LVPREFIX$d-$a
58 mnt=mnt/$lv
59 mkdir -p $mnt
60 if mountpoint -q $mnt; then umount $mnt; fi
61 if [ -b /dev/$VG/$lv ]; then
62 case $forcep in
63 nil) echo >&2 "$0: volume \`$lv' already exists"; exit 2 ;;
64 t) lvremove -f $VG/$lv ;;
65 esac
66 fi
67 lvcreate --yes $LVSZ -n$lv $VG
68 mkfs -j -L$d-$a /dev/$VG/$lv
69 mount -orelatime,data=writeback,commit=3600,barrier=0 /dev/$VG/$lv $mnt/
70 mkdir -m755 $mnt/fs/
71 chmod 750 $mnt/
72 pkgs=ccache,eatmydata,fakeroot,libfile-fcntllock-perl,locales,tzdata
73 eatmydata debootstrap $dbsopts --arch=$a --variant=minbase \
74 --include=$pkgs $d $mnt/fs/ $DEBMIRROR
75
76 case $qemup in
77 t)
78 install $LOCAL/cross/$d-$qhost/QEMU/qemu-$qarch-static \
79 $mnt/fs/usr/bin/
80 chroot $mnt/fs/ /debootstrap/debootstrap --second-stage
81 ln -sf /usr/local.schroot/cross/$d-$qhost/QEMU/qemu-$qarch-static \
82 $mnt/fs/usr/bin/
83 ;;
84 esac
85
86 cd $mnt/fs/usr/
87 rm -rf local/; ln -s local.schroot/$a local
88
89 cd $mnt/fs/etc/apt/
90 rm -rf apt.conf sources.list
91 ln -s /usr/local.schroot/config/apt/conf.d/10sbuild apt.conf.d/
92 ln -s /usr/local.schroot/config/apt/conf.d/90local apt.conf.d/
93 ln -s /usr/local.schroot/config/apt/sources.$d sources.list
94
95 cat >apt.conf.d/20arch <<EOF
96 ### -*-conf-*-
97
98 APT {
99 Architecture "$a";
100 };
101 EOF
102
103 cd $mnt/fs/etc/
104 cp /etc/locale.gen /etc/timezone ./
105 tz=$(cat timezone); ln -sf /usr/share/zoneinfo/$tz localtime
106 ln -sf /proc/mounts mtab
107
108 cd $mnt/fs/etc/default/
109 cp /etc/default/locale .
110
111 cd $mnt/fs/usr/sbin/
112 cat >policy-rc.d <<EOF
113 #! /bin/sh
114 echo >&2 "policy-rc.d: Services disabled by policy."
115 exit 101
116 EOF
117 chmod +x policy-rc.d
118
119 cd $mnt/fs/etc/ld.so.conf.d/
120 cat >libc.conf <<EOF
121 # libc default configuration
122 EOF
123 cat >zzz-local.conf <<EOF
124 ### -*-conf-*-
125 ### Local hack to make /usr/local/ late.
126 /usr/local/lib
127 EOF
128
129 cd /
130 umount $mnt/
131
132 case $qemup in
133 t)
134 schroot -uroot -csource:$LVPREFIX$d-$a -- eatmydata sh -e -c "
135 if dpkg-divert >/dev/null 2>&1 --no-rename --help
136 then no_rename=--no-rename
137 else no_rename=
138 fi
139
140 dpkg-divert --package install-cross-tools \$no_rename \
141 --divert /usr/bin/$qemu.$a --add /usr/bin/$qemu"
142 $STATE/bin/install-cross-tools $d $a
143 ;;
144 esac
145
146 schroot -uroot -csource:$LVPREFIX$d-$a -- eatmydata sh -e -c '
147 apt-get update
148 apt-get -y upgrade
149 locale-gen
150 ldconfig
151 apt-get -y autoremove
152 apt-get clean'
153
154 ###----- That's all, folks --------------------------------------------------