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
8PATH=/sbin:/bin:/usr/sbin:/usr/bin
9
10# We need 20 Mb for the boot partition
11boot_size=200
12
13# 50% for the the test partition
14testfs_ratio=50
15
16# Get a list of hard drives
17hdnamelist=""
18live_dev_name=${1%%/*}
19
20echo "Searching for hard drives ..."
21
22for 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
38done
39
40TARGET_DEVICE_NAME=""
41for 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
66done
67
68if [ -n "$TARGET_DEVICE_NAME" ]; then
69    echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
70else
71    echo "No hard drive selected. Installation aborted."
72    exit 1
73fi
74
75device=$TARGET_DEVICE_NAME
76
77#
78# The udev automounter can cause pain here, kill it
79#
80rm -f /etc/udev/rules.d/automount.rules
81rm -f /etc/udev/scripts/mount*
82
83#
84# Unmount anything the automounter had mounted
85#
86umount /dev/${device}* 2> /dev/null || /bin/true
87
88if [ ! -b /dev/loop0 ] ; then
89    mknod /dev/loop0 b 7 0
90fi
91
92mkdir -p /tmp
93if [ ! -L /etc/mtab ]; then
94	cat /proc/mounts > /etc/mtab
95fi
96
97disk_size=$(parted /dev/${device} unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//")
98
99testfs_size=$((disk_size*testfs_ratio/100))
100rootfs_size=$((disk_size-boot_size-testfs_size))
101
102rootfs_start=$((boot_size))
103rootfs_end=$((rootfs_start+rootfs_size))
104testfs_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)
109rootwait=""
110part_prefix=""
111if [ ! "${device#mmcblk}" = "${device}" ]; then
112	part_prefix="p"
113	rootwait="rootwait"
114fi
115bootfs=/dev/${device}${part_prefix}1
116rootfs=/dev/${device}${part_prefix}2
117testfs=/dev/${device}${part_prefix}3
118
119echo "*****************"
120echo "Boot partition size:   $boot_size MB ($bootfs)"
121echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
122echo "Testfs partition size: $testfs_size MB ($testfs)"
123echo "*****************"
124echo "Deleting partition table on /dev/${device} ..."
125dd if=/dev/zero of=/dev/${device} bs=512 count=2
126
127echo "Creating new partition table on /dev/${device} ..."
128parted /dev/${device} mklabel msdos
129
130echo "Creating boot partition on $bootfs"
131parted /dev/${device} mkpart primary 0% $boot_size
132
133echo "Creating rootfs partition on $rootfs"
134parted /dev/${device} mkpart primary $rootfs_start $rootfs_end
135
136echo "Creating testfs partition on $testfs"
137parted /dev/${device} mkpart primary $testfs_start 100%
138
139parted /dev/${device} print
140
141echo "Formatting $bootfs to ext3..."
142mkfs.ext3 -L "boot" $bootfs
143
144echo "Formatting $rootfs to ext3..."
145mkfs.ext3 -L "rootfs" $rootfs
146
147echo "Formatting $testfs to ext3..."
148mkfs.ext3 -L "testrootfs" $testfs
149
150mkdir /tgt_root
151mkdir /src_root
152mkdir -p /boot
153
154# Handling of the target root partition
155mount $rootfs /tgt_root
156mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
157
158echo "Copying rootfs files..."
159cp -a /src_root/* /tgt_root
160
161touch /tgt_root/etc/controllerimage
162
163if [ -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
167	echo "/dev/${device}" >> /tgt_root/etc/udev/mount.ignorelist
168    fi
169fi
170umount /tgt_root
171umount /src_root
172
173# Handling of the target boot partition
174mount $bootfs /boot
175echo "Preparing boot partition..."
176if [ -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 
181serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1
182terminal_input --append  serial
183terminal_output --append serial
184set timeout_style=hidden
185set timeout=5
186menuentry "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
195fi
196grub-install /dev/${device}
197echo "(hd0) /dev/${device}" > /boot/grub/device.map
198
199# If grub.cfg doesn't exist, assume GRUB 0.97 and create a menu.lst
200if [ ! -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
207fi
208
209cp /run/media/$1/vmlinuz /boot/
210
211umount /boot
212
213sync
214
215echo "Remove your installation media, and press ENTER"
216
217read enter
218
219echo "Rebooting..."
220reboot -f
221