From 201807ffd591d9674bc0b51f7a3bc88c5dcafd77 Mon Sep 17 00:00:00 2001 From: JF Ding Date: Thu, 1 Sep 2011 15:56:49 +0900 Subject: [PATCH] refine the livecd plugin code and cleanup unused part --- mic/imager/livecd.py | 196 +--------------------------------------- plugins/imager/livecd_plugin.py | 83 ++++++++++------- 2 files changed, 56 insertions(+), 223 deletions(-) diff --git a/mic/imager/livecd.py b/mic/imager/livecd.py index ea6b2aa..70e05d7 100644 --- a/mic/imager/livecd.py +++ b/mic/imager/livecd.py @@ -22,14 +22,11 @@ import glob import shutil import subprocess -import mic.utils.fs_related as fs_related -import mic.utils.rpmmisc as rpmmisc -import mic.utils.misc as misc -from mic.utils.errors import * -from loop import LoopImageCreator -from mic import kickstart -from mic import msger +from mic import kickstart, msger +from mic.utils import fs_related, rpmmisc +from mic.utils.errors import CreatorError +from loop import LoopImageCreator class LiveImageCreatorBase(LoopImageCreator): """A base class for LiveCD image creators. @@ -659,194 +656,11 @@ hiddenmenu self._configure_syslinux_bootloader(isodir) self._configure_efi_bootloader(isodir) -class ppcLiveImageCreator(LiveImageCreatorBase): - def _get_mkisofs_options(self, isodir): - return [ "-hfs", "-nodesktop", "-part" - "-map", isodir + "/ppc/mapping", - "-hfs-bless", isodir + "/ppc/mac", - "-hfs-volid", self.fslabel ] - - def _get_required_packages(self): - return ["yaboot"] + \ - LiveImageCreatorBase._get_required_packages(self) - - def _get_excluded_packages(self): - # kind of hacky, but exclude memtest86+ on ppc so it can stay in cfg - return ["memtest86+"] + \ - LiveImageCreatorBase._get_excluded_packages(self) - - def __copy_boot_file(self, destdir, file): - for dir in ["/usr/share/ppc64-utils", - "/usr/lib/moblin-installer-runtime/boot"]: - path = self._instroot + dir + "/" + file - if not os.path.exists(path): - continue - - fs_related.makedirs(destdir) - shutil.copy(path, destdir) - return - - raise CreatorError("Unable to find boot file " + file) - - def __kernel_bits(self, kernel): - testpath = (self._instroot + "/lib/modules/" + - kernel + "/kernel/arch/powerpc/platforms") - - if not os.path.exists(testpath): - return { "32" : True, "64" : False } - else: - return { "32" : False, "64" : True } - - def __copy_kernel_and_initramfs(self, destdir, version): - bootdir = self._instroot + "/boot" - - fs_related.makedirs(destdir) - - shutil.copyfile(bootdir + "/vmlinuz-" + version, - destdir + "/vmlinuz") - - shutil.copyfile(bootdir + "/initrd-" + version + ".img", - destdir + "/initrd.img") - - def __get_basic_yaboot_config(self, **args): - return """ -init-message = "Welcome to %(distroname)s!" -timeout=%(timeout)d -""" % args - - def __get_image_stanza(self, **args): - return """ - -image=/ppc/ppc%(bit)s/vmlinuz - label=%(short)s - initrd=/ppc/ppc%(bit)s/initrd.img - read-only - append="root=CDLABEL=%(fslabel)s rootfstype=iso9660 %(liveargs)s %(extra)s" -""" % args - - - def __write_yaboot_config(isodir, bit): - cfg = self.__get_basic_yaboot_config(name = self.name, - timeout = self._timeout * 100, - distroname = self.distro_name) - - kernel_options = self._get_kernel_options() - - cfg += self.__get_image_stanza(fslabel = self.fslabel, - short = "linux", - long = "Run from image", - extra = "", - bit = bit, - liveargs = kernel_options) - - if self._has_checkisomd5(): - cfg += self.__get_image_stanza(fslabel = self.fslabel, - short = "check", - long = "Verify and run from image", - extra = "check", - bit = bit, - liveargs = kernel_options) - - f = open(isodir + "/ppc/ppc" + bit + "/yaboot.conf", "w") - f.write(cfg) - f.close() - - def __write_not_supported(isodir, bit): - fs_related.makedirs(isodir + "/ppc/ppc" + bit) - - message = "Sorry, this LiveCD does not support your hardware" - - f = open(isodir + "/ppc/ppc" + bit + "/yaboot.conf", "w") - f.write('init-message = "' + message + '"') - f.close() - - - def __write_dualbits_yaboot_config(isodir, **args): - cfg = """ -init-message = "\nWelcome to %(name)s!\nUse 'linux32' for 32-bit kernel.\n\n" -timeout=%(timeout)d -default=linux - -image=/ppc/ppc64/vmlinuz - label=linux64 - alias=linux - initrd=/ppc/ppc64/initrd.img - read-only - -image=/ppc/ppc32/vmlinuz - label=linux32 - initrd=/ppc/ppc32/initrd.img - read-only -""" % args - - f = open(isodir + "/etc/yaboot.conf", "w") - f.write(cfg) - f.close() - - def _configure_bootloader(self, isodir): - """configure the boot loader""" - havekernel = { 32: False, 64: False } - - self.__copy_boot_file("mapping", isodir + "/ppc") - self.__copy_boot_file("bootinfo.txt", isodir + "/ppc") - self.__copy_boot_file("ofboot.b", isodir + "/ppc/mac") - - shutil.copyfile(self._instroot + "/usr/lib/yaboot/yaboot", - isodir + "/ppc/mac/yaboot") - - fs_related.makedirs(isodir + "/ppc/chrp") - shutil.copyfile(self._instroot + "/usr/lib/yaboot/yaboot", - isodir + "/ppc/chrp/yaboot") - - subprocess.call(["/usr/sbin/addnote", isodir + "/ppc/chrp/yaboot"]) - - # - # FIXME: ppc should support multiple kernels too... - # - kernel = self._get_kernel_versions().values()[0][0] - - kernel_bits = self.__kernel_bits(kernel) - - for (bit, present) in kernel_bits.items(): - if not present: - self.__write_not_supported(isodir, bit) - continue - - self.__copy_kernel_and_initramfs(isodir + "/ppc/ppc" + bit, kernel) - self.__write_yaboot_config(isodir, bit) - - fs_related.makedirs(isodir + "/etc") - if kernel_bits["32"] and not kernel_bits["64"]: - shutil.copyfile(isodir + "/ppc/ppc32/yaboot.conf", - isodir + "/etc/yaboot.conf") - elif kernel_bits["64"] and not kernel_bits["32"]: - shutil.copyfile(isodir + "/ppc/ppc64/yaboot.conf", - isodir + "/etc/yaboot.conf") - else: - self.__write_dualbits_yaboot_config(isodir, - name = self.name, - timeout = self._timeout * 100) - - # - # FIXME: build 'netboot' images with kernel+initrd, like mk-images.ppc - # - -class ppc64LiveImageCreator(ppcLiveImageCreator): - def _get_excluded_packages(self): - # FIXME: - # while kernel.ppc and kernel.ppc64 co-exist, - # we can't have both - return ["kernel.ppc"] + \ - ppcLiveImageCreator._get_excluded_packages(self) - arch = rpmmisc.getBaseArch() if arch in ("i386", "x86_64"): LiveCDImageCreator = x86LiveImageCreator -elif arch in ("ppc",): - LiveCDImageCreator = ppcLiveImageCreator -elif arch in ("ppc64",): - LiveCDImageCreator = ppc64LiveImageCreator elif arch.startswith("arm"): LiveCDImageCreator = LiveImageCreatorBase + else: raise CreatorError("Architecture not supported!") diff --git a/plugins/imager/livecd_plugin.py b/plugins/imager/livecd_plugin.py index b9e69a7..76303be 100644 --- a/plugins/imager/livecd_plugin.py +++ b/plugins/imager/livecd_plugin.py @@ -1,20 +1,32 @@ -#!/usr/bin/python -import os, sys +#!/usr/bin/python -tt +# +# Copyright 2011 Intel, Inc. +# +# This copyrighted material is made available to anyone wishing to use, modify, +# copy, or redistribute it subject to the terms and conditions of the GNU +# General Public License v.2. This program is distributed in the hope that it +# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the +# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat +# trademarks that are incorporated in the source code or documentation are not +# subject to the GNU General Public License and may only be used or replicated +# with the express permission of Red Hat, Inc. +# + +import os import subprocess import shutil import tempfile -import mic.chroot as chroot -import mic.utils.misc as misc -import mic.utils.fs_related as fs_related -import mic.utils.cmdln as cmdln -import mic.configmgr as configmgr -import mic.pluginmgr as pluginmgr +from mic import configmgr, pluginmgr, chroot, msger +from mic.utils import misc, fs_related, errors import mic.imager.livecd as livecd -from mic.utils.errors import * from mic.pluginbase import ImagerPlugin - class LiveCDPlugin(ImagerPlugin): name = 'livecd' @@ -25,8 +37,10 @@ class LiveCDPlugin(ImagerPlugin): ${cmd_usage} ${cmd_option_list} """ - if len(args) == 0: - return + + if not args: + raise errors.Usage("More arguments needed") + if len(args) == 1: ksconf = args[0] else: @@ -45,7 +59,7 @@ class LiveCDPlugin(ImagerPlugin): break if not pkgmgr: - raise CreatorError("Can't find backend %s" % pkgmgr) + raise errors.CreatorError("Can't find backend %s" % pkgmgr) creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr) try: @@ -58,44 +72,47 @@ class LiveCDPlugin(ImagerPlugin): outimage = creator.outimage creator.print_outimage_info() outimage = creator.outimage + except CreatorError, e: - raise CreatorError("failed to create image : %s" % e) + raise errors.CreatorError("failed to create image : %s" % e) + finally: creator.cleanup() -# if not creatoropts["image_info"]: - print "Finished." + + msger.info("Finished.") return 0 @classmethod def do_chroot(cls, target): os_image = cls.do_unpack(target) os_image_dir = os.path.dirname(os_image) - #unpack image to target dir + + # unpack image to target dir imgsize = misc.get_file_size(os_image) * 1024L * 1024L extmnt = misc.mkdtemp() tfstype = "ext3" tlabel = "ext3 label" MyDiskMount = fs_related.ExtDiskMount - #if misc.fstype_is_btrfs(os_image): - # tfstype = "btrfs" - # tlabel = "btrfs label" - # MyDiskMount = fs_related.BtrfsDiskMount extloop = MyDiskMount(fs_related.SparseLoopbackDisk(os_image, imgsize), - extmnt, - tfstype, - 4096, - tlabel) + extmnt, + tfstype, + 4096, + tlabel) try: extloop.mount() + except MountError, e: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors = True) shutil.rmtree(os_image_dir, ignore_errors = True) - raise CreatorError("Failed to loopback mount '%s' : %s" %(os_image, e)) + raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(os_image, e)) + try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") + except: - raise CreatorError("Failed to chroot to %s." %target) + raise errors.CreatorError("Failed to chroot to %s." %target) + finally: chroot.cleanup_after_chroot("img", extloop, os_image_dir, extmnt) @@ -107,7 +124,7 @@ class LiveCDPlugin(ImagerPlugin): try: subprocess.call(args, preexec_fn = instance._chroot) except OSError, (err, msg): - raise CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg) + raise errors.CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg) def __run_post_cleanups(instance): kernelver = instance._get_kernel_versions().values()[0][0] @@ -115,7 +132,8 @@ class LiveCDPlugin(ImagerPlugin): try: subprocess.call(args, preexec_fn = instance._chroot) except OSError, (err, msg): - raise CreatorError("Failed to run post cleanups: %s" % msg) + raise errors.CreatorError("Failed to run post cleanups: %s" % msg) + convertor = livecd.LiveCDImageCreator() srcimgsize = (misc.get_file_size(base_on)) * 1024L * 1024L base_on_dir = os.path.dirname(base_on) @@ -140,7 +158,7 @@ class LiveCDPlugin(ImagerPlugin): imgloop.mount() except MountError, e: imgloop.cleanup() - raise CreatorError("Failed to loopback mount '%s' : %s" %(img, e)) + raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(img, e)) # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(imgmnt + "/squashfs.img"): @@ -161,11 +179,12 @@ class LiveCDPlugin(ImagerPlugin): os_image = tmpoutdir + "/LiveOS/ext3fs.img" if not os.path.exists(os_image): - raise CreatorError("'%s' is not a valid live CD ISO : neither " - "LiveOS/ext3fs.img nor os.img exist" %img) + raise errors.CreatorError("'%s' is not a valid live CD ISO : neither " + "LiveOS/ext3fs.img nor os.img exist" %img) rtimage = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), "target.img") shutil.copyfile(os_image, rtimage) + finally: imgloop.cleanup() shutil.rmtree(tmpoutdir, ignore_errors = True) -- 2.7.4