Writing .tgz files with Python that VMWare ESXi’s vmkernel likes
So, I’d better blow the cobwebs out, it’s been 18 months.
Lately I’ve been brewing up a contraption that will network boot ESXi machines.
At the moment, it’s using gpxe + gpxelinux to boot via http, and a python http server to serve up the pxe boot gubbins required to start ESXi - there’s an excellent post at vinternals that gives some hints.
As well as this, I’m loading a state.tgz to persist the configuration.
But I wanted to be able to pass some configuration information and some new scripts from the boot server directly into the just-booted ESXi installation.
So, import tarfile to the rescue. Only it wasn’t - because on Python 2.5 tarfile writes tarfiles with broken headers. Thankfully, you can grab the 2.6b tarfile, and it drops in place on Python 2.5 - and the new version writes out byte-compatible gnu tar format tarfiles.
Some code:
import tarfile26 as tarfile
(...)
def tarwritefile(self, tar, name, writestring, mode):
tarinfo = tarfile.TarInfo()
tarinfo.name = name
tarinfo.uid = 0001637
tarinfo.gid = 0000311
tarinfo.type = tarfile.REGTYPE
tarinfo.mode = mode
tarinfo.uname = "ocremel"
tarinfo.gname = "mts"
tarinfo.mtime = time.mktime(datetime.datetime.now().timetuple())
file = StringIO.StringIO()
file.write(writestring)
file.seek(0)
tarinfo.size = len(writestring)
tar.addfile(tarinfo, file)
file.close()
def maketar():
namelist = {'uuid':uuid,
'boot-clientip':clientip,
'boot-serverip':serverip,
'net-vlan':vlan,
'net-subnet':subnet}
scripts = {'store-storagescript.sh':storagescript}
# Make our directory
tar = tarfile.open("temp.tgz", "w:gz",self.webserver)
for name in namelist.iterkeys():
self.tarwritefile(tar, 'bootinfo/'+name, namelist[name]+'\n',0644)
for name in scripts.iterkeys():
self.tarwritefile(tar, 'bootinfo/'+name, scripts[name]+'\n',0755)
tar.close()
Now I know it’s not a lot - but I hope that helps somebody!
