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