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 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 11 | # figure out how big of a boot partition we need |
| 12 | boot_size=$(du -ms /run/media/$1/ | awk '{print $1}') |
| 13 | # remove rootfs.img ($2) from the size if it exists, as its not installed to /boot |
| 14 | if [ -e /run/media/$1/$2 ]; then |
| 15 | boot_size=$(( boot_size - $( du -ms /run/media/$1/$2 | awk '{print $1}') )) |
| 16 | fi |
| 17 | # remove initrd from size since its not currently installed |
| 18 | if [ -e /run/media/$1/initrd ]; then |
| 19 | boot_size=$(( boot_size - $( du -ms /run/media/$1/initrd | awk '{print $1}') )) |
| 20 | fi |
| 21 | # add 10M to provide some extra space for users and account |
| 22 | # for rounding in the above subtractions |
| 23 | boot_size=$(( boot_size + 10 )) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | |
| 25 | # 5% for swap |
| 26 | swap_ratio=5 |
| 27 | |
| 28 | # Get a list of hard drives |
| 29 | hdnamelist="" |
| 30 | live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'` |
| 31 | live_dev_name=${live_dev_name#\/dev/} |
| 32 | # Only strip the digit identifier if the device is not an mmc |
| 33 | case $live_dev_name in |
| 34 | mmcblk*) |
| 35 | ;; |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 36 | nvme*) |
| 37 | ;; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | *) |
| 39 | live_dev_name=${live_dev_name%%[0-9]*} |
| 40 | ;; |
| 41 | esac |
| 42 | |
| 43 | echo "Searching for hard drives ..." |
| 44 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | # Some eMMC devices have special sub devices such as mmcblk0boot0 etc |
| 46 | # we're currently only interested in the root device so pick them wisely |
| 47 | devices=`ls /sys/block/ | grep -v mmcblk` || true |
| 48 | mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true |
| 49 | devices="$devices $mmc_devices" |
| 50 | |
| 51 | for device in $devices; do |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | case $device in |
| 53 | loop*) |
| 54 | # skip loop device |
| 55 | ;; |
| 56 | sr*) |
| 57 | # skip CDROM device |
| 58 | ;; |
| 59 | ram*) |
| 60 | # skip ram device |
| 61 | ;; |
| 62 | *) |
| 63 | # skip the device LiveOS is on |
| 64 | # Add valid hard drive name to the list |
| 65 | case $device in |
| 66 | $live_dev_name*) |
| 67 | # skip the device we are running from |
| 68 | ;; |
| 69 | *) |
| 70 | hdnamelist="$hdnamelist $device" |
| 71 | ;; |
| 72 | esac |
| 73 | ;; |
| 74 | esac |
| 75 | done |
| 76 | |
| 77 | if [ -z "${hdnamelist}" ]; then |
| 78 | echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted." |
| 79 | exit 1 |
| 80 | fi |
| 81 | |
| 82 | TARGET_DEVICE_NAME="" |
| 83 | for hdname in $hdnamelist; do |
| 84 | # Display found hard drives and their basic info |
| 85 | echo "-------------------------------" |
| 86 | echo /dev/$hdname |
| 87 | if [ -r /sys/block/$hdname/device/vendor ]; then |
| 88 | echo -n "VENDOR=" |
| 89 | cat /sys/block/$hdname/device/vendor |
| 90 | fi |
| 91 | if [ -r /sys/block/$hdname/device/model ]; then |
| 92 | echo -n "MODEL=" |
| 93 | cat /sys/block/$hdname/device/model |
| 94 | fi |
| 95 | if [ -r /sys/block/$hdname/device/uevent ]; then |
| 96 | echo -n "UEVENT=" |
| 97 | cat /sys/block/$hdname/device/uevent |
| 98 | fi |
| 99 | echo |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | done |
| 101 | |
| 102 | # Get user choice |
| 103 | while true; do |
| 104 | echo "Please select an install target or press n to exit ($hdnamelist ): " |
| 105 | read answer |
| 106 | if [ "$answer" = "n" ]; then |
| 107 | echo "Installation manually aborted." |
| 108 | exit 1 |
| 109 | fi |
| 110 | for hdname in $hdnamelist; do |
| 111 | if [ "$answer" = "$hdname" ]; then |
| 112 | TARGET_DEVICE_NAME=$answer |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | break |
| 114 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | done |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 116 | if [ -n "$TARGET_DEVICE_NAME" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | break |
| 118 | fi |
| 119 | done |
| 120 | |
| 121 | if [ -n "$TARGET_DEVICE_NAME" ]; then |
| 122 | echo "Installing image on /dev/$TARGET_DEVICE_NAME ..." |
| 123 | else |
| 124 | echo "No hard drive selected. Installation aborted." |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
| 128 | device=/dev/$TARGET_DEVICE_NAME |
| 129 | |
| 130 | # |
| 131 | # The udev automounter can cause pain here, kill it |
| 132 | # |
| 133 | rm -f /etc/udev/rules.d/automount.rules |
| 134 | rm -f /etc/udev/scripts/mount* |
| 135 | |
| 136 | # |
| 137 | # Unmount anything the automounter had mounted |
| 138 | # |
| 139 | umount ${device}* 2> /dev/null || /bin/true |
| 140 | |
| 141 | mkdir -p /tmp |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 142 | |
| 143 | # Create /etc/mtab if not present |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 144 | if [ ! -e /etc/mtab ] && [ -e /proc/mounts ]; then |
| 145 | ln -sf /proc/mounts /etc/mtab |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 146 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 148 | disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | |
| 150 | swap_size=$((disk_size*swap_ratio/100)) |
| 151 | rootfs_size=$((disk_size-boot_size-swap_size)) |
| 152 | |
| 153 | rootfs_start=$((boot_size)) |
| 154 | rootfs_end=$((rootfs_start+rootfs_size)) |
| 155 | swap_start=$((rootfs_end)) |
| 156 | |
| 157 | # MMC devices are special in a couple of ways |
| 158 | # 1) they use a partition prefix character 'p' |
| 159 | # 2) they are detected asynchronously (need rootwait) |
| 160 | rootwait="" |
| 161 | part_prefix="" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 162 | if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \ |
| 163 | [ ! "${device#/dev/nvme}" = "${device}" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | part_prefix="p" |
| 165 | rootwait="rootwait" |
| 166 | fi |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | |
| 168 | # USB devices also require rootwait |
| 169 | if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then |
| 170 | rootwait="rootwait" |
| 171 | fi |
| 172 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 173 | bootfs=${device}${part_prefix}1 |
| 174 | rootfs=${device}${part_prefix}2 |
| 175 | swap=${device}${part_prefix}3 |
| 176 | |
| 177 | echo "*****************" |
| 178 | echo "Boot partition size: $boot_size MB ($bootfs)" |
| 179 | echo "Rootfs partition size: $rootfs_size MB ($rootfs)" |
| 180 | echo "Swap partition size: $swap_size MB ($swap)" |
| 181 | echo "*****************" |
| 182 | echo "Deleting partition table on ${device} ..." |
| 183 | dd if=/dev/zero of=${device} bs=512 count=35 |
| 184 | |
| 185 | echo "Creating new partition table on ${device} ..." |
| 186 | parted ${device} mklabel gpt |
| 187 | |
| 188 | echo "Creating boot partition on $bootfs" |
| 189 | parted ${device} mkpart boot fat32 0% $boot_size |
| 190 | parted ${device} set 1 boot on |
| 191 | |
| 192 | echo "Creating rootfs partition on $rootfs" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 193 | parted ${device} mkpart root ext4 $rootfs_start $rootfs_end |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 194 | |
| 195 | echo "Creating swap partition on $swap" |
| 196 | parted ${device} mkpart swap linux-swap $swap_start 100% |
| 197 | |
| 198 | parted ${device} print |
| 199 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 200 | echo "Waiting for device nodes..." |
| 201 | C=0 |
| 202 | while [ $C -ne 3 ] && [ ! -e $bootfs -o ! -e $rootfs -o ! -e $swap ]; do |
| 203 | C=$(( C + 1 )) |
| 204 | sleep 1 |
| 205 | done |
| 206 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | echo "Formatting $bootfs to vfat..." |
| 208 | mkfs.vfat $bootfs |
| 209 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 210 | echo "Formatting $rootfs to ext4..." |
| 211 | mkfs.ext4 $rootfs |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | |
| 213 | echo "Formatting swap partition...($swap)" |
| 214 | mkswap $swap |
| 215 | |
| 216 | mkdir /tgt_root |
| 217 | mkdir /src_root |
| 218 | mkdir -p /boot |
| 219 | |
| 220 | # Handling of the target root partition |
| 221 | mount $rootfs /tgt_root |
| 222 | mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root |
| 223 | echo "Copying rootfs files..." |
| 224 | cp -a /src_root/* /tgt_root |
| 225 | if [ -d /tgt_root/etc/ ] ; then |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 226 | boot_uuid=$(blkid -o value -s UUID ${bootfs}) |
| 227 | swap_part_uuid=$(blkid -o value -s PARTUUID ${swap}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 228 | echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab |
| 229 | echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab |
| 230 | # We dont want udev to mount our root device while we're booting... |
| 231 | if [ -d /tgt_root/etc/udev/ ] ; then |
| 232 | echo "${device}" >> /tgt_root/etc/udev/mount.blacklist |
| 233 | fi |
| 234 | fi |
| 235 | |
| 236 | umount /src_root |
| 237 | |
| 238 | # Handling of the target boot partition |
| 239 | mount $bootfs /boot |
| 240 | echo "Preparing boot partition..." |
| 241 | |
| 242 | EFIDIR="/boot/EFI/BOOT" |
| 243 | mkdir -p $EFIDIR |
| 244 | # Copy the efi loader |
| 245 | cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR |
| 246 | |
| 247 | if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 248 | root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | GRUBCFG="$EFIDIR/grub.cfg" |
| 250 | cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG |
| 251 | # Update grub config for the installed image |
| 252 | # Delete the install entry |
| 253 | sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG |
| 254 | # Delete the initrd lines |
| 255 | sed -i "/initrd /d" $GRUBCFG |
| 256 | # Delete any LABEL= strings |
| 257 | sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 258 | # Replace root= and add additional standard boot options |
| 259 | # We use root as a sentinel value, as vmlinuz is no longer guaranteed |
| 260 | sed -i "s/ root=[^ ]*/ root=PARTUUID=$root_part_uuid rw $rootwait quiet /g" $GRUBCFG |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 261 | fi |
| 262 | |
| 263 | if [ -d /run/media/$1/loader ]; then |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 264 | rootuuid=$(blkid -o value -s PARTUUID ${rootfs}) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 265 | SYSTEMDBOOT_CFGS="/boot/loader/entries/*.conf" |
| 266 | # copy config files for systemd-boot |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 267 | cp -dr /run/media/$1/loader /boot |
| 268 | # delete the install entry |
| 269 | rm -f /boot/loader/entries/install.conf |
| 270 | # delete the initrd lines |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 271 | sed -i "/initrd /d" $SYSTEMDBOOT_CFGS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 272 | # delete any LABEL= strings |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 273 | sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 274 | # delete any root= strings |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 275 | sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | # add the root= and other standard boot options |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 277 | sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $SYSTEMDBOOT_CFGS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 278 | fi |
| 279 | |
| 280 | umount /tgt_root |
| 281 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 282 | # Copy kernel artifacts. To add more artifacts just add to types |
| 283 | # For now just support kernel types already being used by something in OE-core |
| 284 | for types in bzImage zImage vmlinux vmlinuz fitImage; do |
| 285 | for kernel in `find /run/media/$1/ -name $types*`; do |
| 286 | cp $kernel /boot |
| 287 | done |
| 288 | done |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 289 | |
| 290 | umount /boot |
| 291 | |
| 292 | sync |
| 293 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 294 | echo "Installation successful. Remove your installation media and press ENTER to reboot." |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 295 | |
| 296 | read enter |
| 297 | |
| 298 | echo "Rebooting..." |
| 299 | reboot -f |