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