blob: d58826a240993ef20341b551658ae27f128b956f [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
Patrick Williamsd7e96312015-09-22 08:09:05 -0500117
118# Create /etc/mtab if not present
119if [ ! -e /etc/mtab ]; then
120 cat /proc/mounts > /etc/mtab
121fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
125swap_size=$((disk_size*swap_ratio/100))
126rootfs_size=$((disk_size-boot_size-swap_size))
127
128rootfs_start=$((boot_size))
129rootfs_end=$((rootfs_start+rootfs_size))
130swap_start=$((rootfs_end))
131
132# MMC devices are special in a couple of ways
133# 1) they use a partition prefix character 'p'
134# 2) they are detected asynchronously (need rootwait)
135rootwait=""
136part_prefix=""
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500137if [ ! "${device#/dev/mmcblk}" = "${device}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 part_prefix="p"
139 rootwait="rootwait"
140fi
141bootfs=${device}${part_prefix}1
142rootfs=${device}${part_prefix}2
143swap=${device}${part_prefix}3
144
145echo "*****************"
146echo "Boot partition size: $boot_size MB ($bootfs)"
147echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
148echo "Swap partition size: $swap_size MB ($swap)"
149echo "*****************"
150echo "Deleting partition table on ${device} ..."
151dd if=/dev/zero of=${device} bs=512 count=35
152
153echo "Creating new partition table on ${device} ..."
154parted ${device} mklabel gpt
155
156echo "Creating boot partition on $bootfs"
157parted ${device} mkpart boot fat32 0% $boot_size
158parted ${device} set 1 boot on
159
160echo "Creating rootfs partition on $rootfs"
161parted ${device} mkpart root ext3 $rootfs_start $rootfs_end
162
163echo "Creating swap partition on $swap"
164parted ${device} mkpart swap linux-swap $swap_start 100%
165
166parted ${device} print
167
168echo "Formatting $bootfs to vfat..."
169mkfs.vfat $bootfs
170
171echo "Formatting $rootfs to ext3..."
172mkfs.ext3 $rootfs
173
174echo "Formatting swap partition...($swap)"
175mkswap $swap
176
177mkdir /tgt_root
178mkdir /src_root
179mkdir -p /boot
180
181# Handling of the target root partition
182mount $rootfs /tgt_root
183mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
184echo "Copying rootfs files..."
185cp -a /src_root/* /tgt_root
186if [ -d /tgt_root/etc/ ] ; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500187 boot_uuid=$(blkid -o value -s UUID ${bootfs})
188 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab
190 echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab
191 # We dont want udev to mount our root device while we're booting...
192 if [ -d /tgt_root/etc/udev/ ] ; then
193 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
194 fi
195fi
196
197umount /src_root
198
199# Handling of the target boot partition
200mount $bootfs /boot
201echo "Preparing boot partition..."
202
203EFIDIR="/boot/EFI/BOOT"
204mkdir -p $EFIDIR
205# Copy the efi loader
206cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
207
208if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500209 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210 GRUBCFG="$EFIDIR/grub.cfg"
211 cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
212 # Update grub config for the installed image
213 # Delete the install entry
214 sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
215 # Delete the initrd lines
216 sed -i "/initrd /d" $GRUBCFG
217 # Delete any LABEL= strings
218 sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
219 # Delete any root= strings
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500220 sed -i "s/ root=[^ ]*/ /g" $GRUBCFG
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 # Add the root= and other standard boot options
222 sed -i "s@linux /vmlinuz *@linux /vmlinuz root=PARTUUID=$root_part_uuid rw $rootwait quiet @" $GRUBCFG
223fi
224
225if [ -d /run/media/$1/loader ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500226 rootuuid=$(blkid -o value -s PARTUUID ${rootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227 GUMMIBOOT_CFGS="/boot/loader/entries/*.conf"
228 # copy config files for gummiboot
229 cp -dr /run/media/$1/loader /boot
230 # delete the install entry
231 rm -f /boot/loader/entries/install.conf
232 # delete the initrd lines
233 sed -i "/initrd /d" $GUMMIBOOT_CFGS
234 # delete any LABEL= strings
235 sed -i "s/ LABEL=[^ ]*/ /" $GUMMIBOOT_CFGS
236 # delete any root= strings
237 sed -i "s/ root=[^ ]*/ /" $GUMMIBOOT_CFGS
238 # add the root= and other standard boot options
239 sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $GUMMIBOOT_CFGS
240fi
241
242umount /tgt_root
243
244cp /run/media/$1/vmlinuz /boot
245
246umount /boot
247
248sync
249
250echo "Remove your installation media, and press ENTER"
251
252read enter
253
254echo "Rebooting..."
255reboot -f