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.
23 from mic.utils import misc, fs_related, runner
24 from mic.utils.errors import CreatorError, MountError
25 from mic.utils.partitionedfs import PartitionedMount
26 from mic.imager.livecd import LiveCDImageCreator
27 from mic.archive import packing
29 class LiveUSBImageCreator(LiveCDImageCreator):
30 img_format = 'liveusb'
32 def __init__(self, *args):
33 LiveCDImageCreator.__init__(self, *args)
35 self._dep_checks.extend(["kpartx", "parted"])
37 # remove dependency of genisoimage in parent class
38 if "genisoimage" in self._dep_checks:
39 self._dep_checks.remove("genisoimage")
41 def _create_usbimg(self, isodir):
42 overlaysizemb = 64 #default
43 #skipcompress = self.skip_compression?
52 if overlaysizemb > 2047:
53 raise CreatorError("Can't have an overlay of 2048MB or "
57 raise CreatorError("Can't have an home overlay of 2048MB or "
61 raise CreatorError("Can't have an swap overlay of 2048MB or "
64 livesize = misc.get_file_size(isodir + "/LiveOS")
66 usbimgsize = (overlaysizemb + \
70 plussize) * 1024L * 1024L
72 disk = fs_related.SparseLoopbackDisk("%s/%s.usbimg" \
73 % (self._outdir, self.name),
75 usbmnt = self._mkdtemp("usb-mnt")
76 usbloop = PartitionedMount(usbmnt)
77 usbloop.add_disk('/dev/sdb', disk)
79 usbloop.add_partition(usbimgsize/1024/1024,
88 fs_related.makedirs(usbmnt + "/LiveOS")
90 if os.path.exists(isodir + "/LiveOS/squashfs.img"):
91 shutil.copyfile(isodir + "/LiveOS/squashfs.img",
92 usbmnt + "/LiveOS/squashfs.img")
94 fs_related.mksquashfs(os.path.dirname(self._image),
95 usbmnt + "/LiveOS/squashfs.img")
97 if os.path.exists(isodir + "/LiveOS/osmin.img"):
98 shutil.copyfile(isodir + "/LiveOS/osmin.img",
99 usbmnt + "/LiveOS/osmin.img")
101 if fstype == "vfat" or fstype == "msdos":
102 uuid = usbloop.partitions[0]['mount'].uuid
103 label = usbloop.partitions[0]['mount'].fslabel
104 usblabel = "UUID=%s" % (uuid)
105 overlaysuffix = "-%s-%s" % (label, uuid)
107 diskmount = usbloop.partitions[0]['mount']
108 usblabel = "UUID=%s" % diskmount.uuid
109 overlaysuffix = "-%s-%s" % (diskmount.fslabel, diskmount.uuid)
111 args = ['cp', "-Rf", isodir + "/isolinux", usbmnt + "/syslinux"]
112 rc = runner.show(args)
114 raise CreatorError("Can't copy isolinux directory %s" \
115 % (isodir + "/isolinux/*"))
117 if os.path.isfile("/usr/share/syslinux/isolinux.bin"):
118 syslinux_path = "/usr/share/syslinux"
119 elif os.path.isfile("/usr/lib/syslinux/isolinux.bin"):
120 syslinux_path = "/usr/lib/syslinux"
122 raise CreatorError("syslinux not installed : "
123 "cannot find syslinux installation path")
125 for f in ("isolinux.bin", "vesamenu.c32"):
126 path = os.path.join(syslinux_path, f)
127 if os.path.isfile(path):
128 args = ['cp', path, usbmnt + "/syslinux/"]
129 rc = runner.show(args)
131 raise CreatorError("Can't copy syslinux file " + path)
133 raise CreatorError("syslinux not installed: "
134 "syslinux file %s not found" % path)
136 fd = open(isodir + "/isolinux/isolinux.cfg", "r")
139 pattern = re.compile('CDLABEL=[^ ]*')
140 text = pattern.sub(usblabel, text)
141 pattern = re.compile('rootfstype=[^ ]*')
142 text = pattern.sub("rootfstype=" + fstype, text)
144 text = text.replace("rd.live.image", "rd.live.image " + kernelargs)
146 if overlaysizemb > 0:
147 msger.info("Initializing persistent overlay file")
148 overfile = "overlay" + overlaysuffix
152 "of=" + usbmnt + "/LiveOS/" + overfile,
153 "count=%d" % overlaysizemb,
158 "of=" + usbmnt + "/LiveOS/" + overfile,
161 "seek=%d" % overlaysizemb]
162 rc = runner.show(args)
164 raise CreatorError("Can't create overlay file")
165 text = text.replace("rd.live.image", "rd.live.image rd.live.overlay=" + usblabel)
166 text = text.replace(" ro ", " rw ")
169 msger.info("Initializing swap file")
170 swapfile = usbmnt + "/LiveOS/" + "swap.img"
174 "count=%d" % swapsizemb,
176 rc = runner.show(args)
178 raise CreatorError("Can't create swap file")
179 args = ["mkswap", "-f", swapfile]
180 rc = runner.show(args)
182 raise CreatorError("Can't mkswap on swap file")
185 msger.info("Initializing persistent /home")
186 homefile = usbmnt + "/LiveOS/" + homefile
191 "count=%d" % homesizemb,
199 "seek=%d" % homesizemb]
200 rc = runner.show(args)
202 raise CreatorError("Can't create home file")
204 mkfscmd = fs_related.find_binary_path("/sbin/mkfs." + fstype)
205 if fstype == "ext2" or fstype == "ext3":
206 args = [mkfscmd, "-F", "-j", homefile]
208 args = [mkfscmd, homefile]
209 rc = runner.show(args)
211 raise CreatorError("Can't mke2fs home file")
212 if fstype == "ext2" or fstype == "ext3":
213 tune2fs = fs_related.find_binary_path("tune2fs")
219 rc = runner.show(args)
221 raise CreatorError("Can't tune2fs home file")
223 if fstype == "vfat" or fstype == "msdos":
224 syslinuxcmd = fs_related.find_binary_path("syslinux")
225 syslinuxcfg = usbmnt + "/syslinux/syslinux.cfg"
229 usbloop.partitions[0]["device"]]
231 elif fstype == "ext2" or fstype == "ext3":
232 extlinuxcmd = fs_related.find_binary_path("extlinux")
233 syslinuxcfg = usbmnt + "/syslinux/extlinux.conf"
236 usbmnt + "/syslinux"]
239 raise CreatorError("Invalid file system type: %s" % (fstype))
241 os.unlink(usbmnt + "/syslinux/isolinux.cfg")
242 fd = open(syslinuxcfg, "w")
245 rc = runner.show(args)
247 raise CreatorError("Can't install boot loader.")
253 # Need to do this after image is unmounted and device mapper is closed
254 msger.info("set MBR")
255 mbrfile = "/usr/lib/syslinux/mbr.bin"
256 if not os.path.exists(mbrfile):
257 mbrfile = "/usr/share/syslinux/mbr.bin"
258 if not os.path.exists(mbrfile):
259 raise CreatorError("mbr.bin file didn't exist.")
260 mbrsize = os.path.getsize(mbrfile)
261 outimg = "%s/%s.usbimg" % (self._outdir, self.name)
269 "count=%d" % (mbrsize)]
270 rc = runner.show(args)
272 raise CreatorError("Can't set MBR.")
274 def _stage_final_image(self):
276 isodir = self._get_isodir()
277 fs_related.makedirs(isodir + "/LiveOS")
279 minimal_size = self._resparse()
281 if not self.skip_minimize:
282 fs_related.create_image_minimizer(isodir + "/LiveOS/osmin.img",
286 if self.skip_compression:
287 shutil.move(self._image,
288 isodir + "/LiveOS/ext3fs.img")
290 fs_related.makedirs(os.path.join(
291 os.path.dirname(self._image),
293 shutil.move(self._image,
294 os.path.join(os.path.dirname(self._image),
295 "LiveOS", "ext3fs.img"))
296 fs_related.mksquashfs(os.path.dirname(self._image),
297 isodir + "/LiveOS/squashfs.img")
299 self._create_usbimg(isodir)
302 self.image_files.update({'image_files': self.pack_to})
303 usbimg = os.path.join(self._outdir, self.name + ".usbimg")
304 packimg = os.path.join(self._outdir, self.pack_to)
305 packing(packimg, usbimg)
308 self.image_files.update({'image_files': self.name + ".usbimg"})
311 shutil.rmtree(isodir, ignore_errors = True)
312 self._set_isodir(None)