blob: dade059c8f1fc9e06d3323741f284e8ca6060dd5 [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 ;;
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 nvme*)
25 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 *)
27 live_dev_name=${live_dev_name%%[0-9]*}
28 ;;
29esac
30
31echo "Searching for hard drives ..."
32
Patrick Williamsc0f7c042017-02-23 20:41:17 -060033# Some eMMC devices have special sub devices such as mmcblk0boot0 etc
34# we're currently only interested in the root device so pick them wisely
35devices=`ls /sys/block/ | grep -v mmcblk` || true
36mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
37devices="$devices $mmc_devices"
38
39for device in $devices; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040 case $device in
41 loop*)
42 # skip loop device
43 ;;
44 sr*)
45 # skip CDROM device
46 ;;
47 ram*)
48 # skip ram device
49 ;;
50 *)
51 # skip the device LiveOS is on
52 # Add valid hard drive name to the list
53 case $device in
54 $live_dev_name*)
55 # skip the device we are running from
56 ;;
57 *)
58 hdnamelist="$hdnamelist $device"
59 ;;
60 esac
61 ;;
62 esac
63done
64
65TARGET_DEVICE_NAME=""
66for hdname in $hdnamelist; do
67 # Display found hard drives and their basic info
68 echo "-------------------------------"
69 echo /dev/$hdname
70 if [ -r /sys/block/$hdname/device/vendor ]; then
71 echo -n "VENDOR="
72 cat /sys/block/$hdname/device/vendor
73 fi
74 if [ -r /sys/block/$hdname/device/model ]; then
75 echo -n "MODEL="
76 cat /sys/block/$hdname/device/model
77 fi
78 if [ -r /sys/block/$hdname/device/uevent ]; then
79 echo -n "UEVENT="
80 cat /sys/block/$hdname/device/uevent
81 fi
82 echo
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083done
84
85# Get user choice
86while true; do
87 echo "Please select an install target or press n to exit ($hdnamelist ): "
88 read answer
89 if [ "$answer" = "n" ]; then
90 echo "Installation manually aborted."
91 exit 1
92 fi
93 for hdname in $hdnamelist; do
94 if [ "$answer" = "$hdname" ]; then
95 TARGET_DEVICE_NAME=$answer
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 break
97 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 done
Patrick Williamsc0f7c042017-02-23 20:41:17 -060099 if [ -n "$TARGET_DEVICE_NAME" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 break
101 fi
102done
103
104if [ -n "$TARGET_DEVICE_NAME" ]; then
105 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
106else
107 echo "No hard drive selected. Installation aborted."
108 exit 1
109fi
110
111device=/dev/$TARGET_DEVICE_NAME
112
113#
114# The udev automounter can cause pain here, kill it
115#
116rm -f /etc/udev/rules.d/automount.rules
117rm -f /etc/udev/scripts/mount*
118
119#
120# Unmount anything the automounter had mounted
121#
122umount ${device}* 2> /dev/null || /bin/true
123
124if [ ! -b /dev/loop0 ] ; then
125 mknod /dev/loop0 b 7 0
126fi
127
128mkdir -p /tmp
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600129if [ ! -L /etc/mtab ] && [ -e /proc/mounts ]; then
130 ln -sf /proc/mounts /etc/mtab
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131fi
132
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135grub_version=$(grub-install -V|sed 's/.* \([0-9]\).*/\1/')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136
137if [ $grub_version -eq 0 ] ; then
138 bios_boot_size=0
139else
140 # For GRUB 2 we need separate parition to store stage2 grub image
141 # 2Mb value is chosen to align partition for best performance.
142 bios_boot_size=2
143fi
144
145swap_size=$((disk_size*swap_ratio/100))
146rootfs_size=$((disk_size-bios_boot_size-boot_size-swap_size))
147
148boot_start=$((bios_boot_size))
149rootfs_start=$((bios_boot_size+boot_size))
150rootfs_end=$((rootfs_start+rootfs_size))
151swap_start=$((rootfs_end))
152
153# MMC devices are special in a couple of ways
154# 1) they use a partition prefix character 'p'
155# 2) they are detected asynchronously (need rootwait)
156rootwait=""
157part_prefix=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
159 [ ! "${device#/dev/nvme}" = "${device}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 part_prefix="p"
161 rootwait="rootwait"
162fi
163
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600164# USB devices also require rootwait
165if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
166 rootwait="rootwait"
167fi
168
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169if [ $grub_version -eq 0 ] ; then
170 bios_boot=''
171 bootfs=${device}${part_prefix}1
172 rootfs=${device}${part_prefix}2
173 swap=${device}${part_prefix}3
174else
175 bios_boot=${device}${part_prefix}1
176 bootfs=${device}${part_prefix}2
177 rootfs=${device}${part_prefix}3
178 swap=${device}${part_prefix}4
179fi
180
181echo "*****************"
182[ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
183echo "Boot partition size: $boot_size MB ($bootfs)"
184echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
185echo "Swap partition size: $swap_size MB ($swap)"
186echo "*****************"
187echo "Deleting partition table on ${device} ..."
188dd if=/dev/zero of=${device} bs=512 count=35
189
190echo "Creating new partition table on ${device} ..."
191if [ $grub_version -eq 0 ] ; then
192 parted ${device} mktable msdos
193 echo "Creating boot partition on $bootfs"
194 parted ${device} mkpart primary ext3 0% $boot_size
195else
196 parted ${device} mktable gpt
197 echo "Creating BIOS boot partition on $bios_boot"
198 parted ${device} mkpart bios_boot 0% $bios_boot_size
199 parted ${device} set 1 bios_grub on
200 echo "Creating boot partition on $bootfs"
201 parted ${device} mkpart boot ext3 $boot_start $boot_size
202fi
203
204echo "Creating rootfs partition on $rootfs"
205[ $grub_version -eq 0 ] && pname='primary' || pname='root'
206parted ${device} mkpart $pname ext3 $rootfs_start $rootfs_end
207
208echo "Creating swap partition on $swap"
209[ $grub_version -eq 0 ] && pname='primary' || pname='swap'
210parted ${device} mkpart $pname linux-swap $swap_start 100%
211
212parted ${device} print
213
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500214echo "Waiting for device nodes..."
215C=0
216while [ $C -ne 3 ] && [ ! -e $bootfs -o ! -e $rootfs -o ! -e $swap ]; do
217 C=$(( C + 1 ))
218 sleep 1
219done
220
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221echo "Formatting $bootfs to ext3..."
222mkfs.ext3 $bootfs
223
224echo "Formatting $rootfs to ext3..."
225mkfs.ext3 $rootfs
226
227echo "Formatting swap partition...($swap)"
228mkswap $swap
229
230mkdir /tgt_root
231mkdir /src_root
232mkdir -p /boot
233
234# Handling of the target root partition
235mount $rootfs /tgt_root
236mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
237echo "Copying rootfs files..."
238cp -a /src_root/* /tgt_root
239if [ -d /tgt_root/etc/ ] ; then
240 if [ $grub_version -ne 0 ] ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500241 boot_uuid=$(blkid -o value -s UUID ${bootfs})
242 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243 bootdev="UUID=$boot_uuid"
244 swapdev=/dev/disk/by-partuuid/$swap_part_uuid
245 else
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500246 bootdev=${bootfs}
247 swapdev=${swap}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 fi
249 echo "$swapdev swap swap defaults 0 0" >> /tgt_root/etc/fstab
250 echo "$bootdev /boot ext3 defaults 1 2" >> /tgt_root/etc/fstab
251 # We dont want udev to mount our root device while we're booting...
252 if [ -d /tgt_root/etc/udev/ ] ; then
253 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
254 fi
255fi
256umount /tgt_root
257umount /src_root
258
259# Handling of the target boot partition
260mount $bootfs /boot
261echo "Preparing boot partition..."
262if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
263 echo "Preparing custom grub2 menu..."
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500264 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
265 boot_uuid=$(blkid -o value -s UUID ${bootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266 GRUBCFG="/boot/grub/grub.cfg"
267 mkdir -p $(dirname $GRUBCFG)
268 cat >$GRUBCFG <<_EOF
269menuentry "Linux" {
270 search --no-floppy --fs-uuid $boot_uuid --set root
271 linux /vmlinuz root=PARTUUID=$root_part_uuid $rootwait rw $5 $3 $4 quiet
272}
273_EOF
274 chmod 0444 $GRUBCFG
275fi
276grub-install ${device}
277
278if [ $grub_version -eq 0 ] ; then
279 echo "(hd0) ${device}" > /boot/grub/device.map
280 echo "Preparing custom grub menu..."
281 echo "default 0" > /boot/grub/menu.lst
282 echo "timeout 30" >> /boot/grub/menu.lst
283 echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst
284 echo "root (hd0,0)" >> /boot/grub/menu.lst
285 echo "kernel /vmlinuz root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst
286fi
287
288cp /run/media/$1/vmlinuz /boot/
289
290umount /boot
291
292sync
293
294echo "Remove your installation media, and press ENTER"
295
296read enter
297
298echo "Rebooting..."
299reboot -f