1#!/bin/sh -e 2# 3# Copyright (c) 2012, Intel Corporation. 4# All rights reserved. 5# 6# install.sh [device_name] [rootfs_name] 7# 8 9PATH=/sbin:/bin:/usr/sbin:/usr/bin 10 11# We need 200 Mb for the boot partition 12boot_size=200 13 14# 50% for the second rootfs 15testfs_ratio=50 16 17found="no" 18 19echo "Searching for a hard drive..." 20for device in 'hda' 'hdb' 'sda' 'sdb' 'mmcblk0' 'mmcblk1' 21do 22 if [ -e /sys/block/${device}/removable ]; then 23 if [ "$(cat /sys/block/${device}/removable)" = "0" ]; then 24 found="yes" 25 26 while true; do 27 # Try sleeping here to avoid getting kernel messages 28 # obscuring/confusing user 29 sleep 5 30 echo "Found drive at /dev/${device}. Do you want to install this image there? [y/n]" 31 read answer 32 if [ "$answer" = "y" ] ; then 33 break 34 fi 35 36 if [ "$answer" = "n" ] ; then 37 found=no 38 break 39 fi 40 41 echo "Please answer y or n" 42 done 43 fi 44 fi 45 46 if [ "$found" = "yes" ]; then 47 break; 48 fi 49 50done 51 52if [ "$found" = "no" ]; then 53 exit 1 54fi 55 56echo "Installing image on /dev/${device}" 57 58# 59# The udev automounter can cause pain here, kill it 60# 61rm -f /etc/udev/rules.d/automount.rules 62rm -f /etc/udev/scripts/mount* 63 64# 65# Unmount anything the automounter had mounted 66# 67umount /dev/${device}* 2> /dev/null || /bin/true 68 69mkdir -p /tmp 70cat /proc/mounts > /etc/mtab 71 72disk_size=$(parted /dev/${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//") 73 74testfs_size=$((disk_size*testfs_ratio/100)) 75rootfs_size=$((disk_size-boot_size-testfs_size)) 76 77rootfs_start=$((boot_size)) 78rootfs_end=$((rootfs_start+rootfs_size)) 79testfs_start=$((rootfs_end)) 80 81# MMC devices are special in a couple of ways 82# 1) they use a partition prefix character 'p' 83# 2) they are detected asynchronously (need rootwait) 84rootwait="" 85part_prefix="" 86if [ ! "${device#mmcblk}" = "${device}" ]; then 87 part_prefix="p" 88 rootwait="rootwait" 89fi 90bootfs=/dev/${device}${part_prefix}1 91rootfs=/dev/${device}${part_prefix}2 92testfs=/dev/${device}${part_prefix}3 93 94echo "*****************" 95echo "Boot partition size: $boot_size MB ($bootfs)" 96echo "Rootfs partition size: $rootfs_size MB ($rootfs)" 97echo "Testfs partition size: $testfs_size MB ($testfs)" 98echo "*****************" 99echo "Deleting partition table on /dev/${device} ..." 100dd if=/dev/zero of=/dev/${device} bs=512 count=2 101 102echo "Creating new partition table on /dev/${device} ..." 103parted /dev/${device} mklabel gpt 104 105echo "Creating boot partition on $bootfs" 106parted /dev/${device} mkpart primary 0% $boot_size 107parted /dev/${device} set 1 boot on 108 109echo "Creating rootfs partition on $rootfs" 110parted /dev/${device} mkpart primary $rootfs_start $rootfs_end 111 112echo "Creating testfs partition on $testfs" 113parted /dev/${device} mkpart primary $testfs_start 100% 114 115parted /dev/${device} print 116 117echo "Formatting $bootfs to vfat..." 118mkfs.vfat -n "boot" $bootfs 119 120echo "Formatting $rootfs to ext3..." 121mkfs.ext3 -L "platform" $rootfs 122 123echo "Formatting $testfs to ext3..." 124mkfs.ext3 -L "testrootfs" $testfs 125 126mkdir /ssd 127mkdir /rootmnt 128mkdir /bootmnt 129 130mount $rootfs /ssd 131mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /rootmnt 132 133echo "Copying rootfs files..." 134cp -a /rootmnt/* /ssd 135 136touch /ssd/etc/controllerimage 137 138if [ -d /ssd/etc/ ] ; then 139 # We dont want udev to mount our root device while we're booting... 140 if [ -d /ssd/etc/udev/ ] ; then 141 echo "/dev/${device}" >> /ssd/etc/udev/mount.ignorelist 142 fi 143fi 144 145umount /ssd 146umount /rootmnt 147 148echo "Preparing boot partition..." 149mount $bootfs /ssd 150 151EFIDIR="/ssd/EFI/BOOT" 152mkdir -p $EFIDIR 153cp /run/media/$1/vmlinuz /ssd 154# Copy the efi loader 155cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR 156 157if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then 158 GRUBCFG="$EFIDIR/grub.cfg" 159 cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG 160 # Update grub config for the installed image 161 # Delete the install entry 162 sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG 163 # Delete the initrd lines 164 sed -i "/initrd /d" $GRUBCFG 165 # Delete any LABEL= strings 166 sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG 167 # Delete any root= strings 168 sed -i "s/ root=[^ ]*/ /" $GRUBCFG 169 # Add the root= and other standard boot options 170 sed -i "s@linux /vmlinuz *@linux /vmlinuz root=$rootfs rw $rootwait quiet @" $GRUBCFG 171fi 172 173if [ -d /run/media/$1/loader ]; then 174 SYSTEMDBOOT_CFGS="/ssd/loader/entries/*.conf" 175 # copy config files for systemd-boot 176 cp -dr /run/media/$1/loader /ssd 177 # delete the install entry 178 rm -f /ssd/loader/entries/install.conf 179 # delete the initrd lines 180 sed -i "/initrd /d" $SYSTEMDBOOT_CFGS 181 # delete any LABEL= strings 182 sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS 183 # delete any root= strings 184 sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS 185 # add the root= and other standard boot options 186 sed -i "s@options *@options root=$rootfs rw $rootwait quiet @" $SYSTEMDBOOT_CFGS 187 # Add the test label 188 echo -ne "title test\nlinux /test-kernel\noptions root=$testfs rw $rootwait quiet\n" > /ssd/loader/entries/test.conf 189fi 190 191umount /ssd 192sync 193 194echo "Remove your installation media, and press ENTER" 195 196read enter 197 198echo "Rebooting..." 199reboot -f 200