]> spindle.queued.net Git - xodist/blob - mkchroot.sh
mkchroot.sh: break out non-olpc stuff
[xodist] / mkchroot.sh
1 #!/bin/bash -e
2 #
3 # Copyright © 2008-2009  Andres Salomon <dilinger@collabora.co.uk>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 . ./functions.sh
20
21 # load & set default values
22 CONFIG_TYPE=generic
23 . ./configs/${CONFIG_TYPE}/variables
24 . ./configs/${CONFIG_TYPE}/hooks
25 BASE_PLIST="./configs/${CONFIG_TYPE}/packages"
26 LOCAL_APT_MIRROR=
27
28 usage()
29 {
30         echo "" 1>&2
31         echo "Usage: $0 [<options>] <root directory>" 1>&2
32         echo "" 1>&2
33         echo "Options:" 1>&2
34         echo "  --config-type <config>    directory name in configs/ to use" 1>&2
35         echo "  --local-apt-mirror <srcs> sources.list for local mirror" 1>&2
36         echo "" 1>&2
37         exit 1
38 }
39
40 while test $# != 0
41 do
42         case $1 in
43         --config-type)
44                 CONFIG_TYPE=$2
45                 [ -d ./configs/${CONFIG_TYPE} ] || {
46                         echo "Error: can't find directory './configs/${CONFIG_TYPE}/'!" 1>&2
47                         exit 2
48                 }
49                 shift
50                 ;;
51         --local-apt-mirror)
52                 LOCAL_APT_MIRROR="$2"
53                 shift
54                 ;;
55         *)
56                 if [ "$#" != "1" ]; then
57                         echo "Unknown option $1" 1>&2
58                         usage
59                 else
60                         ROOT_DIR="$1"
61                 fi
62                 ;;
63         esac
64         shift
65 done
66
67 if [ "$ROOT_DIR" = "" ]; then
68         echo "" 1>&2
69         echo "*** No root directory specified!" 1>&2
70         usage
71 fi
72
73 if [ -d "${ROOT_DIR}" ]; then
74         echo "" 1>&2
75         echo "*** ${ROOT_DIR} already exists!" 1>&2
76         usage
77 fi
78
79 if [ "$UID" != "0" ]; then
80         echo "" 1>&2
81         echo "*** $0 must be run with root privs!" 1>&2
82         exit 1
83 fi
84
85 start_logging $ROOT_DIR
86
87 # load config-specific values
88 . ./configs/${CONFIG_TYPE}/variables
89 . ./configs/${CONFIG_TYPE}/hooks
90 PLIST="./configs/${CONFIG_TYPE}/packages"
91
92 if [ -z "${LOCAL_APT_MIRROR}" ]; then
93     LOCAL_APT_MIRROR="${APT_SOURCES}"
94 fi
95
96 # parse apt mirror
97 MIRROR=$(printf "${LOCAL_APT_MIRROR}\n" | awk '/deb /{print $2}' | head -n1)
98 DIST=$(printf "${LOCAL_APT_MIRROR}\n" | awk '/deb /{print $3}' | head -n1)
99
100 # create chroot
101 debootstrap --arch ${IMG_ARCH} ${DIST} ${ROOT_DIR} ${MIRROR}
102 mount -t proc proc ${ROOT_DIR}/proc
103 mount -t devpts devpts ${ROOT_DIR}/dev/pts
104
105 # allow daemons to be installed without breaking
106 mv ${ROOT_DIR}/sbin/start-stop-daemon ${ROOT_DIR}/sbin/start-stop-daemon.REAL
107 cat >${ROOT_DIR}/sbin/start-stop-daemon<<EOF
108 #!/bin/sh
109 echo
110 echo "Warning: Fake start-stop-daemon called, doing nothing"
111 EOF
112 chmod 755 ${ROOT_DIR}/sbin/start-stop-daemon
113
114 # set up hostname stuff
115 echo "${IMG_HOSTNAME}" > ${ROOT_DIR}/etc/hostname
116 cat >${ROOT_DIR}/etc/hosts<<EOF
117 127.0.0.1 localhost.localdomain localhost
118 127.0.0.1 ${IMG_HOSTNAME}
119
120 # The following lines are desirable for IPv6 capable hosts
121 ::1     ip6-localhost ip6-loopback
122 fe00::0 ip6-localnet
123 ff00::0 ip6-mcastprefix
124 ff02::1 ip6-allnodes
125 ff02::2 ip6-allrouters
126 ff02::3 ip6-allhosts
127 EOF
128
129 # add local network interface
130 cat >>${ROOT_DIR}/etc/network/interfaces<<EOF
131
132 auto lo
133 iface lo inet loopback
134 EOF
135
136 # set the default locale
137 echo "${IMG_LOCALE}" >${ROOT_DIR}/etc/locale.gen
138
139 # run any customizations necessary pre-package install
140 customize_chroot_hook "$ROOT_DIR"
141
142 # initialize apt
143 export DEBIAN_FRONTEND=noninteractive
144 export DEBCONF_PRIORITY=critical
145 printf "${LOCAL_APT_MIRROR}\n" >${ROOT_DIR}/etc/apt/sources.list
146 (chroot ${ROOT_DIR} aptitude update)
147
148 # generic packages are always installed
149 (chroot ${ROOT_DIR} aptitude install -y `grep --invert-match '^#' ${BASE_PLIST}`)
150
151 # install the rest of the packages
152 (chroot ${ROOT_DIR} aptitude install -y `grep --invert-match '^#' ${PLIST}`)
153
154 # post-install customization hook
155 package_configure_hook "${ROOT_DIR}"
156
157 # add default user
158 (chroot ${ROOT_DIR} passwd -l root)
159 rm -rf ${ROOT_DIR}/home/*;      # i have no idea what's adding this crap...
160 (chroot ${ROOT_DIR} useradd -s /bin/bash --create-home ${DEFUSER})
161 (chroot ${ROOT_DIR} passwd -d ${DEFUSER})
162 (chroot ${ROOT_DIR} adduser ${DEFUSER} cdrom)
163 (chroot ${ROOT_DIR} adduser ${DEFUSER} audio)
164 (chroot ${ROOT_DIR} adduser ${DEFUSER} video)
165 (chroot ${ROOT_DIR} adduser ${DEFUSER} plugdev)
166 (chroot ${ROOT_DIR} adduser ${DEFUSER} netdev)
167 (chroot ${ROOT_DIR} adduser ${DEFUSER} powerdev)
168 (chroot ${ROOT_DIR} adduser ${DEFUSER} floppy)
169 echo "${DEFUSER} ALL=(ALL) NOPASSWD: ALL" >> ${ROOT_DIR}/etc/sudoers
170
171 # override sources.list with shipping version
172 printf "${APT_SOURCES}\n" >${ROOT_DIR}/etc/apt/sources.list
173 (chroot ${ROOT_DIR} aptitude update)
174
175 # done, clean up
176 mv ${ROOT_DIR}/sbin/start-stop-daemon.REAL ${ROOT_DIR}/sbin/start-stop-daemon
177 (chroot ${ROOT_DIR} aptitude clean)
178 umount ${ROOT_DIR}/proc
179 umount ${ROOT_DIR}/dev/pts
180
181 # custom cleanup stuff
182 cleanup_chroot_hook "${ROOT_DIR}"