Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 9 | PATH=/sbin:/bin:/usr/sbin:/usr/bin |
| 10 | |
| 11 | # We need 20 Mb for the boot partition |
| 12 | boot_size=20 |
| 13 | |
| 14 | # 5% for swap |
| 15 | swap_ratio=5 |
| 16 | |
| 17 | # Get a list of hard drives |
| 18 | hdnamelist="" |
| 19 | live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'` |
| 20 | live_dev_name=${live_dev_name#\/dev/} |
| 21 | # Only strip the digit identifier if the device is not an mmc |
| 22 | case $live_dev_name in |
| 23 | mmcblk*) |
| 24 | ;; |
| 25 | *) |
| 26 | live_dev_name=${live_dev_name%%[0-9]*} |
| 27 | ;; |
| 28 | esac |
| 29 | |
| 30 | echo "Searching for hard drives ..." |
| 31 | |
| 32 | for device in `ls /sys/block/`; do |
| 33 | case $device in |
| 34 | loop*) |
| 35 | # skip loop device |
| 36 | ;; |
| 37 | sr*) |
| 38 | # skip CDROM device |
| 39 | ;; |
| 40 | ram*) |
| 41 | # skip ram device |
| 42 | ;; |
| 43 | *) |
| 44 | # skip the device LiveOS is on |
| 45 | # Add valid hard drive name to the list |
| 46 | case $device in |
| 47 | $live_dev_name*) |
| 48 | # skip the device we are running from |
| 49 | ;; |
| 50 | *) |
| 51 | hdnamelist="$hdnamelist $device" |
| 52 | ;; |
| 53 | esac |
| 54 | ;; |
| 55 | esac |
| 56 | done |
| 57 | |
| 58 | if [ -z "${hdnamelist}" ]; then |
| 59 | echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted." |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | TARGET_DEVICE_NAME="" |
| 64 | for hdname in $hdnamelist; do |
| 65 | # Display found hard drives and their basic info |
| 66 | echo "-------------------------------" |
| 67 | echo /dev/$hdname |
| 68 | if [ -r /sys/block/$hdname/device/vendor ]; then |
| 69 | echo -n "VENDOR=" |
| 70 | cat /sys/block/$hdname/device/vendor |
| 71 | fi |
| 72 | if [ -r /sys/block/$hdname/device/model ]; then |
| 73 | echo -n "MODEL=" |
| 74 | cat /sys/block/$hdname/device/model |
| 75 | fi |
| 76 | if [ -r /sys/block/$hdname/device/uevent ]; then |
| 77 | echo -n "UEVENT=" |
| 78 | cat /sys/block/$hdname/device/uevent |
| 79 | fi |
| 80 | echo |
| 81 | # Get user choice |
| 82 | while true; do |
| 83 | echo -n "Do you want to install this image there? [y/n] " |
| 84 | read answer |
| 85 | if [ "$answer" = "y" -o "$answer" = "n" ]; then |
| 86 | break |
| 87 | fi |
| 88 | echo "Please answer y or n" |
| 89 | done |
| 90 | if [ "$answer" = "y" ]; then |
| 91 | TARGET_DEVICE_NAME=$hdname |
| 92 | break |
| 93 | fi |
| 94 | done |
| 95 | |
| 96 | if [ -n "$TARGET_DEVICE_NAME" ]; then |
| 97 | echo "Installing image on /dev/$TARGET_DEVICE_NAME ..." |
| 98 | else |
| 99 | echo "No hard drive selected. Installation aborted." |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | device=/dev/$TARGET_DEVICE_NAME |
| 104 | |
| 105 | # |
| 106 | # The udev automounter can cause pain here, kill it |
| 107 | # |
| 108 | rm -f /etc/udev/rules.d/automount.rules |
| 109 | rm -f /etc/udev/scripts/mount* |
| 110 | |
| 111 | # |
| 112 | # Unmount anything the automounter had mounted |
| 113 | # |
| 114 | umount ${device}* 2> /dev/null || /bin/true |
| 115 | |
| 116 | mkdir -p /tmp |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame^] | 117 | |
| 118 | # Create /etc/mtab if not present |
| 119 | if [ ! -e /etc/mtab ]; then |
| 120 | cat /proc/mounts > /etc/mtab |
| 121 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | |
| 123 | disk_size=$(parted ${device} unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//") |
| 124 | |
| 125 | swap_size=$((disk_size*swap_ratio/100)) |
| 126 | rootfs_size=$((disk_size-boot_size-swap_size)) |
| 127 | |
| 128 | rootfs_start=$((boot_size)) |
| 129 | rootfs_end=$((rootfs_start+rootfs_size)) |
| 130 | swap_start=$((rootfs_end)) |
| 131 | |
| 132 | # MMC devices are special in a couple of ways |
| 133 | # 1) they use a partition prefix character 'p' |
| 134 | # 2) they are detected asynchronously (need rootwait) |
| 135 | rootwait="" |
| 136 | part_prefix="" |
| 137 | if [ ! "${device#mmcblk}" = "${device}" ]; then |
| 138 | part_prefix="p" |
| 139 | rootwait="rootwait" |
| 140 | fi |
| 141 | bootfs=${device}${part_prefix}1 |
| 142 | rootfs=${device}${part_prefix}2 |
| 143 | swap=${device}${part_prefix}3 |
| 144 | |
| 145 | echo "*****************" |
| 146 | echo "Boot partition size: $boot_size MB ($bootfs)" |
| 147 | echo "Rootfs partition size: $rootfs_size MB ($rootfs)" |
| 148 | echo "Swap partition size: $swap_size MB ($swap)" |
| 149 | echo "*****************" |
| 150 | echo "Deleting partition table on ${device} ..." |
| 151 | dd if=/dev/zero of=${device} bs=512 count=35 |
| 152 | |
| 153 | echo "Creating new partition table on ${device} ..." |
| 154 | parted ${device} mklabel gpt |
| 155 | |
| 156 | echo "Creating boot partition on $bootfs" |
| 157 | parted ${device} mkpart boot fat32 0% $boot_size |
| 158 | parted ${device} set 1 boot on |
| 159 | |
| 160 | echo "Creating rootfs partition on $rootfs" |
| 161 | parted ${device} mkpart root ext3 $rootfs_start $rootfs_end |
| 162 | |
| 163 | echo "Creating swap partition on $swap" |
| 164 | parted ${device} mkpart swap linux-swap $swap_start 100% |
| 165 | |
| 166 | parted ${device} print |
| 167 | |
| 168 | echo "Formatting $bootfs to vfat..." |
| 169 | mkfs.vfat $bootfs |
| 170 | |
| 171 | echo "Formatting $rootfs to ext3..." |
| 172 | mkfs.ext3 $rootfs |
| 173 | |
| 174 | echo "Formatting swap partition...($swap)" |
| 175 | mkswap $swap |
| 176 | |
| 177 | mkdir /tgt_root |
| 178 | mkdir /src_root |
| 179 | mkdir -p /boot |
| 180 | |
| 181 | # Handling of the target root partition |
| 182 | mount $rootfs /tgt_root |
| 183 | mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root |
| 184 | echo "Copying rootfs files..." |
| 185 | cp -a /src_root/* /tgt_root |
| 186 | if [ -d /tgt_root/etc/ ] ; then |
| 187 | boot_uuid=$(blkid -o value -s UUID ${device}1) |
| 188 | swap_part_uuid=$(blkid -o value -s PARTUUID ${device}3) |
| 189 | echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab |
| 190 | echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab |
| 191 | # We dont want udev to mount our root device while we're booting... |
| 192 | if [ -d /tgt_root/etc/udev/ ] ; then |
| 193 | echo "${device}" >> /tgt_root/etc/udev/mount.blacklist |
| 194 | fi |
| 195 | fi |
| 196 | |
| 197 | umount /src_root |
| 198 | |
| 199 | # Handling of the target boot partition |
| 200 | mount $bootfs /boot |
| 201 | echo "Preparing boot partition..." |
| 202 | |
| 203 | EFIDIR="/boot/EFI/BOOT" |
| 204 | mkdir -p $EFIDIR |
| 205 | # Copy the efi loader |
| 206 | cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR |
| 207 | |
| 208 | if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then |
| 209 | root_part_uuid=$(blkid -o value -s PARTUUID ${device}2) |
| 210 | GRUBCFG="$EFIDIR/grub.cfg" |
| 211 | cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG |
| 212 | # Update grub config for the installed image |
| 213 | # Delete the install entry |
| 214 | sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG |
| 215 | # Delete the initrd lines |
| 216 | sed -i "/initrd /d" $GRUBCFG |
| 217 | # Delete any LABEL= strings |
| 218 | sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG |
| 219 | # Delete any root= strings |
| 220 | sed -i "s/ root=[^ ]*/ /" $GRUBCFG |
| 221 | # Add the root= and other standard boot options |
| 222 | sed -i "s@linux /vmlinuz *@linux /vmlinuz root=PARTUUID=$root_part_uuid rw $rootwait quiet @" $GRUBCFG |
| 223 | fi |
| 224 | |
| 225 | if [ -d /run/media/$1/loader ]; then |
| 226 | GUMMIBOOT_CFGS="/boot/loader/entries/*.conf" |
| 227 | # copy config files for gummiboot |
| 228 | cp -dr /run/media/$1/loader /boot |
| 229 | # delete the install entry |
| 230 | rm -f /boot/loader/entries/install.conf |
| 231 | # delete the initrd lines |
| 232 | sed -i "/initrd /d" $GUMMIBOOT_CFGS |
| 233 | # delete any LABEL= strings |
| 234 | sed -i "s/ LABEL=[^ ]*/ /" $GUMMIBOOT_CFGS |
| 235 | # delete any root= strings |
| 236 | sed -i "s/ root=[^ ]*/ /" $GUMMIBOOT_CFGS |
| 237 | # add the root= and other standard boot options |
| 238 | sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $GUMMIBOOT_CFGS |
| 239 | fi |
| 240 | |
| 241 | umount /tgt_root |
| 242 | |
| 243 | cp /run/media/$1/vmlinuz /boot |
| 244 | |
| 245 | umount /boot |
| 246 | |
| 247 | sync |
| 248 | |
| 249 | echo "Remove your installation media, and press ENTER" |
| 250 | |
| 251 | read enter |
| 252 | |
| 253 | echo "Rebooting..." |
| 254 | reboot -f |