blob: a3ed74b989336b1f4807df7a70a18769d1e51380 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh -e
2#
3# Copyright (c) 2012, Intel Corporation.
4# All rights reserved.
5#
6# install.sh [device_name] [rootfs_name]
7#
8
9PATH=/sbin:/bin:/usr/sbin:/usr/bin
10
11# We need 20 Mb for the boot partition
12boot_size=20
13
14# 5% for swap
15swap_ratio=5
16
17# Get a list of hard drives
18hdnamelist=""
19live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
20live_dev_name=${live_dev_name#\/dev/}
21# Only strip the digit identifier if the device is not an mmc
22case $live_dev_name in
23 mmcblk*)
24 ;;
25 *)
26 live_dev_name=${live_dev_name%%[0-9]*}
27 ;;
28esac
29
30echo "Searching for hard drives ..."
31
32for device in `ls /sys/block/`; do
33 case $device in
34 loop*)
35 # skip loop device
36 ;;
37 sr*)
38 # skip CDROM device
39 ;;
40 ram*)
41 # skip ram device
42 ;;
43 *)
44 # skip the device LiveOS is on
45 # Add valid hard drive name to the list
46 case $device in
47 $live_dev_name*)
48 # skip the device we are running from
49 ;;
50 *)
51 hdnamelist="$hdnamelist $device"
52 ;;
53 esac
54 ;;
55 esac
56done
57
58if [ -z "${hdnamelist}" ]; then
59 echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted."
60 exit 1
61fi
62
63TARGET_DEVICE_NAME=""
64for 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
81 # Get user choice
82 while true; do
83 echo -n "Do you want to install this image there? [y/n] "
84 read answer
85 if [ "$answer" = "y" -o "$answer" = "n" ]; then
86 break
87 fi
88 echo "Please answer y or n"
89 done
90 if [ "$answer" = "y" ]; then
91 TARGET_DEVICE_NAME=$hdname
92 break
93 fi
94done
95
96if [ -n "$TARGET_DEVICE_NAME" ]; then
97 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
98else
99 echo "No hard drive selected. Installation aborted."
100 exit 1
101fi
102
103device=/dev/$TARGET_DEVICE_NAME
104
105#
106# The udev automounter can cause pain here, kill it
107#
108rm -f /etc/udev/rules.d/automount.rules
109rm -f /etc/udev/scripts/mount*
110
111#
112# Unmount anything the automounter had mounted
113#
114umount ${device}* 2> /dev/null || /bin/true
115
116mkdir -p /tmp
117cat /proc/mounts > /etc/mtab
118
119disk_size=$(parted ${device} unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//")
120
121swap_size=$((disk_size*swap_ratio/100))
122rootfs_size=$((disk_size-boot_size-swap_size))
123
124rootfs_start=$((boot_size))
125rootfs_end=$((rootfs_start+rootfs_size))
126swap_start=$((rootfs_end))
127
128# MMC devices are special in a couple of ways
129# 1) they use a partition prefix character 'p'
130# 2) they are detected asynchronously (need rootwait)
131rootwait=""
132part_prefix=""
133if [ ! "${device#mmcblk}" = "${device}" ]; then
134 part_prefix="p"
135 rootwait="rootwait"
136fi
137bootfs=${device}${part_prefix}1
138rootfs=${device}${part_prefix}2
139swap=${device}${part_prefix}3
140
141echo "*****************"
142echo "Boot partition size: $boot_size MB ($bootfs)"
143echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
144echo "Swap partition size: $swap_size MB ($swap)"
145echo "*****************"
146echo "Deleting partition table on ${device} ..."
147dd if=/dev/zero of=${device} bs=512 count=35
148
149echo "Creating new partition table on ${device} ..."
150parted ${device} mklabel gpt
151
152echo "Creating boot partition on $bootfs"
153parted ${device} mkpart boot fat32 0% $boot_size
154parted ${device} set 1 boot on
155
156echo "Creating rootfs partition on $rootfs"
157parted ${device} mkpart root ext3 $rootfs_start $rootfs_end
158
159echo "Creating swap partition on $swap"
160parted ${device} mkpart swap linux-swap $swap_start 100%
161
162parted ${device} print
163
164echo "Formatting $bootfs to vfat..."
165mkfs.vfat $bootfs
166
167echo "Formatting $rootfs to ext3..."
168mkfs.ext3 $rootfs
169
170echo "Formatting swap partition...($swap)"
171mkswap $swap
172
173mkdir /tgt_root
174mkdir /src_root
175mkdir -p /boot
176
177# Handling of the target root partition
178mount $rootfs /tgt_root
179mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
180echo "Copying rootfs files..."
181cp -a /src_root/* /tgt_root
182if [ -d /tgt_root/etc/ ] ; then
183 boot_uuid=$(blkid -o value -s UUID ${device}1)
184 swap_part_uuid=$(blkid -o value -s PARTUUID ${device}3)
185 echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab
186 echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab
187 # We dont want udev to mount our root device while we're booting...
188 if [ -d /tgt_root/etc/udev/ ] ; then
189 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
190 fi
191fi
192
193umount /src_root
194
195# Handling of the target boot partition
196mount $bootfs /boot
197echo "Preparing boot partition..."
198
199EFIDIR="/boot/EFI/BOOT"
200mkdir -p $EFIDIR
201# Copy the efi loader
202cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
203
204if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
205 root_part_uuid=$(blkid -o value -s PARTUUID ${device}2)
206 GRUBCFG="$EFIDIR/grub.cfg"
207 cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
208 # Update grub config for the installed image
209 # Delete the install entry
210 sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
211 # Delete the initrd lines
212 sed -i "/initrd /d" $GRUBCFG
213 # Delete any LABEL= strings
214 sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
215 # Delete any root= strings
216 sed -i "s/ root=[^ ]*/ /" $GRUBCFG
217 # Add the root= and other standard boot options
218 sed -i "s@linux /vmlinuz *@linux /vmlinuz root=PARTUUID=$root_part_uuid rw $rootwait quiet @" $GRUBCFG
219fi
220
221if [ -d /run/media/$1/loader ]; then
222 GUMMIBOOT_CFGS="/boot/loader/entries/*.conf"
223 # copy config files for gummiboot
224 cp -dr /run/media/$1/loader /boot
225 # delete the install entry
226 rm -f /boot/loader/entries/install.conf
227 # delete the initrd lines
228 sed -i "/initrd /d" $GUMMIBOOT_CFGS
229 # delete any LABEL= strings
230 sed -i "s/ LABEL=[^ ]*/ /" $GUMMIBOOT_CFGS
231 # delete any root= strings
232 sed -i "s/ root=[^ ]*/ /" $GUMMIBOOT_CFGS
233 # add the root= and other standard boot options
234 sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $GUMMIBOOT_CFGS
235fi
236
237umount /tgt_root
238
239cp /run/media/$1/vmlinuz /boot
240
241umount /boot
242
243sync
244
245echo "Remove your installation media, and press ENTER"
246
247read enter
248
249echo "Rebooting..."
250reboot -f