8 from mic.pluginbase.imager_plugin import ImagerPlugin
9 import mic.imager.liveusb as liveusb
10 import mic.utils.misc as misc
11 import mic.utils.fs_related as fs_related
12 import mic.utils.cmdln as cmdln
13 import mic.configmgr as configmgr
14 import mic.pluginmgr as pluginmgr
15 from mic.utils.partitionedfs import PartitionedMount
16 from mic.utils.errors import *
17 import mic.chroot as chroot
19 class LiveUSBPlugin(ImagerPlugin):
22 def do_create(self, subcmd, opts, *args):
23 """${cmd_name}: create liveusb image
33 raise errors.Usage("Extra arguments given")
36 cfgmgr = configmgr.getConfigMgr()
37 creatoropts = cfgmgr.create
38 cfgmgr.setProperty("ksconf", args[0])
39 plgmgr = pluginmgr.PluginMgr()
42 for (key, pcls) in plgmgr.getBackendPlugins():
43 if key == creatoropts['pkgmgr']:
46 creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr)
48 creator.check_depend_tools()
49 creator.mount(None, creatoropts["cachedir"])
51 creator.configure(creatoropts["repomd"])
53 creator.package(creatoropts["outdir"])
54 outimage = creator.outimage
55 creator.print_outimage_info()
56 outimage = creator.outimage
57 except CreatorError, e:
58 raise CreatorError("failed to create image : %s" % e)
65 def do_chroot(cls, target):
66 os_image = cls.do_unpack(target)
67 os_image_dir = os.path.dirname(os_image)
68 #unpack image to target dir
69 imgsize = misc.get_file_size(os_image) * 1024L * 1024L
70 extmnt = misc.mkdtemp()
73 MyDiskMount = fs_related.ExtDiskMount
74 #if imgcreate.fstype_is_btrfs(os_image):
76 # tlabel = "btrfs label"
77 # MyDiskMount = fs_related.BtrfsDiskMount
78 extloop = MyDiskMount(fs_related.SparseLoopbackDisk(os_image, imgsize),
87 shutil.rmtree(extmnt, ignore_errors = True)
88 raise CreatorError("Failed to loopback mount '%s' : %s" %(os_image, e))
90 chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash")
92 raise CreatorError("Failed to chroot to %s." %target)
94 chroot.cleanup_after_chroot("img", extloop, os_image_dir, extmnt)
97 def do_pack(cls, base_on):
98 def __mkinitrd(instance):
99 kernelver = instance._get_kernel_versions().values()[0][0]
100 args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ]
102 subprocess.call(args, preexec_fn = instance._chroot)
103 except OSError, (err, msg):
104 raise CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg)
106 def __run_post_cleanups(instance):
107 kernelver = instance._get_kernel_versions().values()[0][0]
108 args = ["rm", "-f", "/boot/initrd-%s.img" % kernelver]
110 subprocess.call(args, preexec_fn = instance._chroot)
111 except OSError, (err, msg):
112 raise CreatorError("Failed to run post cleanups: %s" % msg)
114 convertor = liveusb.LiveUSBImageCreator()
115 srcimgsize = (misc.get_file_size(base_on)) * 1024L * 1024L
116 convertor._set_fstype("ext3")
117 convertor._set_image_size(srcimgsize)
118 base_on_dir = os.path.dirname(base_on)
119 convertor._LoopImageCreator__imgdir = base_on_dir
121 __mkinitrd(convertor)
122 convertor._create_bootconfig()
123 __run_post_cleanups(convertor)
126 convertor.print_outimage_info()
127 shutil.rmtree(base_on_dir, ignore_errors = True)
130 def do_unpack(cls, srcimg):
132 imgsize = misc.get_file_size(img) * 1024L * 1024L
133 imgmnt = misc.mkdtemp()
134 disk = fs_related.SparseLoopbackDisk(img, imgsize)
135 imgloop = PartitionedMount({'/dev/sdb':disk}, imgmnt, skipformat = True)
136 imgloop.add_partition(imgsize/1024/1024, "/dev/sdb", "/", "vfat", boot=False)
139 except MountError, e:
141 raise CreatorError("Failed to loopback mount '%s' : %s" %(img, e))
143 # legacy LiveOS filesystem layout support, remove for F9 or F10
144 if os.path.exists(imgmnt + "/squashfs.img"):
145 squashimg = imgmnt + "/squashfs.img"
147 squashimg = imgmnt + "/LiveOS/squashfs.img"
149 tmpoutdir = misc.mkdtemp()
150 # unsquashfs requires outdir mustn't exist
151 shutil.rmtree(tmpoutdir, ignore_errors = True)
152 misc.uncompress_squashfs(squashimg, tmpoutdir)
155 # legacy LiveOS filesystem layout support, remove for F9 or F10
156 if os.path.exists(tmpoutdir + "/os.img"):
157 os_image = tmpoutdir + "/os.img"
159 os_image = tmpoutdir + "/LiveOS/ext3fs.img"
161 if not os.path.exists(os_image):
162 raise CreatorError("'%s' is not a valid live CD ISO : neither "
163 "LiveOS/ext3fs.img nor os.img exist" %img)
164 rtimage = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), "target.img")
165 shutil.copyfile(os_image, rtimage)
168 shutil.rmtree(tmpoutdir, ignore_errors = True)
169 shutil.rmtree(imgmnt, ignore_errors = True)
173 mic_plugin = ["liveusb", LiveUSBPlugin]