-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup
More file actions
240 lines (232 loc) · 8.5 KB
/
setup
File metadata and controls
240 lines (232 loc) · 8.5 KB
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
#!/bin/python2
import glob, gtk, os, re, stat, subprocess, sys
def execGetOutput(cmd, withError = False, shell = True, env = {'LANG' : 'en_US'}):
"""
Executes a command and return its output in a list, line by line.
In case of error, it returns a subprocess.CalledProcessorError exception.
The command is executed by default in a /bin/sh shell with en_US locale.
"""
stdErr = None
if withError:
stdErr = subprocess.STDOUT
if sys.version_info[0] > 2 or (sys.version_info[0] == 2 and sys.version_info[1] >= 7): # ver >= 2.7
return subprocess.check_output(cmd, stderr = stdErr, shell = shell, env = env).splitlines()
else:
wrappedCmd = []
if shell:
wrappedCmd.append('sh')
wrappedCmd.append('-c')
if isinstance(cmd, list):
wrappedCmd.append(' '.join(cmd))
else:
wrappedCmd.append(cmd)
else:
if isinstance(cmd, list):
wrappedCmd = cmd
else:
wrappedCmd.append(cmd)
p = subprocess.Popen(wrappedCmd, stdout = subprocess.PIPE, stderr = stdErr)
output = p.communicate()[0]
if p.returncode == 0:
return output.splitlines()
else:
raise subprocess.CalledProcessError(returncode = p.returncode, cmd = cmd)
def getFsType(partitionDevice):
"""
Returns the file system type for that partition.
'partitionDevice' should no be prefixed with '/dev/' if it's a block device.
It can be a full path if the partition is contained in a file.
Returns 'Extended' if the partition is an extended partition and has no filesystem.
"""
if os.path.exists('/dev/{0}'.format(partitionDevice)) and stat.S_ISBLK(os.stat('/dev/{0}'.format(partitionDevice)).st_mode):
path = '/dev/{0}'.format(partitionDevice)
elif os.path.isfile(partitionDevice):
path = partitionDevice
else:
fstype = False
path = False
if path:
try:
fstype = execGetOutput(['/sbin/blkid', '-s', 'TYPE', '-o', 'value', path], shell = False)
if fstype:
fstype = fstype[0]
else:
fstype = False
except subprocess.CalledProcessError as e:
fstype = False
if not fstype:
# is it a real error or is it an extended partition?
try:
filetype = execGetOutput(['/usr/bin/file', '-s', path], shell = False)
if 'extended partition table' in filetype:
fstype = 'Extended'
except subprocess.CalledProcessError:
pass
return fstype
def getPartitions(diskDevice):
"""
Returns partitions matching exclusion filters.
"""
if stat.S_ISBLK(os.stat('/dev/{0}'.format(diskDevice)).st_mode):
parts = [p.replace('/sys/block/{0}/'.format(diskDevice), '') for p in glob.glob('/sys/block/{0}/{0}*'.format(diskDevice))]
fsexclude = [False, 'Extended']
return [part for part in parts if getFsType(part) not in fsexclude]
else:
return None
disks = []
for l in open('/proc/partitions', 'r').read().splitlines():
if re.search(r' sd[^0-9]+$', l):
disks.append(re.sub(r'.*(sd.*)', r'\1', l))
partitions = []
for d in disks:
partitions.append(getPartitions(d))
mount_dialog = gtk.Dialog(flags = gtk.DIALOG_MODAL, buttons = (gtk.STOCK_OK, gtk.RESPONSE_OK))
for x in partitions:
for y in x:
hbox = gtk.HBox()
label = gtk.Label(y)
entry = gtk.Entry()
hbox.pack_start(label)
hbox.pack_start(entry)
mount_dialog.vbox.pack_start(hbox)
hbox = gtk.HBox()
label = gtk.Label("Install bootloader to:")
entry = gtk.Entry()
entry.set_text("/dev/sda")
hbox.pack_start(label)
hbox.pack_start(entry)
mount_dialog.vbox.pack_start(hbox)
hbox = gtk.HBox()
label = gtk.Label("Username:")
entry = gtk.Entry()
hbox.pack_start(label)
hbox.pack_start(entry)
mount_dialog.vbox.pack_start(hbox)
mount_dialog.show_all()
if mount_dialog.run() != gtk.RESPONSE_OK:
sys.exit(1)
mount_dialog.hide()
has_root = False
for x in mount_dialog.vbox.get_children():
for y in x.get_children():
if type(y) is gtk.Entry:
if y.get_text() == "/":
has_root = True
break
if not has_root:
print "Need '/' mountpoint"
sys.exit(1)
mountpoints = {}
for x in mount_dialog.vbox.get_children():
if type(x) is gtk.HButtonBox:
continue
elif type(y) is gtk.Separator:
continue
for y in x.get_children():
if type(y) is gtk.Label:
m1 = y.get_text()
elif type(y) is gtk.Entry:
m2 = y.get_text()
else:
print "Internal error"
sys.exit(1)
if m1[:5] == "/dev/":
if m2[:1] == "/":
mountpoints[m1] = m2
elif m2 == "swap":
mountpoints[m1] = m2
elif m2 == "":
continue
else:
print "Invalid mountpoint: '%s'" % m2
sys.exit(1)
elif m1 == "Install bootloader to:":
if m2[:5] != "/dev/":
sys.exit(1)
bootldr = m2
elif m1 == "Username:":
if m2 == "":
print "Empty username not allowed"
sys.exit(1)
username = m2
else:
print "Internal error"
sys.exit(1)
DEST = '/mnt/dest'
if subprocess.call(['install', '-d', DEST]) == 1:
sys.exit(1)
print 'Installing...'
for m in mountpoints:
if mountpoints[m] != "swap":
subprocess.call(["install", "-d", DEST + mountpoints[m]])
subprocess.call(["mount", m, DEST + mountpoints[m]])
subprocess.call(['unsquashfs', '-f', '-d', DEST, '/mnt/cdrom/image.squashfs'])
subprocess.call(['post_unpack', DEST])
subprocess.call(['mount', '-B', '/proc', DEST + '/proc'])
subprocess.call(['touch', DEST + '/etc/resolv.conf'])
subprocess.call(['mount', '-B', '/etc/resolv.conf', DEST + '/etc/resolv.conf'])
fstab = open(DEST + "/etc/fstab", "w")
print >>fstab, """
# /etc/fstab: static file system information.
#
# noatime turns off atimes for increased performance (atimes normally aren't
# needed); notail increases performance of ReiserFS (at the expense of storage
# efficiency). It's safe to drop the noatime options if you want and to
# switch between notail / tail freely.
#
# The root filesystem should have a pass number of either 0 or 1.
# All other filesystems should have a pass number of 0 or greater than 1.
#
# See the manpage fstab(5) for more information.
#
# <fs> <mountpoint> <type> <opts> <dump/pass>
# NOTE: If your BOOT partition is ReiserFS, add the notail option to opts.
"""
for x in mountpoints:
if mountpoints[m] == "/":
print >>fstab, m + " / " + getFsType(m) + " noatime 0 1"
elif mountpoints[m] != "swap":
print >>fstab, m + " " + mountpoints[m] + " " + getFsType(m) + " noatime 1 2"
else:
print >>fstab, m + " none swap sw 0 0"
print >>fstab, "/dev/cdrom /mnt/cdrom auto noauto,ro 0 0"
print >>fstab, "/dev/fd0 /mnt/floppy auto noauto 0 0"
print >>fstab, ""
fstab.close()
repos_conf = open(DEST + "/etc/portage/repos.conf", "w")
print >>repos_conf, '[DEFAULT]'
print >>repos_conf, 'main-repo = gentoo'
print >>repos_conf, ''
print >>repos_conf, '[gentoo]'
print >>repos_conf, 'location = /usr/portage'
print >>repos_conf, 'sync-type = git'
print >>repos_conf, 'sync-uri = git://github.com/matijaskala/ports-2013.git'
print >>repos_conf, 'auto-sync = yes'
print >>repos_conf, ''
repos_conf.close()
package_keywords = open(DEST + "/etc/portage/package.accept_keywords", "w+")
print >>package_keywords, "sys-apps/shadow ~amd64"
print >>package_keywords, "www-client/google-chrome ~amd64"
package_keywords.close()
package_license = open(DEST + "/etc/portage/package.license", "w+")
print >>package_license, "www-client/google-chrome google-chrome"
package_license.close()
subprocess.call(['chroot', DEST, 'grub2-install', bootldr])
subprocess.call(['chroot', DEST, 'grub2-mkconfig', '-o', '/boot/grub/grub.cfg'])
subprocess.call(['chroot', DEST, 'sed', '-i', 's/# %sudo/%sudo/', '/etc/sudoers'])
subprocess.call(['chroot', DEST, 'groupadd', '-g', '20', 'sudo'])
subprocess.call(['chroot', DEST, 'useradd', '-mp', '', '-G', 'audio,disk,sudo,users,video,wheel', username])
subprocess.call(['chroot', DEST, 'git', 'clone', 'git://github.com/matijaskala/ports-2013.git', '/usr/portage'])
subprocess.call(['chroot', DEST, 'emerge', '-q', 'google-chrome', 'shadow'])
subprocess.call(['umount', DEST + '/etc/resolv.conf'])
subprocess.call(['umount', DEST + '/proc'])
for x in mount_dialog.vbox.get_children():
if type(x) is gtk.HButtonBox:
continue
for y in x.get_children():
if type(y) is gtk.Label:
m1 = y.get_text()
elif type(y) is gtk.Entry:
m2 = y.get_text()
if m1[:5] == "/dev/" and m2[:1] == "/":
subprocess.call(["umount", DEST + m2])