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 # 20 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
152 if [ "$1" = "" ] ; then
155 grep -q $1 /proc/mounts
156 if [ $? -eq 0 ]; then
157 debug "Unmounting $1"
165 # Parse and validate arguments
167 if [ "$1" != "-v" ] && [ $# -ne 2 ]; then
170 if [ "$1" == "-v" ] && [ $# -ne 3 ]; then
174 if [ "$1" = "-v" ] ; then
183 LINK=$(readlink $DEVICE)
184 if [ $? -eq 0 ]; then
188 if [ ! -w "$DEVICE" ]; then
189 if [ ! -e "${DEVICE}" ] ; then
190 die "Device $DEVICE cannot be found"
192 die "Device $DEVICE is not writable (need to run under sudo?)"
196 if [ ! -e "$HDDIMG" ]; then
197 die "HDDIMG $HDDIMG does not exist"
201 # Ensure the hddimg is not mounted
203 unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
206 # Check if any $DEVICE partitions are mounted
208 unmount_device || die "Failed to unmount $DEVICE"
211 # Confirm device with user
213 image_details $HDDIMG
214 device_details $(basename $DEVICE)
215 echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
217 if [ "$RESPONSE" != "y" ]; then
218 echo "Image creation aborted"
224 # Prepare the temporary working space
226 TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
227 HDDIMG_MNT=$TMPDIR/hddimg
228 HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
229 ROOTFS_MNT=$TMPDIR/rootfs
230 BOOTFS_MNT=$TMPDIR/bootfs
231 mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
232 mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
233 mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
234 mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
240 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
241 # If the device size is not reported there may not be a valid label
242 if [ "$DEVICE_SIZE" = "" ] ; then
243 parted -s $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
244 DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
246 ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE))
247 ROOTFS_START=$((BOOT_SIZE))
248 ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
250 # MMC devices use a partition prefix character 'p'
252 if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
255 BOOTFS=$DEVICE${PART_PREFIX}1
256 ROOTFS=$DEVICE${PART_PREFIX}2
258 TARGET_PART_PREFIX=""
259 if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
260 TARGET_PART_PREFIX="p"
262 TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
265 info "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
266 info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
269 # Use MSDOS by default as GPT cannot be reliably distributed in disk image form
270 # as it requires the backup table to be on the last block of the device, which
271 # of course varies from device to device.
273 info "Partitioning installation media ($DEVICE)"
275 debug "Deleting partition table on $DEVICE"
276 dd if=/dev/zero of=$DEVICE bs=512 count=2 >$OUT 2>&1 || die "Failed to zero beginning of $DEVICE"
278 debug "Creating new partition table (MSDOS) on $DEVICE"
279 parted -s $DEVICE mklabel msdos >$OUT 2>&1 || die "Failed to create MSDOS partition table"
281 debug "Creating boot partition on $BOOTFS"
282 parted -s $DEVICE mkpart primary 0% $BOOT_SIZE >$OUT 2>&1 || die "Failed to create BOOT partition"
284 debug "Enabling boot flag on $BOOTFS"
285 parted -s $DEVICE set 1 boot on >$OUT 2>&1 || die "Failed to enable boot flag"
287 debug "Creating ROOTFS partition on $ROOTFS"
288 parted -s $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END >$OUT 2>&1 || die "Failed to create ROOTFS partition"
290 # as blkid does not provide PARTUUID on Ubuntu LTS 14.04 we myst hack via fdisk
291 #ROOTFS_PARTUUID=$(blkid |grep -e "$ROOTFS" |sed -n 's/^.*PARTUUID=/PARTUUID=/p')
293 ROOTFS_DISKID=$(fdisk -l "$DEVICE" | grep -e "Disk identifier" | sed -n 's/^.*Disk identifier: 0x/PARTUUID=/p')
294 if [ $ROOTFS_DISKID == "" ]; then
295 die "Failed to read DISKID"
297 ROOTFS_PARTUUID="$ROOTFS_DISKID-02"
298 debug "PARTUUID for ROOTFS in grub.conf is $ROOTFS_PARTUUID"
300 if [ $DEBUG -eq 1 ]; then
301 parted -s $DEVICE print
306 # Check if any $DEVICE partitions are mounted after partitioning
308 unmount_device || die "Failed to unmount $DEVICE partitions"
312 # Format $DEVICE partitions
314 info "Formatting partitions"
315 debug "Formatting $BOOTFS as vfat"
316 if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
317 mkfs.vfat -I $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
319 mkfs.vfat $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
322 debug "Formatting $ROOTFS as ext4"
323 mkfs.ext4 -F $ROOTFS -L "ROOT" >$OUT 2>&1 || die "Failed to format $ROOTFS"
327 # Installing to $DEVICE
329 debug "Mounting images and device in preparation for installation"
330 mount -o loop $HDDIMG $HDDIMG_MNT >$OUT 2>&1 || error "Failed to mount $HDDIMG"
331 mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount rootfs.img"
332 mount $ROOTFS $ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
333 mount $BOOTFS $BOOTFS_MNT >$OUT 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
335 info "Preparing boot partition"
336 EFIDIR="$BOOTFS_MNT/EFI/BOOT"
337 cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy vmlinuz"
338 if [ -f $HDDIMG_MNT/initrd ]; then
339 cp $HDDIMG_MNT/initrd $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy initrd"
341 echo "bootx64.efi" > $BOOTFS_MNT/startup.nsh || error "Failed to create startup.nsh"
342 # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
343 cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy EFI dir"
344 # Silently ignore a missing systemd-boot or gummiboot loader dir (we might just be a GRUB image)
345 cp -r $HDDIMG_MNT/loader $BOOTFS_MNT >$OUT 2>&1
347 # Update the boot loaders configurations for an installed image
348 # Remove any existing root= kernel parameters and:
349 # o Add a root= parameter with the target rootfs
350 # o Specify ro so fsck can be run during boot
351 # o Specify rootwait in case the target media is an asyncronous block device
352 # such as MMC or USB disks
353 # o Specify "quiet" to minimize boot time when using slow serial consoles
355 # Look for a GRUB installation
356 GRUB_CFG="$EFIDIR/grub.cfg"
357 if [ -e "$GRUB_CFG" ]; then
358 info "Configuring GRUB"
359 # Delete the install entry
360 sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
361 # Delete any LABEL= strings
362 sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
364 sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
365 sed -i "s@vmlinuz @vmlinuz root=$ROOTFS_PARTUUID ro rootwait quiet @" $GRUB_CFG
368 # look for a systemd-boot loader.conf file and create a default boot entry
369 SYSTEMDBOOT_CFG="$BOOTFS_MNT/loader/loader.conf"
370 SYSTEMDBOOT_BOOT="$BOOTFS_MNT/loader/entries/boot.conf"
371 SYSTEMDBOOT_DEBUG="$BOOTFS_MNT/loader/entries/debug.conf"
372 if [ -e "$SYSTEMDBOOT_CFG" ]; then
373 info "Configuring SYSTEMD-BOOT"
374 # Delete any existing entries
375 rm -rf "$BOOTFS_MNT/loader/entries" >$OUT 2>&1
376 mkdir "$BOOTFS_MNT/loader/entries" >$OUT 2>&1
377 # create the new loader.conf file
378 echo "# Created by mkefi-agl.sh script `date`" > $SYSTEMDBOOT_CFG
379 echo "default boot" >> $SYSTEMDBOOT_CFG
380 echo "timout 5" >> $SYSTEMDBOOT_CFG
381 # create the boot entry
382 echo "title boot" > $SYSTEMDBOOT_BOOT
383 echo "linux /vmlinuz" >> $SYSTEMDBOOT_BOOT
384 echo "initrd /initrd" >> $SYSTEMDBOOT_BOOT
385 echo "options LABEL=boot root=$ROOTFS_PARTUUID ro quiet rootwait console=ttyS0,115200 console=tty0" >> $SYSTEMDBOOT_BOOT
386 # create the debug entry
387 echo "title debug" > $SYSTEMDBOOT_DEBUG
388 echo "linux /vmlinuz" >> $SYSTEMDBOOT_DEBUG
389 echo "initrd /initrd" >> $SYSTEMDBOOT_DEBUG
390 echo "options LABEL=debug root=$ROOTFS_PARTUUID ro debug rootwait console=ttyS0,115200 console=tty0" >> $SYSTEMDBOOT_DEBUG
395 # Ensure we have at least one EFI bootloader configured
396 if [ ! -e $GRUB_CFG ] && [ ! -e $SYSTEMDBOOT_CFG ] ; then
397 die "No EFI bootloader configuration found"
400 info "Copying ROOTFS files (this may take a while)"
401 cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT >$OUT 2>&1 || die "Root FS copy failed"
403 # We dont want udev to mount our root device while we're booting...
404 if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
405 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
409 # Call cleanup to unmount devices and images and remove the TMPDIR
413 if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
414 echo "${YELLOW}Installation completed with warnings${CLEAR}"
415 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
416 elif [ $ERRORS -ne 0 ]; then
417 echo "${RED}Installation encountered errors${CLEAR}"
418 echo "${RED}Errors: $ERRORS${CLEAR}"
419 echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
421 success "Installation completed successfully"