diff options
Diffstat (limited to 'common/recipes-core/initrdscripts/files/intel-x86-common/init-install-efi.sh')
-rw-r--r-- | common/recipes-core/initrdscripts/files/intel-x86-common/init-install-efi.sh | 339 |
1 files changed, 339 insertions, 0 deletions
diff --git a/common/recipes-core/initrdscripts/files/intel-x86-common/init-install-efi.sh b/common/recipes-core/initrdscripts/files/intel-x86-common/init-install-efi.sh new file mode 100644 index 00000000..71480055 --- /dev/null +++ b/common/recipes-core/initrdscripts/files/intel-x86-common/init-install-efi.sh | |||
@@ -0,0 +1,339 @@ | |||
1 | #!/bin/sh -e | ||
2 | # | ||
3 | # Copyright (c) 2016, Intel Corporation. | ||
4 | # All rights reserved. | ||
5 | # | ||
6 | # install.sh [device_name] [rootfs_name] | ||
7 | # | ||
8 | # This file is a copy of file with same name in OE: | ||
9 | # meta/recipes-core/initrdscripts/files/. We modify | ||
10 | # it for RMC feature to deploy file blobs from RMC | ||
11 | # database file to target. | ||
12 | |||
13 | PATH=/sbin:/bin:/usr/sbin:/usr/bin | ||
14 | |||
15 | # We need 20 Mb for the boot partition | ||
16 | boot_size=20 | ||
17 | |||
18 | # 5% for swap | ||
19 | swap_ratio=5 | ||
20 | |||
21 | # Get a list of hard drives | ||
22 | hdnamelist="" | ||
23 | live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'` | ||
24 | live_dev_name=${live_dev_name#\/dev/} | ||
25 | # Only strip the digit identifier if the device is not an mmc | ||
26 | case $live_dev_name in | ||
27 | mmcblk*) | ||
28 | ;; | ||
29 | *) | ||
30 | live_dev_name=${live_dev_name%%[0-9]*} | ||
31 | ;; | ||
32 | esac | ||
33 | |||
34 | echo "Searching for hard drives ..." | ||
35 | |||
36 | for device in `ls /sys/block/`; do | ||
37 | case $device in | ||
38 | loop*) | ||
39 | # skip loop device | ||
40 | ;; | ||
41 | sr*) | ||
42 | # skip CDROM device | ||
43 | ;; | ||
44 | ram*) | ||
45 | # skip ram device | ||
46 | ;; | ||
47 | *) | ||
48 | # skip the device LiveOS is on | ||
49 | # Add valid hard drive name to the list | ||
50 | case $device in | ||
51 | $live_dev_name*) | ||
52 | # skip the device we are running from | ||
53 | ;; | ||
54 | *) | ||
55 | hdnamelist="$hdnamelist $device" | ||
56 | ;; | ||
57 | esac | ||
58 | ;; | ||
59 | esac | ||
60 | done | ||
61 | |||
62 | if [ -z "${hdnamelist}" ]; then | ||
63 | echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted." | ||
64 | exit 1 | ||
65 | fi | ||
66 | |||
67 | TARGET_DEVICE_NAME="" | ||
68 | for hdname in $hdnamelist; do | ||
69 | # Display found hard drives and their basic info | ||
70 | echo "-------------------------------" | ||
71 | echo /dev/$hdname | ||
72 | if [ -r /sys/block/$hdname/device/vendor ]; then | ||
73 | echo -n "VENDOR=" | ||
74 | cat /sys/block/$hdname/device/vendor | ||
75 | fi | ||
76 | if [ -r /sys/block/$hdname/device/model ]; then | ||
77 | echo -n "MODEL=" | ||
78 | cat /sys/block/$hdname/device/model | ||
79 | fi | ||
80 | if [ -r /sys/block/$hdname/device/uevent ]; then | ||
81 | echo -n "UEVENT=" | ||
82 | cat /sys/block/$hdname/device/uevent | ||
83 | fi | ||
84 | echo | ||
85 | done | ||
86 | |||
87 | # Get user choice | ||
88 | while true; do | ||
89 | echo "Please select an install target or press n to exit ($hdnamelist ): " | ||
90 | read answer | ||
91 | if [ "$answer" = "n" ]; then | ||
92 | echo "Installation manually aborted." | ||
93 | exit 1 | ||
94 | fi | ||
95 | for hdname in $hdnamelist; do | ||
96 | if [ "$answer" = "$hdname" ]; then | ||
97 | TARGET_DEVICE_NAME=$answer | ||
98 | break | ||
99 | fi | ||
100 | done | ||
101 | if [ -n "$TARGET_DEVICE_NAME" ]; then | ||
102 | break | ||
103 | fi | ||
104 | done | ||
105 | |||
106 | if [ -n "$TARGET_DEVICE_NAME" ]; then | ||
107 | echo "Installing image on /dev/$TARGET_DEVICE_NAME ..." | ||
108 | else | ||
109 | echo "No hard drive selected. Installation aborted." | ||
110 | exit 1 | ||
111 | fi | ||
112 | |||
113 | device=/dev/$TARGET_DEVICE_NAME | ||
114 | |||
115 | # | ||
116 | # The udev automounter can cause pain here, kill it | ||
117 | # | ||
118 | rm -f /etc/udev/rules.d/automount.rules | ||
119 | rm -f /etc/udev/scripts/mount* | ||
120 | |||
121 | # | ||
122 | # Unmount anything the automounter had mounted | ||
123 | # | ||
124 | umount ${device}* 2> /dev/null || /bin/true | ||
125 | |||
126 | mkdir -p /tmp | ||
127 | |||
128 | # Create /etc/mtab if not present | ||
129 | if [ ! -e /etc/mtab ]; then | ||
130 | cat /proc/mounts > /etc/mtab | ||
131 | fi | ||
132 | |||
133 | disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//") | ||
134 | |||
135 | swap_size=$((disk_size*swap_ratio/100)) | ||
136 | rootfs_size=$((disk_size-boot_size-swap_size)) | ||
137 | |||
138 | rootfs_start=$((boot_size)) | ||
139 | rootfs_end=$((rootfs_start+rootfs_size)) | ||
140 | swap_start=$((rootfs_end)) | ||
141 | |||
142 | # MMC devices are special in a couple of ways | ||
143 | # 1) they use a partition prefix character 'p' | ||
144 | # 2) they are detected asynchronously (need rootwait) | ||
145 | rootwait="" | ||
146 | part_prefix="" | ||
147 | if [ ! "${device#/dev/mmcblk}" = "${device}" ]; then | ||
148 | part_prefix="p" | ||
149 | rootwait="rootwait" | ||
150 | fi | ||
151 | bootfs=${device}${part_prefix}1 | ||
152 | rootfs=${device}${part_prefix}2 | ||
153 | swap=${device}${part_prefix}3 | ||
154 | |||
155 | echo "*****************" | ||
156 | echo "Boot partition size: $boot_size MB ($bootfs)" | ||
157 | echo "Rootfs partition size: $rootfs_size MB ($rootfs)" | ||
158 | echo "Swap partition size: $swap_size MB ($swap)" | ||
159 | echo "*****************" | ||
160 | echo "Deleting partition table on ${device} ..." | ||
161 | dd if=/dev/zero of=${device} bs=512 count=35 | ||
162 | |||
163 | echo "Creating new partition table on ${device} ..." | ||
164 | parted ${device} mklabel gpt | ||
165 | |||
166 | echo "Creating boot partition on $bootfs" | ||
167 | parted ${device} mkpart boot fat32 0% $boot_size | ||
168 | parted ${device} set 1 boot on | ||
169 | |||
170 | echo "Creating rootfs partition on $rootfs" | ||
171 | parted ${device} mkpart root ext3 $rootfs_start $rootfs_end | ||
172 | |||
173 | echo "Creating swap partition on $swap" | ||
174 | parted ${device} mkpart swap linux-swap $swap_start 100% | ||
175 | |||
176 | parted ${device} print | ||
177 | |||
178 | echo "Formatting $bootfs to vfat..." | ||
179 | mkfs.vfat $bootfs | ||
180 | |||
181 | echo "Formatting $rootfs to ext3..." | ||
182 | mkfs.ext3 $rootfs | ||
183 | |||
184 | echo "Formatting swap partition...($swap)" | ||
185 | mkswap $swap | ||
186 | |||
187 | mkdir /tgt_root | ||
188 | mkdir /src_root | ||
189 | mkdir -p /boot | ||
190 | |||
191 | # Handling of the target root partition | ||
192 | mount $rootfs /tgt_root | ||
193 | mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root | ||
194 | echo "Copying rootfs files..." | ||
195 | cp -a /src_root/* /tgt_root | ||
196 | if [ -d /tgt_root/etc/ ] ; then | ||
197 | boot_uuid=$(blkid -o value -s UUID ${bootfs}) | ||
198 | swap_part_uuid=$(blkid -o value -s PARTUUID ${swap}) | ||
199 | echo "/dev/disk/by-partuuid/$swap_part_uuid swap swap defaults 0 0" >> /tgt_root/etc/fstab | ||
200 | echo "UUID=$boot_uuid /boot vfat defaults 1 2" >> /tgt_root/etc/fstab | ||
201 | # We dont want udev to mount our root device while we're booting... | ||
202 | if [ -d /tgt_root/etc/udev/ ] ; then | ||
203 | echo "${device}" >> /tgt_root/etc/udev/mount.blacklist | ||
204 | fi | ||
205 | fi | ||
206 | |||
207 | # Handling of the target boot partition | ||
208 | mount $bootfs /boot | ||
209 | echo "Preparing boot partition..." | ||
210 | |||
211 | EFIDIR="/boot/EFI/BOOT" | ||
212 | mkdir -p $EFIDIR | ||
213 | # Copy the efi loader | ||
214 | cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR | ||
215 | |||
216 | # RMC deployment | ||
217 | RMC_CMD=/src_root/usr/bin/rmc | ||
218 | RMC_DB=/run/media/$1/rmc.db | ||
219 | |||
220 | # We don't want to quit when a step failed. For example, | ||
221 | # a file system could not support some operations. | ||
222 | set +e | ||
223 | |||
224 | if [ -f "${RMC_DB}" ] && [ -f "${RMC_CMD}" ]; then | ||
225 | echo "Found RMC database and tool, start RMC deployment" | ||
226 | # query INSTALLER.CONFIG from RMC DB | ||
227 | if ${RMC_CMD} -B INSTALLER.CONFIG -d "${RMC_DB}" -o /tmp/installer.config; then | ||
228 | while IFS=':' read -r NAME TGT_UID TGT_GID TGT_MODE TGT_PATH; do | ||
229 | # skip comment | ||
230 | # The regexp in grep works with busybox grep which doesn't | ||
231 | # seem to have a -P to recognize '\t'. But this expression could not | ||
232 | # work with gnu grep... | ||
233 | if echo "$NAME"|grep -q $'^[ \t]*#'; then | ||
234 | continue | ||
235 | fi | ||
236 | # check if we should create a directory (last char in target path is '/') | ||
237 | # or deploy a file | ||
238 | LAST_CHAR=$(echo "${TGT_PATH:$((${#TGT_PATH}-1)):1}") | ||
239 | |||
240 | # Do not bail out for failures but user should get stderr message | ||
241 | if [ ${LAST_CHAR} = "/" ]; then | ||
242 | # name field is skipped for directory | ||
243 | echo "DIR: ${TGT_UID}:${TGT_GID}:${TGT_MODE} => ${TGT_PATH}" | ||
244 | mkdir -p "$TGT_PATH" | ||
245 | chown "${TGT_UID}:${TGT_GID}" "$TGT_PATH" | ||
246 | chmod "${TGT_MODE}" "$TGT_PATH" | ||
247 | else | ||
248 | ${RMC_CMD} -B "${NAME}" -d "${RMC_DB}" -o "${TGT_PATH}" | ||
249 | echo "FILE: ${NAME}:${TGT_UID}:${TGT_GID}:${TGT_MODE} => ${TGT_PATH}" | ||
250 | chown "${TGT_UID}:${TGT_GID}" "$TGT_PATH" | ||
251 | chmod "${TGT_MODE}" "$TGT_PATH" | ||
252 | fi | ||
253 | done < /tmp/installer.config | ||
254 | rm -rf /tmp/installer.config | ||
255 | |||
256 | # remove rmc from target since we don't think it is a valid | ||
257 | # case to run rmc after installation. | ||
258 | rm -rf /tgt_root/usr/bin/rmc | ||
259 | echo "RMC deployment finished" | ||
260 | else | ||
261 | echo "INSTALLER.CONFIG is not found, skip RMC deployment" | ||
262 | fi | ||
263 | |||
264 | # Final retouching by calling post-install hook | ||
265 | if ${RMC_CMD} -B POSTINSTALL.sh -d "${RMC_DB}" -o /tmp/POSTINSTALL.sh; then | ||
266 | echo "Found POSTINSTALL.sh execute it..." | ||
267 | chmod 500 /tmp/POSTINSTALL.sh | ||
268 | /tmp/POSTINSTALL.sh | ||
269 | rm -rf /tmp/POSTINSTALL.sh | ||
270 | fi | ||
271 | fi | ||
272 | set -e | ||
273 | |||
274 | if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then | ||
275 | root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs}) | ||
276 | GRUBCFG="$EFIDIR/grub.cfg" | ||
277 | cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG | ||
278 | # Update grub config for the installed image | ||
279 | # Delete the install entry | ||
280 | sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG | ||
281 | # Delete the initrd lines | ||
282 | sed -i "/initrd /d" $GRUBCFG | ||
283 | # Delete any LABEL= strings | ||
284 | sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG | ||
285 | # Delete any root= strings | ||
286 | sed -i "s/ root=[^ ]*/ /g" $GRUBCFG | ||
287 | # Add the root= and other standard boot options | ||
288 | sed -i "s@linux /vmlinuz *@linux /vmlinuz root=PARTUUID=$root_part_uuid rw $rootwait quiet @" $GRUBCFG | ||
289 | fi | ||
290 | |||
291 | if [ -d /run/media/$1/loader ]; then | ||
292 | rootuuid=$(blkid -o value -s PARTUUID ${rootfs}) | ||
293 | GUMMIBOOT_CFGS="/boot/loader/entries/*.conf" | ||
294 | if [ -d /boot/loader ]; then | ||
295 | # Don't override loader.conf RMC already deployed | ||
296 | if [ ! -f /boot/loader/loader.conf ]; then | ||
297 | cp /run/media/$1/loader/loader.conf /boot/loader/ | ||
298 | fi | ||
299 | # only copy built OE entries when RMC entries don't exist. | ||
300 | if [ ! -d /boot/loader/entries ] || [ ! ls /boot/loader/entries/*.conf &>/dev/null ]; then | ||
301 | cp -dr /run/media/$1/loader/entries /boot/loader | ||
302 | fi | ||
303 | else | ||
304 | # copy config files for gummiboot | ||
305 | cp -dr /run/media/$1/loader /boot | ||
306 | # delete the install entry | ||
307 | rm -f /boot/loader/entries/install.conf | ||
308 | fi | ||
309 | # delete the initrd lines | ||
310 | sed -i "/initrd /d" $GUMMIBOOT_CFGS | ||
311 | # delete any LABEL= strings | ||
312 | sed -i "s/ LABEL=[^ ]*/ /" $GUMMIBOOT_CFGS | ||
313 | # delete any root= strings | ||
314 | sed -i "s/ root=[^ ]*/ /" $GUMMIBOOT_CFGS | ||
315 | # add the root= and other standard boot options | ||
316 | sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $GUMMIBOOT_CFGS | ||
317 | # if RMC feature presents, append global kernel command line fragment when it exists. | ||
318 | if [ -f "${RMC_DB}" ] && [ -f "${RMC_CMD}" ]; then | ||
319 | if ${RMC_CMD} -B KBOOTPARAM -d "${RMC_DB}" -o /tmp/kbootparam; then | ||
320 | sed -i "/^[ \t]*options/ s/$/ $(cat /tmp/kbootparam)/" $GUMMIBOOT_CFGS | ||
321 | rm /tmp/kbootparam | ||
322 | fi | ||
323 | fi | ||
324 | fi | ||
325 | |||
326 | cp /run/media/$1/vmlinuz /boot | ||
327 | |||
328 | umount /src_root | ||
329 | umount /tgt_root | ||
330 | umount /boot | ||
331 | |||
332 | sync | ||
333 | |||
334 | echo "Remove your installation media, and press ENTER" | ||
335 | |||
336 | read enter | ||
337 | |||
338 | echo "Rebooting..." | ||
339 | reboot -f | ||