blob: c5623eeb26724846500de7b95332d5da52c0af7f [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
31for device in `ls /sys/block/`; do
32 case $device in
33 loop*)
34 # skip loop device
35 ;;
36 sr*)
37 # skip CDROM device
38 ;;
39 ram*)
40 # skip ram device
41 ;;
42 *)
43 # skip the device LiveOS is on
44 # Add valid hard drive name to the list
45 case $device in
46 $live_dev_name*)
47 # skip the device we are running from
48 ;;
49 *)
50 hdnamelist="$hdnamelist $device"
51 ;;
52 esac
53 ;;
54 esac
55done
56
57TARGET_DEVICE_NAME=""
58for hdname in $hdnamelist; do
59 # Display found hard drives and their basic info
60 echo "-------------------------------"
61 echo /dev/$hdname
62 if [ -r /sys/block/$hdname/device/vendor ]; then
63 echo -n "VENDOR="
64 cat /sys/block/$hdname/device/vendor
65 fi
66 if [ -r /sys/block/$hdname/device/model ]; then
67 echo -n "MODEL="
68 cat /sys/block/$hdname/device/model
69 fi
70 if [ -r /sys/block/$hdname/device/uevent ]; then
71 echo -n "UEVENT="
72 cat /sys/block/$hdname/device/uevent
73 fi
74 echo
75 # Get user choice
76 while true; do
77 echo -n "Do you want to install this image there? [y/n] "
78 read answer
79 if [ "$answer" = "y" -o "$answer" = "n" ]; then
80 break
81 fi
82 echo "Please answer y or n"
83 done
84 if [ "$answer" = "y" ]; then
85 TARGET_DEVICE_NAME=$hdname
86 break
87 fi
88done
89
90if [ -n "$TARGET_DEVICE_NAME" ]; then
91 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
92else
93 echo "No hard drive selected. Installation aborted."
94 exit 1
95fi
96
97device=/dev/$TARGET_DEVICE_NAME
98
99#
100# The udev automounter can cause pain here, kill it
101#
102rm -f /etc/udev/rules.d/automount.rules
103rm -f /etc/udev/scripts/mount*
104
105#
106# Unmount anything the automounter had mounted
107#
108umount ${device}* 2> /dev/null || /bin/true
109
110if [ ! -b /dev/loop0 ] ; then
111 mknod /dev/loop0 b 7 0
112fi
113
114mkdir -p /tmp
115if [ ! -L /etc/mtab ]; then
116 cat /proc/mounts > /etc/mtab
117fi
118
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120
121grub_version=$(grub-install -v|sed 's/.* \([0-9]\).*/\1/')
122
123if [ $grub_version -eq 0 ] ; then
124 bios_boot_size=0
125else
126 # For GRUB 2 we need separate parition to store stage2 grub image
127 # 2Mb value is chosen to align partition for best performance.
128 bios_boot_size=2
129fi
130
131swap_size=$((disk_size*swap_ratio/100))
132rootfs_size=$((disk_size-bios_boot_size-boot_size-swap_size))
133
134boot_start=$((bios_boot_size))
135rootfs_start=$((bios_boot_size+boot_size))
136rootfs_end=$((rootfs_start+rootfs_size))
137swap_start=$((rootfs_end))
138
139# MMC devices are special in a couple of ways
140# 1) they use a partition prefix character 'p'
141# 2) they are detected asynchronously (need rootwait)
142rootwait=""
143part_prefix=""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144if [ ! "${device#/dev/mmcblk}" = "${device}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 part_prefix="p"
146 rootwait="rootwait"
147fi
148
149if [ $grub_version -eq 0 ] ; then
150 bios_boot=''
151 bootfs=${device}${part_prefix}1
152 rootfs=${device}${part_prefix}2
153 swap=${device}${part_prefix}3
154else
155 bios_boot=${device}${part_prefix}1
156 bootfs=${device}${part_prefix}2
157 rootfs=${device}${part_prefix}3
158 swap=${device}${part_prefix}4
159fi
160
161echo "*****************"
162[ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
163echo "Boot partition size: $boot_size MB ($bootfs)"
164echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
165echo "Swap partition size: $swap_size MB ($swap)"
166echo "*****************"
167echo "Deleting partition table on ${device} ..."
168dd if=/dev/zero of=${device} bs=512 count=35
169
170echo "Creating new partition table on ${device} ..."
171if [ $grub_version -eq 0 ] ; then
172 parted ${device} mktable msdos
173 echo "Creating boot partition on $bootfs"
174 parted ${device} mkpart primary ext3 0% $boot_size
175else
176 parted ${device} mktable gpt
177 echo "Creating BIOS boot partition on $bios_boot"
178 parted ${device} mkpart bios_boot 0% $bios_boot_size
179 parted ${device} set 1 bios_grub on
180 echo "Creating boot partition on $bootfs"
181 parted ${device} mkpart boot ext3 $boot_start $boot_size
182fi
183
184echo "Creating rootfs partition on $rootfs"
185[ $grub_version -eq 0 ] && pname='primary' || pname='root'
186parted ${device} mkpart $pname ext3 $rootfs_start $rootfs_end
187
188echo "Creating swap partition on $swap"
189[ $grub_version -eq 0 ] && pname='primary' || pname='swap'
190parted ${device} mkpart $pname linux-swap $swap_start 100%
191
192parted ${device} print
193
194echo "Formatting $bootfs to ext3..."
195mkfs.ext3 $bootfs
196
197echo "Formatting $rootfs to ext3..."
198mkfs.ext3 $rootfs
199
200echo "Formatting swap partition...($swap)"
201mkswap $swap
202
203mkdir /tgt_root
204mkdir /src_root
205mkdir -p /boot
206
207# Handling of the target root partition
208mount $rootfs /tgt_root
209mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
210echo "Copying rootfs files..."
211cp -a /src_root/* /tgt_root
212if [ -d /tgt_root/etc/ ] ; then
213 if [ $grub_version -ne 0 ] ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500214 boot_uuid=$(blkid -o value -s UUID ${bootfs})
215 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216 bootdev="UUID=$boot_uuid"
217 swapdev=/dev/disk/by-partuuid/$swap_part_uuid
218 else
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500219 bootdev=${bootfs}
220 swapdev=${swap}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 fi
222 echo "$swapdev swap swap defaults 0 0" >> /tgt_root/etc/fstab
223 echo "$bootdev /boot ext3 defaults 1 2" >> /tgt_root/etc/fstab
224 # We dont want udev to mount our root device while we're booting...
225 if [ -d /tgt_root/etc/udev/ ] ; then
226 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
227 fi
228fi
229umount /tgt_root
230umount /src_root
231
232# Handling of the target boot partition
233mount $bootfs /boot
234echo "Preparing boot partition..."
235if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
236 echo "Preparing custom grub2 menu..."
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
238 boot_uuid=$(blkid -o value -s UUID ${bootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239 GRUBCFG="/boot/grub/grub.cfg"
240 mkdir -p $(dirname $GRUBCFG)
241 cat >$GRUBCFG <<_EOF
242menuentry "Linux" {
243 search --no-floppy --fs-uuid $boot_uuid --set root
244 linux /vmlinuz root=PARTUUID=$root_part_uuid $rootwait rw $5 $3 $4 quiet
245}
246_EOF
247 chmod 0444 $GRUBCFG
248fi
249grub-install ${device}
250
251if [ $grub_version -eq 0 ] ; then
252 echo "(hd0) ${device}" > /boot/grub/device.map
253 echo "Preparing custom grub menu..."
254 echo "default 0" > /boot/grub/menu.lst
255 echo "timeout 30" >> /boot/grub/menu.lst
256 echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst
257 echo "root (hd0,0)" >> /boot/grub/menu.lst
258 echo "kernel /vmlinuz root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst
259fi
260
261cp /run/media/$1/vmlinuz /boot/
262
263umount /boot
264
265sync
266
267echo "Remove your installation media, and press ENTER"
268
269read enter
270
271echo "Rebooting..."
272reboot -f