init-install: fix rootfs.img not found issue
[pti/rtp.git] / meta-oran / recipes-core / initrdscripts / files / init-install-efi.sh
1 #!/bin/sh -e
2 #
3 # Copyright (c) 2012, Intel Corporation.
4 # All rights reserved.
5 #
6 # install.sh [device_name] [rootfs_name]
7 #
8
9 PATH=/sbin:/bin:/usr/sbin:/usr/bin
10
11 # figure out how big of a boot partition we need
12 boot_size=$(du -ms /run/media/$1/ | awk '{print $1}')
13 # remove rootfs.img ($2) from the size if it exists, as its not installed to /boot
14 if [ -e /run/media/$1/$2 ]; then
15     boot_size=$(( boot_size - $( du -ms /run/media/$1/$2 | awk '{print $1}') ))
16 fi
17 # remove initrd from size since its not currently installed
18 if [ -e /run/media/$1/initrd ]; then
19     boot_size=$(( boot_size - $( du -ms /run/media/$1/initrd | awk '{print $1}') ))
20 fi
21 # add 10M to provide some extra space for users and account
22 # for rounding in the above subtractions
23 boot_size=$(( boot_size + 10 ))
24
25 # 5% for swap
26 swap_ratio=5
27
28 # Get a list of hard drives
29 hdnamelist=""
30 live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
31 live_dev_name=${live_dev_name#\/dev/}
32 # Only strip the digit identifier if the device is not an mmc
33 case $live_dev_name in
34     mmcblk*)
35     ;;
36     nvme*)
37     ;;
38     *)
39         live_dev_name=${live_dev_name%%[0-9]*}
40     ;;
41 esac
42
43 echo "Searching for hard drives ..."
44
45 # Some eMMC devices have special sub devices such as mmcblk0boot0 etc
46 # we're currently only interested in the root device so pick them wisely
47 devices=`ls /sys/block/ | grep -v "mmcblk\|md\|dm"` || true
48 mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
49 md_devices=`cat /proc/mdstat  |grep -w active |awk -F":" '{print $1}'` || true
50 devices="$devices $mmc_devices $md_devices"
51
52 for device in $devices; do
53     case $device in
54         loop*)
55             # skip loop device
56             ;;
57         sr*)
58             # skip CDROM device
59             ;;
60         ram*)
61             # skip ram device
62             ;;
63         *)
64             # skip the device LiveOS is on
65             # Add valid hard drive name to the list
66             case $device in
67                 $live_dev_name*)
68                 # skip the device we are running from
69                 ;;
70                 *)
71                     hdnamelist="$hdnamelist $device"
72                 ;;
73             esac
74             ;;
75     esac
76 done
77
78 if [ -z "${hdnamelist}" ]; then
79     echo "You need another device (besides the live device /dev/${live_dev_name}) to install the image. Installation aborted."
80     exit 1
81 fi
82
83 TARGET_DEVICE_NAME=""
84 for hdname in $hdnamelist; do
85     # Display found hard drives and their basic info
86     echo "-------------------------------"
87     echo /dev/$hdname
88     if [ -r /sys/block/$hdname/device/vendor ]; then
89         echo -n "VENDOR="
90         cat /sys/block/$hdname/device/vendor
91     fi
92     if [ -r /sys/block/$hdname/device/model ]; then
93         echo -n "MODEL="
94         cat /sys/block/$hdname/device/model
95     fi
96     if [ -r /sys/block/$hdname/device/uevent ]; then
97         echo -n "UEVENT="
98         cat /sys/block/$hdname/device/uevent
99     fi
100     echo
101 done
102
103 # Get user choice
104 while true; do
105     echo "Please select an install target or press n to exit ($hdnamelist ): "
106     read answer
107     if [ "$answer" = "n" ]; then
108         echo "Installation manually aborted."
109         exit 1
110     fi
111     for hdname in $hdnamelist; do
112         if [ "$answer" = "$hdname" ]; then
113             TARGET_DEVICE_NAME=$answer
114             break
115         fi
116     done
117     if [ -n "$TARGET_DEVICE_NAME" ]; then
118         break
119     fi
120 done
121
122 if [ -n "$TARGET_DEVICE_NAME" ]; then
123     echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
124 else
125     echo "No hard drive selected. Installation aborted."
126     exit 1
127 fi
128
129 device=/dev/$TARGET_DEVICE_NAME
130
131 #
132 # The udev automounter can cause pain here, kill it
133 #
134 #rm -f /etc/udev/rules.d/automount.rules
135 #rm -f /etc/udev/scripts/mount*
136
137 #
138 # Unmount anything the automounter had mounted
139 #
140 #umount ${device}* 2> /dev/null || /bin/true
141
142 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts | grep $TARGET_DEVICE_NAME`; do
143         umount $dir
144 done
145
146 mkdir -p /tmp
147
148 # Create /etc/mtab if not present
149 if [ ! -e /etc/mtab ] && [ -e /proc/mounts ]; then
150     ln -sf /proc/mounts /etc/mtab
151 fi
152
153 disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
154
155 swap_size=$((disk_size*swap_ratio/100))
156 rootfs_size=$((disk_size-boot_size-swap_size))
157
158 rootfs_start=$((boot_size))
159 rootfs_end=$((rootfs_start+rootfs_size))
160 swap_start=$((rootfs_end))
161
162 # MMC devices are special in a couple of ways
163 # 1) they use a partition prefix character 'p'
164 # 2) they are detected asynchronously (need rootwait)
165 rootwait=""
166 part_prefix=""
167 if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
168    [ ! "${device#/dev/nvme}" = "${device}" ]; then
169     part_prefix="p"
170     rootwait="rootwait"
171 fi
172
173 # MD raid device use partition prefix charater 'p'
174 # and it need a larger capacity to store initrd,
175 # considering some debug purpose, enlarge it to 1G.
176 if [ ! "${device#/dev/md}" = "${device}" ]; then
177     part_prefix="p"
178     boot_size=1024
179 fi
180
181 # USB devices also require rootwait
182 if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
183     rootwait="rootwait"
184 fi
185
186 bootfs=${device}${part_prefix}1
187 rootfs=${device}${part_prefix}2
188 swap=${device}${part_prefix}3
189
190 echo "*****************"
191 echo "Boot partition size:   $boot_size MB ($bootfs)"
192 echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
193 echo "Swap partition size:   $swap_size MB ($swap)"
194 echo "*****************"
195 echo "Deleting partition table on ${device} ..."
196 dd if=/dev/zero of=${device} bs=512 count=35
197
198 echo "Creating new partition table on ${device} ..."
199 parted ${device} mklabel gpt
200
201 echo "Creating boot partition on $bootfs"
202 parted ${device} mkpart boot fat32 0% $boot_size
203 parted ${device} set 1 boot on
204
205 echo "Creating rootfs partition on $rootfs"
206 parted ${device} mkpart root ext4 $rootfs_start $rootfs_end
207
208 echo "Creating swap partition on $swap"
209 parted ${device} mkpart swap linux-swap $swap_start 100%
210
211 parted ${device} print
212
213 echo "Waiting for device nodes..."
214 C=0
215 while [ $C -ne 3 ] && [ ! -e $bootfs  -o ! -e $rootfs -o ! -e $swap ]; do
216     C=$(( C + 1 ))
217     sleep 1
218 done
219
220 echo "Formatting $bootfs to vfat..."
221 mkfs.vfat $bootfs
222
223 echo "Formatting $rootfs to ext4..."
224 mkfs.ext4 $rootfs
225
226 echo "Formatting swap partition...($swap)"
227 mkswap $swap
228
229 mkdir /tgt_root
230 mkdir /src_root
231 mkdir -p /boot
232
233 # Handling of the target root partition
234 mount $rootfs /tgt_root
235
236 if [ ! -f /run/media/$1/$2 ]; then
237     mkdir -p /run/media/$1
238     mount /dev/$1 /run/media/$1
239 fi
240 mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
241 echo "Copying rootfs files..."
242 cp -a /src_root/* /tgt_root
243 if [ -d /tgt_root/etc/ ] ; then
244     boot_uuid=$(blkid -o value -s UUID ${bootfs})
245     swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
246     echo "/dev/disk/by-partuuid/$swap_part_uuid                swap             swap       defaults              0  0" >> /tgt_root/etc/fstab
247     echo "UUID=$boot_uuid              /boot            vfat       defaults              1  2" >> /tgt_root/etc/fstab
248     # We dont want udev to mount our root device while we're booting...
249     if [ -d /tgt_root/etc/udev/ ] ; then
250         echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
251     fi
252 fi
253
254 umount /src_root
255
256 # Handling of the target boot partition
257 mount $bootfs /boot
258 echo "Preparing boot partition..."
259
260 EFIDIR="/boot/EFI/BOOT"
261 mkdir -p $EFIDIR
262 # Copy the efi loader
263 cp /run/media/$1/EFI/BOOT/*.efi $EFIDIR
264
265 if [ -f /run/media/$1/EFI/BOOT/grub.cfg ]; then
266     root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
267     GRUBCFG="$EFIDIR/grub.cfg"
268     cp /run/media/$1/EFI/BOOT/grub.cfg $GRUBCFG
269     # Update grub config for the installed image
270     # Delete the install entry
271     sed -i "/menuentry 'install/,/^}/d" $GRUBCFG
272     # initrd is necessary to boot from MD device
273     if [ ! "${device#/dev/md}" = "${device}" ]; then
274        cp /run/media/$1/initrd /boot
275        cp /run/media/$1/startup.nsh /boot
276     else
277        # Delete the initrd lines
278        sed -i "/initrd /d" $GRUBCFG
279     fi
280     # Delete any LABEL= strings
281     sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
282     # Replace root= and add additional standard boot options
283     # We use root as a sentinel value, as vmlinuz is no longer guaranteed
284     sed -i "s/ root=[^ ]*/ root=PARTUUID=$root_part_uuid rw $rootwait quiet /g" $GRUBCFG
285 fi
286
287 if [ -d /run/media/$1/loader ]; then
288     rootuuid=$(blkid -o value -s PARTUUID ${rootfs})
289     SYSTEMDBOOT_CFGS="/boot/loader/entries/*.conf"
290     # copy config files for systemd-boot
291     cp -dr /run/media/$1/loader /boot
292     # delete the install entry
293     rm -f /boot/loader/entries/install.conf
294     # delete the initrd lines
295     sed -i "/initrd /d" $SYSTEMDBOOT_CFGS
296     # delete any LABEL= strings
297     sed -i "s/ LABEL=[^ ]*/ /" $SYSTEMDBOOT_CFGS
298     # delete any root= strings
299     sed -i "s/ root=[^ ]*/ /" $SYSTEMDBOOT_CFGS
300     # add the root= and other standard boot options
301     sed -i "s@options *@options root=PARTUUID=$rootuuid rw $rootwait quiet @" $SYSTEMDBOOT_CFGS
302 fi
303
304 umount /tgt_root
305
306 # Copy kernel artifacts. To add more artifacts just add to types
307 # For now just support kernel types already being used by something in OE-core
308 for types in bzImage zImage vmlinux vmlinuz fitImage; do
309     for kernel in `find /run/media/$1/ -name $types*`; do
310         cp $kernel /boot
311     done
312 done
313
314 umount /boot
315
316 sync
317
318 echo "Installation successful. Remove your installation media and press ENTER to reboot."
319
320 read enter
321
322 echo "Rebooting..."
323 reboot -f