3 # Copyright (c) 2012, Intel Corporation.
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.
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.
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
20 # Modification from mkefidisk.sh provided by the Yocto project by Dominig
21 # to install Automotive Grade Linux (AGL) on Minnowboard and any PC with UEFI boot
25 # - keep initrd if present
26 # - create a grub config with PARTUID to ease boot from various devices automaticaly
27 # - add a UEFI startup.nsh script for autoboot
28 # - does not allocate swap
33 # Set to 1 to enable additional output
40 # 100 Mb for the boot partition
45 debug "Syncing and unmounting devices"
46 # Unmount anything we mounted
47 unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
48 unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
49 unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
50 unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT"
53 debug "Removing temporary files"
54 if [ -d "$TMPDIR" ]; then
55 rm -rf $TMPDIR || error "Failed to remove $TMPDIR"
59 trap 'die "Signal Received, Aborting..."' HUP INT TERM
66 RED="$(tput setaf 1)$(tput bold)"
67 GREEN="$(tput setaf 2)$(tput bold)"
68 YELLOW="$(tput setaf 3)$(tput bold)"
70 echo "${INFO}$1${CLEAR}"
74 echo "${RED}$1${CLEAR}"
77 WARNINGS=$((WARNINGS+1))
78 echo "${YELLOW}$1${CLEAR}"
81 echo "${GREEN}$1${CLEAR}"
89 if [ $DEBUG -eq 1 ]; then
95 echo "Install AGL on a removable device to boot on IA UEFI based computer"
96 echo "In particular is can create USB or SD bootable support for Minnowboard"
98 echo "Usage: $(basename $0) [-v] HDDIMG REMOVABLE_DEVICE"
99 echo " -v: Verbose debug"
100 echo " HDDIMG: The hddimg file to generate the efi disk from"
101 echo " REMOVABLE_DEVICE: The block device to write the image to, e.g. /dev/sdh"
103 echo " mkefi-agl.sh agl-demo-platform-intel-corei7-64.hddimg /dev/sdd"
110 echo " image: $(stat --printf '%N\n' $IMG)"
111 echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
112 echo " modified: $(stat -L --printf '%y\n' $IMG)"
113 echo " type: $(file -L -b $IMG)"
121 info "Device details"
122 echo " device: $DEVICE"
123 if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
124 echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
126 echo " vendor: UNKOWN"
128 if [ -f "/sys/class/block/$DEV/device/model" ]; then
129 echo " model: $(cat /sys/class/block/$DEV/device/model)"
131 echo " model: UNKNOWN"
133 if [ -f "/sys/class/block/$DEV/size" ]; then
134 echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
136 echo " size: UNKNOWN"
142 grep -q $DEVICE /proc/mounts
143 if [ $? -eq 0 ]; then
144 warn "$DEVICE listed in /proc/mounts, attempting to unmount"
145 umount $DEVICE* 2>/dev/null
146 ! grep -q $DEVICE /proc/mounts && info "Unmounted successfully"
153 if [ "$1" = "" ] ; then
156 grep -q $1 /proc/mounts
157 if [ $? -eq 0 ]; then
158 debug "Unmounting $1"
160 ! grep -q $1 /proc/mounts # check if unmounted successfully
167 # Parse and validate arguments
169 if [ "$1" != "-v" ] && [ $# -ne 2 ]; then
172 if [ "$1" = "-v" ] && [ $# -ne 3 ]; then
176 if [ "$1" = "-v" ] ; then
185 LINK=$(readlink $DEVICE)
186 if [ $? -eq 0 ]; then
190 if [ ! -w "$DEVICE" ]; then
191 if [ ! -e "${DEVICE}" ] ; then
192 die "Device $DEVICE cannot be found"
194 die "Device $DEVICE is not writable (need to run under sudo?)"
198 if [ ! -e "$HDDIMG" ]; then
199 die "HDDIMG $HDDIMG does not exist"
203 # Ensure the hddimg is not mounted
205 unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
208 # Check if any $DEVICE partitions are mounted
210 unmount_device || die "Failed to unmount $DEVICE"
213 # Confirm device with user
215 image_details $HDDIMG
216 device_details $(basename $DEVICE)
217 echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
219 if [ "$RESPONSE" != "y" ]; then
220 echo "Image creation aborted"
226 # Prepare the temporary working space
228 TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
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 || die "Failed to create $HDDIMG_MNT"
234 mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
235 mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
236 mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
242 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
243 # If the device size is not reported there may not be a valid label
244 if [ "$DEVICE_SIZE" = "" ] ; then
245 parted -s $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
246 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
248 ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE))
249 ROOTFS_START=$((BOOT_SIZE))
250 ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
252 # MMC devices use a partition prefix character 'p'
254 if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
257 BOOTFS=$DEVICE${PART_PREFIX}1
258 ROOTFS=$DEVICE${PART_PREFIX}2
260 TARGET_PART_PREFIX=""
261 if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
262 TARGET_PART_PREFIX="p"
264 TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
267 info "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
268 info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
271 # Use MSDOS by default as GPT cannot be reliably distributed in disk image form
272 # as it requires the backup table to be on the last block of the device, which
273 # of course varies from device to device.
275 info "Partitioning installation media ($DEVICE)"
277 debug "Deleting partition table on $DEVICE"
278 dd if=/dev/zero of=$DEVICE bs=512 count=2 1>&3 2>&1 || die "Failed to zero beginning of $DEVICE"
280 debug "Creating new partition table (MSDOS) on $DEVICE"
281 parted -s $DEVICE mklabel msdos 1>&3 2>&1 || die "Failed to create MSDOS partition table"
283 debug "Creating boot partition on $BOOTFS"
284 parted -s $DEVICE mkpart primary 0% $BOOT_SIZE 1>&3 2>&1 || die "Failed to create BOOT partition"
286 debug "Enabling boot flag on $BOOTFS"
287 parted -s $DEVICE set 1 boot on 1>&3 2>&1 || die "Failed to enable boot flag"
289 debug "Creating ROOTFS partition on $ROOTFS"
290 parted -s $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END 1>&3 2>&1 || die "Failed to create ROOTFS partition"
292 # as blkid does not provide PARTUUID on Ubuntu LTS 14.04 we myst hack via fdisk
293 #ROOTFS_PARTUUID=$(blkid |grep -e "$ROOTFS" |sed -n 's/^.*PARTUUID=/PARTUUID=/p')
295 ROOTFS_DISKID=$(fdisk -l "$DEVICE" | grep -e "Disk identifier" | sed -n 's/^.*Disk identifier: 0x/PARTUUID=/p')
296 if [ $ROOTFS_DISKID = "" ]; then
297 die "Failed to read DISKID"
299 ROOTFS_PARTUUID="$ROOTFS_DISKID-02"
300 debug "PARTUUID for ROOTFS is $ROOTFS_PARTUUID"
302 if [ $DEBUG -eq 1 ]; then
303 parted -s $DEVICE print
308 # Check if any $DEVICE partitions are mounted after partitioning
310 unmount_device || die "Failed to unmount $DEVICE partitions"
314 # Format $DEVICE partitions
316 info "Formatting partitions"
317 debug "Formatting $BOOTFS as vfat"
318 if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
319 mkfs.vfat -I $BOOTFS -n "EFI" 1>&3 2>&1 || die "Failed to format $BOOTFS"
321 mkfs.vfat $BOOTFS -n "EFI" 1>&3 2>&1 || die "Failed to format $BOOTFS"
324 debug "Formatting $ROOTFS as ext4"
325 mkfs.ext4 -F $ROOTFS -L "ROOT" 1>&3 2>&1 || die "Failed to format $ROOTFS"
329 # Installing to $DEVICE
331 debug "Mounting images and device in preparation for installation"
332 mount -o loop $HDDIMG $HDDIMG_MNT 1>&3 2>&1 || error "Failed to mount $HDDIMG"
333 mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT 1>&3 2>&1 || error "Failed to mount rootfs.img"
334 mount $ROOTFS $ROOTFS_MNT 1>&3 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
335 mount $BOOTFS $BOOTFS_MNT 1>&3 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
337 info "Preparing boot partition"
338 EFIDIR="$BOOTFS_MNT/EFI/BOOT"
339 cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT 1>&3 2>&1 || error "Failed to copy vmlinuz"
340 if [ -f $HDDIMG_MNT/initrd ]; then
341 cp $HDDIMG_MNT/initrd $BOOTFS_MNT 1>&3 2>&1 || error "Failed to copy initrd"
343 echo "bootx64.efi" > $BOOTFS_MNT/startup.nsh || error "Failed to create startup.nsh"
344 # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
345 cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT 1>&3 2>&1 || error "Failed to copy EFI dir"
346 # Silently ignore a missing systemd-boot or gummiboot loader dir (we might just be a GRUB image)
347 cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 1>&3 2>&1
349 # Update the boot loaders configurations for an installed image
350 # Remove any existing root= kernel parameters and:
351 # o Add a root= parameter with the target rootfs
352 # o Specify ro so fsck can be run during boot
353 # o Specify rootwait in case the target media is an asyncronous block device
354 # such as MMC or USB disks
355 # o Specify "quiet" to minimize boot time when using slow serial consoles
357 # Look for a GRUB installation
358 GRUB_CFG="$EFIDIR/grub.cfg"
359 if [ -e "$GRUB_CFG" ]; then
360 info "Configuring GRUB"
361 # Delete the install entry
362 sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
363 # Delete any LABEL= strings
364 sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
366 sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
367 sed -i "s@vmlinuz @vmlinuz root=$ROOTFS_PARTUUID @" $GRUB_CFG
370 # look for a systemd-boot loader.conf file and create a default boot entry
371 SYSTEMDBOOT_CFG="$BOOTFS_MNT/loader/loader.conf"
372 if [ -e "$SYSTEMDBOOT_CFG" ]; then
373 info "Configuring SYSTEMD-BOOT"
374 SYSTEMDBOOT_BOOT="$BOOTFS_MNT/loader/entries/boot.conf"
375 SYSTEMDBOOT_DEBUG="$BOOTFS_MNT/loader/entries/debug.conf"
376 # Delete the install entry
377 sed -i "/menuentry 'install'/,/^}/d" $SYSTEMDBOOT_CFG
378 rm -rf "$BOOTFS_MNT/loader/entries/install.conf" 1>&3 2>&1
379 # Add PARTUUID to the boot entry file
380 if [ ! -e "$SYSTEMDBOOT_BOOT" ]; then
381 die "no boot.conf entry found in systemd-boot directories"
383 # Delete any LABEL= strings
384 sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_BOOT
386 sed -i "s@ root=[^ ]*@ @" $SYSTEMDBOOT_BOOT
387 sed -i "s@options @options root=$ROOTFS_PARTUUID @" $SYSTEMDBOOT_BOOT
391 # Ensure we have at least one EFI bootloader configured
392 if [ ! -e $GRUB_CFG ] && [ ! -e $SYSTEMDBOOT_CFG ] ; then
393 die "No EFI bootloader configuration found"
396 info "Copying ROOTFS files (this may take a while)"
397 command -v rsync >/dev/null 2>&1 # check if rsync exists
398 if [ $DEBUG -eq 1 ] && [ $? -eq 0 ]; then
399 rsync --info=progress2 -h -aHAXW --no-compress $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT 1>&3 2>&1 || die "Root FS copy failed"
401 cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT 1>&3 2>&1 || die "Root FS copy failed"
404 # We dont want udev to mount our root device while we're booting...
405 if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
406 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
410 # Call cleanup to unmount devices and images and remove the TMPDIR
414 if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
415 echo "${YELLOW}Installation completed with warnings${CLEAR}"
416 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
417 elif [ $ERRORS -ne 0 ]; then
418 echo "${RED}Installation encountered errors${CLEAR}"
419 echo "${RED}Errors: $ERRORS${CLEAR}"
420 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
422 success "Installation completed successfully"