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=200 |
| 12 | |
| 13 | # 50% for the the test partition |
| 14 | testfs_ratio=50 |
| 15 | |
| 16 | # Get a list of hard drives |
| 17 | hdnamelist="" |
| 18 | live_dev_name=${1%%/*} |
| 19 | |
| 20 | echo "Searching for hard drives ..." |
| 21 | |
| 22 | for device in `ls /sys/block/`; do |
| 23 | case $device in |
| 24 | loop*) |
| 25 | # skip loop device |
| 26 | ;; |
| 27 | ram*) |
| 28 | # skip ram device |
| 29 | ;; |
| 30 | *) |
| 31 | # skip the device LiveOS is on |
| 32 | # Add valid hard drive name to the list |
| 33 | if [ $device != $live_dev_name -a -e /dev/$device ]; then |
| 34 | hdnamelist="$hdnamelist $device" |
| 35 | fi |
| 36 | ;; |
| 37 | esac |
| 38 | done |
| 39 | |
| 40 | TARGET_DEVICE_NAME="" |
| 41 | for hdname in $hdnamelist; do |
| 42 | # Display found hard drives and their basic info |
| 43 | echo "-------------------------------" |
| 44 | echo /dev/$hdname |
| 45 | if [ -r /sys/block/$hdname/device/vendor ]; then |
| 46 | echo -n "VENDOR=" |
| 47 | cat /sys/block/$hdname/device/vendor |
| 48 | fi |
| 49 | echo -n "MODEL=" |
| 50 | cat /sys/block/$hdname/device/model |
| 51 | cat /sys/block/$hdname/device/uevent |
| 52 | echo |
| 53 | # Get user choice |
| 54 | while true; do |
| 55 | echo -n "Do you want to install this image there? [y/n] " |
| 56 | read answer |
| 57 | if [ "$answer" = "y" -o "$answer" = "n" ]; then |
| 58 | break |
| 59 | fi |
| 60 | echo "Please answer y or n" |
| 61 | done |
| 62 | if [ "$answer" = "y" ]; then |
| 63 | TARGET_DEVICE_NAME=$hdname |
| 64 | break |
| 65 | fi |
| 66 | done |
| 67 | |
| 68 | if [ -n "$TARGET_DEVICE_NAME" ]; then |
| 69 | echo "Installing image on /dev/$TARGET_DEVICE_NAME ..." |
| 70 | else |
| 71 | echo "No hard drive selected. Installation aborted." |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | device=$TARGET_DEVICE_NAME |
| 76 | |
| 77 | # |
| 78 | # The udev automounter can cause pain here, kill it |
| 79 | # |
| 80 | rm -f /etc/udev/rules.d/automount.rules |
| 81 | rm -f /etc/udev/scripts/mount* |
| 82 | |
| 83 | # |
| 84 | # Unmount anything the automounter had mounted |
| 85 | # |
| 86 | umount /dev/${device}* 2> /dev/null || /bin/true |
| 87 | |
| 88 | if [ ! -b /dev/loop0 ] ; then |
| 89 | mknod /dev/loop0 b 7 0 |
| 90 | fi |
| 91 | |
| 92 | mkdir -p /tmp |
| 93 | if [ ! -L /etc/mtab ]; then |
| 94 | cat /proc/mounts > /etc/mtab |
| 95 | fi |
| 96 | |
| 97 | disk_size=$(parted /dev/${device} unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//") |
| 98 | |
| 99 | testfs_size=$((disk_size*testfs_ratio/100)) |
| 100 | rootfs_size=$((disk_size-boot_size-testfs_size)) |
| 101 | |
| 102 | rootfs_start=$((boot_size)) |
| 103 | rootfs_end=$((rootfs_start+rootfs_size)) |
| 104 | testfs_start=$((rootfs_end)) |
| 105 | |
| 106 | # MMC devices are special in a couple of ways |
| 107 | # 1) they use a partition prefix character 'p' |
| 108 | # 2) they are detected asynchronously (need rootwait) |
| 109 | rootwait="" |
| 110 | part_prefix="" |
| 111 | if [ ! "${device#mmcblk}" = "${device}" ]; then |
| 112 | part_prefix="p" |
| 113 | rootwait="rootwait" |
| 114 | fi |
| 115 | bootfs=/dev/${device}${part_prefix}1 |
| 116 | rootfs=/dev/${device}${part_prefix}2 |
| 117 | testfs=/dev/${device}${part_prefix}3 |
| 118 | |
| 119 | echo "*****************" |
| 120 | echo "Boot partition size: $boot_size MB ($bootfs)" |
| 121 | echo "Rootfs partition size: $rootfs_size MB ($rootfs)" |
| 122 | echo "Testfs partition size: $testfs_size MB ($testfs)" |
| 123 | echo "*****************" |
| 124 | echo "Deleting partition table on /dev/${device} ..." |
| 125 | dd if=/dev/zero of=/dev/${device} bs=512 count=2 |
| 126 | |
| 127 | echo "Creating new partition table on /dev/${device} ..." |
| 128 | parted /dev/${device} mklabel msdos |
| 129 | |
| 130 | echo "Creating boot partition on $bootfs" |
| 131 | parted /dev/${device} mkpart primary 0% $boot_size |
| 132 | |
| 133 | echo "Creating rootfs partition on $rootfs" |
| 134 | parted /dev/${device} mkpart primary $rootfs_start $rootfs_end |
| 135 | |
| 136 | echo "Creating testfs partition on $testfs" |
| 137 | parted /dev/${device} mkpart primary $testfs_start 100% |
| 138 | |
| 139 | parted /dev/${device} print |
| 140 | |
| 141 | echo "Formatting $bootfs to ext3..." |
| 142 | mkfs.ext3 -L "boot" $bootfs |
| 143 | |
| 144 | echo "Formatting $rootfs to ext3..." |
| 145 | mkfs.ext3 -L "rootfs" $rootfs |
| 146 | |
| 147 | echo "Formatting $testfs to ext3..." |
| 148 | mkfs.ext3 -L "testrootfs" $testfs |
| 149 | |
| 150 | mkdir /tgt_root |
| 151 | mkdir /src_root |
| 152 | mkdir -p /boot |
| 153 | |
| 154 | # Handling of the target root partition |
| 155 | mount $rootfs /tgt_root |
| 156 | mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root |
| 157 | |
| 158 | echo "Copying rootfs files..." |
| 159 | cp -a /src_root/* /tgt_root |
| 160 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 161 | touch /tgt_root/etc/controllerimage |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | |
| 163 | if [ -d /tgt_root/etc/ ] ; then |
| 164 | echo "$bootfs /boot ext3 defaults 1 2" >> /tgt_root/etc/fstab |
| 165 | # We dont want udev to mount our root device while we're booting... |
| 166 | if [ -d /tgt_root/etc/udev/ ] ; then |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 167 | echo "/dev/${device}" >> /tgt_root/etc/udev/mount.ignorelist |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | fi |
| 169 | fi |
| 170 | umount /tgt_root |
| 171 | umount /src_root |
| 172 | |
| 173 | # Handling of the target boot partition |
| 174 | mount $bootfs /boot |
| 175 | echo "Preparing boot partition..." |
| 176 | if [ -f /etc/grub.d/00_header ] ; then |
| 177 | echo "Preparing custom grub2 menu..." |
| 178 | GRUBCFG="/boot/grub/grub.cfg" |
| 179 | mkdir -p $(dirname $GRUBCFG) |
| 180 | cat >$GRUBCFG <<_EOF |
| 181 | serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1 |
| 182 | terminal_input --append serial |
| 183 | terminal_output --append serial |
| 184 | set timeout_style=hidden |
| 185 | set timeout=5 |
| 186 | menuentry "Linux" { |
| 187 | set root=(hd0,1) |
| 188 | linux /vmlinuz root=$rootfs $rootwait rw $5 $3 $4 quiet |
| 189 | } |
| 190 | _EOF |
| 191 | # Add the test label |
| 192 | echo -ne "\nmenuentry 'test' --hotkey x {\nlinux /test-kernel root=$testfs rw $rootwait quiet\n}\n" >> $GRUBCFG |
| 193 | |
| 194 | chmod 0444 $GRUBCFG |
| 195 | fi |
| 196 | grub-install /dev/${device} |
| 197 | echo "(hd0) /dev/${device}" > /boot/grub/device.map |
| 198 | |
| 199 | # If grub.cfg doesn't exist, assume GRUB 0.97 and create a menu.lst |
| 200 | if [ ! -f /boot/grub/grub.cfg ] ; then |
| 201 | echo "Preparing custom grub menu..." |
| 202 | echo "default 0" > /boot/grub/menu.lst |
| 203 | echo "timeout 30" >> /boot/grub/menu.lst |
| 204 | echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst |
| 205 | echo "root (hd0,0)" >> /boot/grub/menu.lst |
| 206 | echo "kernel /vmlinuz root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst |
| 207 | fi |
| 208 | |
| 209 | cp /run/media/$1/vmlinuz /boot/ |
| 210 | |
| 211 | umount /boot |
| 212 | |
| 213 | sync |
| 214 | |
| 215 | echo "Remove your installation media, and press ENTER" |
| 216 | |
| 217 | read enter |
| 218 | |
| 219 | echo "Rebooting..." |
| 220 | reboot -f |