diff options
author | Darren Hart <dvhart@linux.intel.com> | 2012-07-30 16:18:44 -0700 |
---|---|---|
committer | Darren Hart <dvhart@linux.intel.com> | 2012-07-30 16:24:01 -0700 |
commit | c10b3557ee6c9d48be93791b1481fe7c14d990bb (patch) | |
tree | 1dab9c0ff7237682ce9f21d2b089c82dc4eaa96d | |
parent | 46b58ca46f9c9d29831a600a8ce3dcffaef2a654 (diff) | |
download | meta-intel-c10b3557ee6c9d48be93791b1481fe7c14d990bb.tar.gz |
fri2: Add mkefidisk.sh script
As the denzil release of the Yocto Project does not yet have a
functional EFI installer, provide a script (mkefidisk.sh) to
install a live image to a device which can then be booted on the target.
The script requires root privileges. See the usage statement for
details.
Signed-off-by: Darren Hart <dvhart@linux.intel.com>
-rwxr-xr-x | meta-fri2/scripts/mkefidisk.sh | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/meta-fri2/scripts/mkefidisk.sh b/meta-fri2/scripts/mkefidisk.sh new file mode 100755 index 00000000..233cf2ee --- /dev/null +++ b/meta-fri2/scripts/mkefidisk.sh | |||
@@ -0,0 +1,252 @@ | |||
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 | |||
21 | # | ||
22 | # Defaults | ||
23 | # | ||
24 | # 20 Mb for the boot partition | ||
25 | BOOT_SIZE=20 | ||
26 | # 5% for swap | ||
27 | SWAP_RATIO=5 | ||
28 | |||
29 | function usage() { | ||
30 | echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" | ||
31 | } | ||
32 | |||
33 | function image_details() { | ||
34 | IMG=$1 | ||
35 | echo "Image details" | ||
36 | echo "=============" | ||
37 | echo " image: $(stat --printf '%N\n' $IMG)" | ||
38 | echo " size: $(stat -L --printf '%s bytes\n' $IMG)" | ||
39 | echo " modified: $(stat -L --printf '%y\n' $IMG)" | ||
40 | echo " type: $(file -L -b $IMG)" | ||
41 | echo "" | ||
42 | } | ||
43 | |||
44 | function device_details() { | ||
45 | DEV=$1 | ||
46 | BLOCK_SIZE=512 | ||
47 | |||
48 | echo "Device details" | ||
49 | echo "==============" | ||
50 | echo " device: $DEVICE" | ||
51 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then | ||
52 | echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)" | ||
53 | else | ||
54 | echo " vendor: UNKOWN" | ||
55 | fi | ||
56 | if [ -f "/sys/class/block/$DEV/device/model" ]; then | ||
57 | echo " model: $(cat /sys/class/block/$DEV/device/model)" | ||
58 | else | ||
59 | echo " model: UNKNOWN" | ||
60 | fi | ||
61 | if [ -f "/sys/class/block/$DEV/size" ]; then | ||
62 | echo " size: $[$(cat /sys/class/block/$DEV/size)*BLOCK_SIZE] bytes" | ||
63 | else | ||
64 | echo " size: UNKNOWN" | ||
65 | fi | ||
66 | echo "" | ||
67 | } | ||
68 | |||
69 | |||
70 | # | ||
71 | # Parse and validate arguments | ||
72 | # | ||
73 | if [ $# -ne 3 ]; then | ||
74 | usage | ||
75 | exit 1 | ||
76 | fi | ||
77 | |||
78 | DEVICE=$1 | ||
79 | HDDIMG=$2 | ||
80 | TARGET_DEVICE=$3 | ||
81 | |||
82 | if [ ! -w "$DEVICE" ]; then | ||
83 | echo "ERROR: Device $DEVICE does not exist or is not writable" | ||
84 | usage | ||
85 | exit 1 | ||
86 | fi | ||
87 | |||
88 | if [ ! -e "$HDDIMG" ]; then | ||
89 | echo "ERROR: HDDIMG $HDDIMG does not exist" | ||
90 | usage | ||
91 | exit 1 | ||
92 | fi | ||
93 | |||
94 | |||
95 | # | ||
96 | # Check if any $DEVICE partitions are mounted | ||
97 | # | ||
98 | grep -q $DEVICE /proc/mounts | ||
99 | if [ $? -eq 0 ]; then | ||
100 | echo "ERROR: $DEVICE partitions mounted:" | ||
101 | grep $DEVICE /proc/mounts | cut -f 1 -d " " | ||
102 | echo "Unmount the partitions listed and try again." | ||
103 | exit 1 | ||
104 | fi | ||
105 | |||
106 | |||
107 | # | ||
108 | # Confirm device with user | ||
109 | # | ||
110 | image_details $HDDIMG | ||
111 | device_details $(basename $DEVICE) | ||
112 | echo -n "Prepare EFI image on $DEVICE [y/N]? " | ||
113 | read RESPONSE | ||
114 | if [ "$RESPONSE" != "y" ]; then | ||
115 | echo "Image creation aborted" | ||
116 | exit 0 | ||
117 | fi | ||
118 | |||
119 | |||
120 | # | ||
121 | # Partition $DEVICE | ||
122 | # | ||
123 | DEVICE_SIZE=$(parted $DEVICE unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//") | ||
124 | SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) | ||
125 | ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE)) | ||
126 | ROOTFS_START=$((BOOT_SIZE)) | ||
127 | ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE)) | ||
128 | SWAP_START=$((ROOTFS_END)) | ||
129 | |||
130 | # MMC devices are special in a couple of ways | ||
131 | # 1) they use a partition prefix character 'p' | ||
132 | # 2) they are detected asynchronously (need ROOTWAIT) | ||
133 | PART_PREFIX="" | ||
134 | if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ]; then | ||
135 | PART_PREFIX="p" | ||
136 | fi | ||
137 | BOOTFS=$DEVICE${PART_PREFIX}1 | ||
138 | ROOTFS=$DEVICE${PART_PREFIX}2 | ||
139 | SWAP=$DEVICE${PART_PREFIX}3 | ||
140 | |||
141 | ROOTWAIT="" | ||
142 | TARGET_PART_PREFIX="" | ||
143 | if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then | ||
144 | TARGET_PART_PREFIX="p" | ||
145 | ROOTWAIT="rootwait" | ||
146 | fi | ||
147 | TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2 | ||
148 | TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3 | ||
149 | |||
150 | echo "*****************" | ||
151 | echo "Boot partition size: $BOOT_SIZE MB ($BOOTFS)" | ||
152 | echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)" | ||
153 | echo "Swap partition size: $SWAP_SIZE MB ($SWAP)" | ||
154 | echo "*****************" | ||
155 | echo "Deleting partition table on $DEVICE ..." | ||
156 | dd if=/dev/zero of=$DEVICE bs=512 count=2 | ||
157 | |||
158 | echo "Creating new partition table (GPT) on $DEVICE ..." | ||
159 | parted $DEVICE mklabel gpt | ||
160 | |||
161 | echo "Creating boot partition on $BOOTFS" | ||
162 | parted $DEVICE mkpart primary 0% $BOOT_SIZE | ||
163 | |||
164 | echo "Creating ROOTFS partition on $ROOTFS" | ||
165 | parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END | ||
166 | |||
167 | echo "Creating swap partition on $SWAP" | ||
168 | parted $DEVICE mkpart primary $SWAP_START 100% | ||
169 | |||
170 | parted $DEVICE print | ||
171 | |||
172 | |||
173 | # | ||
174 | # Format $DEVICE partitions | ||
175 | # | ||
176 | echo "" | ||
177 | echo "Formatting $BOOTFS as vfat..." | ||
178 | mkfs.vfat $BOOTFS | ||
179 | |||
180 | echo "Formatting $ROOTFS as ext3..." | ||
181 | mkfs.ext3 $ROOTFS | ||
182 | |||
183 | echo "Formatting swap partition...($SWAP)" | ||
184 | mkswap $SWAP | ||
185 | |||
186 | |||
187 | # | ||
188 | # Installing to $DEVICE | ||
189 | # | ||
190 | echo "" | ||
191 | echo "Mounting images and device in preparation for installation..." | ||
192 | TMPDIR=$(mktemp -d mkefidisk-XXX) | ||
193 | if [ $? -ne 0 ]; then | ||
194 | echo "ERROR: Failed to create temporary mounting directory." | ||
195 | exit 1 | ||
196 | fi | ||
197 | HDDIMG_MNT=$TMPDIR/hddimg | ||
198 | HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs | ||
199 | ROOTFS_MNT=$TMPDIR/rootfs | ||
200 | BOOTFS_MNT=$TMPDIR/bootfs | ||
201 | mkdir $HDDIMG_MNT | ||
202 | mkdir $HDDIMG_ROOTFS_MNT | ||
203 | mkdir $ROOTFS_MNT | ||
204 | mkdir $BOOTFS_MNT | ||
205 | |||
206 | mount -o loop $HDDIMG $HDDIMG_MNT | ||
207 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT | ||
208 | mount $ROOTFS $ROOTFS_MNT | ||
209 | mount $BOOTFS $BOOTFS_MNT | ||
210 | |||
211 | echo "Copying ROOTFS files..." | ||
212 | cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT | ||
213 | |||
214 | echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab | ||
215 | |||
216 | # We dont want udev to mount our root device while we're booting... | ||
217 | if [ -d $ROOTFS_MNT/etc/udev/ ] ; then | ||
218 | echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist | ||
219 | fi | ||
220 | |||
221 | umount $ROOTFS_MNT | ||
222 | umount $HDDIMG_ROOTFS_MNT | ||
223 | |||
224 | echo "Preparing boot partition..." | ||
225 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" | ||
226 | mkdir -p $EFIDIR | ||
227 | GRUBCFG="$EFIDIR/grub.cfg" | ||
228 | |||
229 | cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT | ||
230 | # Copy the efi loader and config (booti*.efi and grub.cfg) | ||
231 | cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR | ||
232 | |||
233 | # Update grub config for the installed image | ||
234 | # Delete the install entry | ||
235 | sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG | ||
236 | # Delete the initrd lines | ||
237 | sed -i "/initrd /d" $GRUBCFG | ||
238 | # Delete any LABEL= strings | ||
239 | sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG | ||
240 | # Replace the ramdisk root with the install device and include other options | ||
241 | sed -i "s@ root=[^ ]*@ root=$TARGET_ROOTFS rw $ROOTWAIT quiet@" $GRUBCFG | ||
242 | |||
243 | # Provide a startup.nsh script for older firmware with non-standard boot | ||
244 | # directories and paths. | ||
245 | echo "bootia32.efi" > $BOOTFS_MNT/startup.nsh | ||
246 | |||
247 | umount $BOOTFS_MNT | ||
248 | umount $HDDIMG_MNT | ||
249 | rm -rf $TMPDIR | ||
250 | sync | ||
251 | |||
252 | echo "Installation complete." | ||