diff options
Diffstat (limited to 'scripts/contrib/mkefidisk.sh')
-rwxr-xr-x | scripts/contrib/mkefidisk.sh | 286 |
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 | |||
21 | LANG=C | ||
22 | |||
23 | # | ||
24 | # Defaults | ||
25 | # | ||
26 | # 20 Mb for the boot partition | ||
27 | BOOT_SIZE=20 | ||
28 | # 5% for swap | ||
29 | SWAP_RATIO=5 | ||
30 | |||
31 | usage() { | ||
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 | |||
38 | image_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 | |||
49 | device_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 | |||
74 | unmount_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 | # | ||
92 | if [ $# -ne 3 ]; then | ||
93 | usage | ||
94 | exit 1 | ||
95 | fi | ||
96 | |||
97 | DEVICE=$1 | ||
98 | HDDIMG=$2 | ||
99 | TARGET_DEVICE=$3 | ||
100 | |||
101 | if [ ! -w "$DEVICE" ]; then | ||
102 | echo "ERROR: Device $DEVICE does not exist or is not writable" | ||
103 | usage | ||
104 | exit 1 | ||
105 | fi | ||
106 | |||
107 | if [ ! -e "$HDDIMG" ]; then | ||
108 | echo "ERROR: HDDIMG $HDDIMG does not exist" | ||
109 | usage | ||
110 | exit 1 | ||
111 | fi | ||
112 | |||
113 | |||
114 | # | ||
115 | # Check if any $DEVICE partitions are mounted | ||
116 | # | ||
117 | unmount_device | ||
118 | |||
119 | |||
120 | # | ||
121 | # Confirm device with user | ||
122 | # | ||
123 | image_details $HDDIMG | ||
124 | device_details $(basename $DEVICE) | ||
125 | echo -n "Prepare EFI image on $DEVICE [y/N]? " | ||
126 | read RESPONSE | ||
127 | if [ "$RESPONSE" != "y" ]; then | ||
128 | echo "Image creation aborted" | ||
129 | exit 0 | ||
130 | fi | ||
131 | |||
132 | |||
133 | # | ||
134 | # Partition $DEVICE | ||
135 | # | ||
136 | DEVICE_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 | ||
138 | if [ "$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//") | ||
141 | fi | ||
142 | SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) | ||
143 | ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE)) | ||
144 | ROOTFS_START=$((BOOT_SIZE)) | ||
145 | ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE)) | ||
146 | SWAP_START=$((ROOTFS_END)) | ||
147 | |||
148 | # MMC devices use a partition prefix character 'p' | ||
149 | PART_PREFIX="" | ||
150 | if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then | ||
151 | PART_PREFIX="p" | ||
152 | fi | ||
153 | BOOTFS=$DEVICE${PART_PREFIX}1 | ||
154 | ROOTFS=$DEVICE${PART_PREFIX}2 | ||
155 | SWAP=$DEVICE${PART_PREFIX}3 | ||
156 | |||
157 | TARGET_PART_PREFIX="" | ||
158 | if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then | ||
159 | TARGET_PART_PREFIX="p" | ||
160 | fi | ||
161 | TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2 | ||
162 | TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3 | ||
163 | |||
164 | echo "*****************" | ||
165 | echo "Boot partition size: $BOOT_SIZE MB ($BOOTFS)" | ||
166 | echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)" | ||
167 | echo "Swap partition size: $SWAP_SIZE MB ($SWAP)" | ||
168 | echo "*****************" | ||
169 | |||
170 | echo "Deleting partition table on $DEVICE ..." | ||
171 | dd 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. | ||
176 | echo "Creating new partition table (MSDOS) on $DEVICE ..." | ||
177 | parted $DEVICE mklabel msdos | ||
178 | |||
179 | echo "Creating boot partition on $BOOTFS" | ||
180 | parted $DEVICE mkpart primary 0% $BOOT_SIZE | ||
181 | |||
182 | echo "Enabling boot flag on $BOOTFS" | ||
183 | parted $DEVICE set 1 boot on | ||
184 | |||
185 | echo "Creating ROOTFS partition on $ROOTFS" | ||
186 | parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END | ||
187 | |||
188 | echo "Creating swap partition on $SWAP" | ||
189 | parted $DEVICE mkpart primary $SWAP_START 100% | ||
190 | |||
191 | parted $DEVICE print | ||
192 | |||
193 | |||
194 | # | ||
195 | # Check if any $DEVICE partitions are mounted after partitioning | ||
196 | # | ||
197 | unmount_device | ||
198 | |||
199 | |||
200 | # | ||
201 | # Format $DEVICE partitions | ||
202 | # | ||
203 | echo "" | ||
204 | echo "Formatting $BOOTFS as vfat..." | ||
205 | if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then | ||
206 | mkfs.vfat -I $BOOTFS -n "efi" | ||
207 | else | ||
208 | mkfs.vfat $BOOTFS -n "efi" | ||
209 | |||
210 | fi | ||
211 | |||
212 | echo "Formatting $ROOTFS as ext3..." | ||
213 | mkfs.ext3 $ROOTFS -L "root" | ||
214 | |||
215 | echo "Formatting swap partition...($SWAP)" | ||
216 | mkswap $SWAP | ||
217 | |||
218 | |||
219 | # | ||
220 | # Installing to $DEVICE | ||
221 | # | ||
222 | echo "" | ||
223 | echo "Mounting images and device in preparation for installation..." | ||
224 | TMPDIR=$(mktemp -d mkefidisk-XXX) | ||
225 | if [ $? -ne 0 ]; then | ||
226 | echo "ERROR: Failed to create temporary mounting directory." | ||
227 | exit 1 | ||
228 | fi | ||
229 | HDDIMG_MNT=$TMPDIR/hddimg | ||
230 | HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs | ||
231 | ROOTFS_MNT=$TMPDIR/rootfs | ||
232 | BOOTFS_MNT=$TMPDIR/bootfs | ||
233 | mkdir $HDDIMG_MNT | ||
234 | mkdir $HDDIMG_ROOTFS_MNT | ||
235 | mkdir $ROOTFS_MNT | ||
236 | mkdir $BOOTFS_MNT | ||
237 | |||
238 | mount -o loop $HDDIMG $HDDIMG_MNT | ||
239 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT | ||
240 | mount $ROOTFS $ROOTFS_MNT | ||
241 | mount $BOOTFS $BOOTFS_MNT | ||
242 | |||
243 | echo "Copying ROOTFS files..." | ||
244 | cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT | ||
245 | |||
246 | echo "$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... | ||
249 | if [ -d $ROOTFS_MNT/etc/udev/ ] ; then | ||
250 | echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist | ||
251 | fi | ||
252 | |||
253 | umount $ROOTFS_MNT | ||
254 | umount $HDDIMG_ROOTFS_MNT | ||
255 | |||
256 | echo "Preparing boot partition..." | ||
257 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" | ||
258 | mkdir -p $EFIDIR | ||
259 | GRUBCFG="$EFIDIR/grub.cfg" | ||
260 | |||
261 | cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT | ||
262 | # Copy the efi loader and config (booti*.efi and grub.cfg) | ||
263 | cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR | ||
264 | |||
265 | # Update grub config for the installed image | ||
266 | # Delete the install entry | ||
267 | sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG | ||
268 | # Delete the initrd lines | ||
269 | sed -i "/initrd /d" $GRUBCFG | ||
270 | # Delete any LABEL= strings | ||
271 | sed -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 | ||
278 | sed -i "s@ root=[^ ]*@ @" $GRUBCFG | ||
279 | sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet @" $GRUBCFG | ||
280 | |||
281 | umount $BOOTFS_MNT | ||
282 | umount $HDDIMG_MNT | ||
283 | rm -rf $TMPDIR | ||
284 | sync | ||
285 | |||
286 | echo "Installation complete." | ||