~xubuntu-dev/ubiquity/lp1437180_feh

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
#!/usr/bin/python

# Last modified by Antonio Olmo Titos <aolmo@emergya.info> on 26 july 2005.

import sys
import shutil
import os
import subprocess
import debconf

class CommandFailed(Exception): pass

class Installer:
    source = '/source'
    target = '/target'

    def frontend(self, type='noui'):
      ui = getattr(__import__('frontend.%s' % type), type)

      self.wizard = ui.Wizard()
      hostname = self.wizard.get_hostname()
      timezone, keymap, locales    = self.wizard.get_locales()
      fullname, username, password = self.wizard.get_user()

      self.install(timezone,keymap,locales,username,password,fullname,hostname)

    def install(self, timezone, keymap, locales, username, password, fullname, hostname):
	mountpoints = self.wizard.get_partitions()
        self.mount_target(mountpoints)
        self.mount_source()
        try:
            self.copy_all()
        finally:
            self.unmount_source()

        try:
            self.configure_fstab(mountpoints)
            self.configure_timezone(timezone)
            self.configure_keymap(keymap)
            self.configure_user(username, password, fullname)
            self.configure_hostname(hostname)
            self.configure_network()
            self.configure_hardware()
        finally:
            self.unmount_target(mountpoints)


    def configure_fstab(self, mountpoints):
        fstab = open(os.path.join(self.target,'etc/fstab'), 'w')
        for path, device in mountpoints.items():
            if path == '/':
                passno = 1
            else:
                passno = 2

            filesystem = 'ext3'
            options = 'defaults'
            
            print >>fstab, '%s\t%s\t%s\t%s\t%d\t%d' % (device, path, filesystem, options, 0, passno)
        fstab.close()

    def configure_timezone(self, timezone):
        # tzsetup ignores us if these exist
        for tzfile in ('etc/timezone', 'etc/localtime'):
            path = os.path.join(self.target, tzfile)
            if os.path.exists(path):
                os.unlink(path)

        self.set_debconf('base-config', 'tzconfig/preseed_zone', timezone)
        self.chrex('tzsetup', '-y')

    def configure_keymap(self, keymap):
        self.set_debconf('debian-installer', 'debian-installer/keymap', keymap)
        self.chrex('install-keymap', keymap)

    def configure_user(self, username, password, fullname):
        self.chrex('passwd', '-l', 'root')
        self.set_debconf('passwd', 'passwd/username', username)
        self.set_debconf('passwd', 'passwd/user-fullname', fullname)
        self.set_debconf('passwd', 'passwd/user-password', password)
        self.set_debconf('passwd', 'passwd/user-password-again', password)
        self.reconfigure('passwd')

    def configure_hostname(self, hostname):
        fp = open(os.path.join(self.target, 'etc/hostname'), 'w')
        print >>fp, hostname
        fp.close()

    def configure_hardware(self):
        self.chrex('mount', '-t', 'proc', 'proc', '/proc')
        self.chrex('mount', '-t', 'sysfs', 'sysfs', '/sys')

        kernel_version = open('/proc/sys/kernel/osrelease').readline().strip()
        packages = ['gnome-panel', 'xserver-xorg', 'linux-image-' + kernel_version]
        
        try:
            for package in packages:
                self.copy_debconf(package)
                self.reconfigure(package)
        finally:
            self.chrex('umount', '/proc')
            self.chrex('umount', '/sys')

    def configure_network(self):
        shutil.copyfile('/etc/network/interfaces',
                        os.path.join(self.target, 'etc/network/interfaces'))
    
    def mount_target(self, mountpoints):
        os.mkdir(self.target)
        self.ex('mount', mountpoints['/'], self.target)

        for path, device in mountpoints.items():
            if path in ('/', 'swap'):
                continue
            path = os.path.join(self.target, path[1:])
            os.mkdir(path)
            self.ex('mount', device, path)

    def unmount_target(self, mountpoints):
        for path, device in mountpoints.items():
            if path in ('/', 'swap'):
                continue
            path = os.path.join(self.target, path[1:])
            self.ex('umount', path)
        self.ex('umount', self.target)

    def copy_all(self):
        files = []
        total_size = 0
        
        for dirpath, dirnames, filenames in os.walk(self.source):
            sourcepath = dirpath[len(self.source)+1:]

            for name in dirnames + filenames:
                relpath = os.path.join(sourcepath, name)
                fqpath = os.path.join(self.source, dirpath, name)

                if os.path.isfile(fqpath):
		    size = os.path.getsize(fqpath)
		    total_size += size	
                    files.append((relpath, size))
                else:
                    files.append((relpath, None))

        copy = subprocess.Popen(['cpio', '-d0mp', self.target],
                                cwd=self.source,
                                stdin=subprocess.PIPE)

        copied_bytes = 0
        for path, size in files:
            copy.stdin.write(path + '\0')
            if size is not None:
                copied_bytes += size
            per = (copied_bytes * 100) / total_size
            self.wizard.set_progress(per)

        copy.stdin.close()
        copy.wait()
        

    def mount_source(self):
	from os import path
	files = ['/cdrom/casper/filesystem.cloop', '/cdrom/META/META.squashfs']
	for f in files:
		if path.isfile(f) and path.splitext(f)[1] == '.cloop':
			file = f
			self.dev = '/dev/cloop1'
		elif path.isfile(f) and path.splitext(f)[1] == '.squashfs':
			file = f
			self.dev = '/dev/loop3'
		else:
                    return -1

        self.ex('losetup', self.dev, file)
        os.mkdir(self.source)
        self.ex('mount', self.dev, self.source)
	return 0

    def unmount_source(self):
        self.ex('umount', self.source)
        self.ex('losetup', '-d', self.dev)

    def ex(self, *args):
        status = subprocess.call(args)
        if status != 0:
            raise CommandFailed(str(args))

    def chrex(self, *args):
        self.ex('chroot', self.target, *args)

    def copy_debconf(self, package):
        targetdb = os.path.join(self.target, 'var/cache/debconf/config.dat')
        self.ex('debconf-copydb', 'configdb', 'targetdb', '-p', '^%s/' % package,
                '--config=Name:targetdb', '--config=Driver:File','--config=Filename:' + targetdb)

    def set_debconf(self, owner, question, value):
        dccomm = subprocess.Popen(['chroot', self.target, 'debconf-communicate', '-fnoninteractive', owner],
                                  stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
        dc = debconf.Debconf(read=dccomm.stdout, write=dccomm.stdin)
        dc.set(question, value)
        dc.fset(question, 'seen', 'true')
        dccomm.stdin.close()
        dccomm.wait()

    def reconfigure(self, package):
        self.chrex('dpkg-reconfigure', '-fnoninteractive', package)

if __name__ == '__main__':
    Installer().frontend()

    # What to do when no arguments are provided:
    if len (sys.argv) < 2:
        sys.stderr.write ('%s: no arguments provided.\n' % sys.argv [0])
        sys.exit (2)
    else:
        Installer().install({'/' : sys.argv[1]}, 'US/Pacific', 'dvorak', 'mdz', 'mdz2', 'Matt Zimmerman', 'ubuntu')

# vim:ai:et:sts=4:tw=80:sw=4: