/
create_iso.py
executable file
·208 lines (158 loc) · 6.42 KB
/
create_iso.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
#!/usr/bin/env python2
import os
import six
import sys
import glob
import tarfile
import explodeinstaller
from subprocess import Popen, PIPE, check_call
from backports import lzma
def get_vm_name(path):
name = os.path.splitext(os.path.basename(path))[0]
name = name.replace("-install-dvd", "-vagrant")
return name
def get_tag_name(path):
name = os.path.splitext(os.path.basename(path))[0]
name = name.replace("-install-dvd", "")
return name
ADDITIONS_ISO = "/opt/VirtualBox/additions/VBoxGuestAdditions.iso"
# Change to false to avoid compile of additions.
ADDITIONS = True
ISO_PATH = sys.argv[1]
VM_NAME = get_vm_name(ISO_PATH)
VBOX_SOCKET = "/tmp/%s" % VM_NAME
TEMP_DIR = "/ram/" + VM_NAME + "_tmp"
ISO_NAME = "/ram/" + VM_NAME + ".iso"
DISK_NAME = "/ram/" + VM_NAME + ".vdi"
SUPP_TAG_NAME = get_tag_name(ISO_PATH)
PKG_DIR, VERSION = SUPP_TAG_NAME.split("-")
# These are all the disk sets, not related to any one version.
DISK_SETS = "a ap d e f k kde kdei l n t tcl x xap xfce y".split()
def get_disk_set_removals(disk_set):
primary = os.path.join("tags", disk_set + ".skp")
supplemental = os.path.join("tags", disk_set, SUPP_TAG_NAME + ".skp")
total = []
found = False
for name in [primary, supplemental]:
if os.path.exists(name):
found = True
total += open(name, "rb").read().split()
return total if found else None
def update_tagfiles(disk_sets_dir):
"""
Update contents of original tagfiles based on the list of packages to skip.
There's no point to keep the original tagfiles around as this is all generated.
"""
for disk_set in DISK_SETS:
disk_set_subdir = os.path.join(disk_sets_dir, disk_set)
if not os.path.exists(disk_set_subdir):
continue
removals = get_disk_set_removals(disk_set)
out_tags = []
p = os.path.join(disk_set_subdir, "tagfile")
print("Updating %r" % p)
for line in open(p).readlines():
name = line.split(":")[0]
if (removals is None) or (name in removals):
out_tags.append(name + ":SKP")
else:
out_tags.append(name + ":ADD")
open(p, "wb").write("\n".join(out_tags).encode("utf-8"))
def is_included(disk_set, name):
"""
Find if the package is SKP.
:param name: name of package
:return: True/False
"""
wild = os.path.join(TEMP_DIR, "isofs", "slackware*", disk_set, "tagfile")
poss = glob.glob(wild)
if len(poss) != 1:
raise ValueError("unable to find tagfile for %s series" % disk_set)
name += ":"
for line in open(poss[0], "rb").readlines():
if line.startswith(name):
return line.strip() == (name + "ADD")
raise ValueError("%s unknown package name in %s series" % (name, disk_set))
def open_package_tarfile(name):
if name.endswith("xz"):
xz = lzma.LZMAFile(name)
return tarfile.open(fileobj=xz)
elif name.endswith("gz"):
return tarfile.open(name, "r:gz")
else:
raise ValueError("unknown compression type")
def check_lilo_utf8():
wild = os.path.join(TEMP_DIR, "isofs", "slackware*", "a", "lilo-*.t?z")
poss = glob.glob(wild)
if len(poss) != 1:
raise ValueError("unable to find lilo package in A disk series")
name = poss[0]
tf = open_package_tarfile(name)
data = tf.extractfile("sbin/liloconfig").read()
if data.find(b'USE UTF-8 TEXT CONSOLE?') != -1:
return True
else:
return False
def make_expect_parameters():
fp = open("includes.exp", "wb")
fp.write(b'set VM_NAME "%s"\n' % VM_NAME.encode("utf-8"))
fp.write(b'set VBOX_SOCKET "%s"\n' % VBOX_SOCKET.encode("utf-8"))
mouse = is_included("a", "gpm")
fp.write(b'set mouse %s\n' % str(mouse).lower().encode("utf-8"))
network = is_included("n", "net-tools")
fp.write(b'set network %s\n' % str(network).lower().encode("utf-8"))
utf8lilo = check_lilo_utf8()
fp.write(b'set utf8lilo %s\n' % str(utf8lilo).lower().encode("utf-8"))
fp.write(b'set additions %s\n' % str(ADDITIONS).lower().encode("utf-8"))
fp.close()
def make_shell_parameters():
fp = open("includes.sh", "wb")
fp.write(b'VM_NAME=%s\n' % VM_NAME.encode("utf-8"))
fp.write(b'VBOX_SOCKET=%s\n' % VBOX_SOCKET.encode("utf-8"))
fp.write(b'DISK_NAME=%s\n' % DISK_NAME.encode("utf-8"))
fp.write(b'ISO_NAME=%s\n' % ISO_NAME.encode("utf-8"))
fp.write(b'TEMP_DIR=%s\n' % TEMP_DIR.encode("utf-8"))
fp.write(b'SUPP_TAG_NAME=%s\n' % SUPP_TAG_NAME.encode("utf-8"))
fp.write(b'PKG_DIR=%s\n' % PKG_DIR.encode("utf-8"))
fp.write(b'VERSION=%s\n' % VERSION.encode("utf-8"))
fp.close()
def additions_to_extra():
"""Copy the additions into the extra folder"""
os.system("iso-read -e VBoxLinuxAdditions.run -i %s "
"-o %s/isofs/extra/VBoxLinuxAdditions.run" % (ADDITIONS_ISO, TEMP_DIR))
def vagrant_pub_key_to_extra():
"""Copy the additions into the extra folder"""
os.system("wget https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub"
" -O %s/isofs/extra/vagrant.pub" % TEMP_DIR)
def zerofree_to_extra():
"""Copy to the extra folder"""
os.system("cp /usr/sbin/zerofree %s/isofs/extra/zerofree" % TEMP_DIR)
def boot_on_serial():
CFG=os.path.join(TEMP_DIR, "isofs/isolinux/isolinux.cfg")
data = b"serial 0 115200\n" + open(CFG, "rb").read()
data = data.replace(b"SLACK_KERNEL=huge.s", b"SLACK_KERNEL=huge.s console=ttyS0")
data = data.replace(b"SLACK_KERNEL=hugesmp.s", b"SLACK_KERNEL=hugesmp.s console=ttyS0")
open(CFG, "wb").write(data)
def remove_boot_timeout():
CFG=os.path.join(TEMP_DIR, "isofs/isolinux/isolinux.cfg")
data = open(CFG, "rb").read()
data = data.replace("timeout 1200\n", "timeout 10\n", 1)
open(CFG, "wb").write(data)
# Adapt a Slackware ISO so it boots over serial port
# And then create a VirtualBox VM to run the ISO.
make_expect_parameters()
make_shell_parameters()
os.system("rm -rf %s" % TEMP_DIR)
# Extract the ISO
explodeinstaller.extract_all(ISO_PATH, TEMP_DIR)
additions_to_extra()
vagrant_pub_key_to_extra()
zerofree_to_extra()
disk_sets_dir = os.path.join(TEMP_DIR, "isofs/slackware64")
if not os.path.exists(disk_sets_dir):
disk_sets_dir = os.path.join(TEMP_DIR, "isofs/slackware")
update_tagfiles(disk_sets_dir)
remove_boot_timeout()
check_call("./installpkg_initrd.py")
# boot_on_serial()
explodeinstaller.assemble_all(TEMP_DIR, ISO_NAME)