3 # Copyright (c) 2011 Intel, Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 from mic import chroot, msger
23 from mic.utils import misc, fs_related, errors
24 from mic.conf import configmgr
25 import mic.imager.livecd as livecd
26 from mic.plugin import pluginmgr
28 from mic.pluginbase import ImagerPlugin
29 class LiveCDPlugin(ImagerPlugin):
33 def do_create(self, subcmd, opts, *args):
34 """${cmd_name}: create livecd image
37 ${name} ${cmd_name} <ksfile> [OPTS]
42 creatoropts = configmgr.create
45 if creatoropts['arch'] and creatoropts['arch'].startswith('arm'):
46 msger.warning('livecd cannot support arm images, Quit')
50 if len(creatoropts['record_pkgs']) > 0:
51 recording_pkgs = creatoropts['record_pkgs']
53 if creatoropts['release'] is not None:
54 if 'name' not in recording_pkgs:
55 recording_pkgs.append('name')
57 ksconf = misc.normalize_ksfile(ksconf,
58 creatoropts['release'],
61 configmgr._ksconf = ksconf
63 # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
64 if creatoropts['release'] is not None:
65 creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
67 # try to find the pkgmgr
69 for (key, pcls) in pluginmgr.get_plugins('backend').iteritems():
70 if key == creatoropts['pkgmgr']:
75 pkgmgrs = pluginmgr.get_plugins('backend').keys()
76 raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
78 creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr)
80 if len(recording_pkgs) > 0:
81 creator._recording_pkgs = recording_pkgs
83 self.check_image_exists(creator.destdir,
85 [creator.name + ".iso"],
86 creatoropts['release'])
89 creator.check_depend_tools()
90 creator.mount(None, creatoropts["cachedir"])
92 creator.configure(creatoropts["repomd"])
95 creator.package(creatoropts["outdir"])
96 if creatoropts['release'] is not None:
97 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
98 creator.print_outimage_info()
100 except errors.CreatorError:
105 msger.info("Finished.")
109 def do_chroot(cls, target):
110 os_image = cls.do_unpack(target)
111 os_image_dir = os.path.dirname(os_image)
113 # unpack image to target dir
114 imgsize = misc.get_file_size(os_image) * 1024L * 1024L
115 imgtype = misc.get_image_type(os_image)
116 if imgtype == "btrfsimg":
118 myDiskMount = fs_related.BtrfsDiskMount
119 elif imgtype in ("ext3fsimg", "ext4fsimg"):
121 myDiskMount = fs_related.ExtDiskMount
123 raise errors.CreatorError("Unsupported filesystem type: %s" % fstype)
125 extmnt = misc.mkdtemp()
126 extloop = myDiskMount(fs_related.SparseLoopbackDisk(os_image, imgsize),
134 except errors.MountError:
136 shutil.rmtree(extmnt, ignore_errors = True)
137 shutil.rmtree(os_image_dir, ignore_errors = True)
141 envcmd = fs_related.find_binary_inchroot("env", extmnt)
143 cmdline = "%s HOME=/root /bin/bash" % envcmd
145 cmdline = "/bin/bash"
146 chroot.chroot(extmnt, None, cmdline)
148 raise errors.CreatorError("Failed to chroot to %s." %target)
150 chroot.cleanup_after_chroot("img", extloop, os_image_dir, extmnt)
153 def do_pack(cls, base_on):
156 def __mkinitrd(instance):
157 kernelver = instance._get_kernel_versions().values()[0][0]
158 args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ]
160 subprocess.call(args, preexec_fn = instance._chroot)
161 except OSError, (err, msg):
162 raise errors.CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg)
164 def __run_post_cleanups(instance):
165 kernelver = instance._get_kernel_versions().values()[0][0]
166 args = ["rm", "-f", "/boot/initrd-%s.img" % kernelver]
169 subprocess.call(args, preexec_fn = instance._chroot)
170 except OSError, (err, msg):
171 raise errors.CreatorError("Failed to run post cleanups: %s" % msg)
173 convertoropts = configmgr.convert
174 convertoropts['name'] = os.path.splitext(os.path.basename(base_on))[0]
175 convertor = livecd.LiveCDImageCreator(convertoropts)
176 imgtype = misc.get_image_type(base_on)
177 if imgtype == "btrfsimg":
179 elif imgtype in ("ext3fsimg", "ext4fsimg"):
182 raise errors.CreatorError("Unsupported filesystem type: %s" % fstype)
183 convertor._set_fstype(fstype)
185 convertor.mount(base_on)
186 __mkinitrd(convertor)
187 convertor._create_bootconfig()
188 __run_post_cleanups(convertor)
189 convertor.launch_shell(convertoropts['shell'])
192 convertor.print_outimage_info()
194 shutil.rmtree(os.path.dirname(base_on), ignore_errors = True)
197 def do_unpack(cls, srcimg):
199 imgmnt = misc.mkdtemp()
200 imgloop = fs_related.DiskMount(fs_related.LoopbackDisk(img, 0), imgmnt)
203 except errors.MountError:
207 # legacy LiveOS filesystem layout support, remove for F9 or F10
208 if os.path.exists(imgmnt + "/squashfs.img"):
209 squashimg = imgmnt + "/squashfs.img"
211 squashimg = imgmnt + "/LiveOS/squashfs.img"
213 tmpoutdir = misc.mkdtemp()
214 # unsquashfs requires outdir mustn't exist
215 shutil.rmtree(tmpoutdir, ignore_errors = True)
216 misc.uncompress_squashfs(squashimg, tmpoutdir)
219 # legacy LiveOS filesystem layout support, remove for F9 or F10
220 if os.path.exists(tmpoutdir + "/os.img"):
221 os_image = tmpoutdir + "/os.img"
223 os_image = tmpoutdir + "/LiveOS/ext3fs.img"
225 if not os.path.exists(os_image):
226 raise errors.CreatorError("'%s' is not a valid live CD ISO : neither "
227 "LiveOS/ext3fs.img nor os.img exist" %img)
229 imgname = os.path.basename(srcimg)
230 imgname = os.path.splitext(imgname)[0] + ".img"
231 rtimage = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), imgname)
232 shutil.copyfile(os_image, rtimage)
236 shutil.rmtree(tmpoutdir, ignore_errors = True)
237 shutil.rmtree(imgmnt, ignore_errors = True)