blob: 04ce5fb4b5d5e8d5038ba3b7776c402947bf5624 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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=20
12
13# 5% for the swap
14swap_ratio=5
15
16# Get a list of hard drives
17hdnamelist=""
18live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
19live_dev_name=${live_dev_name#\/dev/}
20# Only strip the digit identifier if the device is not an mmc
21case $live_dev_name in
22 mmcblk*)
23 ;;
24 *)
25 live_dev_name=${live_dev_name%%[0-9]*}
26 ;;
27esac
28
29echo "Searching for hard drives ..."
30
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031# 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
33devices=`ls /sys/block/ | grep -v mmcblk` || true
34mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
35devices="$devices $mmc_devices"
36
37for device in $devices; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 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
61done
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
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081done
82
83# Get user choice
84while 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 Williamsc124f4f2015-09-15 14:41:29 -050094 break
95 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 done
Patrick Williamsc0f7c042017-02-23 20:41:17 -060097 if [ -n "$TARGET_DEVICE_NAME" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 break
99 fi
100done
101
102if [ -n "$TARGET_DEVICE_NAME" ]; then
103 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
104else
105 echo "No hard drive selected. Installation aborted."
106 exit 1
107fi
108
109device=/dev/$TARGET_DEVICE_NAME
110
111#
112# The udev automounter can cause pain here, kill it
113#
114rm -f /etc/udev/rules.d/automount.rules
115rm -f /etc/udev/scripts/mount*
116
117#
118# Unmount anything the automounter had mounted
119#
120umount ${device}* 2> /dev/null || /bin/true
121
122if [ ! -b /dev/loop0 ] ; then
123 mknod /dev/loop0 b 7 0
124fi
125
126mkdir -p /tmp
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600127if [ ! -L /etc/mtab ] && [ -e /proc/mounts ]; then
128 ln -sf /proc/mounts /etc/mtab
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129fi
130
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500131disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132
133grub_version=$(grub-install -v|sed 's/.* \([0-9]\).*/\1/')
134
135if [ $grub_version -eq 0 ] ; then
136 bios_boot_size=0
137else
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
141fi
142
143swap_size=$((disk_size*swap_ratio/100))
144rootfs_size=$((disk_size-bios_boot_size-boot_size-swap_size))
145
146boot_start=$((bios_boot_size))
147rootfs_start=$((bios_boot_size+boot_size))
148rootfs_end=$((rootfs_start+rootfs_size))
149swap_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)
154rootwait=""
155part_prefix=""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156if [ ! "${device#/dev/mmcblk}" = "${device}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157 part_prefix="p"
158 rootwait="rootwait"
159fi
160
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600161# USB devices also require rootwait
162if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
163 rootwait="rootwait"
164fi
165
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166if [ $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
171else
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
176fi
177
178echo "*****************"
179[ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
180echo "Boot partition size: $boot_size MB ($bootfs)"
181echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
182echo "Swap partition size: $swap_size MB ($swap)"
183echo "*****************"
184echo "Deleting partition table on ${device} ..."
185dd if=/dev/zero of=${device} bs=512 count=35
186
187echo "Creating new partition table on ${device} ..."
188if [ $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
192else
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
199fi
200
201echo "Creating rootfs partition on $rootfs"
202[ $grub_version -eq 0 ] && pname='primary' || pname='root'
203parted ${device} mkpart $pname ext3 $rootfs_start $rootfs_end
204
205echo "Creating swap partition on $swap"
206[ $grub_version -eq 0 ] && pname='primary' || pname='swap'
207parted ${device} mkpart $pname linux-swap $swap_start 100%
208
209parted ${device} print
210
211echo "Formatting $bootfs to ext3..."
212mkfs.ext3 $bootfs
213
214echo "Formatting $rootfs to ext3..."
215mkfs.ext3 $rootfs
216
217echo "Formatting swap partition...($swap)"
218mkswap $swap
219
220mkdir /tgt_root
221mkdir /src_root
222mkdir -p /boot
223
224# Handling of the target root partition
225mount $rootfs /tgt_root
226mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
227echo "Copying rootfs files..."
228cp -a /src_root/* /tgt_root
229if [ -d /tgt_root/etc/ ] ; then
230 if [ $grub_version -ne 0 ] ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500231 boot_uuid=$(blkid -o value -s UUID ${bootfs})
232 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 bootdev="UUID=$boot_uuid"
234 swapdev=/dev/disk/by-partuuid/$swap_part_uuid
235 else
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500236 bootdev=${bootfs}
237 swapdev=${swap}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238 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
245fi
246umount /tgt_root
247umount /src_root
248
249# Handling of the target boot partition
250mount $bootfs /boot
251echo "Preparing boot partition..."
252if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
253 echo "Preparing custom grub2 menu..."
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500254 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
255 boot_uuid=$(blkid -o value -s UUID ${bootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 GRUBCFG="/boot/grub/grub.cfg"
257 mkdir -p $(dirname $GRUBCFG)
258 cat >$GRUBCFG <<_EOF
259menuentry "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
265fi
266grub-install ${device}
267
268if [ $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
276fi
277
278cp /run/media/$1/vmlinuz /boot/
279
280umount /boot
281
282sync
283
284echo "Remove your installation media, and press ENTER"
285
286read enter
287
288echo "Rebooting..."
289reboot -f