def move_clonezilla_files(srcdir="tmp"): srcdir = path(srcdir) donedir = path("done") livedir = srcdir / "live" # stuff for tftpboot vmlinuz = "vmlinuz1" initrd = "initrd1.img" # stuff for nfsroot fs = "filesystem.squashfs" fsp = "filesystem.packages" freedos = "freedos.img" # start with nfsroot nfsrootdir = donedir / "nfsroot" cz_nfsrootdir = nfsrootdir / "clonezilla" / "live" if not cz_nfsrootdir.isdir(): cz_nfsrootdir.makedirs() for basename in [fs, fsp, freedos]: srcname = livedir / basename destname = cz_nfsrootdir / basename os.rename(srcname, destname) # now to tftpboot tftpbootdir = donedir / "tftpboot" cz_tftpbootdir = tftpbootdir / "clonezilla" if not cz_tftpbootdir.isdir(): cz_tftpbootdir.mkdir() for basename in [vmlinuz, initrd]: srcname = livedir / basename destname = cz_tftpbootdir / basename os.rename(srcname, destname)
def move_clonezilla_files(srcdir='tmp'): srcdir = path(srcdir) donedir = path('done') livedir = srcdir / 'live' # stuff for tftpboot vmlinuz = 'vmlinuz1' initrd = 'initrd1.img' # stuff for nfsroot fs = 'filesystem.squashfs' fsp = 'filesystem.packages' freedos = 'freedos.img' # start with nfsroot nfsrootdir = donedir / 'nfsroot' cz_nfsrootdir = nfsrootdir / 'clonezilla' / 'live' if not cz_nfsrootdir.isdir(): cz_nfsrootdir.makedirs() for basename in [fs, fsp, freedos]: srcname = livedir / basename destname = cz_nfsrootdir / basename os.rename(srcname, destname) # now to tftpboot tftpbootdir = donedir / 'tftpboot' cz_tftpbootdir = tftpbootdir / 'clonezilla' if not cz_tftpbootdir.isdir(): cz_tftpbootdir.mkdir() for basename in [vmlinuz, initrd]: srcname = livedir / basename destname = cz_tftpbootdir / basename os.rename(srcname, destname)
def install_syslinux_files(menu): for filename in ['vesamenu.c32']: copy_from_syslinux_libdir(filename, path(USB_PARENT)) # THIS IS VERY UGLY, FIXME splash_src = path(USB_PARENT) / splash_filename(path(''), 'installer', 'i386') basename = splash_src.basename() splash_dest = path(USB_PARENT) / basename if not splash_dest.isfile(): subprocess.check_call(['cp', splash_src, splash_dest]) maincfg = path(USB_PARENT) / 'syslinux.cfg' maincfg.write_lines(menu)
def extract_initrd(initrd, dirname): here = os.getcwd() dirname = path(dirname) initrd = path(initrd).abspath() if dirname.exists(): raise RuntimeError , "The temp build directory, %s, shouldn't exist." % dirname dirname.mkdir() os.chdir(dirname) cmd = ['cpio', '-id'] subprocess.check_call(cmd, stdin=file(initrd)) os.chdir(here)
def main(): prepare() tftpbootdir = path('done/tftpboot') machines = template_config.sections() for machine in machines: print "Creating images for machine: %s" % machine create_netboot_images(machine)
def remove_chroot(): dirname = path('chroot') if dirname.isdir(): print "chroot/ exists......removing" rmdir(str(dirname)) if dirname.exists(): raise RuntimeError , "chroot/ not removed correctly."
def remove_chroot(): dirname = path('chroot') if dirname.isdir(): print "chroot/ exists......removing" rmdir(str(dirname)) if dirname.exists(): raise RuntimeError, "chroot/ not removed correctly."
def copy_all_boot_files(): src = ORIG_BOOT_DIR + '/' dest = USB_BOOT_DIR + '/' d = path(dest) if not d.isdir(): d.makedirs() cmd = ['rsync', '-aHX', src, dest] subprocess.check_call(cmd)
def unzip_clonezilla(url, dldir=DOWNLOAD_DIR, destdir='tmp'): destdir = path(destdir) if not destdir.isdir(): destdir.mkdir() basename = local_basename(url) filename = dldir / basename zipped = ZipFile(filename) for entry in zipped.namelist(): #print "handling entry,", entry entry = path(entry) dest = destdir / entry if entry.endswith('/'): if not dest.isdir(): dest.makedirs() else: contents = zipped.read(str(entry)) dest.write_bytes(contents)
def copy_bootdir(): src = '%s/' % orig_bootdir() dest = '%s/' % usb_bootdir() d = path(dest) if not d.dirname().isdir(): d.dirname().makedirs() cmd = ['rsync', '-aHX', src, dest] subprocess.check_call(cmd)
def unzip_clonezilla(url, dldir=DOWNLOAD_DIR, destdir="tmp"): destdir = path(destdir) if not destdir.isdir(): destdir.mkdir() basename = local_basename(url) filename = dldir / basename zipped = ZipFile(filename) for entry in zipped.namelist(): # print "handling entry,", entry entry = path(entry) dest = destdir / entry if entry.endswith("/"): if not dest.isdir(): dest.makedirs() else: contents = zipped.read(str(entry)) dest.write_bytes(contents)
def prepare_stagedir(): stagedir = path('.stage') if stagedir.isdir(): print ".stage/ exists...removing" rmdir(str(stagedir)) stagedir.mkdir() bootstrap_stage = stagedir / 'bootstrap' # touch empty file file(bootstrap_stage, 'w')
def install_programs(remove=False): src = orig_rootdir() / 'Programs' usbdir = path(USB_ROOT_DIR).dirname() dest = usbdir / 'Programs' hardlink_dirs(src, dest, remove=remove) wintaylor = orig_rootdir() / 'WinTaylor.exe' #hardlink_dirs(wintaylor, usbdir) cmd = ['cp', str(wintaylor), str(usbdir)] subprocess.check_call(cmd)
def copy_caine(mount_point, verbose=False): donedir = path('done') nfsrootdir = donedir / 'nfsroot/caine/' tftpbootdir = donedir / 'tftpboot/caine/' cmd = ['rsync', '-a'] if verbose: cmd.append('-v') if not mount_point.endswith('/'): mount_point = path('%s/' % mount_point) cmd += [str(mount_point), str(nfsrootdir)] subprocess.check_call(cmd) if not tftpbootdir.isdir(): tftpbootdir.makedirs() casper = nfsrootdir / 'casper' for basename in ['vmlinuz', 'initrd.gz']: src = casper / basename dest = tftpbootdir / basename cmd = ['cp', src, dest] subprocess.check_call(cmd)
def setup_config(data): filenames = ['binary', 'bootstrap', 'chroot'] configdir = path('config') for config in filenames: template_basename = '%s.template' % config template_filename = configdir / template_basename text = template_filename.text() contents = text % data configfile = configdir / config configfile.write_bytes(contents)
def move_files(target, machine, arch): target = path(target) if not target.isdir(): target.mkdir() tftpbootdir = target / path('tftpboot/%s' % machine) if not tftpbootdir.isdir(): tftpbootdir.makedirs() nfsrootdir = target / path('nfsroot/%s' % machine) if not nfsrootdir.isdir(): nfsrootdir.makedirs() os.rename('binary', nfsrootdir / arch) os.rename('tftpboot', tftpbootdir / arch) packages_filename = '%s.%s.packages' % (machine, arch) os.rename('binary.packages', nfsrootdir / arch / packages_filename) print "Removing chroot/" cmd = ['rm', '-fr', 'chroot/'] retval = subprocess.call(cmd) if retval: raise RuntimeError, "Problem removing chroot/ directory."
def setup_cachedirs(archs): dirnames = ['chroot.%s' % a for a in archs] extract = True dirfound = False for dirname in dirnames: if path(dirname).isdir(): dirfound = True extract = False else: extract = True if extract and dirfound: print "WARNING, something funny is happening..." filename = path('cachedirs.tar.gz') if filename.exists() and extract: print "extracting cache directories..." cmd = ['tar', 'xfz', str(filename)] retval = subprocess.call(cmd) if retval: print "RETVAL: %d" % retval raise RuntimeError, "Something bad happened when extracting %s" % filename
def create_cachedirs_tarball(): filename = path('cachedirs.tar.gz') if not filename.exists(): print "Creating %s" % filename cmd = ['tar', 'cfz', str(filename), 'chroot.amd64', 'chroot.i386'] retval = subprocess.call(cmd) if retval: print "RETVAL: %d" % retval raise RuntimeError, "Something bad happened with creating %s ." % filename else: print "%s already exists, skipping this step." % filename
def move_files(target, machine, arch): target = path(target) if not target.isdir(): target.mkdir() tftpbootdir = target / path('tftpboot/%s' % machine) if not tftpbootdir.isdir(): tftpbootdir.makedirs() nfsrootdir = target / path('nfsroot/%s' % machine) if not nfsrootdir.isdir(): nfsrootdir.makedirs() os.rename('binary', nfsrootdir / arch) os.rename('tftpboot', tftpbootdir / arch) packages_filename = '%s.%s.packages' % (machine, arch) os.rename('binary.packages', nfsrootdir / arch / packages_filename) print "Removing chroot/" cmd = ['rm', '-fr', 'chroot/'] retval = subprocess.call(cmd) if retval: raise RuntimeError , "Problem removing chroot/ directory."
def create_cachedirs_tarball(): filename = path('cachedirs.tar.gz') if not filename.exists(): print "Creating %s" % filename cmd = ['tar', 'cfz', str(filename), 'chroot.amd64', 'chroot.i386'] retval = subprocess.call(cmd) if retval: print "RETVAL: %d" % retval raise RuntimeError , "Something bad happened with creating %s ." % filename else: print "%s already exists, skipping this step." % filename
def setup_cachedirs(archs): dirnames = ['chroot.%s' % a for a in archs] extract = True dirfound = False for dirname in dirnames: if path(dirname).isdir(): dirfound = True extract = False else: extract = True if extract and dirfound: print "WARNING, something funny is happening..." filename = path('cachedirs.tar.gz') if filename.exists() and extract: print "extracting cache directories..." cmd = ['tar', 'xfz', str(filename)] retval = subprocess.call(cmd) if retval: print "RETVAL: %d" % retval raise RuntimeError , "Something bad happened when extracting %s" % filename
def mount_caine(filename): blocksize = 512 mount_point = path(tempfile.mkdtemp('_loopback', 'caine_')) mbr = MBRParser(file(filename).read(blocksize)) startLBA = mbr.PartitionTable.Entry0.StartLBA offset = blocksize * startLBA cmd = ['mount', '-o', 'loop,offset=%d' % offset] cmd += [str(filename), str(mount_point)] subprocess.check_call(cmd) return mount_point
def restore_chroot(machine, arch, remove_chroot=True): stored = stored_chroot_filename(machine, arch) if not stored.isfile(): raise RuntimeError, "%s doesn't exist." % stored dest = path('chroot') if dest.isdir(): if remove_chroot: rmdir(str(dest)) else: RuntimeError, "Won't remove chroot/" cmd = ['tar', 'xfz', str(stored)] subprocess.check_call(cmd)
def restore_chroot(machine, arch, remove_chroot=True): stored = stored_chroot_filename(machine, arch) if not stored.isfile(): raise RuntimeError , "%s doesn't exist." % stored dest = path('chroot') if dest.isdir(): if remove_chroot: rmdir(str(dest)) else: RuntimeError , "Won't remove chroot/" cmd = ['tar', 'xfz', str(stored)] subprocess.check_call(cmd)
def fix_menu_file(tftpbootdir, machine, arch): marker = 'debian-live/%s/' % arch filename = pxelinux_menu_filename(tftpbootdir, machine, arch) menu_lines = filename.lines() bkup = path('%s.orig' % filename) if not bkup.isfile(): filename.rename(bkup) new_lines = [] for line in menu_lines: if marker in line: line = fix_menu_line(line, machine, arch) new_lines.append(line) filename.write_lines(new_lines)
def fix_menu_file(bootdir, machine, arch): marker = 'debian-live/%s/' % arch filename = pxelinux_menu_filename(bootdir, machine, arch) menu_lines = filename.lines() bkup = path('%s.orig' % filename) if not bkup.isfile(): filename.rename(bkup) new_lines = [] for line in menu_lines: if marker in line: line = fix_menu_line(line, machine, arch) new_lines.append(line) filename.write_lines(new_lines)
def download_caine(dldir=DOWNLOAD_DIR): if not dldir.isdir(): print "creating downloads directory..." dldir.mkdir() basename = path(CAINE_FILENAME).basename() dest = dldir / basename if not dest.exists(): cmd = ['cp', '-a', CAINE_FILENAME, str(dest)] subprocess.check_call(cmd) mysum = md5sum(file(dest)) if mysum == CAINE_MDSUM: print "Success, md5sum of caine image matches. :)" else: msg = "Improper file: Please remove %s and try again." % filename raise RuntimeError, msg
def make_image(arch, machine): prepare_stagedir() # try to install from cache first cachedir = 'chroot.%s' % arch cachedir = path(cachedir) if cachedir.isdir(): print "Using cachedir: %s" % cachedir remove_chroot() subprocess.call(['cp', '-al', 'chroot.%s' % arch, 'chroot']) else: install_chroot(arch, machine) # if we didn't install from cache, create it if USE_CACHE and not cachedir.isdir(): print "creating cache directory %s" % cachedir subprocess.call(['cp', '-al', 'chroot', 'chroot.%s' % arch]) logfilename = 'live-build-log.%s' % arch call_live_build(logfilename)
def create_transportable_archive(filename, target): archive = filename taropts = 'c' if path(archive).isfile(): print "removing old", archive os.remove(archive) print "creating new", archive here = os.getcwd() os.chdir(target) cmd = ['tar', taropts, '.'] tarproc = subprocess.Popen(cmd, stdout=subprocess.PIPE) zipproc = subprocess.Popen(['pigz'], stdin=tarproc.stdout, stdout=file(archive, 'w')) tar_retval = tarproc.wait() zip_retval = zipproc.wait() if tar_retval: raise RuntimeError , "problem tarring up %s" % archive if zip_retval: raise RuntimeError , "problem zipping up %s" % archive os.chdir(here) print "All Done!"
def create_transportable_archive(filename, target): archive = filename taropts = 'c' if path(archive).isfile(): print "removing old", archive os.remove(archive) print "creating new", archive here = os.getcwd() os.chdir(target) cmd = ['tar', taropts, '.'] tarproc = subprocess.Popen(cmd, stdout=subprocess.PIPE) zipproc = subprocess.Popen(['pigz'], stdin=tarproc.stdout, stdout=file(archive, 'w')) tar_retval = tarproc.wait() zip_retval = zipproc.wait() if tar_retval: raise RuntimeError, "problem tarring up %s" % archive if zip_retval: raise RuntimeError, "problem zipping up %s" % archive os.chdir(here) print "All Done!"
def usb_rootdir(): return path(USB_ROOT_DIR) / 'clonezilla'
def orig_rootdir(): return path(ORIG_ROOT_DIR) / 'clonezilla'
def usb_bootdir(): return path(USB_BOOT_DIR) / 'clonezilla'
def menu_filename(machine, arch): prefix = make_prefix(path(USB_BOOT_DIR), machine, arch) base = base_pxelinux_menu_filename(arch) return prefix / base
import os, sys import subprocess from livebuild.path import path syslinux_libdir = path('/usr/lib/syslinux') vesamenu_filename = syslinux_libdir / 'vesamenu.c32' pxelinux_boot_filename = syslinux_libdir / 'pxelinux.0' default_menu_contents = '''default vesamenu.c32 prompt 0 label i386 \tMENU LABEL Paella Installer (i386) include debian-live/i386/boot-screens/menu.cfg label amd64 \tMENU LABEL Paella Installer (amd64) include debian-live/amd64/boot-screens/menu.cfg label Clonezilla live \tMENU LABEL Clonezilla live (Default settings, VGA 800x600) \tkernel clonezilla/vmlinuz1 \tappend initrd=clonezilla/initrd1.img boot=live config noswap nolocales edd=on nomodeset nopromp ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_keymap="NONE" ocs_live_batch="no" ocs_lang="" ocs_prerun="mount 10.0.1.1:/media/bigdisk/share/erasable/images /home/partimag" vga=788 xnosplash netboot=nfs nfsroot=10.0.1.1:/freespace/paella/clonezilla ''' def copyfile(src, dest): cmd = ['cp', '-a', src, dest] return subprocess.call(cmd)
def prepare(): if os.getuid(): raise RuntimeError , "You need to be root to run this script" if path('done').isdir(): msg = "Please remove the done/ directory and try again." raise RuntimeError , msg
def orig_bootdir(): return path(ORIG_BOOT_DIR) / 'caine'
def orig_rootdir(): return path(ORIG_ROOT_DIR) / 'caine'
def make_relative_prefix(machine, arch): return path('boot/%s' % machine) / arch
import os import subprocess import tempfile from livebuild.path import path from livebuild import md5sum from mbr_parser import MBRParser # You must be root to run this # Packages required: live-helper, paella-installer, wget CAINE_FILENAME = '/opt/common/disk_images/nbcaine2.5.1.dd' CAINE_MDSUM = 'd94bf0890be2c6e1c9d7301c222aa876' DOWNLOAD_DIR = path('downloads') def rmdir(dirname): return subprocess.call(['rm', '-fr', dirname]) def local_basename(url): # sourceforge url is http://server/path/to/filename/download # so we use this trick to get the actual filename dirname = os.path.dirname(url) basename = os.path.basename(dirname) return basename def download_caine(dldir=DOWNLOAD_DIR):
def usb_bootdir(): return path(USB_BOOT_DIR) / 'caine'
def base_pxelinux_menu_filename(arch): return path('debian-live/%s/boot-screens/live.cfg' % arch)
def make_relative_prefix(machine, arch): return path(machine) / arch
def base_splash_filename(arch): return path('debian-live/%s/boot-screens/splash.png' % arch)
import os, sys import subprocess from livebuild.path import path from livebuild.usb.base import USB_PARENT from livebuild.usb.base import copyfile syslinux_libdir = path('/usr/lib/syslinux') vesamenu_filename = syslinux_libdir / 'vesamenu.c32' # convert -resize 640x480 -depth 16 -colors 14 yourfile.bmp splash.png def copy_from_syslinux_libdir(filename, destdir): src = syslinux_libdir / filename dest = destdir / filename retval = copyfile(str(src), str(dest)) if retval: msg = "There was a problem copying %s, cp returned %d" msg = msg % (filename, retval) raise RuntimeError , msg def fix_menu_line(line, machine, arch): marker = 'debian-live/%s/' % arch corrected = '%s/%s/%s' % (machine, arch, marker) return line.replace(marker, corrected) def fix_menu_file(bootdir, machine, arch):
def usb_rootdir(): return path(USB_ROOT_DIR) / 'caine'