-
Notifications
You must be signed in to change notification settings - Fork 4
/
MakeDebianIso.py
executable file
·163 lines (141 loc) · 7.31 KB
/
MakeDebianIso.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
__author__ = 'Andreas Bader'
__version__ = "0.01"
import argparse
import logging
import Util
import os
import shutil
from fabric.api import *
outputFileNameSuffix="-autoinstall"
neededTools = ["which", "sed","7z", "genisoimage", "gzip", "find", "cpio", "md5sum"]
# Configure ArgumentParser
parser = argparse.ArgumentParser(prog="MakeDebianIso.py",version=__version__,description="Bla", formatter_class=argparse.RawDescriptionHelpFormatter, epilog="")
parser.add_argument("-t", "--tmpfolder", metavar="TMPFOLDER", required=True, help="path to tmp space (> 5 gb)")
parser.add_argument("-i", "--isofile", metavar="ISOFILE", required=True, help="path to Debian Iso")
parser.add_argument("-f", "--outputfolder", metavar="OUTPUTFOLDER", required=True, help="path to outputfolder (will be created if not existent)")
parser.add_argument("-p", "--presseedfile", metavar="PRESEEDFILE", required=True, help="path to preseedfile")
parser.add_argument("-o", "--overwrite", action="store_true", help="overwrite if ISO is existing")
parser.add_argument("-l", "--log", action='store_true', help="Be more verbose, log vagrant output.")
args = parser.parse_args()
# Configure Logging
logLevel = logging.WARN
if args.log:
logLevel = logging.DEBUG
logging.basicConfig(level=logLevel)
logger = logging.getLogger(__name__)
# Check ob alle Tools da sind
for tool in neededTools:
with settings(warn_only=True), hide('output','running','warnings'):
ret=local("which '%s'" %(tool))
if ret.return_code != 0:
logger.error("'%s' not found, please install." %(tool))
exit(-1)
if not (Util.check_file_exists(args.presseedfile) and Util.check_file_readable(args.presseedfile)):
logger.error("Preseedfile %s does not exist or is not readable." %(args.presseedfile))
exit(-1)
if not (Util.check_file_exists(args.isofile) and Util.check_file_readable(args.isofile)):
logger.error("Isofile %s does not exist or is not readable." %(args.isofile))
exit(-1)
if not Util.check_folder(args.outputfolder,logger,False,True):
if not Util.create_folder(args.outputfolder) or not Util.check_folder(args.outputfolder,logger):
logger.error("Can't create %s." %(args.outputfolder))
exit(-1)
if not Util.check_folder(args.tmpfolder,logger,False,True):
if not Util.create_folder(args.tmpfolder) or not Util.check_folder(args.tmpfolder,logger):
logger.error("Can't create %s." %(args.tmpfolder))
exit(-1)
else:
Util.delete_folder(args.tmpfolder,logger)
if not Util.create_folder(args.tmpfolder) or not Util.check_folder(args.tmpfolder,logger):
logger.error("Can't create %s." %(args.tmpfolder))
exit(-1)
isoFileName=os.path.basename(args.isofile)
isoFileBase="default"
if len(os.path.splitext(args.isofile)) > 0:
isoFileBase=os.path.splitext(isoFileName)[0]
isoFileExt=".iso"
if len(os.path.splitext(args.isofile)) > 1:
isoFileExt=os.path.splitext(isoFileName)[1]
preseedSuffix=""
if len(os.path.splitext(os.path.basename(args.presseedfile))[0].split("-")) > 1:
for suffix in os.path.splitext(os.path.basename(args.presseedfile))[0].split("-")[1:]:
preseedSuffix += "-%s" %(suffix)
newIsoFileName="%s%s%s%s" %(isoFileBase,preseedSuffix,outputFileNameSuffix,isoFileExt)
isoPath=os.path.join(args.tmpfolder,isoFileName)
if Util.check_file_exists(os.path.join(args.outputfolder,newIsoFileName)):
if args.overwrite:
if not Util.delete_file(os.path.join(args.outputfolder,newIsoFileName),logger):
logger.error("Error while deleting %s." %(os.path.join(args.outputfolder,newIsoFileName)))
exit(-1)
else:
logger.error("Outputfile %s does exist." %(os.path.join(args.outputfolder,newIsoFileName)))
exit(-1)
# Copy Iso to tmpfolder
try:
shutil.copy(args.isofile, isoPath)
except Exception, e:
logger.error("Can't copy %s to %s." %(args.isofile,args.tmpfolder), exc_info=True)
exit(-1)
# Unpack it with 7z
with lcd(args.tmpfolder), settings(warn_only=True), hide('output','running','warnings'):
ret=local("7z x '%s'" %(isoFileName), capture=True)
if not ret.succeeded:
logger.error("Can't unpack %s. Error: %s" %(isoPath,ret.stderr))
exit(-1)
# Delete ISO
if not Util.delete_file(isoPath, logger):
logger.error("Can't delete %s." %(isoPath))
exit(-1)
# mkdir irmod, unzip initrd.gz
if not Util.create_folder(os.path.join(args.tmpfolder,"irmod")):
logger.error("Can't create %s." %(os.path.join(args.tmpfolder,"irmod")))
exit(-1)
with lcd(os.path.join(args.tmpfolder,"irmod")), settings(warn_only=True), hide('output','running','warnings'):
ret=local("gzip -d < ../install.amd/initrd.gz | cpio --extract --verbose --make-directories --no-absolute-filenames")
if ret.return_code != 2:
logger.error("Can't unpack %s. Error: %s" %(os.path.join(args.tmpfolder,"install.amd/initrd.gz"),ret.stderr))
exit(-1)
# Copy Pressed file in place
if not Util.copy_file(args.presseedfile, os.path.join(args.tmpfolder,"irmod","preseed.cfg"),logger):
logger.error("Can't copy %s to %s." %(args.presseedfile, os.path.join(args.tmpfolder,"irmod","preseed.cfg")))
exit(-1)
# Pack & Compress it again
with lcd(os.path.join(args.tmpfolder,"irmod")), settings(warn_only=True), hide('output','running','warnings'):
ret=local("find . | cpio -H newc --create --verbose | gzip -9 > ../install.amd/initrd.gz")
if not ret.succeeded:
logger.error("Can't pack %s. Error: %s" %(os.path.join(args.tmpfolder,"install.amd/initrd.gz"),ret.stderr))
exit(-1)
# Delete irmod
if not Util.delete_folder(os.path.join(args.tmpfolder,"irmod"),logger):
logger.error("Can't delete %s." %(os.path.join(args.tmpfolder,"irmod")))
exit(-1)
# Running sed + generating md5sum + genisoimage
with lcd(args.tmpfolder), settings(warn_only=True), hide('output','running','warnings'):
ret=local("sed -i \"s/timeout 0/timeout 2/g\" isolinux/isolinux.cfg")
if not ret.succeeded:
logger.error("Error while sed run on %s. Error: %s" %(os.path.join(args.tmpfolder,"isolinux/isolinux.cfg"),ret.stderr))
exit(-1)
ret=local("sed -i \"s/quiet/ipv6.disable=1 quiet/g\" isolinux/txt.cfg")
if not ret.succeeded:
logger.error("Error while sed run on %s. Error: %s" %(os.path.join(args.tmpfolder,"isolinux/txt.cfg"),ret.stderr))
exit(-1)
ret=local("md5sum `find -follow -type f` > md5sum.txt")
if not ret.succeeded:
logger.error("Error while md5sum+find run. Error: %s" %(ret.stderr))
exit(-1)
ret=local("genisoimage -o \"%s\" -r -J -no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/boot.cat ." %(os.path.join(args.tmpfolder,newIsoFileName)))
if not ret.succeeded:
logger.error("Error while genisoimage run. Error: %s" %(ret.stderr))
exit(-1)
# Copy Iso, delete tmpfolder
if os.path.join(args.tmpfolder,newIsoFileName) != os.path.join(args.outputfolder,newIsoFileName):
if not Util.copy_file(os.path.join(args.tmpfolder,newIsoFileName),os.path.join(args.outputfolder,newIsoFileName),logger):
logger.error("Could not copy %s to %s." %(os.path.join(args.tmpfolder,newIsoFileName),os.path.join(args.outputfolder,newIsoFileName)))
exit(-1)
if not Util.delete_folder(args.tmpfolder,logger):
logger.error("Can't delete %s." %(args.tmpfolder))
exit(-1)
logger.info("Done.")
exit(0)