2 # liveusb.py : LiveUSBImageCreator class for creating Live USB images
4 # Copyright 2007, Red Hat Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Library General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 import mic.utils.fs_related as fs_related
28 import mic.utils.misc as misc
29 from livecd import LiveCDImageCreator
30 from mic.utils.errors import *
31 from mic.utils.partitionedfs import PartitionedMount
33 class LiveUSBImageCreator(LiveCDImageCreator):
34 def __init__(self, *args):
35 LiveCDImageCreator.__init__(self, *args)
37 self._dep_checks.extend(["kpartx", "parted"])
38 # remove dependency of genisoimage in parent class
39 if "genisoimage" in self._dep_checks:
40 self._dep_checks.remove("genisoimage")
42 def _create_usbimg(self, isodir):
43 overlaysizemb = 64 #default
44 #skipcompress = self.skip_compression?
52 if overlaysizemb > 2047 and fstype == "vfat":
53 raise CreatorError("Can't have an overlay of 2048MB or greater on VFAT")
54 if homesizemb > 2047 and fstype == "vfat":
55 raise CreatorError("Can't have an home overlay of 2048MB or greater on VFAT")
56 if swapsizemb > 2047 and fstype == "vfat":
57 raise CreatorError("Can't have an swap overlay of 2048MB or greater on VFAT")
59 livesize = misc.get_file_size(isodir + "/LiveOS")
60 mountcmd = fs_related.find_binary_path("mount")
61 umountcmd = fs_related.find_binary_path("umount")
62 ddcmd = fs_related.find_binary_path("dd")
64 # tmpmnt = self._mkdtemp("squashfs-mnt")
65 # rc = subprocess.call([mountcmd, "-o", "loop", isodir + "/LiveOS/squashfs.img", tmpmnt]);
67 # raise CreatorError("Can't mount %s" % (isodir + "/LiveOS/squashfs.img"))
68 # livesize = misc.get_file_size(tmpmnt + "/LiveOS/ext3fs.img")
69 # rc = subprocess.call([umountcmd, tmpmnt]);
71 # raise CreatorError("Can't umount %s" % (tmpmnt))
72 usbimgsize = (overlaysizemb + homesizemb + swapsizemb + livesize + plussize) * 1024L * 1024L
73 disk = fs_related.SparseLoopbackDisk("%s/%s.usbimg" % (self._outdir, self.name), usbimgsize)
74 usbmnt = self._mkdtemp("usb-mnt")
75 usbloop = PartitionedMount({'/dev/sdb':disk}, usbmnt)
77 usbloop.add_partition(usbimgsize/1024/1024, "/dev/sdb", "/", fstype, boot=True)
82 raise CreatorError("Failed mount disks : %s" % e)
85 fs_related.makedirs(usbmnt + "/LiveOS")
87 # if os.path.exists(isodir + "/LiveOS/squashfs.img"):
88 # rc = subprocess.call([mountcmd, "-o", "loop", isodir + "/LiveOS/squashfs.img", tmpmnt]);
90 # raise CreatorError("Can't mount %s" % (isodir + "/LiveOS/squashfs.img"))
91 # shutil.copyfile(tmpmnt + "/LiveOS/ext3fs.img", usbmnt + "/LiveOS/ext3fs.img")
92 # rc = subprocess.call([umountcmd, tmpmnt]);
94 # raise CreatorError("Can't umount %s" % (tmpmnt))
96 # shutil.copyfile(isodir + "/LiveOS/ext3fs.img", usbmnt + "/LiveOS/ext3fs.img")
98 if os.path.exists(isodir + "/LiveOS/squashfs.img"):
99 shutil.copyfile(isodir + "/LiveOS/squashfs.img", usbmnt + "/LiveOS/squashfs.img")
101 fs_related.mksquashfs(os.path.dirname(self._image), usbmnt + "/LiveOS/squashfs.img")
103 if os.path.exists(isodir + "/LiveOS/osmin.img"):
104 shutil.copyfile(isodir + "/LiveOS/osmin.img", usbmnt + "/LiveOS/osmin.img")
106 if fstype == "vfat" or fstype == "msdos":
107 uuid = usbloop.partitions[0]['mount'].uuid
108 label = usbloop.partitions[0]['mount'].fslabel
109 usblabel = "UUID=%s-%s" % (uuid[0:4], uuid[4:8])
110 overlaysuffix = "-%s-%s-%s" % (label, uuid[0:4], uuid[4:8])
112 diskmount = usbloop.partitions[0]['mount']
113 usblabel = "UUID=%s" % diskmount.uuid
114 overlaysuffix = "-%s-%s" % (diskmount.fslabel, diskmount.uuid)
116 copycmd = fs_related.find_binary_path("cp")
117 args = [copycmd, "-Rf", isodir + "/isolinux", usbmnt + "/syslinux"]
118 rc = subprocess.call(args)
120 raise CreatorError("Can't copy isolinux directory %s" % (isodir + "/isolinux/*"))
122 if os.path.isfile("/usr/share/syslinux/isolinux.bin"):
123 syslinux_path = "/usr/share/syslinux"
124 elif os.path.isfile("/usr/lib/syslinux/isolinux.bin"):
125 syslinux_path = "/usr/lib/syslinux"
127 raise CreatorError("syslinux not installed : "
128 "cannot find syslinux installation path")
130 for f in ("isolinux.bin", "vesamenu.c32"):
131 path = os.path.join(syslinux_path, f)
132 if os.path.isfile(path):
133 args = [copycmd, path, usbmnt + "/syslinux/"]
134 rc = subprocess.call(args)
136 raise CreatorError("Can't copy syslinux file %s" % (path))
138 raise CreatorError("syslinux not installed : "
139 "syslinux file %s not found" % path)
141 fd = open(isodir + "/isolinux/isolinux.cfg", "r")
144 pattern = re.compile('CDLABEL=[^ ]*')
145 text = pattern.sub(usblabel, text)
146 pattern = re.compile('rootfstype=[^ ]*')
147 text = pattern.sub("rootfstype=" + fstype, text)
149 text = text.replace("liveimg", "liveimg " + kernelargs)
151 if overlaysizemb > 0:
152 print "Initializing persistent overlay file"
153 overfile = "overlay" + overlaysuffix
155 args = [ddcmd, "if=/dev/zero", "of=" + usbmnt + "/LiveOS/" + overfile, "count=%d" % overlaysizemb, "bs=1M"]
157 args = [ddcmd, "if=/dev/null", "of=" + usbmnt + "/LiveOS/" + overfile, "count=1", "bs=1M", "seek=%d" % overlaysizemb]
158 rc = subprocess.call(args)
160 raise CreatorError("Can't create overlay file")
161 text = text.replace("liveimg", "liveimg overlay=" + usblabel)
162 text = text.replace(" ro ", " rw ")
165 print "Initializing swap file"
166 swapfile = usbmnt + "/LiveOS/" + "swap.img"
167 args = [ddcmd, "if=/dev/zero", "of=" + swapfile, "count=%d" % swapsizemb, "bs=1M"]
168 rc = subprocess.call(args)
170 raise CreatorError("Can't create swap file")
171 args = ["mkswap", "-f", swapfile]
172 rc = subprocess.call(args)
174 raise CreatorError("Can't mkswap on swap file")
177 print "Initializing persistent /home"
178 homefile = usbmnt + "/LiveOS/" + homefile
180 args = [ddcmd, "if=/dev/zero", "of=" + homefile, "count=%d" % homesizemb, "bs=1M"]
182 args = [ddcmd, "if=/dev/null", "of=" + homefile, "count=1", "bs=1M", "seek=%d" % homesizemb]
183 rc = subprocess.call(args)
185 raise CreatorError("Can't create home file")
187 mkfscmd = fs_related.find_binary_path("/sbin/mkfs." + fstype)
188 if fstype == "ext2" or fstype == "ext3":
189 args = [mkfscmd, "-F", "-j", homefile]
191 args = [mkfscmd, homefile]
192 rc = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr)
194 raise CreatorError("Can't mke2fs home file")
195 if fstype == "ext2" or fstype == "ext3":
196 tune2fs = fs_related.find_binary_path("tune2fs")
197 args = [tune2fs, "-c0", "-i0", "-ouser_xattr,acl", homefile]
198 rc = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr)
200 raise CreatorError("Can't tune2fs home file")
202 if fstype == "vfat" or fstype == "msdos":
203 syslinuxcmd = fs_related.find_binary_path("syslinux")
204 syslinuxcfg = usbmnt + "/syslinux/syslinux.cfg"
205 args = [syslinuxcmd, "-d", "syslinux", usbloop.partitions[0]["device"]]
206 elif fstype == "ext2" or fstype == "ext3":
207 extlinuxcmd = fs_related.find_binary_path("extlinux")
208 syslinuxcfg = usbmnt + "/syslinux/extlinux.conf"
209 args = [extlinuxcmd, "-i", usbmnt + "/syslinux"]
211 raise CreatorError("Invalid file system type: %s" % (fstype))
213 os.unlink(usbmnt + "/syslinux/isolinux.cfg")
214 fd = open(syslinuxcfg, "w")
217 rc = subprocess.call(args)
219 raise CreatorError("Can't install boot loader.")
225 #Need to do this after image is unmounted and device mapper is closed
227 mbrfile = "/usr/lib/syslinux/mbr.bin"
228 if not os.path.exists(mbrfile):
229 mbrfile = "/usr/share/syslinux/mbr.bin"
230 if not os.path.exists(mbrfile):
231 raise CreatorError("mbr.bin file didn't exist.")
232 mbrsize = os.path.getsize(mbrfile)
233 outimg = "%s/%s.usbimg" % (self._outdir, self.name)
234 args = [ddcmd, "if=" + mbrfile, "of=" + outimg, "seek=0", "conv=notrunc", "bs=1", "count=%d" % (mbrsize)]
235 rc = subprocess.call(args)
237 raise CreatorError("Can't set MBR.")
239 def _stage_final_image(self):
241 isodir = self._get_isodir()
242 fs_related.makedirs(isodir + "/LiveOS")
244 minimal_size = self._resparse()
246 if not self.skip_minimize:
247 fs_related.create_image_minimizer(isodir + "/LiveOS/osmin.img",
248 self._image, minimal_size)
250 if self.skip_compression:
251 shutil.move(self._image, isodir + "/LiveOS/ext3fs.img")
253 fs_related.makedirs(os.path.join(os.path.dirname(self._image), "LiveOS"))
254 shutil.move(self._image,
255 os.path.join(os.path.dirname(self._image),
256 "LiveOS", "ext3fs.img"))
257 fs_related.mksquashfs(os.path.dirname(self._image),
258 isodir + "/LiveOS/squashfs.img")
260 self._create_usbimg(isodir)
263 shutil.rmtree(isodir, ignore_errors = True)
264 self._set_isodir(None)
266 def _base_on(self, base_on):
267 """Support Image Convertor"""
268 if self.actasconvertor:
269 if os.path.exists(base_on) and not os.path.isfile(base_on):
270 ddcmd = fs_related.find_binary_path("dd")
271 args = [ ddcmd, "if=%s" % base_on, "of=%s" % self._image ]
272 print "dd %s -> %s" % (base_on, self._image)
273 rc = subprocess.call(args)
275 raise CreatorError("Failed to dd from %s to %s" % (base_on, self._image))
276 self._set_image_size(misc.get_file_size(self._image) * 1024L * 1024L)
277 if os.path.isfile(base_on):
278 print "Copying file system..."
279 shutil.copyfile(base_on, self._image)
280 self._set_image_size(misc.get_file_size(self._image) * 1024L * 1024L)
283 #helper function to extract ext3 file system from a live usb image
284 usbimgsize = misc.get_file_size(base_on) * 1024L * 1024L
285 disk = fs_related.SparseLoopbackDisk(base_on, usbimgsize)
286 usbimgmnt = self._mkdtemp("usbimgmnt-")
287 usbloop = PartitionedMount({'/dev/sdb':disk}, usbimgmnt, skipformat = True)
288 usbimg_fstype = "vfat"
289 usbloop.add_partition(usbimgsize/1024/1024, "/dev/sdb", "/", usbimg_fstype, boot=False)
292 except MountError, e:
294 raise CreatorError("Failed to loopback mount '%s' : %s" %
297 #legacy LiveOS filesystem layout support, remove for F9 or F10
298 if os.path.exists(usbimgmnt + "/squashfs.img"):
299 squashimg = usbimgmnt + "/squashfs.img"
301 squashimg = usbimgmnt + "/LiveOS/squashfs.img"
303 tmpoutdir = self._mkdtemp()
304 #unsquashfs requires outdir mustn't exist
305 shutil.rmtree(tmpoutdir, ignore_errors = True)
306 self._uncompress_squashfs(squashimg, tmpoutdir)
309 # legacy LiveOS filesystem layout support, remove for F9 or F10
310 if os.path.exists(tmpoutdir + "/os.img"):
311 os_image = tmpoutdir + "/os.img"
313 os_image = tmpoutdir + "/LiveOS/ext3fs.img"
315 if not os.path.exists(os_image):
316 raise CreatorError("'%s' is not a valid live CD ISO : neither "
317 "LiveOS/ext3fs.img nor os.img exist" %
320 print "Copying file system..."
321 shutil.copyfile(os_image, self._image)
322 self._set_image_size(misc.get_file_size(self._image) * 1024L * 1024L)
324 shutil.rmtree(tmpoutdir, ignore_errors = True)