Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-integ / recipes-core / initrdscripts / files / init-install.sh
1 #!/bin/sh -e
2 #
3 # install.sh [device_name] [rootfs_name] [video_mode] [vga_mode]
4 #
5
6 PATH=/sbin:/bin:/usr/sbin:/usr/bin
7
8 # Get a list of hard drives
9 hdnamelist=""
10 live_dev_name=`cat /proc/mounts | grep ${1%/} | awk '{print $1}'`
11 live_dev_name=${live_dev_name#\/dev/}
12 # Only strip the digit identifier if the device is not an mmc
13 case $live_dev_name in
14     mmcblk*)
15     ;;
16     nvme*)
17     ;;
18     *)
19         live_dev_name=${live_dev_name%%[0-9]*}
20     ;;
21 esac
22
23 echo "Searching for hard drives ..."
24
25 # Some eMMC devices have special sub devices such as mmcblk0boot0 etc
26 # we're currently only interested in the root device so pick them wisely
27 devices=`ls /sys/block/ | grep -v mmcblk` || true
28 mmc_devices=`ls /sys/block/ | grep "mmcblk[0-9]\{1,\}$"` || true
29 devices="$devices $mmc_devices"
30
31 for device in $devices; do
32     case $device in
33         loop*)
34             # skip loop device
35             ;;
36         sr*)
37             # skip CDROM device
38             ;;
39         ram*)
40             # skip ram device
41             ;;
42         *)
43             # skip the device LiveOS is on
44             # Add valid hard drive name to the list
45             case $device in
46                 $live_dev_name*)
47                 # skip the device we are running from
48                 ;;
49                 *)
50                     hdnamelist="$hdnamelist $device"
51                 ;;
52             esac
53             ;;
54     esac
55 done
56
57 TARGET_DEVICE_NAME=""
58 for hdname in $hdnamelist; do
59     # Display found hard drives and their basic info
60     echo "-------------------------------"
61     echo /dev/$hdname
62     if [ -r /sys/block/$hdname/device/vendor ]; then
63         echo -n "VENDOR="
64         cat /sys/block/$hdname/device/vendor
65     fi
66     if [ -r /sys/block/$hdname/device/model ]; then
67         echo -n "MODEL="
68         cat /sys/block/$hdname/device/model
69     fi
70     if [ -r /sys/block/$hdname/device/uevent ]; then
71         echo -n "UEVENT="
72         cat /sys/block/$hdname/device/uevent
73     fi
74     echo
75 done
76
77 # use the first one found
78 for hdname in $hdnamelist; do
79     TARGET_DEVICE_NAME=$hdname
80     break
81 done
82
83 if [ -n "$TARGET_DEVICE_NAME" ]; then
84     echo "Installing image on /dev/$TARGET_DEVICE_NAME ..."
85 else
86     echo "No hard drive found. Installation aborted."
87     exit 1
88 fi
89
90 device=/dev/$TARGET_DEVICE_NAME
91
92 #
93 # Unmount anything the automounter had mounted
94 #
95
96 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts | grep $TARGET_DEVICE_NAME`; do
97         umount $dir
98 done
99
100 if [ ! -b /dev/loop0 ] ; then
101     mknod /dev/loop0 b 7 0
102 fi
103
104 mkdir -p /tmp
105 if [ ! -L /etc/mtab ] && [ -e /proc/mounts ]; then
106     ln -sf /proc/mounts /etc/mtab
107 fi
108
109 disk_size=$(parted ${device} unit mb print | grep '^Disk .*: .*MB' | cut -d" " -f 3 | sed -e "s/MB//")
110
111 grub_version=$(grub-install -V|sed 's/.* \([0-9]\).*/\1/')
112
113 if [ $grub_version -eq 0 ] ; then
114     bios_boot_size=0
115 else
116     # For GRUB 2 we need separate parition to store stage2 grub image
117     # 2Mb value is chosen to align partition for best performance.
118     bios_boot_size=2
119 fi
120
121 boot_size=512
122 rootfs_size=20000
123 log_vol_size=8000
124 scratch_vol_size=8000
125
126 data_size=$((disk_size-bios_boot_size-boot_size-rootfs_size))
127 boot_start=$((bios_boot_size))
128 rootfs_start=$((bios_boot_size+boot_size))
129 rootfs_end=$((rootfs_start+rootfs_size))
130 data_start=$((rootfs_end))
131
132 # MMC devices are special in a couple of ways
133 # 1) they use a partition prefix character 'p'
134 # 2) they are detected asynchronously (need rootwait)
135 rootwait=""
136 part_prefix=""
137 if [ ! "${device#/dev/mmcblk}" = "${device}" ] || \
138    [ ! "${device#/dev/nvme}" = "${device}" ]; then
139     part_prefix="p"
140     rootwait="rootwait"
141 fi
142
143 # USB devices also require rootwait
144 if [ -n `readlink /dev/disk/by-id/usb* | grep $TARGET_DEVICE_NAME` ]; then
145     rootwait="rootwait"
146 fi
147
148 if [ $grub_version -eq 0 ] ; then
149     bios_boot=''
150     bootfs=${device}${part_prefix}1
151     rootfs=${device}${part_prefix}2
152     data=${device}${part_prefix}3
153 else
154     bios_boot=${device}${part_prefix}1
155     bootfs=${device}${part_prefix}2
156     rootfs=${device}${part_prefix}3
157     data=${device}${part_prefix}4
158 fi
159
160 echo "*********************************************"
161 [ $grub_version -ne 0 ] && echo "BIOS boot partition size: $bios_boot_size MB ($bios_boot)"
162 echo "Boot partition size:   $boot_size MB ($bootfs)"
163 echo "Rootfs partition size: $rootfs_size MB ($rootfs)"
164 echo "Data partition size:   $data_size MB ($data)"
165 echo "*********************************************"
166 echo "Deleting partition table on ${device} ..."
167 dd if=/dev/zero of=${device} bs=512 count=35
168
169 echo "Creating new partition table on ${device} ..."
170 if [ $grub_version -eq 0 ] ; then
171     parted ${device} mktable msdos
172     echo "Creating boot partition on $bootfs"
173     parted ${device} mkpart primary ext3 0% $boot_size
174 else
175     parted ${device} mktable gpt
176     echo "Creating BIOS boot partition on $bios_boot"
177     parted ${device} mkpart bios_boot 0% $bios_boot_size
178     parted ${device} set 1 bios_grub on
179     echo "Creating boot partition on $bootfs"
180     parted ${device} mkpart boot ext3 $boot_start $boot_size
181 fi
182
183 echo "Creating rootfs partition on $rootfs"
184 [ $grub_version -eq 0 ] && pname='primary' || pname='root'
185 parted ${device} -s mkpart $pname ext4 $rootfs_start $rootfs_end
186
187 echo "Creating data partition on $data"
188 [ $grub_version -eq 0 ] && pname='primary' || pname='data'
189 parted ${device} -s mkpart $pname $data_start 100%
190 parted ${device} -s set 4 lvm on
191
192 parted ${device} print
193
194 echo "Waiting for device nodes..."
195 C=0
196 while [ $C -ne 3 ] && [ ! -e $bootfs  -o ! -e $rootfs -o ! -e $data ]; do
197     C=$(( C + 1 ))
198     sleep 1
199 done
200
201 echo "Formatting $bootfs to ext3..."
202 mkfs.ext3 -F $bootfs
203
204 echo "Formatting $rootfs to ext4..."
205 mkfs.ext4 -F $rootfs
206
207 echo "Create LVM for $data..."
208 vg_name="cgts-vg"
209
210 # Disable udev scan in lvm.conf
211 sed -i 's/\(md_component_detection =\).*/\1 0/' /etc/lvm/lvm.conf
212
213 pvcreate -y -ff $data
214 vgcreate -y -ff $vg_name $data
215
216 udevd -d
217
218 lvcreate -y -n log-lv --size $log_vol_size $vg_name
219 lvcreate -y -n scratch-lv --size  $scratch_vol_size $vg_name
220
221 mkfs.ext4 -F /dev/$vg_name/log-lv
222 mkfs.ext4 -F /dev/$vg_name/scratch-lv
223
224 mkdir /tgt_root
225 mkdir /tgt_log
226 mkdir /src_root
227 mkdir -p /boot
228
229 if [ ! -f /run/media/$1/$2 ]; then
230     mkdir -p /run/media/$1
231     mount /dev/$1 /run/media/$1
232 fi
233
234 # Handling of the target root partition
235 mount $rootfs /tgt_root
236 mount /dev/$vg_name/log-lv /tgt_log
237 mount -o rw,loop,noatime,nodiratime /run/media/$1/$2 /src_root
238 echo "Copying rootfs files..."
239 cp -a /src_root/* /tgt_root
240 if [ -d /tgt_root/etc/ ] ; then
241     if [ $grub_version -ne 0 ] ; then
242         boot_uuid=$(blkid -o value -s UUID ${bootfs})
243         bootdev="UUID=$boot_uuid"
244     else
245         bootdev=${bootfs}
246     fi
247     sed -i '/vfat/d' /tgt_root/etc/fstab
248     echo "$bootdev  /boot  ext3  defaults  1  2" >> /tgt_root/etc/fstab
249     echo "/dev/$vg_name/log-lv  /var/log  ext4  defaults  1  2" >> /tgt_root/etc/fstab
250     echo "/dev/$vg_name/scratch-lv  /scratch  ext4  defaults  1  2" >> /tgt_root/etc/fstab
251
252     # We dont want udev to mount our root device while we're booting...
253     if [ -d /tgt_root/etc/udev/ ] ; then
254         echo "${device}" >> /tgt_root/etc/udev/mount.blacklist
255     fi
256 fi
257
258 INSTALL_UUID=`uuidgen`
259 cat << _EOF > /tgt_root/etc/platform/platform.conf
260 nodetype=controller
261 subfunction=controller,worker
262 system_type=All-in-one
263 security_profile=standard
264 management_interface=lo
265 http_port=8080
266 INSTALL_UUID=${INSTALL_UUID}
267 _EOF
268
269 # Create first_boot flag
270 touch /tgt_root/etc/platform/.first_boot
271
272 # The grub.cfg is created by installer, so the postinsts script is not needed.
273 rm -f /tgt_root/etc/rpm-postinsts/*-grub
274
275 # /var/log will be mounted to the log-lv
276 # so move the all files to log-lv
277 cp -rf /tgt_root/var/log/* /tgt_log
278 rm -rf /tgt_root/var/log
279
280 # Fake as anaconda to add info needed by stx 3.0
281 cat << _EOF > /tgt_root/etc/rpm-postinsts/999-anaconda
282 # anaconda - postinst
283 #!/bin/sh
284 set -e
285 mkdir -p /var/log/anaconda/
286 echo "Display mode = t" > /var/log/anaconda/anaconda.log
287 _EOF
288 chmod 0755 /tgt_root/etc/rpm-postinsts/999-anaconda
289
290 umount /tgt_root
291 umount /src_root
292
293 echo "Looking for kernels to use as boot target.."
294 # Find kernel to boot to
295 # Give user options if multiple are found
296 kernels="$(find /run/media/$1/ -type f  \
297            -name bzImage* -o -name zImage* \
298            -o -name vmlinux* -o -name vmlinuz* \
299            -o -name fitImage* \
300            | sed s:.*/::)"
301 if [ -n "$(echo $kernels)" ]; then
302     # only one kernel entry if no space
303     if [ -z "$(echo $kernels | grep " ")" ]; then
304         kernel=$kernels
305         echo "$kernel will be used as the boot target"
306     else
307         echo "Which kernel do we want to boot by default? The following kernels were found:"
308         echo $kernels
309         read answer
310         kernel=$answer
311     fi
312 else
313     echo "No kernels found, exiting..."
314     exit 1
315 fi
316
317 # Handling of the target boot partition
318 mount $bootfs /boot
319 echo "Preparing boot partition..."
320
321 if [ -f /etc/grub.d/00_header -a $grub_version -ne 0 ] ; then
322     echo "Preparing custom grub2 menu..."
323     root_part_uuid=$(blkid -o value -s PARTUUID ${rootfs})
324     boot_uuid=$(blkid -o value -s UUID ${bootfs})
325     GRUBCFG="/boot/grub/grub.cfg"
326     mkdir -p $(dirname $GRUBCFG)
327     cat >$GRUBCFG <<_EOF
328 timeout=5
329 default=0
330 menuentry "Yocto Linux with StarlingX @STX_ID@" {
331     search --no-floppy --fs-uuid $boot_uuid --set root
332     linux /$kernel root=$rootfs $rootwait rw console=tty0 console=ttyS0,115200 $5 $3 $4
333 }
334 _EOF
335     chmod 0444 $GRUBCFG
336 fi
337 grub-install ${device}
338
339 if [ $grub_version -eq 0 ] ; then
340     echo "(hd0) ${device}" > /boot/grub/device.map
341     echo "Preparing custom grub menu..."
342     echo "default 0" > /boot/grub/menu.lst
343     echo "timeout 30" >> /boot/grub/menu.lst
344     echo "title Live Boot/Install-Image" >> /boot/grub/menu.lst
345     echo "root  (hd0,0)" >> /boot/grub/menu.lst
346     echo "kernel /$kernel root=$rootfs rw $3 $4 quiet" >> /boot/grub/menu.lst
347 fi
348
349 # Copy kernel artifacts. To add more artifacts just add to types
350 # For now just support kernel types already being used by something in OE-core
351 for types in bzImage zImage vmlinux vmlinuz fitImage; do
352     for kernel in `find /run/media/$1/ -name $types*`; do
353         cp $kernel /boot
354     done
355 done
356
357 umount /boot
358
359 sync
360
361 echo "Remove your installation media, and press ENTER"
362
363 read enter
364
365 echo "Rebooting..."
366 reboot -f