blob: 706418fa9c505885f81ed59d945dbbfe17d646b9 [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 ;;
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 nvme*)
26 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 *)
28 live_dev_name=${live_dev_name%%[0-9]*}
29 ;;
30esac
31
32echo "Searching for hard drives ..."
33
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034# Some eMMC devices have special sub devices such as mmcblk0boot0 etc
35# we're currently only interested in the root device so pick them wisely
36devices=`ls /sys/block/ | grep -v mmcblk` || true
37mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
38devices="$devices $mmc_devices"
39
40for device in $devices; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 case $device in
42 loop*)
43 # skip loop device
44 ;;
45 sr*)
46 # skip CDROM device
47 ;;
48 ram*)
49 # skip ram device
50 ;;
51 *)
52 # skip the device LiveOS is on
53 # Add valid hard drive name to the list
54 case $device in
55 $live_dev_name*)
56 # skip the device we are running from
57 ;;
58 *)
59 hdnamelist="$hdnamelist $device"
60 ;;
61 esac
62 ;;
63 esac
64done
65
66if [ -z "${hdnamelist}" ]; then
67 echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted."
68 exit 1
69fi
70
71TARGET_DEVICE_NAME=""
72for hdname in $hdnamelist; do
73 # Display found hard drives and their basic info
74 echo "-------------------------------"
75 echo /dev/$hdname
76 if [ -r /sys/block/$hdname/device/vendor ]; then
77 echo -n "VENDOR="
78 cat /sys/block/$hdname/device/vendor
79 fi
80 if [ -r /sys/block/$hdname/device/model ]; then
81 echo -n "MODEL="
82 cat /sys/block/$hdname/device/model
83 fi
84 if [ -r /sys/block/$hdname/device/uevent ]; then
85 echo -n "UEVENT="
86 cat /sys/block/$hdname/device/uevent
87 fi
88 echo
Patrick Williamsc0f7c042017-02-23 20:41:17 -060089done
90
91# Get user choice
92while true; do
93 echo "Please select an install target or press n to exit ($hdnamelist ): "
94 read answer
95 if [ "$answer" = "n" ]; then
96 echo "Installation manually aborted."
97 exit 1
98 fi
99 for hdname in $hdnamelist; do
100 if [ "$answer" = "$hdname" ]; then
101 TARGET_DEVICE_NAME=$answer
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102 break
103 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 done
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 if [ -n "$TARGET_DEVICE_NAME" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 break
107 fi
108done
109
110if [ -n "$TARGET_DEVICE_NAME" ]; then
111 echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
112else
113 echo "No hard drive selected. Installation aborted."
114 exit 1
115fi
116
117device=/dev/$TARGET_DEVICE_NAME
118
119#
120# The udev automounter can cause pain here, kill it
121#
122rm -f /etc/udev/rules.d/automount.rules
123rm -f /etc/udev/scripts/mount*
124
125#
126# Unmount anything the automounter had mounted
127#
128umount ${device}* 2> /dev/null || /bin/true
129
130mkdir -p /tmp
Patrick Williamsd7e96312015-09-22 08:09:05 -0500131
132# Create /etc/mtab if not present
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600133if [ ! -e /etc/mtab ] && [ -e /proc/mounts ]; then
134 ln -sf /proc/mounts /etc/mtab
Patrick Williamsd7e96312015-09-22 08:09:05 -0500135fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500137disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138
139swap_size=$((disk_size*swap_ratio/100))
140rootfs_size=$((disk_size-boot_size-swap_size))
141
142rootfs_start=$((boot_size))
143rootfs_end=$((rootfs_start+rootfs_size))
144swap_start=$((rootfs_end))
145
146# MMC devices are special in a couple of ways
147# 1) they use a partition prefix character 'p'
148# 2) they are detected asynchronously (need rootwait)
149rootwait=""
150part_prefix=""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
152 [ ! "${device#/dev/nvme}" = "${device}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 part_prefix="p"
154 rootwait="rootwait"
155fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156
157# USB devices also require rootwait
158if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
159 rootwait="rootwait"
160fi
161
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162bootfs=${device}${part_prefix}1
163rootfs=${device}${part_prefix}2
164swap=${device}${part_prefix}3
165
166echo "*****************"
167echo "Boot partition size: $boot_size MB ($bootfs)"
168echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
169echo "Swap partition size: $swap_size MB ($swap)"
170echo "*****************"
171echo "Deleting partition table on ${device} ..."
172dd if=/dev/zero of=${device} bs=512 count=35
173
174echo "Creating new partition table on ${device} ..."
175parted ${device} mklabel gpt
176
177echo "Creating boot partition on $bootfs"
178parted ${device} mkpart boot fat32 0% $boot_size
179parted ${device} set 1 boot on
180
181echo "Creating rootfs partition on $rootfs"
182parted ${device} mkpart root ext3 $rootfs_start $rootfs_end
183
184echo "Creating swap partition on $swap"
185parted ${device} mkpart swap linux-swap $swap_start 100%
186
187parted ${device} print
188
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189echo "Waiting for device nodes..."
190C=0
191while [ $C -ne 3 ] && [ ! -e $bootfs -o ! -e $rootfs -o ! -e $swap ]; do
192 C=$(( C + 1 ))
193 sleep 1
194done
195
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196echo "Formatting $bootfs to vfat..."
197mkfs.vfat $bootfs
198
199echo "Formatting $rootfs to ext3..."
200mkfs.ext3 $rootfs
201
202echo "Formatting swap partition...($swap)"
203mkswap $swap
204
205mkdir /tgt_root
206mkdir /src_root
207mkdir -p /boot
208
209# Handling of the target root partition
210mount $rootfs /tgt_root
211mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
212echo "Copying rootfs files..."
213cp -a /src_root/* /tgt_root
214if [ -d /tgt_root/etc/ ] ; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500215 boot_uuid=$(blkid -o value -s UUID ${bootfs})
216 swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab
218 echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab
219 # We dont want udev to mount our root device while we're booting...
220 if [ -d /tgt_root/etc/udev/ ] ; then
221 echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
222 fi
223fi
224
225umount /src_root
226
227# Handling of the target boot partition
228mount $bootfs /boot
229echo "Preparing boot partition..."
230
231EFIDIR="/boot/EFI/BOOT"
232mkdir -p $EFIDIR
233# Copy the efi loader
234cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
235
236if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500237 root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238 GRUBCFG="$EFIDIR/grub.cfg"
239 cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
240 # Update grub config for the installed image
241 # Delete the install entry
242 sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
243 # Delete the initrd lines
244 sed -i "/initrd /d" $GRUBCFG
245 # Delete any LABEL= strings
246 sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
247 # Delete any root= strings
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500248 sed -i "s/ root=[^ ]*/ /g" $GRUBCFG
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 # Add the root= and other standard boot options
250 sed -i "s@linux /vmlinuz *@linux /vmlinuz root=PARTUUID=$root_part_uuid rw $rootwait quiet @" $GRUBCFG
251fi
252
253if [ -d /run/media/$1/loader ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500254 rootuuid=$(blkid -o value -s PARTUUID ${rootfs})
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 SYSTEMDBOOT_CFGS="/boot/loader/entries/*.conf"
256 # copy config files for systemd-boot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 cp -dr /run/media/$1/loader /boot
258 # delete the install entry
259 rm -f /boot/loader/entries/install.conf
260 # delete the initrd lines
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 sed -i "/initrd /d" $SYSTEMDBOOT_CFGS
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 # delete any LABEL= strings
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500263 sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264 # delete any root= strings
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500265 sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266 # add the root= and other standard boot options
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500267 sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $SYSTEMDBOOT_CFGS
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268fi
269
270umount /tgt_root
271
272cp /run/media/$1/vmlinuz /boot
273
274umount /boot
275
276sync
277
278echo "Remove your installation media, and press ENTER"
279
280read enter
281
282echo "Rebooting..."
283reboot -f