-
Notifications
You must be signed in to change notification settings - Fork 59
/
rfs.py
251 lines (197 loc) · 9.54 KB
/
rfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (C) 2013 Linutronix GmbH
#
# This file is part of ELBE.
#
# ELBE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ELBE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ELBE. If not, see <http://www.gnu.org/licenses/>.
import apt_pkg
import os
import sys
import urlparse
import elbepack
from elbepack.aptprogress import ElbeAcquireProgress, ElbeInstallProgress
from elbepack.efilesystem import BuildImgFs
from elbepack.templates import write_pack_template, get_preseed, preseed_to_text
from elbepack.shellhelper import CommandError
class DebootstrapException (Exception):
def __init__ (self):
Exception.__init__ (self, "Debootstrap Failed")
class BuildEnv ():
def __init__ (self, xml, log, path, build_sources=False ):
self.xml = xml
self.log = log
self.path = path
self.rfs = BuildImgFs (path, xml.defs["userinterpr"])
# TODO think about reinitialization if elbe_version differs
if not self.rfs.isfile( "etc/elbe_version" ):
self.rfs.rmtree("")
# avoid starting daemons inside the buildenv
self.rfs.mkdir_p ("usr/sbin")
self.rfs.write_file ("usr/sbin/policy-rc.d",
0755, "#!/bin/sh\nexit 101\n")
self.debootstrap ()
self.fresh_debootstrap = True
self.need_dumpdebootstrap = True
else:
print 'work on existing rfs'
self.fresh_debootstrap = False
self.need_dumpdebootstrap = False
self.initialize_dirs (build_sources=build_sources)
def cdrom_umount(self):
if self.xml.prj.has ("mirror/cdrom"):
cdrompath = self.rfs.fname( "cdrom" )
self.log.do ('umount "%s"' % cdrompath)
def cdrom_mount(self):
if self.xml.has("project/mirror/cdrom"):
cdrompath = self.rfs.fname("cdrom")
self.log.do( 'mkdir -p "%s"' % cdrompath )
self.log.do( 'mount -o loop "%s" "%s"'
% (self.xml.text("project/mirror/cdrom"), cdrompath ) )
def __enter__(self):
self.log.do("mv %s/../repo %s" % (self.path, self.path))
self.log.do('echo "deb file:///repo %s main" > %s/etc/apt/sources.list.d/local.list' % (
self.xml.text ("project/suite"), self.path))
self.log.do('echo "deb-src file:///repo %s main" >> %s/etc/apt/sources.list.d/local.list' % (
self.xml.text ("project/suite"), self.path))
self.cdrom_mount()
self.rfs.__enter__()
return self
def __exit__(self, type, value, traceback):
self.rfs.__exit__(type, value, traceback)
self.cdrom_umount()
self.log.do("mv %s/repo %s/../" % (self.path, self.path))
self.log.do("rm %s/etc/apt/sources.list.d/local.list" % self.path)
def debootstrap (self):
cleanup = False
suite = self.xml.prj.text ("suite")
primary_mirror = self.xml.get_primary_mirror(self.rfs.fname('/cdrom/targetrepo') )
if self.xml.prj.has("mirror/primary_proxy"):
os.environ["no_proxy"] = "10.0.2.2,localhost,127.0.0.1"
os.environ["http_proxy"] = self.xml.prj.text(
"mirror/primary_proxy")
os.environ["https_proxy"] = self.xml.prj.text(
"mirror/primary_proxy")
os.environ["LANG"] = "C"
os.environ["LANGUAGE"] = "C"
os.environ["LC_ALL"] = "C"
os.environ["DEBIAN_FRONTEND"]="noninteractive"
os.environ["DEBONF_NONINTERACTIVE_SEEN"]="true"
self.log.h2( "debootstrap log" )
self.create_apt_prefs ()
arch = self.xml.text ("project/buildimage/arch", key="arch")
host_arch = self.log.get_command_out(
"dpkg --print-architecture").strip ()
if not self.xml.is_cross (host_arch):
if self.xml.has("project/noauth"):
cmd = 'debootstrap --include=apt-listchanges --no-check-gpg --arch=%s "%s" "%s" "%s"' % (
arch, suite, self.rfs.path, primary_mirror)
else:
cmd = 'debootstrap --include=apt-listchanges --arch=%s "%s" "%s" "%s"' % (
arch, suite, self.rfs.path, primary_mirror)
try:
self.cdrom_mount()
self.log.do( cmd )
except CommandError:
cleanup = True
raise DebootstrapException ()
finally:
self.cdrom_umount()
if cleanup:
self.rfs.rmtree ("/")
return
if self.xml.has("project/noauth"):
cmd = 'debootstrap --no-check-gpg --foreign --arch=%s "%s" "%s" "%s"' % (
arch, suite, self.rfs.path, primary_mirror)
else:
cmd = 'debootstrap --foreign --arch=%s "%s" "%s" "%s"' % (
arch, suite, self.rfs.path, primary_mirror)
try:
self.cdrom_mount()
self.log.do (cmd)
ui = "/usr/share/elbe/qemu-elbe/" + self.xml.defs["userinterpr"]
if not os.path.exists (ui):
ui = "/usr/bin/" + self.xml.defs["userinterpr"]
self.log.do ('cp %s %s' % (ui, self.rfs.fname( "usr/bin" )))
self.log.chroot (self.rfs.path,
'/debootstrap/debootstrap --second-stage')
self.log.chroot (self.rfs.path, 'dpkg --configure -a')
except CommandError:
cleanup = True
raise DebootstrapException ()
finally:
self.cdrom_umount()
if cleanup:
self.rfs.rmtree ("/")
def virtapt_init_dirs(self):
self.rfs.mkdir_p ("/cache/archives/partial")
self.rfs.mkdir_p ("/etc/apt/preferences.d")
self.rfs.mkdir_p ("/db")
self.rfs.mkdir_p ("/log")
self.rfs.mkdir_p ("/state/lists/partial")
self.rfs.touch_file ("/state/status")
def initialize_dirs (self, build_sources=False):
mirror = self.xml.create_apt_sources_list (build_sources=build_sources)
if self.rfs.exists("etc/apt/sources.list"):
self.rfs.remove("etc/apt/sources.list")
self.rfs.write_file ("etc/apt/sources.list", 0644, mirror)
self.rfs.mkdir_p( "var/cache/elbe" )
preseed = get_preseed( self.xml )
preseed_txt = preseed_to_text( preseed )
self.rfs.write_file( "var/cache/elbe/preseed.txt", 0644, preseed_txt )
with self.rfs:
self.log.chroot( self.rfs.path, 'debconf-set-selections < %s' % self.rfs.fname("var/cache/elbe/preseed.txt") )
def create_apt_prefs (self):
filename = self.rfs.path + "/etc/apt/preferences"
if os.path.exists (filename):
os.remove (filename)
self.rfs.mkdir_p ("/etc/apt")
pinned_origins = []
if self.xml.has('project/mirror/url-list'):
for url in self.xml.node('project/mirror/url-list'):
if not url.has('binary'):
continue
repo = url.node('binary')
if not 'pin' in repo.et.attrib:
continue
origin = urlparse.urlsplit(repo.et.text.strip()).hostname
pin = repo.et.attrib['pin']
if 'package' in repo.et.attrib:
package = repo.et.attrib['package']
else:
package = '*'
pinning = { 'pin': pin,
'origin': origin,
'package': package }
pinned_origins.append(pinning)
d = { "xml": self.xml,
"prj": self.xml.node("/project"),
"pkgs": self.xml.node("/target/pkg-list"),
"porgs": pinned_origins }
write_pack_template( filename, "preferences.mako", d )
def seed_etc( self ):
passwd = self.xml.text("target/passwd")
self.log.chroot( self.rfs.path,
"""/bin/sh -c 'echo "%s\\n%s\\n" | passwd'""" % (passwd, passwd) )
hostname = self.xml.text("target/hostname")
domain = self.xml.text("target/domain")
self.log.chroot( self.rfs.path, """/bin/sh -c 'echo "127.0.0.1 %s.%s %s elbe-daemon" >> /etc/hosts'""" % (hostname, domain, hostname) )
self.log.chroot( self.rfs.path, """/bin/sh -c 'echo "%s" > /etc/hostname'""" % hostname )
self.log.chroot( self.rfs.path, """/bin/sh -c 'echo "%s.%s" > /etc/mailname'""" % (hostname, domain) )
if self.xml.has( "target/console" ):
serial_con, serial_baud = self.xml.text( "target/console" ).split(',')
if serial_baud:
self.log.chroot(self.rfs.path,"""/bin/sh -c '[ -f /etc/inittab ] && echo "T0:23:respawn:/sbin/getty -L %s %s vt100" >> /etc/inittab'""" % (serial_con, serial_baud), allow_fail=True )
self.log.chroot(self.rfs.path, """/bin/sh -c '[ -f /lib/systemd/system/serial-getty@.service ] && ln -s /lib/systemd/system/serial-getty@.service /etc/systemd/system/getty.target.wants/serial-getty@%s.service'""" % serial_con, allow_fail=True )
else:
self.log.printo("parsing console tag failed, needs to be of '/dev/ttyS0,115200' format.")