blob: df33791ec7a85012cc2c40a4ea9c91822bcf2a14 [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
Brad Bishop316dfdd2018-06-25 12:45:53 -040010# figure out how big of a boot partition we need
11boot_size=$(du -ms /run/media/$1/ | awk '{print $1}')
12# remove rootfs.img ($2) from the size if it exists, as its not installed to /boot
13if [ -e /run/media/$1/$2 ]; then
14 boot_size=$(( boot_size - $( du -ms /run/media/$1/$2 | awk '{print $1}') ))
15fi
16# remove initrd from size since its not currently installed
17if [ -e /run/media/$1/initrd ]; then
18 boot_size=$(( boot_size - $( du -ms /run/media/$1/initrd | awk '{print $1}') ))
19fi
20# add 10M to provide some extra space for users and account
21# for rounding in the above subtractions
22boot_size=$(( boot_size + 10 ))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023
24# 5% for the swap
25swap_ratio=5
26
27# Get a list of hard drives
28hdnamelist=""
29live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
30live_dev_name=${live_dev_name#\/dev/}
31# Only strip the digit identifier if the device is not an mmc
32case $live_dev_name in
33 mmcblk*)
34 ;;
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035 nvme*)
36 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 *)
38 live_dev_name=${live_dev_name%%[0-9]*}
39 ;;
40esac
41
42echo "Searching for hard drives ..."
43
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044# Some eMMC devices have special sub devices such as mmcblk0boot0 etc
45# we're currently only interested in the root device so pick them wisely
46devices=`ls /sys/block/ | grep -v mmcblk` || true
47mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
48devices="$devices $mmc_devices"
49
50for device in $devices; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 case $device in
52 loop*)
53 # skip loop device
54 ;;
55 sr*)
56 # skip CDROM device
57 ;;
58 ram*)
59 # skip ram device
60 ;;
61 *)
62 # skip the device LiveOS is on
63 # Add valid hard drive name to the list
64 case $device in
65 $live_dev_name*)
66 # skip the device we are running from
67 ;;
68 *)
69 hdnamelist="$hdnamelist $device"
70 ;;
71 esac
72 ;;
73 esac
74done
75
76TARGET_DEVICE_NAME=""
77for hdname in $hdnamelist; do
78 # Display found hard drives and their basic info
79 echo "-------------------------------"
80 echo /dev/$hdname
81 if [ -r /sys/block/$hdname/device/vendor ]; then
82 echo -n "VENDOR="
83 cat /sys/block/$hdname/device/vendor
84 fi
85 if [ -r /sys/block/$hdname/device/model ]; then
86 echo -n "MODEL="
87 cat /sys/block/$hdname/device/model
88 fi
89 if [ -r /sys/block/$hdname/device/uevent ]; then
90 echo -n "UEVENT="
91 cat /sys/block/$hdname/device/uevent
92 fi
93 echo
Patrick Williamsc0f7c042017-02-23 20:41:17 -060094done
95
96# Get user choice
97while true; do
98 echo "Please select an install target or press n to exit ($hdnamelist ): "
99 read answer
100 if [ "$answer" = "n" ]; then
101 echo "Installation manually aborted."
102 exit 1
103 fi
104 for hdname in $hdnamelist; do
105 if [ "$answer" = "$hdname" ]; then
106 TARGET_DEVICE_NAME=$answer
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 break
108 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 done
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600110 if [ -n "$TARGET_DEVICE_NAME" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 break
112 fi
113done
114
115if [ -n "$TARGET_DEVICE_NAME" ]; then
116 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
117else
118 echo "No hard drive selected. Installation aborted."
119 exit 1
120fi
121
122device=/dev/$TARGET_DEVICE_NAME
123
124#
125# The udev automounter can cause pain here, kill it
126#
127rm -f /etc/udev/rules.d/automount.rules
128rm -f /etc/udev/scripts/mount*
129
130#
131# Unmount anything the automounter had mounted
132#
133umount ${device}* 2> /dev/null || /bin/true
134
135if [ ! -b /dev/loop0 ] ; then
136 mknod /dev/loop0 b 7 0
137fi
138
139mkdir -p /tmp
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600140if [ ! -L /etc/mtab ] && [ -e /proc/mounts ]; then
141 ln -sf /proc/mounts /etc/mtab
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142fi
143
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500146grub_version=$(grub-install -V|sed 's/.* \([0-9]\).*/\1/')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148if [ $grub_version -eq 0 ] ; then
149 bios_boot_size=0
150else
151 # For GRUB 2 we need separate parition to store stage2 grub image
152 # 2Mb value is chosen to align partition for best performance.
153 bios_boot_size=2
154fi
155
156swap_size=$((disk_size*swap_ratio/100))
157rootfs_size=$((disk_size-bios_boot_size-boot_size-swap_size))
158
159boot_start=$((bios_boot_size))
160rootfs_start=$((bios_boot_size+boot_size))
161rootfs_end=$((rootfs_start+rootfs_size))
162swap_start=$((rootfs_end))
163
164# MMC devices are special in a couple of ways
165# 1) they use a partition prefix character 'p'
166# 2) they are detected asynchronously (need rootwait)
167rootwait=""
168part_prefix=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
170 [ ! "${device#/dev/nvme}" = "${device}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 part_prefix="p"
172 rootwait="rootwait"
173fi
174
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600175# USB devices also require rootwait
176if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
177 rootwait="rootwait"
178fi
179
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180if [ $grub_version -eq 0 ] ; then
181 bios_boot=''
182 bootfs=${device}${part_prefix}1
183 rootfs=${device}${part_prefix}2
184 swap=${device}${part_prefix}3
185else
186 bios_boot=${device}${part_prefix}1
187 bootfs=${device}${part_prefix}2
188 rootfs=${device}${part_prefix}3
189 swap=${device}${part_prefix}4
190fi
191
192echo "*****************"
193[ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
194echo "Boot partition size: $boot_size MB ($bootfs)"
195echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
196echo "Swap partition size: $swap_size MB ($swap)"
197echo "*****************"
198echo "Deleting partition table on ${device} ..."
199dd if=/dev/zero of=${device} bs=512 count=35
200
201echo "Creating new partition table on ${device} ..."
202if [ $grub_version -eq 0 ] ; then
203 parted ${device} mktable msdos
204 echo "Creating boot partition on $bootfs"
205 parted ${device} mkpart primary ext3 0% $boot_size
206else
207 parted ${device} mktable gpt
208 echo "Creating BIOS boot partition on $bios_boot"
209 parted ${device} mkpart bios_boot 0% $bios_boot_size
210 parted ${device} set 1 bios_grub on
211 echo "Creating boot partition on $bootfs"
212 parted ${device} mkpart boot ext3 $boot_start $boot_size
213fi
214
215echo "Creating rootfs partition on $rootfs"
216[ $grub_version -eq 0 ] && pname='primary' || pname='root'
Brad Bishop316dfdd2018-06-25 12:45:53 -0400217parted ${device} mkpart $pname ext4 $rootfs_start $rootfs_end
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218
219echo "Creating swap partition on $swap"
220[ $grub_version -eq 0 ] && pname='primary' || pname='swap'
221parted ${device} mkpart $pname linux-swap $swap_start 100%
222
223parted ${device} print
224
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500225echo "Waiting for device nodes..."
226C=0
227while [ $C -ne 3 ] && [ ! -e $bootfs -o ! -e $rootfs -o ! -e $swap ]; do
228 C=$(( C + 1 ))
229 sleep 1
230done
231
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232echo "Formatting $bootfs to ext3..."
233mkfs.ext3 $bootfs
234
Brad Bishop316dfdd2018-06-25 12:45:53 -0400235echo "Formatting $rootfs to ext4..."
236mkfs.ext4 $rootfs
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237
238echo "Formatting swap partition...($swap)"
239mkswap $swap
240
241mkdir /tgt_root
242mkdir /src_root
243mkdir -p /boot
244
245# Handling of the target root partition
246mount $rootfs /tgt_root
247mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
248echo "Copying rootfs files..."
249cp -a /src_root/* /tgt_root
250if [ -d /tgt_root/etc/ ] ; then
251 if [ $grub_version -ne 0 ] ; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500252 boot_uuid=$(blkid -o value -s UUID ${bootfs})
253 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254 bootdev="UUID=$boot_uuid"
255 swapdev=/dev/disk/by-partuuid/$swap_part_uuid
256 else
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500257 bootdev=${bootfs}
258 swapdev=${swap}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 fi
260 echo "$swapdev swap swap defaults 0 0" >> /tgt_root/etc/fstab
261 echo "$bootdev /boot ext3 defaults 1 2" >> /tgt_root/etc/fstab
262 # We dont want udev to mount our root device while we're booting...
263 if [ -d /tgt_root/etc/udev/ ] ; then
Andrew Geisslerd5838332022-05-27 11:33:10 -0500264 echo "${device}" >> /tgt_root/etc/udev/mount.ignorelist
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265 fi
266fi
267umount /tgt_root
268umount /src_root
269
Brad Bishop316dfdd2018-06-25 12:45:53 -0400270echo "Looking for kernels to use as boot target.."
271# Find kernel to boot to
272# Give user options if multiple are found
273kernels="$(find /run/media/$1/ -type f \
274 -name bzImage* -o -name zImage* \
275 -o -name vmlinux* -o -name vmlinuz* \
276 -o -name fitImage* \
277 | sed s:.*/::)"
278if [ -n "$(echo $kernels)" ]; then
279 # only one kernel entry if no space
280 if [ -z "$(echo $kernels | grep " ")" ]; then
281 kernel=$kernels
282 echo "$kernel will be used as the boot target"
283 else
284 echo "Which kernel do we want to boot by default? The following kernels were found:"
285 echo $kernels
286 read answer
287 kernel=$answer
288 fi
289else
290 echo "No kernels found, exiting..."
291 exit 1
292fi
293
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500294# Handling of the target boot partition
295mount $bootfs /boot
296echo "Preparing boot partition..."
Brad Bishop316dfdd2018-06-25 12:45:53 -0400297
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
299 echo "Preparing custom grub2 menu..."
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500300 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
301 boot_uuid=$(blkid -o value -s UUID ${bootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302 GRUBCFG="/boot/grub/grub.cfg"
303 mkdir -p $(dirname $GRUBCFG)
304 cat >$GRUBCFG <<_EOF
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700305timeout=5
306default=0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307menuentry "Linux" {
308 search --no-floppy --fs-uuid $boot_uuid --set root
Brad Bishop316dfdd2018-06-25 12:45:53 -0400309 linux /$kernel root=PARTUUID=$root_part_uuid $rootwait rw $5 $3 $4 quiet
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310}
311_EOF
312 chmod 0444 $GRUBCFG
313fi
314grub-install ${device}
315
316if [ $grub_version -eq 0 ] ; then
317 echo "(hd0) ${device}" > /boot/grub/device.map
318 echo "Preparing custom grub menu..."
319 echo "default 0" > /boot/grub/menu.lst
320 echo "timeout 30" >> /boot/grub/menu.lst
321 echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst
322 echo "root (hd0,0)" >> /boot/grub/menu.lst
Brad Bishop316dfdd2018-06-25 12:45:53 -0400323 echo "kernel /$kernel root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324fi
325
Brad Bishop316dfdd2018-06-25 12:45:53 -0400326# Copy kernel artifacts. To add more artifacts just add to types
327# For now just support kernel types already being used by something in OE-core
328for types in bzImage zImage vmlinux vmlinuz fitImage; do
329 for kernel in `find /run/media/$1/ -name $types*`; do
330 cp $kernel /boot
331 done
332done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333
334umount /boot
335
336sync
337
338echo "Remove your installation media, and press ENTER"
339
340read enter
341
342echo "Rebooting..."
343reboot -f