summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/mkefidisk.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/contrib/mkefidisk.sh')
-rwxr-xr-xscripts/contrib/mkefidisk.sh286
1 files changed, 286 insertions, 0 deletions
diff --git a/scripts/contrib/mkefidisk.sh b/scripts/contrib/mkefidisk.sh
new file mode 100755
index 0000000000..c86849d395
--- /dev/null
+++ b/scripts/contrib/mkefidisk.sh
@@ -0,0 +1,286 @@
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#
24# Defaults
25#
26# 20 Mb for the boot partition
27BOOT_SIZE=20
28# 5% for swap
29SWAP_RATIO=5
30
31usage() {
32 echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
33 echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
34 echo " HDDIMG: The hddimg file to generate the efi disk from"
35 echo " TARGET_DEVICE: The device the target will boot from, e.g. /dev/mmcblk0"
36}
37
38image_details() {
39 IMG=$1
40 echo "Image details"
41 echo "============="
42 echo " image: $(stat --printf '%N\n' $IMG)"
43 echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
44 echo " modified: $(stat -L --printf '%y\n' $IMG)"
45 echo " type: $(file -L -b $IMG)"
46 echo ""
47}
48
49device_details() {
50 DEV=$1
51 BLOCK_SIZE=512
52
53 echo "Device details"
54 echo "=============="
55 echo " device: $DEVICE"
56 if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
57 echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
58 else
59 echo " vendor: UNKOWN"
60 fi
61 if [ -f "/sys/class/block/$DEV/device/model" ]; then
62 echo " model: $(cat /sys/class/block/$DEV/device/model)"
63 else
64 echo " model: UNKNOWN"
65 fi
66 if [ -f "/sys/class/block/$DEV/size" ]; then
67 echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
68 else
69 echo " size: UNKNOWN"
70 fi
71 echo ""
72}
73
74unmount_device() {
75 grep -q $DEVICE /proc/mounts
76 if [ $? -eq 0 ]; then
77 echo -n "$DEVICE listed in /proc/mounts, attempting to unmount..."
78 umount $DEVICE* 2>/dev/null
79 grep -q $DEVICE /proc/mounts
80 if [ $? -eq 0 ]; then
81 echo "FAILED"
82 exit 1
83 fi
84 echo "OK"
85 fi
86}
87
88
89#
90# Parse and validate arguments
91#
92if [ $# -ne 3 ]; then
93 usage
94 exit 1
95fi
96
97DEVICE=$1
98HDDIMG=$2
99TARGET_DEVICE=$3
100
101if [ ! -w "$DEVICE" ]; then
102 echo "ERROR: Device $DEVICE does not exist or is not writable"
103 usage
104 exit 1
105fi
106
107if [ ! -e "$HDDIMG" ]; then
108 echo "ERROR: HDDIMG $HDDIMG does not exist"
109 usage
110 exit 1
111fi
112
113
114#
115# Check if any $DEVICE partitions are mounted
116#
117unmount_device
118
119
120#
121# Confirm device with user
122#
123image_details $HDDIMG
124device_details $(basename $DEVICE)
125echo -n "Prepare EFI image on $DEVICE [y/N]? "
126read RESPONSE
127if [ "$RESPONSE" != "y" ]; then
128 echo "Image creation aborted"
129 exit 0
130fi
131
132
133#
134# Partition $DEVICE
135#
136DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
137# If the device size is not reported there may not be a valid label
138if [ "$DEVICE_SIZE" = "" ] ; then
139 parted $DEVICE mklabel msdos
140 DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
141fi
142SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
143ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
144ROOTFS_START=$((BOOT_SIZE))
145ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
146SWAP_START=$((ROOTFS_END))
147
148# MMC devices use a partition prefix character 'p'
149PART_PREFIX=""
150if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
151 PART_PREFIX="p"
152fi
153BOOTFS=$DEVICE${PART_PREFIX}1
154ROOTFS=$DEVICE${PART_PREFIX}2
155SWAP=$DEVICE${PART_PREFIX}3
156
157TARGET_PART_PREFIX=""
158if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
159 TARGET_PART_PREFIX="p"
160fi
161TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
162TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
163
164echo "*****************"
165echo "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
166echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
167echo "Swap partition size: $SWAP_SIZE MB ($SWAP)"
168echo "*****************"
169
170echo "Deleting partition table on $DEVICE ..."
171dd if=/dev/zero of=$DEVICE bs=512 count=2
172
173# Use MSDOS by default as GPT cannot be reliably distributed in disk image form
174# as it requires the backup table to be on the last block of the device, which
175# of course varies from device to device.
176echo "Creating new partition table (MSDOS) on $DEVICE ..."
177parted $DEVICE mklabel msdos
178
179echo "Creating boot partition on $BOOTFS"
180parted $DEVICE mkpart primary 0% $BOOT_SIZE
181
182echo "Enabling boot flag on $BOOTFS"
183parted $DEVICE set 1 boot on
184
185echo "Creating ROOTFS partition on $ROOTFS"
186parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END
187
188echo "Creating swap partition on $SWAP"
189parted $DEVICE mkpart primary $SWAP_START 100%
190
191parted $DEVICE print
192
193
194#
195# Check if any $DEVICE partitions are mounted after partitioning
196#
197unmount_device
198
199
200#
201# Format $DEVICE partitions
202#
203echo ""
204echo "Formatting $BOOTFS as vfat..."
205if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
206 mkfs.vfat -I $BOOTFS -n "efi"
207else
208 mkfs.vfat $BOOTFS -n "efi"
209
210fi
211
212echo "Formatting $ROOTFS as ext3..."
213mkfs.ext3 $ROOTFS -L "root"
214
215echo "Formatting swap partition...($SWAP)"
216mkswap $SWAP
217
218
219#
220# Installing to $DEVICE
221#
222echo ""
223echo "Mounting images and device in preparation for installation..."
224TMPDIR=$(mktemp -d mkefidisk-XXX)
225if [ $? -ne 0 ]; then
226 echo "ERROR: Failed to create temporary mounting directory."
227 exit 1
228fi
229HDDIMG_MNT=$TMPDIR/hddimg
230HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
231ROOTFS_MNT=$TMPDIR/rootfs
232BOOTFS_MNT=$TMPDIR/bootfs
233mkdir $HDDIMG_MNT
234mkdir $HDDIMG_ROOTFS_MNT
235mkdir $ROOTFS_MNT
236mkdir $BOOTFS_MNT
237
238mount -o loop $HDDIMG $HDDIMG_MNT
239mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT
240mount $ROOTFS $ROOTFS_MNT
241mount $BOOTFS $BOOTFS_MNT
242
243echo "Copying ROOTFS files..."
244cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT
245
246echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
247
248# We dont want udev to mount our root device while we're booting...
249if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
250 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
251fi
252
253umount $ROOTFS_MNT
254umount $HDDIMG_ROOTFS_MNT
255
256echo "Preparing boot partition..."
257EFIDIR="$BOOTFS_MNT/EFI/BOOT"
258mkdir -p $EFIDIR
259GRUBCFG="$EFIDIR/grub.cfg"
260
261cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT
262# Copy the efi loader and config (booti*.efi and grub.cfg)
263cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR
264
265# Update grub config for the installed image
266# Delete the install entry
267sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
268# Delete the initrd lines
269sed -i "/initrd /d" $GRUBCFG
270# Delete any LABEL= strings
271sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
272# Remove any existing root= kernel parameters and:
273# o Add a root= parameter with the target rootfs
274# o Specify ro so fsck can be run during boot
275# o Specify rootwait in case the target media is an asyncronous block device
276# such as MMC or USB disks
277# o Specify "quiet" to minimize boot time when using slow serial consoles
278sed -i "s@ root=[^ ]*@ @" $GRUBCFG
279sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet @" $GRUBCFG
280
281umount $BOOTFS_MNT
282umount $HDDIMG_MNT
283rm -rf $TMPDIR
284sync
285
286echo "Installation complete."