livecd-iso-to-disk.sh 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. #!/bin/bash
  2. # Transfer a Live image so that it's bootable off of a USB/SD device.
  3. # Copyright 2007-2012 Red Hat, Inc.
  4. #
  5. # Jeremy Katz <katzj@redhat.com>
  6. # Brian C. Lane <bcl@redhat.com>
  7. #
  8. # overlay/persistence enhancements by Douglas McClendon <dmc@viros.org>
  9. # GPT+MBR hybrid enhancements by Stewart Adam <s.adam@diffingo.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; version 2 of the License.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Library General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. export PATH=/sbin:/usr/sbin:$PATH
  24. shortusage() {
  25. echo "
  26. SYNTAX
  27. livecd-iso-to-disk [--help] [--noverify] [--format] [--msdos] [--reset-mbr]
  28. [--efi] [--skipcopy] [--force] [--xo] [--xo-no-home]
  29. [--timeout <time>] [--totaltimeout <time>]
  30. [--extra-kernel-args <args>] [--multi] [--livedir <dir>]
  31. [--compress] [--skipcompress] [--swap-size-mb <size>]
  32. [--overlay-size-mb <size>] [--home-size-mb <size>]
  33. [--delete-home] [--crypted-home] [--unencrypted-home]
  34. [--updates updates.img] [--ks kickstart] [--label label]
  35. <source> <target device>
  36. (Enter livecd-iso-to-disk --help on the command line for more information.)"
  37. }
  38. usage() {
  39. echo "
  40. "
  41. shortusage
  42. echo "
  43. livecd-iso-to-disk - Transfer a LiveOS image so that it's bootable off of
  44. a USB/SD device.
  45. The script may be run in simplest form with just the two arguments:
  46. <source>
  47. This may be the filesystem path to a LiveOS .iso image file,
  48. such as from a CD-ROM, DVD, or download. It could also be the
  49. device node reference for the mount point of another LiveOS
  50. filesystem, including the currently-running one (such as a
  51. booted Live CD/DVD/USB, where /dev/live references the running
  52. image device).
  53. <target device>
  54. This should be the device partition name for the attached,
  55. target device, such as /dev/sdb1 or /dev/sdc1. (Issue the
  56. df -Th command to get a listing of the mounted partitions,
  57. where you can confirm the filesystem types, available space,
  58. and device names.) Be careful to specify the correct device,
  59. or you may overwrite important data on another disk!
  60. To execute the script to completion, you will need to run it with root user
  61. permissions.
  62. SYSLINUX must be installed on the computer running the installation script.
  63. DESCRIPTION
  64. livecd-iso-to-disk installs a Live CD/DVD/USB image (LiveOS) onto a USB/SD
  65. storage device (or any storage partition that will boot with a SYSLINUX
  66. bootloader). The target storage device can then boot the installed
  67. operating system on systems that support booting via the USB or the SD
  68. interface. The script requires a LiveOS source image and a target storage
  69. device. The source image may be either a LiveOS .iso file, the currently-
  70. running LiveOS image, the device node reference for an attached device with
  71. an installed LiveOS image, or a file backed by a block device with an
  72. installed LiveOS image. If the operating system supports persistent
  73. overlays for saving system changes, a pre-sized overlay may be included with
  74. the installation.
  75. Unless you request the --format option, the installation does not destroy
  76. data outside of the LiveOS, syslinux, & EFI folders on your target device.
  77. This allows one to maintain other files on the target disk outside of the
  78. LiveOS filesystem.
  79. LiveOS images provide embedded filesystems through the Device-mapper
  80. component of the Linux kernel. The embedded filesystems exist within files
  81. such as /LiveOS/squashfs.img (the default compressed storage) or
  82. /LiveOS/ext3fs.img (an uncompressed version) on the primary volume partition
  83. of the storage device. In use, these are read-only filesystems. Optionally,
  84. one may specify a persistent LiveOS overlay to hold image-change snapshots
  85. (that use write-once, difference-tracking storage) in the
  86. /LiveOS/overlay-<device_id> file, which, *one should note*, always grows in
  87. size due to the storage mechanism. (The fraction of allocated space that
  88. has been consumed by system activity and changes may be displayed by issuing
  89. the 'dmsetup status' command in a terminal session of a running LiveOS
  90. image.) One way to conserve the unrecoverable, overlay file space, is to
  91. specify a persistent home folder for user files, which will be saved in a
  92. /LiveOS/home.img filesystem image file. This file space is encrypted by
  93. default, but is not compressed. (One may bypass encryption with the
  94. --unencrypted-home installation option.) Files in this home folder may be
  95. erased to recover and reuse their storage space. The home.img file is also
  96. convenient for backing up or swapping user account files.
  97. OPTIONS
  98. --help
  99. Displays usage information and exits.
  100. --noverify
  101. Disables the image validation process that occurs before the image is
  102. installed from the original Live CD .iso image. When this option is
  103. specified, the image is not verified before loading onto the target
  104. storage device.
  105. --format
  106. Formats the target device and creates an MS-DOS partition table (or GPT
  107. partition table, if the --efi option is passed).
  108. --msdos
  109. Forces format to use msdos instead of ext4.
  110. --reset-mbr
  111. Sets the Master Boot Record (MBR) of the target storage device to the
  112. mbr.bin file from the installation system's syslinux directory. This
  113. may be helpful in recovering a damaged or corrupted device.
  114. --efi
  115. Creates a GUID partition table when --format is passed, and installs a
  116. hybrid Extensible Firmware Interface (EFI)/MBR bootloader on the disk.
  117. This is necessary for most Intel Macs.
  118. --skipcopy
  119. Skips the copying of the live image to the target device, bypassing the
  120. actions of the --format, --overlay-size-mb, --home-size-mb, &
  121. --swap-size-mb options, if present on the command line. (The --skipcopy
  122. option may be used while testing the script, in order to avoid repeated
  123. and lengthy copy commands, or to repair boot configuration files on a
  124. previously installed device.)
  125. --force
  126. This option allows the installation script to bypass a delete
  127. confirmation dialog in the event that a pre-existing LiveOS directory
  128. is found on the target device.
  129. --xo
  130. Used to prepare an image for the OLPC XO-1 laptop with its compressed,
  131. JFFS2 filesystem. Do not use the following options with --xo:
  132. --overlay-size-mb <size>, home-size-mb <size>, --delete-home,
  133. --compress
  134. --xo-no-home
  135. Used together with the --xo option to prepare an image for an OLPC XO
  136. laptop with the home folder on an SD card instead of the internal flash
  137. storage.
  138. --timeout
  139. Modifies the bootloader's timeout value, which indicates how long to
  140. pause at the boot: prompt before booting automatically. This overrides
  141. the value set during iso creation. Units are 1/10 s. The timeout is
  142. canceled when any key is pressed, the assumption being that the user
  143. will complete the command line. A timeout of zero will disable the
  144. timeout completely.
  145. --totaltimeout
  146. Adds a bootloader totaltimeout, which indicates how long to wait before
  147. booting automatically. This is used to force an automatic boot. This
  148. timeout cannot be canceled by the user. Units are 1/10 s.
  149. --extra-kernel-args <args>
  150. Specifies additional kernel arguments, <args>, that will be inserted
  151. into the syslinux and EFI boot configurations. Multiple arguments
  152. should be specified in one string, i.e.,
  153. --extra-kernel-args \"arg1 arg2 ...\"
  154. --multi
  155. Used when installing multiple image copies to signal configuration of
  156. the boot files for the image in the --livedir <dir> parameter.
  157. --livedir <dir>
  158. Used with multiple image installations to designate the directory <dir>
  159. for the particular image.
  160. --compress (default state for the operating system files)
  161. The default, compressed SquashFS filesystem image is copied on
  162. installation. This option has no effect when the source filesystem is
  163. already expanded.
  164. --skipcompress (default option when --xo is specified)
  165. Expands the source SquashFS image on installation into the read-only
  166. /LiveOS/ext3fs.img filesystem image file.
  167. --swap-size-mb <size>
  168. Sets up a swap file of <size> mebibytes (integer values only) on the
  169. target device.
  170. --overlay-size-mb <size>
  171. This option sets the overlay size in mebibytes (integer values only).
  172. The overlay makes persistent storage available to the live operating
  173. system, if the operating system supports it. The persistent LiveOS
  174. overlay holds image-change snapshots (using write-once, difference-
  175. tracking storage) in the /LiveOS/overlay-<device_id> file, which, *one
  176. should note*, always grows in size due to the storage mechanism. (The
  177. fraction of allocated space that has been consumed may be displayed by
  178. issuing the 'dmsetup status' command in a terminal session of a running
  179. LiveOS installation.) One way to conserve the unrecoverable, overlay
  180. file space, is to specify a persistent home folder for user files, see
  181. --home-size-mb below. The target storage device must have enough free
  182. space for the image and the overlay. A maximum <size> of 4095 MiB is
  183. permitted for vfat-formatted devices. If there is insufficient room on
  184. your device, you will be given information to help in adjusting your
  185. settings.
  186. --home-size-mb <size>
  187. Sets the home directory size in mebibytes (integer values only). A
  188. persistent home directory will be made in the /LiveOS/home.img
  189. filesystem image file. This file space is encrypted by default, but not
  190. compressed (one may bypass encryption with the --unencrypted-home
  191. installation option). Files in this home folder may be erased to
  192. recover and reuse their storage space. The target storage device must
  193. have enough free space for the image, any overlay, and the home
  194. directory. Note that the --delete-home option must also be selected to
  195. replace an existing persistent home with a new, empty one. A maximum
  196. <size> of 4095 MiB is permitted for vfat-formatted devices. If there is
  197. insufficient room on your device, you will be given information to help
  198. in adjusting your settings.
  199. --delete-home
  200. To prevent unwitting deletion of user files, this option must be
  201. explicitly selected when the option --home-size-mb <size> is selected
  202. and there is an existing persistent home directory on the target device.
  203. --crypted-home (default that only applies to new home-size-mb requests)
  204. Specifies the default option to encrypt a new persistent home directory
  205. if --home-size-mb <size> is specified.
  206. --unencrypted-home
  207. Prevents the default option to encrypt a new persistent home directory.
  208. --updates updates.img
  209. Setup inst.updates to point to an updates image on the device. Anaconda
  210. uses this for testing updates to an iso without needing to make a new iso.
  211. --ks kickstart
  212. Setup inst.ks to point to an kickstart file on the device. Use this for
  213. automating installs on boot.
  214. --label label
  215. Specifies a specific label instead of default LIVE. Useful when you do
  216. unattended installs which pas a label to inst.ks
  217. CONTRIBUTORS
  218. livecd-iso-to-disk: David Zeuthen, Jeremy Katz, Douglas McClendon,
  219. Chris Curran and other contributors.
  220. (See the AUTHORS file in the source distribution for
  221. the complete list of credits.)
  222. BUGS
  223. Report bugs to the mailing list
  224. http://admin.fedoraproject.org/mailman/listinfo/livecd or directly to
  225. Bugzilla http://bugzilla.redhat.com/bugzilla/ against the Fedora product,
  226. and the livecd-tools component.
  227. COPYRIGHT
  228. Copyright (C) Fedora Project 2008, 2009, 2010 and various contributors.
  229. This is free software. You may redistribute copies of it under the terms of
  230. the GNU General Public License http://www.gnu.org/licenses/gpl.html.
  231. There is NO WARRANTY, to the extent permitted by law.
  232. SEE ALSO
  233. livecd-creator, project website http://fedoraproject.org/wiki/FedoraLiveCD
  234. "
  235. exit 1
  236. }
  237. cleanup() {
  238. sleep 2
  239. [ -d "$SRCMNT" ] && umount $SRCMNT && rmdir $SRCMNT
  240. [ -d "$TGTMNT" ] && umount $TGTMNT && rmdir $TGTMNT
  241. }
  242. exitclean() {
  243. RETVAL=$?
  244. if [ -d "$SRCMNT" ] || [ -d "$TGTMNT" ];
  245. then
  246. [ "$RETVAL" = 0 ] || echo "Cleaning up to exit..."
  247. cleanup
  248. fi
  249. exit $RETVAL
  250. }
  251. isdevloop() {
  252. [ x"${1#/dev/loop}" != x"$1" ]
  253. }
  254. getdisk() {
  255. DEV=$1
  256. if isdevloop "$DEV"; then
  257. device="$DEV"
  258. return
  259. fi
  260. p=$(udevadm info -q path -n $DEV)
  261. if [ $? -gt 0 ]; then
  262. echo "Error getting udev path to $DEV"
  263. exitclean
  264. fi
  265. if [ -e /sys/$p/device ]; then
  266. device=$(basename /sys/$p)
  267. else
  268. device=$(basename $(readlink -f /sys/$p/../))
  269. fi
  270. if [ ! -e /sys/block/$device -o ! -e /dev/$device ]; then
  271. echo "Error finding block device of $DEV. Aborting!"
  272. exitclean
  273. fi
  274. device="/dev/$device"
  275. # FIXME: weird dev names could mess this up I guess
  276. p=/dev/$(basename $p)
  277. partnum=${p##$device}
  278. }
  279. resetMBR() {
  280. if isdevloop "$DEV"; then
  281. return
  282. fi
  283. getdisk $1
  284. # if efi, we need to use the hybrid MBR
  285. if [ -n "$efi" ]; then
  286. if [ -f /usr/lib/syslinux/gptmbr.bin ]; then
  287. cat /usr/lib/syslinux/gptmbr.bin > $device
  288. elif [ -f /usr/share/syslinux/gptmbr.bin ]; then
  289. cat /usr/share/syslinux/gptmbr.bin > $device
  290. else
  291. echo "Could not find gptmbr.bin (syslinux)"
  292. exitclean
  293. fi
  294. # Make it bootable on EFI and BIOS
  295. parted -s $device set $partnum legacy_boot on
  296. else
  297. if [ -f /usr/lib/syslinux/mbr.bin ]; then
  298. cat /usr/lib/syslinux/mbr.bin > $device
  299. elif [ -f /usr/share/syslinux/mbr.bin ]; then
  300. cat /usr/share/syslinux/mbr.bin > $device
  301. else
  302. echo "Could not find mbr.bin (syslinux)"
  303. exitclean
  304. fi
  305. fi
  306. }
  307. checkMBR() {
  308. if isdevloop "$DEV"; then
  309. return 0
  310. fi
  311. getdisk $1
  312. bs=$(mktemp /tmp/bs.XXXXXX)
  313. dd if=$device of=$bs bs=512 count=1 2>/dev/null || exit 2
  314. mbrword=$(hexdump -n 2 $bs |head -n 1|awk {'print $2;'})
  315. rm -f $bs
  316. if [ "$mbrword" = "0000" ]; then
  317. if [ -z "$format" ]; then
  318. echo "MBR appears to be blank."
  319. echo "Press Enter to replace the MBR and continue or ctrl-c to abort"
  320. read
  321. fi
  322. resetMBR $1
  323. fi
  324. return 0
  325. }
  326. checkPartActive() {
  327. dev=$1
  328. getdisk $dev
  329. # if we're installing to whole-disk and not a partition, then we
  330. # don't need to worry about being active
  331. if [ "$dev" = "$device" ]; then
  332. return
  333. fi
  334. if isdevloop "$DEV"; then
  335. return
  336. fi
  337. if [ "$(/sbin/fdisk -l $device 2>/dev/null |grep -m1 $dev |awk {'print $2;'})" != "*" ]; then
  338. echo "Partition isn't marked bootable!"
  339. echo "You can mark the partition as bootable with "
  340. echo " # /sbin/parted $device"
  341. echo " (parted) toggle N boot"
  342. echo " (parted) quit"
  343. exitclean
  344. fi
  345. }
  346. checkLVM() {
  347. dev=$1
  348. if [ -x /sbin/pvs -a \
  349. "$(/sbin/pvs -o vg_name --noheadings $dev* 2>/dev/null || :)" ]; then
  350. echo "Device, $dev, contains a volume group and cannot be formated!"
  351. echo "You can remove the volume group using vgremove."
  352. exitclean
  353. fi
  354. return 0
  355. }
  356. createGPTLayout() {
  357. dev=$1
  358. getdisk $dev
  359. echo "WARNING: THIS WILL DESTROY ANY DATA ON $device!!!"
  360. echo "Press Enter to continue or ctrl-c to abort"
  361. read
  362. umount ${device}* &> /dev/null || :
  363. wipefs -a ${device}
  364. /sbin/parted --script $device mklabel gpt
  365. partinfo=$(LC_ALL=C /sbin/parted --script -m $device "unit MB print" |grep ^$device:)
  366. dev_size=$(echo $partinfo |cut -d : -f 2 |sed -e 's/MB$//')
  367. p1_size=$(($dev_size - 3))
  368. if [ $p1_size -le 0 ]; then
  369. echo "Your device isn't big enough to hold $SRC"
  370. echo "It is $(($p1_size * -1)) MB too small"
  371. exitclean
  372. fi
  373. p1_start=1
  374. p1_end=$(($p1_size + 1))
  375. /sbin/parted -s $device u MB mkpart '"EFI System Partition"' fat32 $p1_start $p1_end set 1 boot on
  376. # Sometimes automount can be _really_ annoying.
  377. echo "Waiting for devices to settle..."
  378. /sbin/udevadm settle
  379. sleep 5
  380. TGTDEV=${device}1
  381. umount $TGTDEV &> /dev/null || :
  382. /sbin/mkdosfs -n $label $TGTDEV
  383. TGTLABEL="UUID=$(/sbin/blkid -s UUID -o value $TGTDEV)"
  384. }
  385. createMSDOSLayout() {
  386. dev=$1
  387. getdisk $dev
  388. echo "WARNING: THIS WILL DESTROY ANY DATA ON $device!!!"
  389. echo "Press Enter to continue or ctrl-c to abort"
  390. read
  391. umount ${device}* &> /dev/null || :
  392. wipefs -a ${device}
  393. /sbin/parted --script $device mklabel msdos
  394. partinfo=$(LC_ALL=C /sbin/parted --script -m $device "unit MB print" |grep ^$device:)
  395. dev_size=$(echo $partinfo |cut -d : -f 2 |sed -e 's/MB$//')
  396. p1_size=$(($dev_size - 3))
  397. if [ $p1_size -le 0 ]; then
  398. echo "Your device isn't big enough to hold $SRC"
  399. echo "It is $(($p1_size * -1)) MB too small"
  400. exitclean
  401. fi
  402. p1_start=1
  403. p1_end=$(($p1_size + 1))
  404. /sbin/parted -s $device u MB mkpart primary fat32 $p1_start $p1_end set 1 boot on
  405. # Sometimes automount can be _really_ annoying.
  406. echo "Waiting for devices to settle..."
  407. /sbin/udevadm settle
  408. sleep 5
  409. if ! isdevloop "$DEV"; then
  410. TGTDEV=${device}1
  411. else
  412. TGTDEV=${device}
  413. fi
  414. umount $TGTDEV &> /dev/null || :
  415. /sbin/mkdosfs -n LIVE $TGTDEV
  416. TGTLABEL="UUID=$(/sbin/blkid -s UUID -o value $TGTDEV)"
  417. }
  418. createEXTFSLayout() {
  419. dev=$1
  420. getdisk $dev
  421. echo "WARNING: THIS WILL DESTROY ANY DATA ON $device!!!"
  422. echo "Press Enter to continue or ctrl-c to abort"
  423. read
  424. umount ${device}* &> /dev/null || :
  425. wipefs -a ${device}
  426. /sbin/parted -s $device mklabel msdos
  427. partinfo=$(LC_ALL=C /sbin/parted -s -m $device "u MB print" |grep ^$device:)
  428. dev_size=$(echo $partinfo |cut -d : -f 2 |sed -e 's/MB$//')
  429. p1_size=$(($dev_size - 3))
  430. if [ $p1_size -le 0 ]; then
  431. echo "Your device isn't big enough to hold $SRC"
  432. echo "It is $(($p1_size * -1)) MB too small"
  433. exitclean
  434. fi
  435. p1_start=1
  436. p1_end=$(($p1_size + 1))
  437. /sbin/parted -s $device u MB mkpart primary ext2 $p1_start $p1_end set 1 boot on
  438. # Sometimes automount can be _really_ annoying.
  439. echo "Waiting for devices to settle..."
  440. /sbin/udevadm settle
  441. sleep 5
  442. TGTDEV=${device}1
  443. umount $TGTDEV &> /dev/null || :
  444. # Check extlinux version
  445. if extlinux -v 2>&1 | grep -q 'extlinux 3'; then
  446. mkfs=/sbin/mkfs.ext3
  447. else
  448. mkfs=/sbin/mkfs.ext4
  449. fi
  450. $mkfs -L $label $TGTDEV
  451. TGTLABEL="UUID=$(/sbin/blkid -s UUID -o value $TGTDEV)"
  452. }
  453. checkGPT() {
  454. dev=$1
  455. getdisk $dev
  456. if [ "$(LC_ALL=C /sbin/parted -m $device p 2>/dev/null |grep -ic :gpt:)" -eq "0" ]; then
  457. echo "EFI boot requires a GPT partition table."
  458. echo "This can be done manually or you can run with --format"
  459. exitclean
  460. fi
  461. partinfo=$(LC_ALL=C /sbin/parted --script -m $device "print" |grep ^$partnum:)
  462. volname=$(echo $partinfo |cut -d : -f 6)
  463. flags=$(echo $partinfo |cut -d : -f 7)
  464. if [ "$volname" != "EFI System Partition" ]; then
  465. echo "Partition name must be 'EFI System Partition'"
  466. echo "This can be set in parted or you can run with --reset-mbr"
  467. exitclean
  468. fi
  469. if [ "$(echo $flags |grep -c boot)" = "0" ]; then
  470. echo "Partition isn't marked bootable!"
  471. echo "You can mark the partition as bootable with "
  472. echo " # /sbin/parted $device"
  473. echo " (parted) toggle N boot"
  474. echo " (parted) quit"
  475. exitclean
  476. fi
  477. }
  478. checkFilesystem() {
  479. dev=$1
  480. TGTFS=$(/sbin/blkid -s TYPE -o value $dev || :)
  481. if [ "$TGTFS" != "vfat" ] && [ "$TGTFS" != "msdos" ]; then
  482. if [ "$TGTFS" != "ext2" ] && [ "$TGTFS" != "ext3" ] && [ "$TGTFS" != "ext4" ] && [ "$TGTFS" != "btrfs" ]; then
  483. echo "Target filesystem ($dev:$TGTFS) must be vfat, ext[234] or btrfs"
  484. exitclean
  485. fi
  486. fi
  487. if [ "$TGTFS" = "ext2" -o "$TGTFS" = "ext3" -o "$TGTFS" = "ext4" ] && [ ! -x /usr/sbin/extlinux ]; then
  488. echo "Target filesystem ($TGTFS) requires syslinux-extlinux to be installed."
  489. exitclean
  490. fi
  491. TGTLABEL=$(/sbin/blkid -s LABEL -o value $dev)
  492. if [ "$TGTLABEL" != "$label" ]; then
  493. if [ "$TGTFS" = "vfat" -o "$TGTFS" = "msdos" ]; then
  494. /sbin/dosfslabel $dev $label
  495. if [ $? -gt 0 ]; then
  496. echo "dosfslabel failed on $dev, device not setup"
  497. exitclean
  498. fi
  499. elif [ "$TGTFS" = "ext2" -o "$TGTFS" = "ext3" -o "$TGTFS" = "ext4" ]; then
  500. /sbin/e2label $dev $label
  501. if [ $? -gt 0 ]; then
  502. echo "e2label failed on $dev, device not setup"
  503. exitclean
  504. fi
  505. else
  506. echo "Unknown filesystem type. Try setting its label to $label and re-running"
  507. exitclean
  508. fi
  509. fi
  510. # Use UUID if available
  511. TGTUUID=$(/sbin/blkid -s UUID -o value $dev)
  512. if [ -n "$TGTUUID" ]; then
  513. TGTLABEL="UUID=$TGTUUID"
  514. else
  515. TGTLABEL="LABEL=$label"
  516. fi
  517. if [ "$TGTFS" = "vfat" -o "$TGTFS" = "msdos" ]; then
  518. mountopts="-o shortname=winnt,umask=0077"
  519. fi
  520. }
  521. checkSyslinuxVersion() {
  522. if [ ! -x /usr/bin/syslinux ]; then
  523. echo "You need to have syslinux installed to run this script"
  524. exit 1
  525. fi
  526. check=($(syslinux --version 2>&1)) || :
  527. if [[ 'syslinux' != $check ]]; then
  528. SYSLINUXPATH=""
  529. elif [ -n "$multi" ]; then
  530. SYSLINUXPATH="$LIVEOS/syslinux"
  531. else
  532. SYSLINUXPATH="syslinux"
  533. fi
  534. }
  535. checkMounted() {
  536. dev=$1
  537. if grep -q "^$dev " /proc/mounts ; then
  538. echo "$dev is mounted, please unmount for safety"
  539. exitclean
  540. fi
  541. if grep -q "^$dev " /proc/swaps; then
  542. echo "$dev is in use as a swap device, please disable swap"
  543. exitclean
  544. fi
  545. }
  546. checkint() {
  547. if ! test $1 -gt 0 2>/dev/null ; then
  548. shortusage
  549. exit 1
  550. fi
  551. }
  552. if [ $(id -u) != 0 ]; then
  553. echo "You need to be root to run this script"
  554. exit 1
  555. fi
  556. detectsrctype() {
  557. if [[ -e "$SRCMNT/Packages" ]]; then
  558. echo "/Packages found, will copy source packages to target"
  559. packages=1
  560. fi
  561. if [[ -e "$SRCMNT/LiveOS/squashfs.img" ]]; then
  562. # LiveOS style boot image
  563. srctype=live
  564. return
  565. fi
  566. if [ -e $SRCMNT/images/install.img -o -e $SRCMNT/isolinux/initrd.img ]; then
  567. if [ -n "$packages" ]; then
  568. srctype=installer
  569. else
  570. srctype=netinst
  571. fi
  572. imgtype=install
  573. if [ ! -e $SRCMNT/images/install.img ]; then
  574. echo "$SRC uses initrd.img w/o install.img"
  575. imgtype=initrd
  576. fi
  577. return
  578. fi
  579. echo "ERROR: $SRC does not appear to be a Live image or DVD installer."
  580. exitclean
  581. }
  582. cp_p() {
  583. strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
  584. | awk '{
  585. count += $NF
  586. if (count % 10 == 0) {
  587. percent = count / total_size * 100
  588. printf "%3d%% [", percent
  589. for (i=0;i<=percent;i++)
  590. printf "="
  591. printf ">"
  592. for (i=percent;i<100;i++)
  593. printf " "
  594. printf "]\r"
  595. }
  596. }
  597. END { print "" }' total_size=$(stat -c '%s' "${1}") count=0
  598. }
  599. copyFile() {
  600. if [ -x /usr/bin/rsync ]; then
  601. rsync --inplace -P "$1" "$2"
  602. return
  603. fi
  604. if [ -x /usr/bin/gvfs-copy ]; then
  605. gvfs-copy -p "$1" "$2"
  606. return
  607. fi
  608. if [ -x /usr/bin/strace -a -x /bin/awk ]; then
  609. cp_p "$1" "$2"
  610. return
  611. fi
  612. cp "$1" "$2"
  613. }
  614. set -e
  615. set -o pipefail
  616. trap exitclean EXIT
  617. shopt -s extglob
  618. cryptedhome=1
  619. keephome=1
  620. homesizemb=0
  621. swapsizemb=0
  622. overlaysizemb=0
  623. srctype=
  624. imgtype=
  625. packages=
  626. LIVEOS=LiveOS
  627. HOMEFILE="home.img"
  628. updates=
  629. ks=
  630. label="LIVE"
  631. while true ; do
  632. case $1 in
  633. --help | -h | -?)
  634. usage
  635. ;;
  636. --noverify)
  637. noverify=1
  638. ;;
  639. --format)
  640. format=1
  641. ;;
  642. --msdos)
  643. usemsdos=1
  644. ;;
  645. --reset-mbr|--resetmbr)
  646. resetmbr=1
  647. ;;
  648. --efi|--mactel)
  649. efi=1
  650. ;;
  651. --skipcopy)
  652. skipcopy=1
  653. ;;
  654. --force)
  655. force=1
  656. ;;
  657. --xo)
  658. xo=1
  659. skipcompress=1
  660. ;;
  661. --xo-no-home)
  662. xonohome=1
  663. ;;
  664. --timeout)
  665. checkint $2
  666. timeout=$2
  667. shift
  668. ;;
  669. --totaltimeout)
  670. checkint $2
  671. totaltimeout=$2
  672. shift
  673. ;;
  674. --extra-kernel-args)
  675. kernelargs=$2
  676. shift
  677. ;;
  678. --multi)
  679. multi=1
  680. ;;
  681. --livedir)
  682. LIVEOS=$2
  683. shift
  684. ;;
  685. --compress)
  686. skipcompress=""
  687. ;;
  688. --skipcompress)
  689. skipcompress=1
  690. ;;
  691. --swap-size-mb)
  692. checkint $2
  693. swapsizemb=$2
  694. shift
  695. ;;
  696. --overlay-size-mb)
  697. checkint $2
  698. overlaysizemb=$2
  699. shift
  700. ;;
  701. --home-size-mb)
  702. checkint $2
  703. homesizemb=$2
  704. shift
  705. ;;
  706. --crypted-home)
  707. cryptedhome=1
  708. ;;
  709. --unencrypted-home)
  710. cryptedhome=""
  711. ;;
  712. --delete-home)
  713. keephome=""
  714. ;;
  715. --updates)
  716. updates=$2
  717. shift
  718. ;;
  719. --ks)
  720. ks=$2
  721. shift
  722. ;;
  723. --label)
  724. label=$2
  725. shift
  726. ;;
  727. --*)
  728. echo "invalid arg -- $1"
  729. shortusage
  730. exit 1
  731. ;;
  732. *)
  733. break
  734. ;;
  735. esac
  736. shift
  737. done
  738. if [ $# -ne 2 ]; then
  739. echo "Missing arguments"
  740. shortusage
  741. exit 1
  742. fi
  743. SRC=$(readlink -f "$1") || :
  744. TGTDEV=$(readlink -f "$2") || :
  745. if [ -z "$SRC" ]; then
  746. echo "Missing source"
  747. shortusage
  748. exit 1
  749. fi
  750. if [ ! -b "$SRC" -a ! -f "$SRC" ]; then
  751. echo "$SRC is not a file or block device"
  752. shortusage
  753. exit 1
  754. fi
  755. if [ -z "$TGTDEV" ]; then
  756. echo "Missing target device"
  757. shortusage
  758. exit 1
  759. fi
  760. if [ ! -b "$TGTDEV" ]; then
  761. echo "$TGTDEV is not a block device"
  762. shortusage
  763. exit 1
  764. fi
  765. if [ -z "$noverify" ]; then
  766. # verify the image
  767. echo "Verifying image..."
  768. if ! checkisomd5 --verbose "$SRC"; then
  769. echo "Are you SURE you want to continue?"
  770. echo "Press Enter to continue or ctrl-c to abort"
  771. read
  772. fi
  773. fi
  774. # do some basic sanity checks.
  775. checkMounted $TGTDEV
  776. # FIXME: would be better if we had better mountpoints
  777. SRCMNT=$(mktemp -d /media/srctmp.XXXXXX)
  778. if [ -b "$SRC" ]; then
  779. mount -o ro "$SRC" $SRCMNT || exitclean
  780. elif [ -f "$SRC" ]; then
  781. mount -o loop,ro "$SRC" $SRCMNT || exitclean
  782. else
  783. echo "$SRC is not a file or block device."
  784. exitclean
  785. fi
  786. # Figure out what needs to be done based on the source image
  787. detectsrctype
  788. # Format the device
  789. if [ -n "$format" -a -z "$skipcopy" ]; then
  790. checkLVM $TGTDEV
  791. if [ -n "$efi" ]; then
  792. createGPTLayout $TGTDEV
  793. elif [ -n "$usemsdos" -o ! -x /sbin/extlinux ]; then
  794. createMSDOSLayout $TGTDEV
  795. else
  796. createEXTFSLayout $TGTDEV
  797. fi
  798. fi
  799. checkFilesystem $TGTDEV
  800. if [ -n "$efi" ]; then
  801. checkGPT $TGTDEV
  802. fi
  803. checkSyslinuxVersion
  804. # Because we can't set boot flag for EFI Protective on msdos partition tables
  805. [ -z "$efi" ] && checkPartActive $TGTDEV
  806. [ -n "$resetmbr" ] && resetMBR $TGTDEV
  807. checkMBR $TGTDEV
  808. if [ "$overlaysizemb" -gt 0 ]; then
  809. if [ "$TGTFS" = "vfat" -a "$overlaysizemb" -gt 4095 ]; then
  810. echo "Can't have an overlay of 4095MB or greater on VFAT"
  811. exitclean
  812. fi
  813. LABEL=$(/sbin/blkid -s LABEL -o value $TGTDEV)
  814. if [[ "$LABEL" =~ ( ) ]]; then
  815. echo "The LABEL($LABEL) on $TGTDEV has spaces in it, which do not work with the overlay"
  816. echo "You can re-format or use dosfslabel/e2fslabel to change it"
  817. exitclean
  818. fi
  819. fi
  820. if [ "$homesizemb" -gt 0 -a "$TGTFS" = "vfat" ]; then
  821. if [ "$homesizemb" -gt 4095 ]; then
  822. echo "Can't have a home overlay greater than 4095MB on VFAT"
  823. exitclean
  824. fi
  825. fi
  826. if [ "$swapsizemb" -gt 0 -a "$TGTFS" = "vfat" ]; then
  827. if [ "$swapsizemb" -gt 4095 ]; then
  828. echo "Can't have a swap file greater than 4095MB on VFAT"
  829. exitclean
  830. fi
  831. fi
  832. TGTMNT=$(mktemp -d /media/tgttmp.XXXXXX)
  833. mount $mountopts $TGTDEV $TGTMNT || exitclean
  834. trap exitclean SIGINT SIGTERM
  835. if [ -f "$TGTMNT/$LIVEOS/$HOMEFILE" -a -n "$keephome" -a "$homesizemb" -gt 0 ]; then
  836. echo "ERROR: Requested keeping existing /home and specified a size for /home"
  837. echo "Please either don't specify a size or specify --delete-home"
  838. exitclean
  839. fi
  840. if [ -n "$efi" ]; then
  841. if [ -d $SRCMNT/EFI/BOOT ]; then
  842. EFI_BOOT="/EFI/BOOT"
  843. elif [ -d $SRCMNT/EFI/boot ]; then
  844. EFI_BOOT="/EFI/boot"
  845. else
  846. echo "ERROR: This live image does not support EFI booting"
  847. exitclean
  848. fi
  849. fi
  850. # let's try to make sure there's enough room on the target device
  851. if [[ -d $TGTMNT/$LIVEOS ]]; then
  852. tbd=($(du -B 1M $TGTMNT/$LIVEOS))
  853. if [[ -s $TGTMNT/$LIVEOS/$HOMEFILE ]] && [[ -n $keephome ]]; then
  854. homesize=($(du -B 1M $TGTMNT/$LIVEOS/$HOMEFILE))
  855. tbd=$((tbd - homesize))
  856. fi
  857. else
  858. tbd=0
  859. fi
  860. if [[ live == $srctype ]]; then
  861. targets="$TGTMNT/$SYSLINUXPATH"
  862. [[ -n $efi ]] && targets+=" $TGTMNT$EFI_BOOT"
  863. [[ -n $xo ]] && targets+=" $TGTMNT/boot/olpc.fth"
  864. target_size=$(du -s -c -B 1M $targets 2> /dev/null | awk '/total$/ {print $1;}') || :
  865. tbd=$((tbd + target_size))
  866. fi
  867. if [[ -n $skipcompress ]] && [[ -s $SRCMNT/LiveOS/squashfs.img ]]; then
  868. if mount -o loop $SRCMNT/LiveOS/squashfs.img $SRCMNT; then
  869. livesize=($(du -B 1M --apparent-size $SRCMNT/LiveOS/ext3fs.img))
  870. umount $SRCMNT
  871. if ((livesize > 4095)) && [[ vfat == $TGTFS ]]; then
  872. echo "
  873. An uncompressed image size greater than 4095 MB is not suitable
  874. for a VFAT-formatted device. The compressed SquashFS will be
  875. copied to the target device.
  876. "
  877. skipcompress=""
  878. livesize=0
  879. fi
  880. else
  881. echo "WARNING: --skipcompress or --xo was specified but the
  882. currently-running kernel can not mount the SquashFS from the source
  883. file to extract it. Instead, the compressed SquashFS will be copied
  884. to the target device."
  885. skipcompress=""
  886. fi
  887. fi
  888. if [[ live == $srctype ]]; then
  889. thisScriptpath=$(readlink -f "$0")
  890. sources="$SRCMNT/LiveOS/ext3fs.img $SRCMNT/LiveOS/osmin.img"
  891. [[ -z $skipcompress ]] && sources+=" $SRCMNT/LiveOS/squashfs.img"
  892. sources+=" $SRCMNT/isolinux $SRCMNT/syslinux"
  893. [[ -n $efi ]] && sources+=" $SRCMNT$EFI_BOOT"
  894. [[ -n $xo ]] && sources+=" $SRCMNT/boot/olpc.fth"
  895. source_size=$(du -s -c -B 1M "$thisScriptpath" $sources 2> /dev/null | awk '/total$/ {print $1;}') || :
  896. livesize=$((livesize + source_size))
  897. fi
  898. freespace=$(df -B 1M --total $TGTDEV | awk '/^total/ {print $4;}')
  899. if [[ live == $srctype ]]; then
  900. tba=$((overlaysizemb + homesizemb + livesize + swapsizemb))
  901. if ((tba > freespace + tbd)); then
  902. needed=$((tba - freespace - tbd))
  903. printf "\n The live image + overlay, home, & swap space, if requested,
  904. \r will NOT fit in the space available on the target device.\n
  905. \r + Size of live image: %10s MiB\n" $livesize
  906. (($overlaysizemb > 0)) && \
  907. printf " + Overlay size: %16s\n" $overlaysizemb
  908. (($homesizemb > 0)) && \
  909. printf " + Home directory size: %9s\n" $homesizemb
  910. (($swapsizemb > 0)) && \
  911. printf " + Swap overlay size: %11s\n" $swapsizemb
  912. printf " = Total requested space: %6s MiB\n" $tba
  913. printf " - Space available: %12s\n" $((freespace + tbd))
  914. printf " ==============================\n"
  915. printf " Space needed: %15s MiB\n\n" $needed
  916. printf " To fit the installation on this device,
  917. \r free space on the target, or decrease the
  918. \r requested size total by: %6s MiB\n\n" $needed
  919. exitclean
  920. fi
  921. fi
  922. # Verify available space for DVD installer
  923. if [ "$srctype" = "installer" ]; then
  924. if [ "$imgtype" = "install" ]; then
  925. imgpath=images/install.img
  926. else
  927. imgpath=isolinux/initrd.img
  928. fi
  929. installimgsize=$(du -s -B 1M $SRCMNT/$imgpath | awk {'print $1;'})
  930. tbd=0
  931. if [ -e $TGTMNT/$imgpath ]; then
  932. tbd=$(du -s -B 1M $TGTMNT/$imgpath | awk {'print $1;'})
  933. fi
  934. if [ -e "$TGTMNT/$(basename "$SRC")" ]; then
  935. tbd=$(($tbd + $(du -s -B 1M "$TGTMNT/$(basename "$SRC")" | awk {'print $1;'})))
  936. fi
  937. echo "Size of $imgpath: $installimgsize"
  938. echo "Available space: $((freespace + tbd))"
  939. if (( installimgsize > ((freespace + tbd)) )); then
  940. echo "ERROR: Unable to fit DVD image + install.img on available space on the target device."
  941. exitclean
  942. fi
  943. fi
  944. if [ -z "$skipcopy" ] && [ "$srctype" = "live" ]; then
  945. if [ -d $TGTMNT/$LIVEOS -a -z "$force" ]; then
  946. echo "Already set up as live image."
  947. if [ -z "$keephome" -a -e $TGTMNT/$LIVEOS/$HOMEFILE ]; then
  948. echo "WARNING: Persistent /home will be deleted!!!"
  949. echo "Press Enter to continue or ctrl-c to abort"
  950. read
  951. else
  952. echo "Deleting old OS in fifteen seconds..."
  953. sleep 15
  954. [ -e "$TGTMNT/$LIVEOS/$HOMEFILE" -a -n "$keephome" ] && mv $TGTMNT/$LIVEOS/$HOMEFILE $TGTMNT/$HOMEFILE
  955. fi
  956. rm -rf $TGTMNT/$LIVEOS
  957. fi
  958. fi
  959. # Bootloader is always reconfigured, so keep these out of the if skipcopy stuff.
  960. [ ! -d $TGTMNT/$SYSLINUXPATH ] && mkdir -p $TGTMNT/$SYSLINUXPATH
  961. [ -n "$efi" -a ! -d $TGTMNT$EFI_BOOT ] && mkdir -p $TGTMNT$EFI_BOOT
  962. # Live image copy
  963. if [ "$srctype" = "live" -a -z "$skipcopy" ]; then
  964. echo "Copying live image to target device."
  965. [ ! -d $TGTMNT/$LIVEOS ] && mkdir $TGTMNT/$LIVEOS
  966. [ -n "$keephome" -a -f "$TGTMNT/$HOMEFILE" ] && mv $TGTMNT/$HOMEFILE $TGTMNT/$LIVEOS/$HOMEFILE
  967. if [ -n "$skipcompress" -a -f $SRCMNT/LiveOS/squashfs.img ]; then
  968. mount -o loop $SRCMNT/LiveOS/squashfs.img $SRCMNT || exitclean
  969. copyFile $SRCMNT/LiveOS/ext3fs.img $TGTMNT/$LIVEOS/ext3fs.img || {
  970. umount $SRCMNT ; exitclean ; }
  971. umount $SRCMNT
  972. elif [ -f $SRCMNT/LiveOS/squashfs.img ]; then
  973. copyFile $SRCMNT/LiveOS/squashfs.img $TGTMNT/$LIVEOS/squashfs.img || exitclean
  974. elif [ -f $SRCMNT/LiveOS/ext3fs.img ]; then
  975. copyFile $SRCMNT/LiveOS/ext3fs.img $TGTMNT/$LIVEOS/ext3fs.img || exitclean
  976. fi
  977. if [ -f $SRCMNT/LiveOS/osmin.img ]; then
  978. copyFile $SRCMNT/LiveOS/osmin.img $TGTMNT/$LIVEOS/osmin.img || exitclean
  979. fi
  980. sync
  981. fi
  982. # Adjust syslinux sources for replication of installed images
  983. # between filesystem types.
  984. if [[ -d $SRCMNT/isolinux/ ]]; then
  985. cp $SRCMNT/isolinux/* $TGTMNT/$SYSLINUXPATH
  986. elif [[ -d $SRCMNT/syslinux/ ]]; then
  987. cp $SRCMNT/syslinux/* $TGTMNT/$SYSLINUXPATH
  988. if [[ -f $SRCMNT/syslinux/extlinux.conf ]]; then
  989. mv $TGTMNT/$SYSLINUXPATH/extlinux.conf \
  990. $TGTMNT/$SYSLINUXPATH/isolinux.cfg
  991. elif [[ -f $SRCMNT/syslinux/syslinux.cfg ]]; then
  992. mv $TGTMNT/$SYSLINUXPATH/syslinux.cfg $TGTMNT/$SYSLINUXPATH/isolinux.cfg
  993. fi
  994. fi
  995. BOOTCONFIG=$TGTMNT/$SYSLINUXPATH/isolinux.cfg
  996. # Set this to nothing so sed doesn't care
  997. BOOTCONFIG_EFI=
  998. if [ -n "$efi" ]; then
  999. echo "Setting up $EFI_BOOT"
  1000. cp -r $SRCMNT$EFI_BOOT/* $TGTMNT$EFI_BOOT
  1001. # The GRUB EFI config file can be one of:
  1002. # boot?*.conf
  1003. # BOOT?*.conf
  1004. # grub.cfg
  1005. if [ -e $TGTMNT$EFI_BOOT/grub.cfg ]; then
  1006. BOOTCONFIG_EFI=$TGTMNT$EFI_BOOT/grub.cfg
  1007. elif [ -e $TGTMNT$EFI_BOOT/+(BOOT|boot)?*.conf ]; then
  1008. BOOTCONFIG_EFI=$TGTMNT$EFI_BOOT/+(BOOT|boot)?*.conf
  1009. else
  1010. echo "Unable to find EFI config file."
  1011. exitclean
  1012. fi
  1013. rm -f $TGTMNT$EFI_BOOT/grub.conf
  1014. # On some images (RHEL) the BOOT*.efi file isn't in $EFI_BOOT, but is in
  1015. # the eltorito image, so try to extract it if it is missing
  1016. # test for presence of *.efi grub binary
  1017. if [ ! -f $TGTMNT$EFI_BOOT/+(BOOT|boot)?*.efi ]; then
  1018. if [ ! -x /usr/bin/dumpet ]; then
  1019. echo "No /usr/bin/dumpet tool found. EFI image will not boot."
  1020. echo "Source media is missing grub binary in /EFI/BOOT/*efi"
  1021. exitclean
  1022. else
  1023. # dump the eltorito image with dumpet, output is $SRC.1
  1024. dumpet -i "$SRC" -d
  1025. EFIMNT=$(mktemp -d /media/srctmp.XXXXXX)
  1026. mount -o loop "$SRC".1 $EFIMNT
  1027. if [ -f $EFIMNT$EFI_BOOT/+(BOOT|boot)?*.efi ]; then
  1028. cp $EFIMNT$EFI_BOOT/+(BOOT|boot)?*.efi $TGTMNT$EFI_BOOT
  1029. else
  1030. echo "No BOOT*.efi found in eltorito image. EFI will not boot"
  1031. umount $EFIMNT
  1032. rm "$SRC".1
  1033. exitclean
  1034. fi
  1035. umount $EFIMNT
  1036. rm "$SRC".1
  1037. fi
  1038. fi
  1039. fi
  1040. # DVD installer copy
  1041. if [ -z "$skipcopy" -a \( "$srctype" = "installer" -o "$srctype" = "netinst" \) ]; then
  1042. echo "Copying DVD image to target device."
  1043. mkdir -p $TGTMNT/images/
  1044. if [ "$imgtype" = "install" ]; then
  1045. for img in install.img updates.img product.img; do
  1046. if [ -e $SRCMNT/images/$img ]; then
  1047. copyFile $SRCMNT/images/$img $TGTMNT/images/$img || exitclean
  1048. fi
  1049. done
  1050. fi
  1051. fi
  1052. # Copy packages over.
  1053. # Before Fedora17 we could copy the .iso and setup a repo=
  1054. # F17 and later look for repodata on the source media.
  1055. # The presence of packages and LiveOS indicates F17 or later.
  1056. if [ -n "$packages" -a -z "$skipcopy" ]; then
  1057. if [ "$srctype" != "live" ]; then
  1058. echo "Copying $SRC to device"
  1059. copyFile "$SRC" "$TGTMNT/"
  1060. # Setup a repo= to point to the .iso
  1061. sed -i -e "s;initrd.img;initrd.img inst.repo=hd:$TGTLABEL:/;g" $BOOTCONFIG
  1062. if [ -n "$efi" ]; then
  1063. sed -i -e "s;vmlinuz;vmlinuz inst.repo=hd:$TGTLABEL:/;g" $BOOTCONFIG_EFI
  1064. fi
  1065. else
  1066. echo "Copying package data from $SRC to device"
  1067. rsync --inplace -rLDP --exclude EFI/ --exclude images/ --exclude isolinux/ \
  1068. --exclude TRANS.TBL --exclude LiveOS/ "$SRCMNT/" "$TGTMNT/"
  1069. fi
  1070. echo "Waiting for device to finish writing"
  1071. sync
  1072. fi
  1073. if [ "$srctype" = "live" ]; then
  1074. # Copy this installer script.
  1075. cp -fT "$thisScriptpath" $TGTMNT/$LIVEOS/livecd-iso-to-disk
  1076. chmod +x $TGTMNT/$LIVEOS/livecd-iso-to-disk &> /dev/null || :
  1077. # When the source is an installed Live USB/SD image, restore the boot config
  1078. # file to a base state before updating.
  1079. if [[ -d $SRCMNT/syslinux/ ]]; then
  1080. echo "Preparing boot config file."
  1081. sed -i -e "s/root=live:[^ ]*/root=live:CDLABEL=name/"\
  1082. -e "s/\(r*d*.*live.*ima*ge*\) .* quiet/\1 quiet/"\
  1083. $BOOTCONFIG $BOOTCONFIG_EFI
  1084. sed -i -e "s/^timeout.*$/timeout\ 100/"\
  1085. -e "/^totaltimeout.*$/d" $BOOTCONFIG
  1086. fi
  1087. fi
  1088. # Setup the updates.img
  1089. if [ -n "$updates" ]; then
  1090. copyFile "$updates" "$TGTMNT/updates.img"
  1091. kernelargs+=" inst.updates=hd:$TGTLABEL:/updates.img"
  1092. fi
  1093. # Setup the kickstart
  1094. if [ -n "$ks" ]; then
  1095. copyFile "$ks" "$TGTMNT/ks.cfg"
  1096. kernelargs+=" inst.ks=hd:$TGTLABEL:/ks.cfg"
  1097. fi
  1098. echo "Updating boot config file"
  1099. # adjust label and fstype
  1100. sed -i -e "s/CDLABEL=[^ ]*/$TGTLABEL/" -e "s/rootfstype=[^ ]*/rootfstype=$TGTFS/" -e "s/LABEL=[^ :]*/$TGTLABEL/" $BOOTCONFIG $BOOTCONFIG_EFI
  1101. if [ -n "$kernelargs" ]; then
  1102. sed -i -e "s;initrd.\?\.img;& ${kernelargs};" $BOOTCONFIG
  1103. if [ -n "$efi" ]; then
  1104. sed -i -e "s;vmlinuz.\?;& ${kernelargs} ;" $BOOTCONFIG_EFI
  1105. fi
  1106. fi
  1107. if [ "$LIVEOS" != "LiveOS" ]; then
  1108. sed -i -e "s;r*d*.*live.*ima*ge*;& live_dir=$LIVEOS;"\
  1109. $BOOTCONFIG $BOOTCONFIG_EFI
  1110. fi
  1111. # EFI images are in $SYSLINUXPATH now
  1112. if [ -n "$efi" ]; then
  1113. sed -i -e "s;/isolinux/;/$SYSLINUXPATH/;g" $BOOTCONFIG_EFI
  1114. sed -i -e "s;/images/pxeboot/;/$SYSLINUXPATH/;g" $BOOTCONFIG_EFI
  1115. sed -i -e "s;findiso;;g" $BOOTCONFIG_EFI
  1116. fi
  1117. # DVD Installer for netinst
  1118. if [ "$srctype" != "live" ]; then
  1119. if [ "$imgtype" = "install" ]; then
  1120. sed -i -e "s;initrd.img;initrd.img inst.stage2=hd:$TGTLABEL:/images/install.img;g" $BOOTCONFIG
  1121. if [ -n "$efi" ]; then
  1122. sed -i -e "s;vmlinuz;vmlinuz inst.stage2=hd:$TGTLABEL:/images/install.img;g" $BOOTCONFIG_EFI
  1123. fi
  1124. else
  1125. # The initrd has everything, so no stage2
  1126. sed -i -e "s;\S*stage2=\S*;;g" $BOOTCONFIG $BOOTCONFIG_EFI
  1127. fi
  1128. fi
  1129. # Adjust the boot timeouts
  1130. if [ -n "$timeout" ]; then
  1131. sed -i -e "s/^timeout.*$/timeout\ $timeout/" $BOOTCONFIG
  1132. fi
  1133. if [ -n "$totaltimeout" ]; then
  1134. sed -i -e "/^timeout.*$/a\totaltimeout\ $totaltimeout" $BOOTCONFIG
  1135. fi
  1136. if [ "$overlaysizemb" -gt 0 ]; then
  1137. echo "Initializing persistent overlay file"
  1138. OVERFILE="overlay-$( /sbin/blkid -s LABEL -o value $TGTDEV )-$( /sbin/blkid -s UUID -o value $TGTDEV )"
  1139. if [ -z "$skipcopy" ]; then
  1140. if [ "$TGTFS" = "vfat" ]; then
  1141. # vfat can't handle sparse files
  1142. dd if=/dev/zero of=$TGTMNT/$LIVEOS/$OVERFILE count=$overlaysizemb bs=1M
  1143. else
  1144. dd if=/dev/null of=$TGTMNT/$LIVEOS/$OVERFILE count=1 bs=1M seek=$overlaysizemb
  1145. fi
  1146. fi
  1147. sed -i -e "s/r*d*.*live.*ima*ge*/& rd.live.overlay=${TGTLABEL}/"\
  1148. $BOOTCONFIG $BOOTCONFIG_EFI
  1149. sed -i -e "s/\ ro\ /\ rw\ /" $BOOTCONFIG $BOOTCONFIG_EFI
  1150. fi
  1151. if [ "$swapsizemb" -gt 0 -a -z "$skipcopy" ]; then
  1152. echo "Initializing swap file"
  1153. dd if=/dev/zero of=$TGTMNT/$LIVEOS/swap.img count=$swapsizemb bs=1M
  1154. mkswap -f $TGTMNT/$LIVEOS/swap.img
  1155. fi
  1156. if [ "$homesizemb" -gt 0 -a -z "$skipcopy" ]; then
  1157. echo "Initializing persistent /home"
  1158. homesource=/dev/zero
  1159. [ -n "$cryptedhome" ] && homesource=/dev/urandom
  1160. if [ "$TGTFS" = "vfat" ]; then
  1161. # vfat can't handle sparse files
  1162. dd if=${homesource} of=$TGTMNT/$LIVEOS/$HOMEFILE count=$homesizemb bs=1M
  1163. else
  1164. dd if=/dev/null of=$TGTMNT/$LIVEOS/$HOMEFILE count=1 bs=1M seek=$homesizemb
  1165. fi
  1166. if [ -n "$cryptedhome" ]; then
  1167. loop=$(losetup -f)
  1168. losetup $loop $TGTMNT/$LIVEOS/$HOMEFILE
  1169. echo "Encrypting persistent /home"
  1170. while ! cryptsetup luksFormat -y -q $loop; do :; done;
  1171. echo "Please enter the password again to unlock the device"
  1172. while ! cryptsetup luksOpen $loop EncHomeFoo; do :; done;
  1173. mkfs.ext4 -j /dev/mapper/EncHomeFoo
  1174. tune2fs -c0 -i0 -ouser_xattr,acl /dev/mapper/EncHomeFoo
  1175. sleep 2
  1176. cryptsetup luksClose EncHomeFoo
  1177. losetup -d $loop
  1178. else
  1179. echo "Formatting unencrypted /home"
  1180. mkfs.ext4 -F -j $TGTMNT/$LIVEOS/$HOMEFILE
  1181. tune2fs -c0 -i0 -ouser_xattr,acl $TGTMNT/$LIVEOS/$HOMEFILE
  1182. fi
  1183. fi
  1184. # create the forth files for booting on the XO if requested
  1185. # we'd do this unconditionally, but you have to have a kernel that will
  1186. # boot on the XO anyway.
  1187. if [ -n "$xo" ]; then
  1188. echo "Setting up /boot/olpc.fth file"
  1189. args=$(grep "^ *append" $TGTMNT/$SYSLINUXPATH/isolinux.cfg |head -n1 |sed -e 's/.*initrd=[^ ]*//')
  1190. if [ -z "$xonohome" -a ! -f $TGTMNT/$LIVEOS/$HOMEFILE ]; then
  1191. args="$args persistenthome=mtd0"
  1192. fi
  1193. args="$args reset_overlay"
  1194. xosyspath=$(echo $SYSLINUXPATH | sed -e 's;/;\\;')
  1195. if [ ! -d $TGTMNT/boot ]; then
  1196. mkdir -p $TGTMNT/boot
  1197. fi
  1198. cat > $TGTMNT/boot/olpc.fth <<EOF
  1199. \ Boot script for USB boot
  1200. hex rom-pa fffc7 + 4 \$number drop h# 2e19 < [if]
  1201. patch 2drop erase claim-params
  1202. : high-ramdisk ( -- )
  1203. cv-load-ramdisk
  1204. h# 22c +lp l@ 1+ memory-limit umin /ramdisk - ffff.f000 and ( new-ramdisk-adr )
  1205. ramdisk-adr over /ramdisk move ( new-ramdisk-adr )
  1206. to ramdisk-adr
  1207. ;
  1208. ' high-ramdisk to load-ramdisk
  1209. [then]
  1210. : set-bootpath-dev ( -- )
  1211. " /chosen" find-package if ( phandle )
  1212. " bootpath" rot get-package-property 0= if ( propval$ )
  1213. get-encoded-string ( bootpath$ )
  1214. [char] \ left-parse-string 2nip ( dn$ )
  1215. dn-buf place ( )
  1216. then
  1217. then
  1218. " /sd" dn-buf count sindex 0>= if
  1219. " sd:"
  1220. else
  1221. " u:"
  1222. then
  1223. " BOOTPATHDEV" \$set-macro
  1224. ;
  1225. set-bootpath-dev
  1226. " $args" to boot-file
  1227. " \${BOOTPATHDEV}$xosyspath\initrd0.img" expand$ to ramdisk
  1228. " \${BOOTPATHDEV}$xosyspath\vmlinuz0" expand$ to boot-device
  1229. unfreeze
  1230. boot
  1231. EOF
  1232. fi
  1233. if [ -z "$multi" ]; then
  1234. echo "Installing boot loader"
  1235. if [ -n "$efi" ]; then
  1236. # replace the ia32 hack
  1237. if [ -f "$TGTMNT$EFI_BOOT/BOOT.conf" ]; then
  1238. cp -f $TGTMNT$EFI_BOOT/BOOTia32.conf $TGTMNT$EFI_BOOT/BOOT.conf
  1239. fi
  1240. fi
  1241. # this is a bit of a kludge, but syslinux doesn't guarantee the API for its com32 modules :/
  1242. if [ -f $TGTMNT/$SYSLINUXPATH/vesamenu.c32 -a -f /usr/share/syslinux/vesamenu.c32 ]; then
  1243. cp /usr/share/syslinux/vesamenu.c32 $TGTMNT/$SYSLINUXPATH/vesamenu.c32
  1244. elif [ -f $TGTMNT/$SYSLINUXPATH/vesamenu.c32 -a -f /usr/lib/syslinux/vesamenu.c32 ]; then
  1245. cp /usr/lib/syslinux/vesamenu.c32 $TGTMNT/$SYSLINUXPATH/vesamenu.c32
  1246. elif [ -f $TGTMNT/$SYSLINUXPATH/menu.c32 -a -f /usr/share/syslinux/menu.c32 ]; then
  1247. cp /usr/share/syslinux/menu.c32 $TGTMNT/$SYSLINUXPATH/menu.c32
  1248. elif [ -f $TGTMNT/$SYSLINUXPATH/menu.c32 -a -f /usr/lib/syslinux/menu.c32 ]; then
  1249. cp /usr/lib/syslinux/menu.c32 $TGTMNT/$SYSLINUXPATH/menu.c32
  1250. fi
  1251. if [ "$TGTFS" == "vfat" -o "$TGTFS" == "msdos" ]; then
  1252. # syslinux expects the config to be named syslinux.cfg
  1253. # and has to run with the file system unmounted
  1254. mv $TGTMNT/$SYSLINUXPATH/isolinux.cfg $TGTMNT/$SYSLINUXPATH/syslinux.cfg
  1255. # deal with mtools complaining about ldlinux.sys
  1256. if [ -f $TGTMNT/$SYSLINUXPATH/ldlinux.sys ]; then
  1257. rm -f $TGTMNT/$SYSLINUXPATH/ldlinux.sys
  1258. fi
  1259. cleanup
  1260. if [ -n "$SYSLINUXPATH" ]; then
  1261. syslinux -d $SYSLINUXPATH $TGTDEV
  1262. else
  1263. syslinux $TGTDEV
  1264. fi
  1265. elif [ "$TGTFS" == "ext2" -o "$TGTFS" == "ext3" -o "$TGTFS" == "ext4" -o "$TGTFS" == "btrfs" ]; then
  1266. # extlinux expects the config to be named extlinux.conf
  1267. # and has to be run with the file system mounted
  1268. mv $TGTMNT/$SYSLINUXPATH/isolinux.cfg $TGTMNT/$SYSLINUXPATH/extlinux.conf
  1269. extlinux -i $TGTMNT/$SYSLINUXPATH
  1270. # Starting with syslinux 4 ldlinux.sys is used on all file systems.
  1271. if [ -f "$TGTMNT/$SYSLINUXPATH/extlinux.sys" ]; then
  1272. chattr -i $TGTMNT/$SYSLINUXPATH/extlinux.sys
  1273. elif [ -f "$TGTMNT/$SYSLINUXPATH/ldlinux.sys" ]; then
  1274. chattr -i $TGTMNT/$SYSLINUXPATH/ldlinux.sys
  1275. fi
  1276. cleanup
  1277. fi
  1278. else
  1279. # we need to do some more config file tweaks for multi-image mode
  1280. sed -i -e "s;kernel vm;kernel /$LIVEOS/syslinux/vm;" $TGTMNT/$SYSLINUXPATH/isolinux.cfg
  1281. sed -i -e "s;initrd=i;initrd=/$LIVEOS/syslinux/i;" $TGTMNT/$SYSLINUXPATH/isolinux.cfg
  1282. mv $TGTMNT/$SYSLINUXPATH/isolinux.cfg $TGTMNT/$SYSLINUXPATH/syslinux.cfg
  1283. cleanup
  1284. fi
  1285. echo "Target device is now set up with a Live image!"