summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/mkefidisk.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/contrib/mkefidisk.sh')
-rwxr-xr-xscripts/contrib/mkefidisk.sh396
1 files changed, 396 insertions, 0 deletions
diff --git a/scripts/contrib/mkefidisk.sh b/scripts/contrib/mkefidisk.sh
new file mode 100755
index 0000000000..b96b7d4f7d
--- /dev/null
+++ b/scripts/contrib/mkefidisk.sh
@@ -0,0 +1,396 @@
1#!/bin/sh
2#
3# Copyright (c) 2012, Intel Corporation.
4# All rights reserved.
5#
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; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14# the GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19#
20
21LANG=C
22
23# Set to 1 to enable additional output
24DEBUG=0
25OUT="/dev/null"
26
27#
28# Defaults
29#
30# 20 Mb for the boot partition
31BOOT_SIZE=20
32# 5% for swap
33SWAP_RATIO=5
34
35# Cleanup after die()
36cleanup() {
37 debug "Syncing and unmounting devices"
38 # Unmount anything we mounted
39 unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
40 unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
41 unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
42 unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT"
43
44 # Remove the TMPDIR
45 debug "Removing temporary files"
46 if [ -d "$TMPDIR" ]; then
47 rm -rf $TMPDIR || error "Failed to remove $TMPDIR"
48 fi
49}
50
51trap 'die "Signal Received, Aborting..."' HUP INT TERM
52
53# Logging routines
54WARNINGS=0
55ERRORS=0
56CLEAR="$(tput sgr0)"
57INFO="$(tput bold)"
58RED="$(tput setaf 1)$(tput bold)"
59GREEN="$(tput setaf 2)$(tput bold)"
60YELLOW="$(tput setaf 3)$(tput bold)"
61info() {
62 echo "${INFO}$1${CLEAR}"
63}
64error() {
65 ERRORS=$((ERRORS+1))
66 echo "${RED}$1${CLEAR}"
67}
68warn() {
69 WARNINGS=$((WARNINGS+1))
70 echo "${YELLOW}$1${CLEAR}"
71}
72success() {
73 echo "${GREEN}$1${CLEAR}"
74}
75die() {
76 error "$1"
77 cleanup
78 exit 1
79}
80debug() {
81 if [ $DEBUG -eq 1 ]; then
82 echo "$1"
83 fi
84}
85
86usage() {
87 echo "Usage: $(basename $0) [-v] DEVICE HDDIMG TARGET_DEVICE"
88 echo " -v: Verbose debug"
89 echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
90 echo " HDDIMG: The hddimg file to generate the efi disk from"
91 echo " TARGET_DEVICE: The device the target will boot from, e.g. /dev/mmcblk0"
92}
93
94image_details() {
95 IMG=$1
96 info "Image details"
97 echo " image: $(stat --printf '%N\n' $IMG)"
98 echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
99 echo " modified: $(stat -L --printf '%y\n' $IMG)"
100 echo " type: $(file -L -b $IMG)"
101 echo ""
102}
103
104device_details() {
105 DEV=$1
106 BLOCK_SIZE=512
107
108 info "Device details"
109 echo " device: $DEVICE"
110 if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
111 echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
112 else
113 echo " vendor: UNKOWN"
114 fi
115 if [ -f "/sys/class/block/$DEV/device/model" ]; then
116 echo " model: $(cat /sys/class/block/$DEV/device/model)"
117 else
118 echo " model: UNKNOWN"
119 fi
120 if [ -f "/sys/class/block/$DEV/size" ]; then
121 echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
122 else
123 echo " size: UNKNOWN"
124 fi
125 echo ""
126}
127
128unmount_device() {
129 grep -q $DEVICE /proc/mounts
130 if [ $? -eq 0 ]; then
131 warn "$DEVICE listed in /proc/mounts, attempting to unmount"
132 umount $DEVICE* 2>/dev/null
133 return $?
134 fi
135 return 0
136}
137
138unmount() {
139 grep -q $1 /proc/mounts
140 if [ $? -eq 0 ]; then
141 debug "Unmounting $1"
142 umount $1
143 return $?
144 fi
145 return 0
146}
147
148#
149# Parse and validate arguments
150#
151if [ $# -lt 3 ] || [ $# -gt 4 ]; then
152 usage
153 exit 1
154fi
155
156if [ "$1" = "-v" ]; then
157 DEBUG=1
158 OUT="1"
159 shift
160fi
161
162DEVICE=$1
163HDDIMG=$2
164TARGET_DEVICE=$3
165
166LINK=$(readlink $DEVICE)
167if [ $? -eq 0 ]; then
168 DEVICE="$LINK"
169fi
170
171if [ ! -w "$DEVICE" ]; then
172 usage
173 die "Device $DEVICE does not exist or is not writable"
174fi
175
176if [ ! -e "$HDDIMG" ]; then
177 usage
178 die "HDDIMG $HDDIMG does not exist"
179fi
180
181#
182# Ensure the hddimg is not mounted
183#
184unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
185
186#
187# Check if any $DEVICE partitions are mounted
188#
189unmount_device || die "Failed to unmount $DEVICE"
190
191#
192# Confirm device with user
193#
194image_details $HDDIMG
195device_details $(basename $DEVICE)
196echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
197read RESPONSE
198if [ "$RESPONSE" != "y" ]; then
199 echo "Image creation aborted"
200 exit 0
201fi
202
203
204#
205# Prepare the temporary working space
206#
207TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
208HDDIMG_MNT=$TMPDIR/hddimg
209HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
210ROOTFS_MNT=$TMPDIR/rootfs
211BOOTFS_MNT=$TMPDIR/bootfs
212mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
213mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
214mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
215mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
216
217
218#
219# Partition $DEVICE
220#
221DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
222# If the device size is not reported there may not be a valid label
223if [ "$DEVICE_SIZE" = "" ] ; then
224 parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
225 DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
226fi
227SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
228ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
229ROOTFS_START=$((BOOT_SIZE))
230ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
231SWAP_START=$((ROOTFS_END))
232
233# MMC devices use a partition prefix character 'p'
234PART_PREFIX=""
235if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
236 PART_PREFIX="p"
237fi
238BOOTFS=$DEVICE${PART_PREFIX}1
239ROOTFS=$DEVICE${PART_PREFIX}2
240SWAP=$DEVICE${PART_PREFIX}3
241
242TARGET_PART_PREFIX=""
243if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
244 TARGET_PART_PREFIX="p"
245fi
246TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
247TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
248
249echo ""
250info "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
251info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
252info "Swap partition size: $SWAP_SIZE MB ($SWAP)"
253echo ""
254
255# Use MSDOS by default as GPT cannot be reliably distributed in disk image form
256# as it requires the backup table to be on the last block of the device, which
257# of course varies from device to device.
258
259info "Partitioning installation media ($DEVICE)"
260
261debug "Deleting partition table on $DEVICE"
262dd if=/dev/zero of=$DEVICE bs=512 count=2 >$OUT 2>&1 || die "Failed to zero beginning of $DEVICE"
263
264debug "Creating new partition table (MSDOS) on $DEVICE"
265parted $DEVICE mklabel msdos >$OUT 2>&1 || die "Failed to create MSDOS partition table"
266
267debug "Creating boot partition on $BOOTFS"
268parted $DEVICE mkpart primary 0% $BOOT_SIZE >$OUT 2>&1 || die "Failed to create BOOT partition"
269
270debug "Enabling boot flag on $BOOTFS"
271parted $DEVICE set 1 boot on >$OUT 2>&1 || die "Failed to enable boot flag"
272
273debug "Creating ROOTFS partition on $ROOTFS"
274parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END >$OUT 2>&1 || die "Failed to create ROOTFS partition"
275
276debug "Creating swap partition on $SWAP"
277parted $DEVICE mkpart primary $SWAP_START 100% >$OUT 2>&1 || die "Failed to create SWAP partition"
278
279if [ $DEBUG -eq 1 ]; then
280 parted $DEVICE print
281fi
282
283
284#
285# Check if any $DEVICE partitions are mounted after partitioning
286#
287unmount_device || die "Failed to unmount $DEVICE partitions"
288
289
290#
291# Format $DEVICE partitions
292#
293info "Formatting partitions"
294debug "Formatting $BOOTFS as vfat"
295if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
296 mkfs.vfat -I $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
297else
298 mkfs.vfat $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
299fi
300
301debug "Formatting $ROOTFS as ext3"
302mkfs.ext3 -F $ROOTFS -L "ROOT" >$OUT 2>&1 || die "Failed to format $ROOTFS"
303
304debug "Formatting swap partition ($SWAP)"
305mkswap $SWAP >$OUT 2>&1 || die "Failed to prepare swap"
306
307
308#
309# Installing to $DEVICE
310#
311debug "Mounting images and device in preparation for installation"
312mount -o loop $HDDIMG $HDDIMG_MNT >$OUT 2>&1 || error "Failed to mount $HDDIMG"
313mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount rootfs.img"
314mount $ROOTFS $ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
315mount $BOOTFS $BOOTFS_MNT >$OUT 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
316
317info "Preparing boot partition"
318EFIDIR="$BOOTFS_MNT/EFI/BOOT"
319cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy vmlinuz"
320# Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
321cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy EFI dir"
322# Silently ignore a missing gummiboot loader dir (we might just be a GRUB image)
323cp -r $HDDIMG_MNT/loader $BOOTFS_MNT >$OUT 2>&1
324
325# Update the boot loaders configurations for an installed image
326# Remove any existing root= kernel parameters and:
327# o Add a root= parameter with the target rootfs
328# o Specify ro so fsck can be run during boot
329# o Specify rootwait in case the target media is an asyncronous block device
330# such as MMC or USB disks
331# o Specify "quiet" to minimize boot time when using slow serial consoles
332
333# Look for a GRUB installation
334GRUB_CFG="$EFIDIR/grub.cfg"
335if [ -e "$GRUB_CFG" ]; then
336 info "Configuring GRUB"
337 # Delete the install entry
338 sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
339 # Delete the initrd lines
340 sed -i "/initrd /d" $GRUB_CFG
341 # Delete any LABEL= strings
342 sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
343
344 sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
345 sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet @" $GRUB_CFG
346fi
347
348# Look for a gummiboot installation
349GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries"
350GUMMI_CFG="$GUMMI_ENTRIES/boot.conf"
351if [ -d "$GUMMI_ENTRIES" ]; then
352 info "Configuring Gummiboot"
353 # remove the install target if it exists
354 rm $GUMMI_ENTRIES/install.conf >$OUT 2>&1
355
356 if [ ! -e "$GUMMI_CFG" ]; then
357 echo "ERROR: $GUMMI_CFG not found"
358 fi
359
360 sed -i "/initrd /d" $GUMMI_CFG
361 sed -i "s@ root=[^ ]*@ @" $GUMMI_CFG
362 sed -i "s@options *LABEL=boot @options LABEL=Boot root=$TARGET_ROOTFS ro rootwait quiet @" $GUMMI_CFG
363fi
364
365# Ensure we have at least one EFI bootloader configured
366if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then
367 die "No EFI bootloader configuration found"
368fi
369
370
371info "Copying ROOTFS files (this may take a while)"
372cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT >$OUT 2>&1 || die "Root FS copy failed"
373
374echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
375
376# We dont want udev to mount our root device while we're booting...
377if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
378 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
379fi
380
381
382# Call cleanup to unmount devices and images and remove the TMPDIR
383cleanup
384
385echo ""
386if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
387 echo "${YELLOW}Installation completed with warnings${CLEAR}"
388 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
389elif [ $ERRORS -ne 0 ]; then
390 echo "${RED}Installation encountered errors${CLEAR}"
391 echo "${RED}Errors: $ERRORS${CLEAR}"
392 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
393else
394 success "Installation completed successfully"
395fi
396echo ""