Beispiel #1
0
def savefs_before_chroot(chrootdir, saveto = None):
    """ backup chrootdir to another directory before chrooting in """
    if configmgr.chroot['saveto']:
        savefs = True
        saveto = configmgr.chroot['saveto']
        wrnmsg = "Can't save chroot fs for dir %s exists" % saveto
        if saveto == chrootdir:
            savefs = False
            wrnmsg = "Dir %s is being used to chroot" % saveto
        elif os.path.exists(saveto):
            if msger.ask("Dir %s already exists, cleanup and continue?" %
                         saveto):
                shutil.rmtree(saveto, ignore_errors = True)
                savefs = True
            else:
                savefs = False

        if savefs:
            msger.info("Saving image to directory %s" % saveto)
            fs_related.makedirs(os.path.dirname(os.path.abspath(saveto)))
            runner.quiet("cp -af %s %s" % (chrootdir, saveto))
            devs = ['dev/fd',
                    'dev/stdin',
                    'dev/stdout',
                    'dev/stderr',
                    'etc/mtab']
            ignlst = [os.path.join(saveto, x) for x in devs]
            map(os.unlink, filter(os.path.exists, ignlst))
        else:
            msger.warning(wrnmsg)
Beispiel #2
0
    def check_image_exists(self, destdir, apacking=None,
                                          images=(),
                                          release=None):

        # if it's a packing file, reset images
        if apacking:
            images = [apacking]

        # release option will override images
        if release is not None:
            images = [os.path.basename(destdir.rstrip('/'))]
            destdir = os.path.dirname(destdir.rstrip('/'))

        for name in images:
            if not name:
                continue

            image = os.path.join(destdir, name)
            if not os.path.exists(image):
                continue

            if msger.ask("Target image/dir: %s already exists, "
                         "clean up and continue?" % image):
                if os.path.isdir(image):
                    shutil.rmtree(image)
                else:
                    os.unlink(image)
            else:
                raise errors.Abort("Canceled")
Beispiel #3
0
    def check_image_exists(self, destdir, apacking=None,
                                          images=(),
                                          release=None):

        # if it's a packing file, reset images
        if apacking:
            images = [apacking]

        # release option will override images
        if release is not None:
            images = [os.path.basename(destdir.rstrip('/'))]
            destdir = os.path.dirname(destdir.rstrip('/'))

        for name in images:
            if not name:
                continue

            image = os.path.join(destdir, name)
            if not os.path.exists(image):
                continue
            if msger.ask("Target image/dir: %s already exists, "
                         "clean up the old files and continue?" % image):
                if os.path.isdir(image):
                    for path, dirs, files in os.walk(os.path.abspath(image)):
                        for fname in files:
                            fpath = os.path.join(path, fname)
                            if not fpath.endswith('.log'):
                                os.remove(fpath)
                else:
                    os.unlink(image)
            else:
                raise errors.Abort("Canceled")
Beispiel #4
0
def bootstrap_mic(argv=None):


    def mychroot():
        os.chroot(rootdir)
        os.chdir(cwd)

    # by default, sys.argv is used to run mic in bootstrap
    if not argv:
        argv = sys.argv
    if argv[0] not in ('/usr/bin/mic', 'mic'):
        argv[0] = '/usr/bin/mic'

    cropts = configmgr.create
    bsopts = configmgr.bootstrap
    distro = bsopts['distro_name'].lower()

    rootdir = bsopts['rootdir']
    pkglist = bsopts['packages']
    cwd = os.getcwd()

    # create bootstrap and run mic in bootstrap
    bsenv = bootstrap.Bootstrap(rootdir, distro, cropts['arch'])
    bsenv.logfile = cropts['logfile']
    # rootdir is regenerated as a temp dir
    rootdir = bsenv.rootdir

    if 'optional' in bsopts:
        optlist = bsopts['optional']
    else:
        optlist = []

    try:
        msger.info("Creating %s bootstrap ..." % distro)
        bsenv.create(cropts['repomd'], pkglist, optlist)

        # bootstrap is relocated under "bootstrap"
        if os.path.exists(os.path.join(rootdir, "bootstrap")):
            rootdir = os.path.join(rootdir, "bootstrap")

        bsenv.dirsetup(rootdir)
        sync_mic(rootdir)

        #FIXME: sync the ks file to bootstrap
        if "/" == os.path.dirname(os.path.abspath(configmgr._ksconf)):
            safecopy(configmgr._ksconf, rootdir)

        msger.info("Start mic in bootstrap: %s\n" % rootdir)
        bindmounts = get_bindmounts(cropts)
        ret = bsenv.run(argv, cwd, rootdir, bindmounts)

    except errors.BootstrapError, err:
        msger.warning('\n%s' % err)
        if msger.ask("Switch to native mode and continue?"):
            return
        else:
            raise errors.BootstrapError("Failed to create bootstrap: %s" % err)
Beispiel #5
0
def read_kickstart(path):
    """Parse a kickstart file and return a KickstartParser instance.

    This is a simple utility function which takes a path to a kickstart file,
    parses it and returns a pykickstart KickstartParser instance which can
    be then passed to an ImageCreator constructor.

    If an error occurs, a CreatorError exception is thrown.
    """

    #version = ksversion.makeVersion()
    #ks = ksparser.KickstartParser(version)

    using_version = ksversion.DEVEL
    commandMap[using_version]["desktop"] = desktop.Moblin_Desktop
    commandMap[using_version]["repo"] = moblinrepo.Moblin_Repo
    commandMap[using_version]["bootloader"] = micboot.Moblin_Bootloader
    commandMap[using_version]["part"] = partition.MeeGo_Partition
    commandMap[using_version]["partition"] = partition.MeeGo_Partition
    commandMap[using_version]["btrfs"] = btrfs.BTRFS

    dataMap[using_version]["RepoData"] = moblinrepo.Moblin_RepoData
    dataMap[using_version]["PartData"] = partition.MeeGo_PartData
    dataMap[using_version]["BTRFSData"] = btrfs.BTRFSData

    superclass = ksversion.returnClassForVersion(version=using_version)

    class KSHandlers(superclass):
        def __init__(self, mapping={}):
            superclass.__init__(self, mapping=commandMap[using_version])
            self.prepackages = PrePackages()
            self.attachment = Attachment()

        def __str__(self):
            retval = superclass.__str__(self)
            if self.prepackages:
                retval += self.prepackages.__str__()
            if self.attachment:
                retval += self.attachment.__str__()
            return retval

    ks = ksparser.KickstartParser(KSHandlers(), errorsAreFatal=False)
    ks.registerSection(PackScriptSection(ks.handler, dataObj=PackScript))
    ks.registerSection(PrepackageSection(ks.handler))
    ks.registerSection(AttachmentSection(ks.handler))

    try:
        ks.readKickstart(path)
    except (kserrors.KickstartParseError, kserrors.KickstartError) as err:
        if msger.ask("Errors occured on kickstart file, skip and continue?"):
            msger.warning("%s" % err)
            pass
        else:
            raise errors.KsError("%s" % err)

    return ks
Beispiel #6
0
def bootstrap_mic(argv=None):


    def mychroot():
        os.chroot(rootdir)
        os.chdir(cwd)

    # by default, sys.argv is used to run mic in bootstrap
    if not argv:
        argv = sys.argv
    if argv[0] not in ('/usr/bin/mic', 'mic'):
        argv[0] = '/usr/bin/mic'

    cropts = configmgr.create
    bsopts = configmgr.bootstrap
    distro = bsopts['distro_name'].lower()

    rootdir = bsopts['rootdir']
    pkglist = bsopts['packages']
    cwd = os.getcwd()

    # create bootstrap and run mic in bootstrap
    bsenv = bootstrap.Bootstrap(rootdir, distro, cropts['arch'])
    bsenv.logfile = cropts['logfile']
    # rootdir is regenerated as a temp dir
    rootdir = bsenv.rootdir

    if 'optional' in bsopts:
        optlist = bsopts['optional']
    else:
        optlist = []

    try:
        msger.info("Creating %s bootstrap ..." % distro)
        bsenv.create(cropts['repomd'], pkglist, optlist)

        # bootstrap is relocated under "bootstrap"
        if os.path.exists(os.path.join(rootdir, "bootstrap")):
            rootdir = os.path.join(rootdir, "bootstrap")

        bsenv.dirsetup(rootdir)
        sync_mic(rootdir)

        #FIXME: sync the ks file to bootstrap
        if "/" == os.path.dirname(os.path.abspath(configmgr._ksconf)):
            safecopy(configmgr._ksconf, rootdir)

        msger.info("Start mic in bootstrap: %s\n" % rootdir)
        bindmounts = get_bindmounts(cropts)
        ret = bsenv.run(argv, cwd, rootdir, bindmounts)

    except errors.BootstrapError, err:
        msger.warning('\n%s' % err)
        if msger.ask("Switch to native mode and continue?"):
            return
        raise
Beispiel #7
0
 def wrapper(*kargs, **kwargs):
     try:
         func(*kargs, **kwargs)
     except (OSError, IOError, errors.KsError) as err:
         cfgcls = kargs[0].__class__.__name__
         if msger.ask("Failed to apply %s, skip and continue?" % cfgcls):
             msger.warning("%s" % err)
             pass
         else:
             # just throw out the exception
             raise
Beispiel #8
0
 def wrapper(*kargs, **kwargs):
     try:
         func(*kargs, **kwargs)
     except (OSError, IOError, errors.KsError), err:
         cfgcls = kargs[0].__class__.__name__
         if msger.ask("Failed to apply %s, skip and continue?" % cfgcls):
             msger.warning("%s" % err)
             pass
         else:
             # just throw out the exception
             raise
Beispiel #9
0
def read_kickstart(path):
    """Parse a kickstart file and return a KickstartParser instance.

    This is a simple utility function which takes a path to a kickstart file,
    parses it and returns a pykickstart KickstartParser instance which can
    be then passed to an ImageCreator constructor.

    If an error occurs, a CreatorError exception is thrown.
    """

    #version = ksversion.makeVersion()
    #ks = ksparser.KickstartParser(version)

    using_version = ksversion.DEVEL
    commandMap[using_version]["desktop"] = desktop.Mic_Desktop
    commandMap[using_version]["repo"] = micrepo.Mic_Repo
    commandMap[using_version]["tpk_repo"] = micrepo.Mic_Tpk_Repo
    commandMap[using_version]["bootloader"] = micboot.Mic_Bootloader
    commandMap[using_version]["part"] = partition.Mic_Partition
    commandMap[using_version]["partition"] = partition.Mic_Partition
    commandMap[using_version][
        "installerfw_plugins"] = installerfw.Mic_installerfw
    dataMap[using_version]["RepoData"] = micrepo.Mic_RepoData
    dataMap[using_version]["Tpk_RepoData"] = micrepo.Mic_Tpk_RepoData
    dataMap[using_version]["PartData"] = partition.Mic_PartData
    superclass = ksversion.returnClassForVersion(version=using_version)

    class KSHandlers(superclass):
        def __init__(self):
            superclass.__init__(self, mapping=commandMap[using_version])
            self.prepackages = ksparser.Packages()
            self.attachment = ksparser.Packages()

    ks = ksparser.KickstartParser(KSHandlers(), errorsAreFatal=False)
    ks.registerSection(PrepackageSection(ks.handler))
    ks.registerSection(AttachmentSection(ks.handler))

    try:
        ks.readKickstart(path)
    except (kserrors.KickstartParseError, kserrors.KickstartError), err:
        if msger.ask("Errors occured on kickstart file, skip and continue?"):
            msger.warning("%s" % err)
            pass
        else:
            raise errors.KsError("%s" % err)
Beispiel #10
0
def read_kickstart(path):
    """Parse a kickstart file and return a KickstartParser instance.

    This is a simple utility function which takes a path to a kickstart file,
    parses it and returns a pykickstart KickstartParser instance which can
    be then passed to an ImageCreator constructor.

    If an error occurs, a CreatorError exception is thrown.
    """

    #version = ksversion.makeVersion()
    #ks = ksparser.KickstartParser(version)

    using_version = ksversion.DEVEL
    commandMap[using_version]["desktop"] = desktop.Mic_Desktop
    commandMap[using_version]["repo"] = micrepo.Mic_Repo
    commandMap[using_version]["bootloader"] = micboot.Mic_Bootloader
    commandMap[using_version]["part"] = partition.Mic_Partition
    commandMap[using_version]["partition"] = partition.Mic_Partition
    commandMap[using_version]["installerfw_plugins"] = installerfw.Mic_installerfw
    dataMap[using_version]["RepoData"] = micrepo.Mic_RepoData
    dataMap[using_version]["PartData"] = partition.Mic_PartData
    superclass = ksversion.returnClassForVersion(version=using_version)

    class KSHandlers(superclass):
        def __init__(self):
            superclass.__init__(self, mapping=commandMap[using_version])
            self.prepackages = ksparser.Packages()
            self.attachment = ksparser.Packages()

    ks = ksparser.KickstartParser(KSHandlers(), errorsAreFatal=False)
    ks.registerSection(PrepackageSection(ks.handler))
    ks.registerSection(AttachmentSection(ks.handler))

    try:
        ks.readKickstart(path)
    except (kserrors.KickstartParseError, kserrors.KickstartError), err:
        if msger.ask("Errors occured on kickstart file, skip and continue?"):
            msger.warning("%s" % err)
            pass
        else:
            raise errors.KsError("%s" % err)
Beispiel #11
0
def checkRepositoryEULA(name, repo):
    """ This function is to check the EULA file if provided.
        return True: no EULA or accepted
        return False: user declined the EULA
    """
    proxies = {}
    proxy = repo.proxy

    if proxy:
        proxy_username = repo.proxy_username
        proxy_password = repo.proxy_password

        if proxy_username:
            proxy_netloc = urlparse.urlsplit(proxy).netloc
            if proxy_password:
                proxy = 'http://%s:%s@%s' % (proxy_username, proxy_password, proxy_netloc)
            else:
                proxy = 'http://%s@%s' % (proxy_username, proxy_netloc)

    else:
        proxy = get_proxy_for(repo.baseurl[0])

    if proxy:
        proxies = {str(repo.baseurl[0].split(':')[0]): str(proxy)}

    # download all remote files to one temp dir
    baseurl = None
    repo_lic_dir = tempfile.mkdtemp(prefix = 'repolic')

    for url in repo.baseurl:

        # try to download
        repo_eula_url = urlparse.urljoin(url, "LICENSE.txt")

        repo_eula_path = myurlgrab(repo_eula_url,
                                   os.path.join(repo_lic_dir, repo.id + '_LICENSE.txt'),
                                   proxies, ignore_404 = True)
        if repo_eula_path:
            # found
            baseurl = url
            break

    if not baseurl:
        shutil.rmtree(repo_lic_dir) #cleanup
        return True

    # show the license file
    msger.info('For the software packages in this yum repo:')
    msger.info('    %s: %s' % (name, baseurl))
    msger.info('There is an "End User License Agreement" file that need to be checked.')
    msger.info('Please read the terms and conditions outlined in it and answer the followed qustions.')
    msger.pause()

    _pager_file(repo_eula_path)

    # Asking for the "Accept/Decline"
    if not msger.ask('Would you agree to the terms and conditions outlined in the above End User License Agreement?'):
        msger.warning('Will not install pkgs from this repo.')
        shutil.rmtree(repo_lic_dir) #cleanup
        return False

    # try to find support_info.html for extra infomation
    repo_info_url = urlparse.urljoin(baseurl, "support_info.html")
    repo_info_path = myurlgrab(repo_eula_url,
                               os.path.join(repo_lic_dir, repo.id + '_LICENSE.txt'),
                               proxies, ignore_404 = True)
    if repo_info_path:
        msger.info('There is one more file in the repo for additional support information, please read it')
        msger.pause()
        _pager_file(repo_info_path)

    #cleanup
    shutil.rmtree(repo_lic_dir)
    return True
Beispiel #12
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create loop image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if creatoropts['runtime'] == "bootstrap":
            configmgr._ksconf = ksconf
            rt_util.bootstrap_mic()
        elif not rt_util.inbootstrap():
            try:
                fs_related.find_binary_path('mic-native')
            except errors.CreatorError:
                if not msger.ask("Subpackage \"mic-native\" has not been "
                                 "installed in your host system, still "
                                 "continue with \"native\" running mode?",
                                 False):
                    raise errors.Abort("Abort because subpackage 'mic-native' "
                                       "has not been installed")

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']

        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            if 'vcs' not in recording_pkgs:
                recording_pkgs.append('vcs')

        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        backends = pluginmgr.get_plugins('backend')
        if 'auto' == creatoropts['pkgmgr']:
            for key in configmgr.prefer_backends:
                if key in backends:
                    pkgmgr = backends[key]
                    break
        else:
            for key in backends.keys():
                if key == creatoropts['pkgmgr']:
                    pkgmgr = backends[key]
                    break

        if not pkgmgr:
            raise errors.CreatorError("Can't find backend: %s, "
                                      "available choices: %s" %
                                      (creatoropts['pkgmgr'],
                                       ','.join(backends.keys())))

        creator = LoopImageCreator(creatoropts,
                                   pkgmgr,
                                   opts.compress_image,
                                   opts.shrink)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        image_names = [creator.name + ".img"]
        image_names.extend(creator.get_image_names())
        self.check_image_exists(creator.destdir,
                                creator.pack_to,
                                image_names,
                                creatoropts['release'])

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.copy_kernel()
            creator.unmount()
            creator.package(creatoropts["destdir"])

            if creatoropts['release'] is not None:
                creator.release_output(ksconf,
                                       creatoropts['destdir'],
                                       creatoropts['release'])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #13
0
def chroot(chrootdir, bindmounts = None, execute = "/bin/bash"):
    def mychroot():
        os.chroot(chrootdir)
        os.chdir("/")

    if configmgr.chroot['saveto']:
        savefs = True
        saveto = configmgr.chroot['saveto']
        wrnmsg = "Can't save chroot fs for dir %s exists" % saveto
        if saveto == chrootdir:
            savefs = False
            wrnmsg = "Dir %s is being used to chroot" % saveto
        elif os.path.exists(saveto):
            if msger.ask("Dir %s already exists, cleanup and continue?" %
                         saveto):
                shutil.rmtree(saveto, ignore_errors = True)
                savefs = True
            else:
                savefs = False

        if savefs:
            msger.info("Saving image to directory %s" % saveto)
            fs_related.makedirs(os.path.dirname(os.path.abspath(saveto)))
            runner.quiet("cp -af %s %s" % (chrootdir, saveto))
            devs = ['dev/fd',
                    'dev/stdin',
                    'dev/stdout',
                    'dev/stderr',
                    'etc/mtab']
            ignlst = [os.path.join(saveto, x) for x in devs]
            map(os.unlink, filter(os.path.exists, ignlst))
        else:
            msger.warning(wrnmsg)

    dev_null = os.open("/dev/null", os.O_WRONLY)
    files_to_check = ["/bin/bash", "/sbin/init"]

    architecture_found = False

    """ Register statically-linked qemu-arm if it is an ARM fs """
    qemu_emulator = None

    for ftc in files_to_check:
        ftc = "%s/%s" % (chrootdir,ftc)

        # Return code of 'file' is "almost always" 0 based on some man pages
        # so we need to check the file existance first.
        if not os.path.exists(ftc):
            continue

        for line in runner.outs(['file', ftc]).splitlines():
            if 'ARM' in line:
                qemu_emulator = misc.setup_qemu_emulator(chrootdir, "arm")
                architecture_found = True
                break

            if 'Intel' in line:
                architecture_found = True
                break

        if architecture_found:
            break

    os.close(dev_null)
    if not architecture_found:
        raise errors.CreatorError("Failed to get architecture from any of the "
                                  "following files %s from chroot." \
                                  % files_to_check)

    try:
        msger.info("Launching shell. Exit to continue.\n"
                   "----------------------------------")
        globalmounts = setup_chrootenv(chrootdir, bindmounts)
        subprocess.call(execute, preexec_fn = mychroot, shell=True)

    except OSError, err:
        raise errors.CreatorError("chroot err: %s" % str(err))
Beispiel #14
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create raw image

        ${cmd_usage}
        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("More arguments needed")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']
        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
            name = os.path.splitext(os.path.basename(ksconf))[0]
            creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name)
        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
            if key == creatoropts['pkgmgr']:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins('backend').keys()
            raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))

        creator = raw.RawImageCreator(creatoropts, pkgmgr)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts['release'] is None:
            imagefile = "%s-sda.raw" % os.path.join(creator.destdir, creator.name)
            if os.path.exists(imagefile):
                if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile):
                    os.unlink(imagefile)
                else:
                    raise errors.Abort('Canceled')

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.unmount()
            creator.package(creatoropts["outdir"])
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #15
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create livecd image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("need one argument as the path of ks file")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        if creatoropts["arch"] and creatoropts["arch"].startswith("arm"):
            msger.warning("livecd cannot support arm images, Quit")
            return

        recording_pkgs = []
        if len(creatoropts["record_pkgs"]) > 0:
            recording_pkgs = creatoropts["record_pkgs"]

        if creatoropts["release"] is not None:
            if "name" not in recording_pkgs:
                recording_pkgs.append("name")
            ksconf = misc.save_ksconf_file(ksconf, creatoropts["release"])

        configmgr._ksconf = ksconf

        # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
        if creatoropts["release"] is not None:
            creatoropts["outdir"] = "%s/%s/images/%s/" % (
                creatoropts["outdir"],
                creatoropts["release"],
                creatoropts["name"],
            )

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins("backend").iteritems():
            if key == creatoropts["pkgmgr"]:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins("backend").keys()
            raise errors.CreatorError(
                "Can't find package manager: %s (availables: %s)" % (creatoropts["pkgmgr"], ", ".join(pkgmgrs))
            )

        if creatoropts["runtime"]:
            rt_util.runmic_in_runtime(creatoropts["runtime"], creatoropts, ksconf, None)

        creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts["release"] is None:
            imagefile = "%s.iso" % os.path.join(creator.destdir, creator.name)
            if os.path.exists(imagefile):
                if msger.ask("The target image: %s already exists, cleanup and continue?" % imagefile):
                    os.unlink(imagefile)
                else:
                    raise errors.Abort("Canceled")

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.unmount()
            creator.package(creatoropts["outdir"])
            if creatoropts["release"] is not None:
                creator.release_output(ksconf, creatoropts["outdir"], creatoropts["release"])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
    def __init__(self, createopts = None, pkgmgr = None):
        """Initialize an ImageCreator instance.

        ks -- a pykickstart.KickstartParser instance; this instance will be
              used to drive the install by e.g. providing the list of packages
              to be installed, the system configuration and %post scripts

        name -- a name for the image; used for e.g. image filenames or
                filesystem labels
        """

        self.pkgmgr = pkgmgr

        self.__builddir = None
        self.__bindmounts = []

        self.ks = None
        self.name = "target"
        self.tmpdir = "/var/tmp/mic"
        self.cachedir = "/var/tmp/mic/cache"
        self.destdir = "."
        self.target_arch = "noarch"
        self._local_pkgs_path = None

        # If the kernel is save to the destdir when copy_kernel cmd is called.
        self._need_copy_kernel = False

        # The compression method for disk image.
        self._img_compression_method = None

        if createopts:
            # Mapping table for variables that have different names.
            optmap = {"pkgmgr" : "pkgmgr_name",
                      "outdir" : "destdir",
                      "arch" : "target_arch",
                      "local_pkgs_path" : "_local_pkgs_path",
                      "compress_disk_image" : "_img_compression_method",
                      "copy_kernel" : "_need_copy_kernel",
                     }
    
            # update setting from createopts
            for key in createopts.keys():
                if key in optmap:
                    option = optmap[key]
                else:
                    option = key
                setattr(self, option, createopts[key])

            self.destdir = os.path.abspath(os.path.expanduser(self.destdir))

            if 'release' in createopts and createopts['release']:
                self.name += '-' + createopts['release']

                if os.path.exists(self.destdir):
                    if msger.ask("Image dir: %s already exists, cleanup and" \
                                 "continue?" % self.destdir):
                        shutil.rmtree(self.destdir, ignore_errors = True)
                    else:
                        raise Abort("Canceled")

                    # pending FEA: save log by default for --release

        self._dep_checks = ["ls", "bash", "cp", "echo", "modprobe", "passwd"]

        # Output image file names
        self.outimage = []

        # A flag to generate checksum
        self._genchecksum = False

        self._alt_initrd_name = None

        self._recording_pkgs = []

        # available size in root fs, init to 0
        self._root_fs_avail = 0

        # Name of the disk image file that is created.
        self._img_name = None

        self.image_format = None

        # Save qemu emulator file name in order to clean up it finally
        self.qemu_emulator = None

        # No ks provided when called by convertor, so skip the dependency check
        if self.ks:
            # If we have btrfs partition we need to check that we have toosl for those
            for part in self.ks.handler.partition.partitions:
                if part.fstype and part.fstype == "btrfs":
                    self._dep_checks.append("mkfs.btrfs")
                    break

        if self.target_arch and self.target_arch.startswith("arm"):
            for dep in self._dep_checks:
                if dep == "extlinux":
                    self._dep_checks.remove(dep)

            if not os.path.exists("/usr/bin/qemu-arm") or \
               not misc.is_statically_linked("/usr/bin/qemu-arm"):
                self._dep_checks.append("qemu-arm-static")

            if os.path.exists("/proc/sys/vm/vdso_enabled"):
                vdso_fh = open("/proc/sys/vm/vdso_enabled","r")
                vdso_value = vdso_fh.read().strip()
                vdso_fh.close()
                if (int)(vdso_value) == 1:
                    msger.warning("vdso is enabled on your host, which might "
                        "cause problems with arm emulations.\n"
                        "\tYou can disable vdso with following command before "
                        "starting image build:\n"
                        "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled")

        # make sure the specified tmpdir and cachedir exist
        if not os.path.exists(self.tmpdir):
            os.makedirs(self.tmpdir)
        if not os.path.exists(self.cachedir):
            os.makedirs(self.cachedir)

        if self._img_compression_method != None and \
           self._img_compression_method not in self.zips:
            raise CreatorError("Given disk image compression method ('%s') is "
                               "not valid. Valid values are: %s." \
                               % (self._img_compression_method,
                                  ', '.join(self.zips.keys())))
Beispiel #17
0
    def __init__(self, createopts=None, pkgmgr=None):
        """Initialize an ImageCreator instance.

        ks -- a pykickstart.KickstartParser instance; this instance will be
              used to drive the install by e.g. providing the list of packages
              to be installed, the system configuration and %post scripts

        name -- a name for the image; used for e.g. image filenames or
                filesystem labels
        """

        self.pkgmgr = pkgmgr

        self.__builddir = None
        self.__bindmounts = []

        self.ks = None
        self.name = "target"
        self.tmpdir = "/var/tmp/mic"
        self.cachedir = "/var/tmp/mic/cache"
        self.destdir = "."
        self.target_arch = "noarch"
        self._local_pkgs_path = None

        # If the kernel is save to the destdir when copy_kernel cmd is called.
        self._need_copy_kernel = False

        # The compression method for disk image.
        self._img_compression_method = None

        if createopts:
            # Mapping table for variables that have different names.
            optmap = {
                "pkgmgr": "pkgmgr_name",
                "outdir": "destdir",
                "arch": "target_arch",
                "local_pkgs_path": "_local_pkgs_path",
                "compress_disk_image": "_img_compression_method",
                "copy_kernel": "_need_copy_kernel",
            }

            # update setting from createopts
            for key in createopts.keys():
                if key in optmap:
                    option = optmap[key]
                else:
                    option = key
                setattr(self, option, createopts[key])

            self.destdir = os.path.abspath(os.path.expanduser(self.destdir))

            if 'release' in createopts and createopts['release']:
                self.name += '-' + createopts['release']

                if os.path.exists(self.destdir):
                    if msger.ask("Image dir: %s already exists, cleanup and" \
                                 "continue?" % self.destdir):
                        shutil.rmtree(self.destdir, ignore_errors=True)
                    else:
                        raise Abort("Canceled")

                    # pending FEA: save log by default for --release

        self._dep_checks = ["ls", "bash", "cp", "echo", "modprobe", "passwd"]

        # Output image file names
        self.outimage = []

        # A flag to generate checksum
        self._genchecksum = False

        self._alt_initrd_name = None

        self._recording_pkgs = []

        # available size in root fs, init to 0
        self._root_fs_avail = 0

        # Name of the disk image file that is created.
        self._img_name = None

        self.image_format = None

        # Save qemu emulator file name in order to clean up it finally
        self.qemu_emulator = None

        # No ks provided when called by convertor, so skip the dependency check
        if self.ks:
            # If we have btrfs partition we need to check that we have toosl for those
            for part in self.ks.handler.partition.partitions:
                if part.fstype and part.fstype == "btrfs":
                    self._dep_checks.append("mkfs.btrfs")
                    break

        if self.target_arch and self.target_arch.startswith("arm"):
            for dep in self._dep_checks:
                if dep == "extlinux":
                    self._dep_checks.remove(dep)

            if not os.path.exists("/usr/bin/qemu-arm") or \
               not misc.is_statically_linked("/usr/bin/qemu-arm"):
                self._dep_checks.append("qemu-arm-static")

            if os.path.exists("/proc/sys/vm/vdso_enabled"):
                vdso_fh = open("/proc/sys/vm/vdso_enabled", "r")
                vdso_value = vdso_fh.read().strip()
                vdso_fh.close()
                if (int)(vdso_value) == 1:
                    msger.warning(
                        "vdso is enabled on your host, which might "
                        "cause problems with arm emulations.\n"
                        "\tYou can disable vdso with following command before "
                        "starting image build:\n"
                        "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled")

        # make sure the specified tmpdir and cachedir exist
        if not os.path.exists(self.tmpdir):
            os.makedirs(self.tmpdir)
        if not os.path.exists(self.cachedir):
            os.makedirs(self.cachedir)

        if self._img_compression_method != None and \
           self._img_compression_method not in self.zips:
            raise CreatorError("Given disk image compression method ('%s') is "
                               "not valid. Valid values are: %s." \
                               % (self._img_compression_method,
                                  ', '.join(self.zips.keys())))
Beispiel #18
0
def checkRepositoryEULA(name, repo):
    """ This function is to check the EULA file if provided.
        return True: no EULA or accepted
        return False: user declined the EULA
    """
    proxies = {}
    proxy = repo.proxy

    if proxy:
        proxy_username = repo.proxy_username
        proxy_password = repo.proxy_password

        if proxy_username:
            proxy_netloc = urllib.parse.urlsplit(proxy).netloc
            if proxy_password:
                proxy = 'http://%s:%s@%s' % (proxy_username, proxy_password,
                                             proxy_netloc)
            else:
                proxy = 'http://%s@%s' % (proxy_username, proxy_netloc)

    else:
        proxy = get_proxy_for(repo.baseurl[0])

    if proxy:
        proxies = {str(repo.baseurl[0].split(':')[0]): str(proxy)}

    # download all remote files to one temp dir
    baseurl = None
    repo_lic_dir = tempfile.mkdtemp(prefix='repolic')

    for url in repo.baseurl:

        # try to download
        repo_eula_url = urllib.parse.urljoin(url, "LICENSE.txt")

        repo_eula_path = myurlgrab(repo_eula_url,
                                   os.path.join(repo_lic_dir,
                                                repo.id + '_LICENSE.txt'),
                                   proxies,
                                   ignore_404=True)
        if repo_eula_path:
            # found
            baseurl = url
            break

    if not baseurl:
        shutil.rmtree(repo_lic_dir)  #cleanup
        return True

    # show the license file
    msger.info('For the software packages in this yum repo:')
    msger.info('    %s: %s' % (name, baseurl))
    msger.info(
        'There is an "End User License Agreement" file that need to be checked.'
    )
    msger.info(
        'Please read the terms and conditions outlined in it and answer the followed qustions.'
    )
    msger.pause()

    _pager_file(repo_eula_path)

    # Asking for the "Accept/Decline"
    if not msger.ask(
            'Would you agree to the terms and conditions outlined in the above End User License Agreement?'
    ):
        msger.warning('Will not install pkgs from this repo.')
        shutil.rmtree(repo_lic_dir)  #cleanup
        return False

    # try to find support_info.html for extra infomation
    repo_info_url = urllib.parse.urljoin(baseurl, "support_info.html")
    repo_info_path = myurlgrab(repo_eula_url,
                               os.path.join(repo_lic_dir,
                                            repo.id + '_LICENSE.txt'),
                               proxies,
                               ignore_404=True)
    if repo_info_path:
        msger.info(
            'There is one more file in the repo for additional support information, please read it'
        )
        msger.pause()
        _pager_file(repo_info_path)

    #cleanup
    shutil.rmtree(repo_lic_dir)
    return True
Beispiel #19
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create fs image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if creatoropts['runtime'] == 'bootstrap':
            configmgr._ksconf = ksconf
            rt_util.bootstrap_mic()
        elif not rt_util.inbootstrap():
            try:
                fs_related.find_binary_path('mic-native')
            except errors.CreatorError:
                if not msger.ask(
                        "Subpackage \"mic-native\" has not been "
                        "installed in your host system, still "
                        "continue with \"native\" running mode?", False):
                    raise errors.Abort("Abort because subpackage 'mic-native' "
                                       "has not been installed")

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']

        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            if 'vcs' not in recording_pkgs:
                recording_pkgs.append('vcs')

        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        backends = pluginmgr.get_plugins('backend')
        if 'auto' == creatoropts['pkgmgr']:
            for key in configmgr.prefer_backends:
                if key in backends:
                    pkgmgr = backends[key]
                    break
        else:
            for key in backends.keys():
                if key == creatoropts['pkgmgr']:
                    pkgmgr = backends[key]
                    break

        if not pkgmgr:
            raise errors.CreatorError(
                "Can't find backend: %s, "
                "available choices: %s" %
                (creatoropts['pkgmgr'], ','.join(backends.keys())))

        creator = fs.FsImageCreator(creatoropts, pkgmgr)
        creator._include_src = opts.include_src

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        self.check_image_exists(creator.destdir, creator.pack_to,
                                [creator.name], creatoropts['release'])

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            #Download the source packages ###private options
            if opts.include_src:
                installed_pkgs = creator.get_installed_packages()
                msger.info(
                    '--------------------------------------------------')
                msger.info(
                    'Generating the image with source rpms included ...')
                if not misc.SrcpkgsDownload(
                        installed_pkgs, creatoropts["repomd"],
                        creator._instroot, creatoropts["cachedir"]):
                    msger.warning("Source packages can't be downloaded")

            creator.configure(creatoropts["repomd"])
            creator.copy_kernel()
            creator.unmount()
            creator.package(creatoropts["destdir"])
            creator.create_manifest()
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['destdir'],
                                       creatoropts['release'])
            creator.print_outimage_info()
        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #20
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create fs image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if creatoropts['runtime'] == 'bootstrap':
            configmgr._ksconf = ksconf
            rt_util.bootstrap_mic()
        elif not rt_util.inbootstrap():
            try:
                fs_related.find_binary_path('mic-native')
            except errors.CreatorError:
                if not msger.ask("Subpackage \"mic-native\" has not been "
                                 "installed in your host system, still "
                                 "continue with \"native\" running mode?",
                                 False):
                    raise errors.Abort("Abort because subpackage 'mic-native' "
                                       "has not been installed")

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']

        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            if 'vcs' not in recording_pkgs:
                recording_pkgs.append('vcs')

        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        backends = pluginmgr.get_plugins('backend')
        if 'auto' == creatoropts['pkgmgr']:
            for key in configmgr.prefer_backends:
                if key in backends:
                    pkgmgr = backends[key]
                    break
        else:
            for key in backends.keys():
                if key == creatoropts['pkgmgr']:
                    pkgmgr = backends[key]
                    break

        if not pkgmgr:
            raise errors.CreatorError("Can't find backend: %s, "
                                      "available choices: %s" %
                                      (creatoropts['pkgmgr'],
                                       ','.join(backends.keys())))

        creator = fs.FsImageCreator(creatoropts, pkgmgr)
        creator._include_src = opts.include_src

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        self.check_image_exists(creator.destdir,
                                creator.pack_to,
                                [creator.name],
                                creatoropts['release'])

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            #Download the source packages ###private options
            if opts.include_src:
                installed_pkgs =  creator.get_installed_packages()
                msger.info('--------------------------------------------------')
                msger.info('Generating the image with source rpms included ...')
                if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
                    msger.warning("Source packages can't be downloaded")

            creator.configure(creatoropts["repomd"])
            creator.copy_kernel()
            creator.unmount()
            creator.package(creatoropts["destdir"])
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['destdir'],
                        creatoropts['release'])
            creator.print_outimage_info()
        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create loop image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("need one argument as the path of ks file")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']

        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])

        configmgr._ksconf = ksconf

        # Called After setting the configmgr._ksconf
        # as the creatoropts['name'] is reset there.
        if creatoropts['release'] is not None:
            creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'],
                                                          creatoropts['release'],
                                                          creatoropts['name'])

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
            if key == creatoropts['pkgmgr']:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins('backend').keys()
            raise errors.CreatorError("Can't find package manager: %s "
                                      "(availables: %s)" \
                                      % (creatoropts['pkgmgr'],
                                         ', '.join(pkgmgrs)))

        if creatoropts['runtime']:
            rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts, ksconf, None)

        creator = LoopImageCreator(creatoropts, pkgmgr, opts.compress_to)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts['release'] is None:
            if opts.compress_to:
                imagefile = "%s" % os.path.join(creator.destdir, creator.compress_to)
            else:
                imagefile = "%s.img" % os.path.join(creator.destdir, creator.name)

            if os.path.exists(imagefile):
                if msger.ask('The target image: %s already exists, cleanup '
                             'and continue?' % imagefile):
                    os.unlink(imagefile)
                else:
                    raise errors.Abort('Canceled')

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.copy_kernel()            
            creator.unmount()
            creator.package(creatoropts["outdir"])

            if creatoropts['release'] is not None:
                creator.release_output(ksconf, 
                                       creatoropts['outdir'],
                                       creatoropts['release'])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #22
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create fs image

        ${cmd_usage}
        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("More arguments needed")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']
        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
            name = os.path.splitext(os.path.basename(ksconf))[0]
            creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name)
        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
            if key == creatoropts['pkgmgr']:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins('backend').keys()
            raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))

        creator = fs.FsImageCreator(creatoropts, pkgmgr)
        creator._include_src = opts.include_src

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts['release'] is None:
            fsdir = os.path.join(creator.destdir, creator.name)
            if os.path.exists(fsdir):
                if msger.ask('The target dir: %s already exists, cleanup and continue?' % fsdir):
                    import shutil
                    shutil.rmtree(fsdir)
                else:
                    raise errors.Abort('Canceled')

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            #Download the source packages ###private options
            if opts.include_src:
                installed_pkgs =  creator.get_installed_packages()
                msger.info('--------------------------------------------------')
                msger.info('Generating the image with source rpms included, The number of source packages is %d.' %(len(installed_pkgs)))
                if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
                    msger.warning("Source packages can't be downloaded")

            creator.configure(creatoropts["repomd"])
            creator.unmount()
            creator.package(creatoropts["outdir"])
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
            creator.print_outimage_info()
        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #23
0
    # show the license file
    msger.info('For the software packages in this yum repo:')
    msger.info('    %s: %s' % (name, baseurl))
    msger.info(
        'There is an "End User License Agreement" file that need to be checked.'
    )
    msger.info(
        'Please read the terms and conditions outlined in it and answer the followed qustions.'
    )
    msger.pause()

    _pager_file(repo_eula_path)

    # Asking for the "Accept/Decline"
    if not msger.ask(
            'Would you agree to the terms and conditions outlined in the above End User License Agreement?'
    ):
        msger.warning('Will not install pkgs from this repo.')
        shutil.rmtree(repo_lic_dir)  #cleanup
        return False

    # try to find support_info.html for extra infomation
    repo_info_url = urlparse.urljoin(baseurl, "support_info.html")
    repo_info_path = _check_and_download_url(
        u2opener, repo_info_url,
        os.path.join(repo_lic_dir, repo.id + '_support_info.html'))
    if repo_info_path:
        msger.info(
            'There is one more file in the repo for additional support information, please read it'
        )
        msger.pause()
    if not baseurl:
        shutil.rmtree(repo_lic_dir) #cleanup
        return True

    # show the license file
    msger.info('For the software packages in this yum repo:')
    msger.info('    %s: %s' % (name, baseurl))
    msger.info('There is an "End User License Agreement" file that need to be checked.')
    msger.info('Please read the terms and conditions outlined in it and answer the followed qustions.')
    msger.pause()

    _pager_file(repo_eula_path)

    # Asking for the "Accept/Decline"
    if not msger.ask('Would you agree to the terms and conditions outlined in the above End User License Agreement?'):
        msger.warning('Will not install pkgs from this repo.')
        shutil.rmtree(repo_lic_dir) #cleanup
        return False

    # try to find support_info.html for extra infomation
    repo_info_url = urlparse.urljoin(baseurl, "support_info.html")
    repo_info_path = _check_and_download_url(
                            u2opener,
                            repo_info_url,
                            os.path.join(repo_lic_dir, repo.id + '_support_info.html'))
    if repo_info_path:
        msger.info('There is one more file in the repo for additional support information, please read it')
        msger.pause()
        _pager_file(repo_info_path)
Beispiel #25
0
    def __init__(self, createopts=None, pkgmgr=None):
        """Initialize an ImageCreator instance.

        ks -- a pykickstart.KickstartParser instance; this instance will be
              used to drive the install by e.g. providing the list of packages
              to be installed, the system configuration and %post scripts

        name -- a name for the image; used for e.g. image filenames or
                filesystem labels
        """

        self.pkgmgr = pkgmgr

        self.__builddir = None
        self.__bindmounts = []

        # Eeach image type can change these values as they might be image type
        # specific
        if not hasattr(self, "_valid_compression_methods"):
            self._valid_compression_methods = ["bz2"]

        # The compression method for disk image.
        self._img_compression_method = None

        if createopts:
            # A pykickstart.KickstartParser instance."""
            self.ks = createopts["ks"]

            self.destdir = os.path.abspath(os.path.expanduser(createopts["outdir"]))

            # A name for the image."""
            self.name = createopts["name"]
            if createopts["release"]:
                self.name += "-" + createopts["release"]

                # check whether destine dir exist
                if os.path.exists(self.destdir):
                    if msger.ask("Image dir: %s already exists, cleanup and continue?" % self.destdir):
                        shutil.rmtree(self.destdir, ignore_errors=True)
                    else:
                        raise Abort("Canceled")

                # pending FEA: save log by default for --release

            # The directory in which all temporary files will be created."""
            self.tmpdir = createopts["tmpdir"]
            self.cachedir = createopts["cachedir"]
            self.target_arch = createopts["arch"]
            self._local_pkgs_path = createopts["local_pkgs_path"]
            self._img_compression_method = createopts["compress_disk_image"]

        else:
            self.ks = None
            self.name = "target"
            self.tmpdir = "/var/tmp/mic"
            self.cachedir = "/var/tmp/mic/cache"
            self.destdir = "."
            self.target_arch = "noarch"
            self._local_pkgs_path = None

        self._dep_checks = ["ls", "bash", "cp", "echo", "modprobe", "passwd"]

        # FIXME to be obsolete, make it configurable
        self.distro_name = "Tizen"

        # Output image file names
        self.outimage = []

        # A flag to generate checksum
        self._genchecksum = False

        self._alt_initrd_name = None

        self._recording_pkgs = []

        # available size in root fs, init to 0
        self._root_fs_avail = 0

        # Name of the disk image file that is created.
        self._img_name = None

        self.image_format = None

        # Save qemu emulator file name in order to clean up it finally
        self.qemu_emulator = None

        # No ks provided when called by convertor, so skip the dependency check
        if self.ks:
            # If we have btrfs partition we need to check that we have toosl for those
            for part in self.ks.handler.partition.partitions:
                if part.fstype and part.fstype == "btrfs":
                    self._dep_checks.append("mkfs.btrfs")
                    break

        if self.target_arch and self.target_arch.startswith("arm"):
            for dep in self._dep_checks:
                if dep == "extlinux":
                    self._dep_checks.remove(dep)

            if not os.path.exists("/usr/bin/qemu-arm") or not misc.is_statically_linked("/usr/bin/qemu-arm"):
                self._dep_checks.append("qemu-arm-static")

            if os.path.exists("/proc/sys/vm/vdso_enabled"):
                vdso_fh = open("/proc/sys/vm/vdso_enabled", "r")
                vdso_value = vdso_fh.read().strip()
                vdso_fh.close()
                if (int)(vdso_value) == 1:
                    msger.warning(
                        "vdso is enabled on your host, which might cause problems with arm emulations.\n"
                        "\tYou can disable vdso with following command before starting image build:\n"
                        "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled"
                    )

        # make sure the specified tmpdir and cachedir exist
        if not os.path.exists(self.tmpdir):
            os.makedirs(self.tmpdir)
        if not os.path.exists(self.cachedir):
            os.makedirs(self.cachedir)

        if self._img_compression_method != None and self._img_compression_method not in self._valid_compression_methods:
            raise CreatorError(
                "Given disk image compression method ('%s') is not valid. Valid values are '%s'."
                % (self._img_compression_method, " ".join(self._valid_compression_methods))
            )
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create fs image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("need one argument as the path of ks file")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']
        
        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])

        configmgr._ksconf = ksconf
    
        # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
        if creatoropts['release'] is not None:
            creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
            if key == creatoropts['pkgmgr']:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins('backend').keys()
            raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))

        if creatoropts['runtime']:
            rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts, ksconf, None)

        creator = fs.FsImageCreator(creatoropts, pkgmgr)
        creator._include_src = opts.include_src

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts['release'] is None:
            fsdir = os.path.join(creator.destdir, creator.name)
            if os.path.exists(fsdir):
                if msger.ask('The target dir: %s already exists, cleanup and continue?' % fsdir):
                    import shutil
                    shutil.rmtree(fsdir)
                else:
                    raise errors.Abort('Canceled')

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            #Download the source packages ###private options
            if opts.include_src:
                installed_pkgs =  creator.get_installed_packages()
                msger.info('--------------------------------------------------')
                msger.info('Generating the image with source rpms included ...')
                if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
                    msger.warning("Source packages can't be downloaded")

            creator.configure(creatoropts["repomd"])
            creator.copy_kernel()
            creator.unmount()
            creator.package(creatoropts["outdir"])
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
            creator.print_outimage_info()
        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #27
0
    def __init__(self, createopts = None, pkgmgr = None):
        """Initialize an ImageCreator instance.

        ks -- a pykickstart.KickstartParser instance; this instance will be
              used to drive the install by e.g. providing the list of packages
              to be installed, the system configuration and %post scripts

        name -- a name for the image; used for e.g. image filenames or
                filesystem labels
        """

        self.pkgmgr = pkgmgr

        self.__builddir = None
        self.__bindmounts = []

        # Eeach image type can change these values as they might be image type
        # specific
        if not hasattr(self, "_valid_compression_methods"):
            self._valid_compression_methods = ['bz2']

        # The compression method for disk image.
        self._img_compression_method = None

        if createopts:
            # A pykickstart.KickstartParser instance."""
            self.ks = createopts['ks']

            self.destdir = os.path.abspath(os.path.expanduser(createopts["outdir"]))

            # A name for the image."""
            self.name = createopts['name']
            if createopts['release']:
                self.name += '-' + createopts['release']

                # check whether destine dir exist
                if os.path.exists(self.destdir):
                    if msger.ask('Image dir: %s already exists, cleanup and continue?' % self.destdir):
                        shutil.rmtree(self.destdir, ignore_errors = True)
                    else:
                        raise Abort('Canceled')

                # pending FEA: save log by default for --release

            # The directory in which all temporary files will be created."""
            self.tmpdir = createopts['tmpdir']
            self.cachedir = createopts['cachedir']
            self.target_arch = createopts['arch']
            self._local_pkgs_path = createopts['local_pkgs_path']
            self._img_compression_method = createopts['compress_disk_image']

        else:
            self.ks = None
            self.name = "target"
            self.tmpdir = "/var/tmp/mic"
            self.cachedir = "/var/tmp/mic/cache"
            self.destdir = "."
            self.target_arch = "noarch"
            self._local_pkgs_path = None

        self._dep_checks = ["ls", "bash", "cp", "echo", "modprobe", "passwd"]

        #FIXME to be obsolete, make it configurable
        self.distro_name = "Tizen"

        # Output image file names
        self.outimage = []

        # A flag to generate checksum
        self._genchecksum = False

        self._alt_initrd_name = None

        self._recording_pkgs = []

        # available size in root fs, init to 0
        self._root_fs_avail = 0

        # Name of the disk image file that is created.
        self._img_name = None

        self.image_format = None

        # Save qemu emulator file name in order to clean up it finally
        self.qemu_emulator = None

        # No ks provided when called by convertor, so skip the dependency check
        if self.ks:
            # If we have btrfs partition we need to check that we have toosl for those
            for part in self.ks.handler.partition.partitions:
                if part.fstype and part.fstype == "btrfs":
                    self._dep_checks.append("mkfs.btrfs")
                    break

        if self.target_arch and self.target_arch.startswith("arm"):
            for dep in self._dep_checks:
                if dep == "extlinux":
                    self._dep_checks.remove(dep)

            if not os.path.exists("/usr/bin/qemu-arm") or not misc.is_statically_linked("/usr/bin/qemu-arm"):
                self._dep_checks.append("qemu-arm-static")

            if os.path.exists("/proc/sys/vm/vdso_enabled"):
                vdso_fh = open("/proc/sys/vm/vdso_enabled","r")
                vdso_value = vdso_fh.read().strip()
                vdso_fh.close()
                if (int)(vdso_value) == 1:
                    msger.warning("vdso is enabled on your host, which might cause problems with arm emulations.\n"
                                  "\tYou can disable vdso with following command before starting image build:\n"
                                  "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled")

        # make sure the specified tmpdir and cachedir exist
        if not os.path.exists(self.tmpdir):
            os.makedirs(self.tmpdir)
        if not os.path.exists(self.cachedir):
            os.makedirs(self.cachedir)

        if self._img_compression_method != None and self._img_compression_method not in self._valid_compression_methods:
            raise CreatorError("Given disk image compression method ('%s') is not valid. Valid values are '%s'." % (self._img_compression_method, ' '.join(self._valid_compression_methods)))
Beispiel #28
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create liveusb image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if creatoropts['runtime'] == "bootstrap":
            configmgr._ksconf = ksconf
            rt_util.bootstrap_mic()
        elif not rt_util.inbootstrap():
            try:
                fs_related.find_binary_path('mic-native')
            except errors.CreatorError:
                if not msger.ask(
                        "Subpackage \"mic-native\" has not been "
                        "installed in your host system, still "
                        "continue with \"native\" running mode?", False):
                    raise errors.Abort("Abort because subpackage 'mic-native' "
                                       "has not been installed")

        if creatoropts['arch'] and creatoropts['arch'].startswith('arm'):
            msger.warning('liveusb cannot support arm images, Quit')
            return

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']

        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            if 'vcs' not in recording_pkgs:
                recording_pkgs.append('vcs')

        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        backends = pluginmgr.get_plugins('backend')
        if 'auto' == creatoropts['pkgmgr']:
            for key in configmgr.prefer_backends:
                if key in backends:
                    pkgmgr = backends[key]
                    break
        else:
            for key in backends.keys():
                if key == creatoropts['pkgmgr']:
                    pkgmgr = backends[key]
                    break

        if not pkgmgr:
            raise errors.CreatorError(
                "Can't find backend: %s, "
                "available choices: %s" %
                (creatoropts['pkgmgr'], ','.join(backends.keys())))

        creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        self.check_image_exists(creator.destdir, creator.pack_to,
                                [creator.name + ".usbimg"],
                                creatoropts['release'])
        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.copy_kernel()
            creator.unmount()
            creator.package(creatoropts["destdir"])
            creator.create_manifest()
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['destdir'],
                                       creatoropts['release'])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #29
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create loop image

        ${cmd_usage}
        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("More arguments needed")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']
        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
            name = os.path.splitext(os.path.basename(ksconf))[0]
            creatoropts['outdir'] = "%s/%s/images/%s/" % (
                creatoropts['outdir'], creatoropts['release'], name)
        configmgr._ksconf = ksconf

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
            if key == creatoropts['pkgmgr']:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins('backend').keys()
            raise errors.CreatorError(
                "Can't find package manager: %s (availables: %s)" %
                (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))

        creator = loop.LoopImageCreator(creatoropts, pkgmgr, opts.taring_to)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts['release'] is None:
            if opts.taring_to:
                imagefile = "%s.tar" % os.path.join(creator.destdir,
                                                    opts.taring_to)
            else:
                imagefile = "%s.img" % os.path.join(creator.destdir,
                                                    creator.name)
            if os.path.exists(imagefile):
                if msger.ask(
                        'The target image: %s already exists, cleanup and continue?'
                        % imagefile):
                    os.unlink(imagefile)
                else:
                    raise errors.Abort('Canceled')

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.unmount()
            creator.package(creatoropts["outdir"])

            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['outdir'],
                                       creatoropts['release'])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #30
0
    def do_create(self, subcmd, opts, *args):
        """${cmd_name}: create raw image

        Usage:
            ${name} ${cmd_name} <ksfile> [OPTS]

        ${cmd_option_list}
        """

        if not args:
            raise errors.Usage("need one argument as the path of ks file")

        if len(args) != 1:
            raise errors.Usage("Extra arguments given")

        creatoropts = configmgr.create
        ksconf = args[0]

        if not os.path.exists(ksconf):
            raise errors.CreatorError("Can't find the file: %s" % ksconf)

        recording_pkgs = []
        if len(creatoropts['record_pkgs']) > 0:
            recording_pkgs = creatoropts['record_pkgs']

        if creatoropts['release'] is not None:
            if 'name' not in recording_pkgs:
                recording_pkgs.append('name')
            ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])

        configmgr._ksconf = ksconf

        # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
        if creatoropts['release'] is not None:
            creatoropts['outdir'] = "%s/%s/images/%s/" % (
                creatoropts['outdir'], creatoropts['release'],
                creatoropts['name'])

        # try to find the pkgmgr
        pkgmgr = None
        for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
            if key == creatoropts['pkgmgr']:
                pkgmgr = pcls
                break

        if not pkgmgr:
            pkgmgrs = pluginmgr.get_plugins('backend').keys()
            raise errors.CreatorError(
                "Can't find package manager: %s (availables: %s)" %
                (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))

        if creatoropts['runtime']:
            rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts,
                                      ksconf, None)

        creator = raw.RawImageCreator(creatoropts, pkgmgr)

        if len(recording_pkgs) > 0:
            creator._recording_pkgs = recording_pkgs

        if creatoropts['release'] is None:
            for item in creator.get_diskinfo():
                imagefile = "%s-%s.raw" % (os.path.join(
                    creator.destdir, creator.name), item['name'])
                if os.path.exists(imagefile):
                    if msger.ask(
                            'The target image: %s already exists, cleanup and continue?'
                            % imagefile):
                        os.unlink(imagefile)
                    else:
                        raise errors.Abort('Canceled')

        try:
            creator.check_depend_tools()
            creator.mount(None, creatoropts["cachedir"])
            creator.install()
            creator.configure(creatoropts["repomd"])
            creator.unmount()
            creator.package(creatoropts["outdir"])
            if creatoropts['release'] is not None:
                creator.release_output(ksconf, creatoropts['outdir'],
                                       creatoropts['release'])
            creator.print_outimage_info()

        except errors.CreatorError:
            raise
        finally:
            creator.cleanup()

        msger.info("Finished.")
        return 0
Beispiel #31
0
def chroot(chrootdir, bindmounts=None, execute="/bin/bash"):
    def mychroot():
        os.chroot(chrootdir)
        os.chdir("/")

    if configmgr.chroot['saveto']:
        savefs = True
        saveto = configmgr.chroot['saveto']
        wrnmsg = "Can't save chroot fs for dir %s exists" % saveto
        if saveto == chrootdir:
            savefs = False
            wrnmsg = "Dir %s is being used to chroot" % saveto
        elif os.path.exists(saveto):
            if msger.ask("Dir %s already exists, cleanup and continue?" %
                         saveto):
                shutil.rmtree(saveto, ignore_errors=True)
                savefs = True
            else:
                savefs = False

        if savefs:
            msger.info("Saving image to directory %s" % saveto)
            runner.quiet("cp -af %s %s" % (chrootdir, saveto))
            devs = [
                'dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr', 'etc/mtab'
            ]
            ignlst = [os.path.join(saveto, x) for x in devs]
            map(os.unlink, filter(os.path.exists, ignlst))
        else:
            msger.warning(wrnmsg)

    dev_null = os.open("/dev/null", os.O_WRONLY)
    files_to_check = ["/bin/bash", "/sbin/init"]

    architecture_found = False
    """ Register statically-linked qemu-arm if it is an ARM fs """
    qemu_emulator = None

    for ftc in files_to_check:
        ftc = "%s/%s" % (chrootdir, ftc)

        # Return code of 'file' is "almost always" 0 based on some man pages
        # so we need to check the file existance first.
        if not os.path.exists(ftc):
            continue

        for line in runner.outs(['file', ftc]).splitlines():
            if 'ARM' in line:
                qemu_emulator = misc.setup_qemu_emulator(chrootdir, "arm")
                architecture_found = True
                break

            if 'Intel' in line:
                architecture_found = True
                break

        if architecture_found:
            break

    os.close(dev_null)
    if not architecture_found:
        raise errors.CreatorError("Failed to get architecture from any of the "
                                  "following files %s from chroot." \
                                  % files_to_check)

    try:
        msger.info("Launching shell. Exit to continue.\n"
                   "----------------------------------")
        globalmounts = setup_chrootenv(chrootdir, bindmounts)
        subprocess.call(execute, preexec_fn=mychroot, shell=True)

    except OSError, err:
        raise errors.CreatorError("chroot err: %s" % str(err))