init-install: fix rootfs.img not found issue
[pti/rtp.git] / meta-oran / recipes-core / initrdscripts / files / init-install.sh
1 #!/bin/sh -e
2 #
3 # Copyright (C) 2008-2011 Intel
4 #
5 # install.sh [device_name] [rootfs_name] [video_mode] [vga_mode]
6 #
7
8 PATH=/sbin:/bin:/usr/sbin:/usr/bin
9
10 # figure out how big of a boot partition we need
11 boot_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
13 if [ -e /run/media/$1/$2 ]; then
14     boot_size=$(( boot_size - $( du -ms /run/media/$1/$2 | awk '{print $1}') ))
15 fi
16 # remove initrd from size since its not currently installed
17 if [ -e /run/media/$1/initrd ]; then
18     boot_size=$(( boot_size - $( du -ms /run/media/$1/initrd | awk '{print $1}') ))
19 fi
20 # add 10M to provide some extra space for users and account
21 # for rounding in the above subtractions
22 boot_size=$(( boot_size + 10 ))
23
24 # 5% for the swap
25 swap_ratio=5
26
27 # Get a list of hard drives
28 hdnamelist=""
29 live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
30 live_dev_name=${live_dev_name#\/dev/}
31 # Only strip the digit identifier if the device is not an mmc
32 case $live_dev_name in
33     mmcblk*)
34     ;;
35     nvme*)
36     ;;
37     *)
38         live_dev_name=${live_dev_name%%[0-9]*}
39     ;;
40 esac
41
42 echo "Searching for hard drives ..."
43
44 # 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
46 devices=`ls /sys/block/ | grep -v mmcblk` || true
47 mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
48 devices="$devices $mmc_devices"
49
50 for device in $devices; do
51     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
74 done
75
76 TARGET_DEVICE_NAME=""
77 for 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
94 done
95
96 # Get user choice
97 while 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
107             break
108         fi
109     done
110     if [ -n "$TARGET_DEVICE_NAME" ]; then
111         break
112     fi
113 done
114
115 if [ -n "$TARGET_DEVICE_NAME" ]; then
116     echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
117 else
118     echo "No hard drive selected. Installation aborted."
119     exit 1
120 fi
121
122 device=/dev/$TARGET_DEVICE_NAME
123
124 #
125 # The udev automounter can cause pain here, kill it
126 #
127 #rm -f /etc/udev/rules.d/automount.rules
128 #rm -f /etc/udev/scripts/mount*
129
130 #
131 # Unmount anything the automounter had mounted
132 #
133 #umount ${device}* 2> /dev/null || /bin/true
134
135 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts | grep $TARGET_DEVICE_NAME`; do
136         umount $dir
137 done
138
139 if [ ! -b /dev/loop0 ] ; then
140     mknod /dev/loop0 b 7 0
141 fi
142
143 mkdir -p /tmp
144 if [ ! -L /etc/mtab ] && [ -e /proc/mounts ]; then
145     ln -sf /proc/mounts /etc/mtab
146 fi
147
148 disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
149
150 grub_version=$(grub-install -V|sed 's/.* \([0-9]\).*/\1/')
151
152 if [ $grub_version -eq 0 ] ; then
153     bios_boot_size=0
154 else
155     # For GRUB 2 we need separate parition to store stage2 grub image
156     # 2Mb value is chosen to align partition for best performance.
157     bios_boot_size=2
158 fi
159
160 swap_size=$((disk_size*swap_ratio/100))
161 rootfs_size=$((disk_size-bios_boot_size-boot_size-swap_size))
162
163 boot_start=$((bios_boot_size))
164 rootfs_start=$((bios_boot_size+boot_size))
165 rootfs_end=$((rootfs_start+rootfs_size))
166 swap_start=$((rootfs_end))
167
168 # MMC devices are special in a couple of ways
169 # 1) they use a partition prefix character 'p'
170 # 2) they are detected asynchronously (need rootwait)
171 rootwait=""
172 part_prefix=""
173 if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
174    [ ! "${device#/dev/nvme}" = "${device}" ]; then
175     part_prefix="p"
176     rootwait="rootwait"
177 fi
178
179 # USB devices also require rootwait
180 if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
181     rootwait="rootwait"
182 fi
183
184 if [ $grub_version -eq 0 ] ; then
185     bios_boot=''
186     bootfs=${device}${part_prefix}1
187     rootfs=${device}${part_prefix}2
188     swap=${device}${part_prefix}3
189 else
190     bios_boot=${device}${part_prefix}1
191     bootfs=${device}${part_prefix}2
192     rootfs=${device}${part_prefix}3
193     swap=${device}${part_prefix}4
194 fi
195
196 echo "*****************"
197 [ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
198 echo "Boot partition size:   $boot_size MB ($bootfs)"
199 echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
200 echo "Swap partition size:   $swap_size MB ($swap)"
201 echo "*****************"
202 echo "Deleting partition table on ${device} ..."
203 dd if=/dev/zero of=${device} bs=512 count=35
204
205 echo "Creating new partition table on ${device} ..."
206 if [ $grub_version -eq 0 ] ; then
207     parted ${device} mktable msdos
208     echo "Creating boot partition on $bootfs"
209     parted ${device} mkpart primary ext3 0% $boot_size
210 else
211     parted ${device} mktable gpt
212     echo "Creating BIOS boot partition on $bios_boot"
213     parted ${device} mkpart bios_boot 0% $bios_boot_size
214     parted ${device} set 1 bios_grub on
215     echo "Creating boot partition on $bootfs"
216     parted ${device} mkpart boot ext3 $boot_start $boot_size
217 fi
218
219 echo "Creating rootfs partition on $rootfs"
220 [ $grub_version -eq 0 ] && pname='primary' || pname='root'
221 parted ${device} mkpart $pname ext4 $rootfs_start $rootfs_end
222
223 echo "Creating swap partition on $swap"
224 [ $grub_version -eq 0 ] && pname='primary' || pname='swap'
225 parted ${device} mkpart $pname linux-swap $swap_start 100%
226
227 parted ${device} print
228
229 echo "Waiting for device nodes..."
230 C=0
231 while [ $C -ne 3 ] && [ ! -e $bootfs  -o ! -e $rootfs -o ! -e $swap ]; do
232     C=$(( C + 1 ))
233     sleep 1
234 done
235
236 echo "Formatting $bootfs to ext3..."
237 mkfs.ext3 $bootfs
238
239 echo "Formatting $rootfs to ext4..."
240 mkfs.ext4 $rootfs
241
242 echo "Formatting swap partition...($swap)"
243 mkswap $swap
244
245 mkdir /tgt_root
246 mkdir /src_root
247 mkdir -p /boot
248
249 # Handling of the target root partition
250 mount $rootfs /tgt_root
251
252 if [ ! -f /run/media/$1/$2 ]; then
253     mkdir -p /run/media/$1
254     mount /dev/$1 /run/media/$1
255 fi
256 mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
257
258 echo "Copying rootfs files..."
259 cp -a /src_root/* /tgt_root
260 if [ -d /tgt_root/etc/ ] ; then
261     if [ $grub_version -ne 0 ] ; then
262         boot_uuid=$(blkid -o value -s UUID ${bootfs})
263         swap_part_uuid=$(blkid -o value -s PARTUUID ${swap})
264         bootdev="UUID=$boot_uuid"
265         swapdev=/dev/disk/by-partuuid/$swap_part_uuid
266     else
267         bootdev=${bootfs}
268         swapdev=${swap}
269     fi
270     echo "$swapdev                swap             swap       defaults              0  0" >> /tgt_root/etc/fstab
271     echo "$bootdev              /boot            ext3       defaults              1  2" >> /tgt_root/etc/fstab
272     # We dont want udev to mount our root device while we're booting...
273     if [ -d /tgt_root/etc/udev/ ] ; then
274         echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
275     fi
276 fi
277 umount /tgt_root
278 umount /src_root
279
280 echo "Looking for kernels to use as boot target.."
281 # Find kernel to boot to
282 # Give user options if multiple are found
283 kernels="$(find /run/media/$1/ -type f  \
284            -name bzImage* -o -name zImage* \
285            -o -name vmlinux* -o -name vmlinuz* \
286            -o -name fitImage* \
287            | sed s:.*/::)"
288 if [ -n "$(echo $kernels)" ]; then
289     # only one kernel entry if no space
290     if [ -z "$(echo $kernels | grep " ")" ]; then
291         kernel=$kernels
292         echo "$kernel will be used as the boot target"
293     else
294         echo "Which kernel do we want to boot by default? The following kernels were found:"
295         echo $kernels
296         read answer
297         kernel=$answer
298     fi
299 else
300     echo "No kernels found, exiting..."
301     exit 1
302 fi
303
304 # Handling of the target boot partition
305 mount $bootfs /boot
306 echo "Preparing boot partition..."
307
308 if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
309     echo "Preparing custom grub2 menu..."
310     root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
311     boot_uuid=$(blkid -o value -s UUID ${bootfs})
312     GRUBCFG="/boot/grub/grub.cfg"
313     mkdir -p $(dirname $GRUBCFG)
314     cat >$GRUBCFG <<_EOF
315 timeout=5
316 default=0
317 menuentry "Linux" {
318     search --no-floppy --fs-uuid $boot_uuid --set root
319     linux /$kernel root=PARTUUID=$root_part_uuid $rootwait rw $5 $3 $4 quiet
320 }
321 _EOF
322     chmod 0444 $GRUBCFG
323 fi
324 grub-install ${device}
325
326 if [ $grub_version -eq 0 ] ; then
327     echo "(hd0) ${device}" > /boot/grub/device.map
328     echo "Preparing custom grub menu..."
329     echo "default 0" > /boot/grub/menu.lst
330     echo "timeout 30" >> /boot/grub/menu.lst
331     echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst
332     echo "root  (hd0,0)" >> /boot/grub/menu.lst
333     echo "kernel /$kernel root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst
334 fi
335
336 # Copy kernel artifacts. To add more artifacts just add to types
337 # For now just support kernel types already being used by something in OE-core
338 for types in bzImage zImage vmlinux vmlinuz fitImage; do
339     for kernel in `find /run/media/$1/ -name $types*`; do
340         cp $kernel /boot
341     done
342 done
343
344 umount /boot
345
346 sync
347
348 echo "Remove your installation media, and press ENTER"
349
350 read enter
351
352 echo "Rebooting..."
353 reboot -f