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