Skip to content

Instantly share code, notes, and snippets.

@weldpua2008
Created January 26, 2017 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weldpua2008/1ddbed6fa8373bbf8ccd95e2cfe6e399 to your computer and use it in GitHub Desktop.
Save weldpua2008/1ddbed6fa8373bbf8ccd95e2cfe6e399 to your computer and use it in GitHub Desktop.
Script as part of initrd boot
#!/bin/sh
##########################################################################
#
# Script as a part of initrd to load /
#
##########################################################################
# Author: Valeriy Soloviov <weldpua2008@gmail.com>
##########################################################################
# isolinux config
# append boot=live.sh components ip=frommedia action=install flow=install_prod
#set -e
#. /bin/live-boot
. /scripts/functions
export application_cdrom_searchpath=/cdrom/application/
echo "!!!!Application preparation!!!"
echo "Starting to mount CD/DVD-ROM"
_tmp_mounted_cd="false"
_tmp_i=0
mkdir /cdrom
while true;do
for disk in $(find /dev/disk/by-id/ -name "*DVD*");do
mount -t iso9660 $disk /cdrom
_tmp_mounted_cd="true"
echo "$disk was mounted as /cdrom"
break
done
if [ "$_tmp_mounted_cd" = "false" ];then
for disk in $(find /dev/disk/by-id/ -name "*CD*");do
mount -t iso9660 $disk /cdrom
_tmp_mounted_cd="true"
echo "$disk was mounted as /cdrom"
break
done
fi
let "_tmp_i+=1"
if [ "$_tmp_mounted_cd" = "true" ];then
break
fi
if [ "$_tmp_i" = "15" ];then
break
fi
sleep 1
done
if [ "$_tmp_mounted_cd" = "false" ];then
panic "Failed to mount CD/DVD-ROM"
# exit 1
fi
unset _tmp_mounted_cd
echo "CD/DVDROM was mounted successfully"
# sleep 2
echo "--- Starting to search HDD for application"
_tmp_i=0
_tmp_hdd_name=""
while true;do
for disk in $(find /dev/disk/by-id/ -name "*HDD*");do
_tmp_hdd_name=$(readlink -f "$disk")
if [ "$_tmp_hdd_name" = "" ];then
# _tmp_hdd_name="$disk"
continue
fi
break
done
let "_tmp_i+=1"
if [ "$_tmp_hdd_name" != "" ];then
break
fi
if [ "$_tmp_i" = "10" ];then
break
fi
sleep 1
done
if [ "$_tmp_hdd_name" = "" ];then
_tmp_hdd_name="/dev/sda"
fi
if [ ! -e "$_tmp_hdd_name" ];then
panic "Can't found HDD"
# exit 1
fi
# 2 disk configuration
# if [ ! -e "${_tmp_hdd_name}1" -o ! -e "${_tmp_hdd_name}2" ];then
if [ ! -e "${_tmp_hdd_name}1" ];then
# to create the partitions programatically (rather than manually)
# we're going to simulate the manual input to fdisk
# The sed script strips off all the comments so that we can
# document what we're doing in-line with the actual commands
# Note that a blank line (commented as "defualt" will send a empty
# line terminated with a newline to take the fdisk default.
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${_tmp_hdd_name}"
o # clear the in memory partition table
n # new partition
p # primary partition
1 # partition number 1
# default - start at beginning of disk
#+100M # 1Gb / parttion
p # print the in-memory partition table
w # write the partition table
q # and we're done
EOF
if [ $? -ne 0 ];then
panic "Can't format disk"
fi
_is_formated="false"
echo "Formated ${_tmp_hdd_name} with partitions"
for __dev in "${_tmp_hdd_name}1" "${_tmp_hdd_name}2";do
for i in 1 2 3; do
echo "Trying to format ${__dev} [$i].."
yes | mkfs.ext4 "${__dev}"
if [ $? -ne 0 ];then
continue
fi
_is_formated="true"
break
done
if [ "$_is_formated" = "false" ];then
panic "Can't create ext4 on $__dev"
fi
echo "Created ext4 for ${__dev}"
done
fi
e2label "${_tmp_hdd_name}1" SLASH && echo "Setup label for ${_tmp_hdd_name}1" || echo "Can't setup a label for ${_tmp_hdd_name}1"
#if [ -e "${_tmp_hdd_name}2" ];then
# e2label "${_tmp_hdd_name}2" APPLICATION && echo "Setup label for ${_tmp_hdd_name}2" || echo "Can't setup a label for ${_tmp_hdd_name}2"
#fi
[ "$rootmnt" = "" ] && export rootmnt=/root || echo "ROOTFS is at $rootmnt"
if [ ! -d "${rootmnt}" ];then
mkdir -p ${rootmnt} || panic "Can't create ${rootmnt}"
fi
mount "${_tmp_hdd_name}1" ${rootmnt}
if [ $? -ne 0 ];then
echo "Can't mount ${_tmp_hdd_name}1. Starting to format ${_tmp_hdd_name}1..."
# in case any error
umount "${_tmp_hdd_name}1" || true
echo "Trying to format ${__dev} [$i].."
_is_formated="true"
yes | mkfs.ext4 "${__dev}"
if [ $? -ne 0 ];then
panic "[FAILED]"
fi
mount "${_tmp_hdd_name}1" ${rootmnt} || panic "Can't mount ${_tmp_hdd_name}1"
fi
# Persistent data between reboot
export application_tmp_folder="${rootmnt}/application_tmp"
echo "Creating temp folder to store files between reboot"
rm -rf "$application_tmp_folder/" || true
mkdir -p $application_tmp_folder || panic "Can't create $application_tmp_folder"
export application_persistent_folders="/opt/application /var/core"
for persistent_folder in $application_persistent_folders;do
if [ -e "${rootmnt}/$persistent_folder" ];then
echo "Moving persistent folder $persistent_folder to $application_tmp_folder"
mv ${rootmnt}/$persistent_folder $application_tmp_folder
fi
done
echo "Cleanup rootfs at ${rootmnt}/"
for folder in `ls ${rootmnt}/`;do
[ "$folder" = "lost+found" ] && continue
[ "$folder" = "proc" ] && continue
[ "$folder" = "dev" ] && continue
[ "$folder" = "sys" ] && continue
[ "$folder" = "mnt" ] && continue
[ "$folder" = "disk" ] && continue
[ "$folder" = "application_app" ] && continue
[ "$folder" = "" ] && continue
[ "$folder" = " " ] && continue
rm -rf ${rootmnt}/${folder}/ || true
done
mkdir -p ${rootmnt} || panic "Can't create ${rootmnt}"
for persistent_folder in $application_persistent_folders;do
_tmp_foldername=$(basename "$persistent_folder")
if [ -e "${application_tmp_folder}/$_tmp_foldername" ];then
echo "Moving back $_tmp_foldername ... "
mkdir -p ${rootmnt}/${persistent_folder%/*} || panic "Can't create $persistent_folder"
mv $application_tmp_folder/$_tmp_foldername ${rootmnt}/$persistent_folder || panic "Can't move back $persistent_folder"
fi
done
#if [ "$_is_formated" = "true" ];then
#
#fi
unsquashfs -f -d ${rootmnt} /cdrom/live/filesystem.squashfs || panic "Can't unsquashfs to ${rootmnt}"
# [ ! -e /dev/loop0 ] && mknod /dev/loop0 b 7 0
# losetup /dev/loop0 /cdrom/live/filesystem.squashfs || panic "Can't losetup squashfs"
# panic "smo"
export application_mountpoint=${rootmnt}/application_app
# It's where we store before previous boot Application*.img
export application_prev=${rootmnt}/application_app/prev/
# It's folder where we store before previous boot Application*.img
export application_prev2=${rootmnt}/application_app/prev2/
# It's where we store current Application*.img
# it's a foilder where /opt/rdwr/adcva/bin/_tmp_adc expects to find the Application*.img
export application_mnt_folder=${rootmnt}/mnt/cf/
mkdir $application_mountpoint -p || panic "Can't create $application_app"
# mount "${_tmp_hdd_name}2" $application_mountpoint || panic "Can't mount ${_tmp_hdd_name}2"
application_app_cdrom_path=$(find $application_cdrom_searchpath -name "*.img")
[ "$application_app_cdrom_path" = "" ] && panic "Can't find Application image at $application_cdrom_searchpath"
### moving previous Application versions
alt_prev_moved="false"
alt_curr_moved="false"
[ -d "$application_prev2" ] && rm -rf $application_prev2 || true
if [ -d "$application_prev" ];then
mv $application_prev $application_prev2 && alt_prev_moved="true" || alt_prev_moved="failed"
fi
if [ -d "$application_mnt_folder/" ];then
mkdir -p $application_prev
mv $application_mnt_folder/Application*.img $application_prev && alt_curr_moved="true" || alt_curr_moved="failed"
fi
# cleanup current image
rm -rf $application_mnt_folder/Application*.img ||true
_application_copied="false"
for i in 1 2 3;do
echo "Starting to copy $application_app_cdrom_path to $application_mnt_folder [$i]..."
mkdir -p "$application_mnt_folder/" || echo "Can't create $application_mnt_folder"
cp $application_app_cdrom_path $application_mnt_folder || continue
echo " [FINISHED]"
_application_copied="true"
break
done
[ "$_application_copied" != "true" ] && panic "Can't copy $application_app_cdrom_path to $application_mnt_folder"
rm -rf "$application_prev2"
umount /cdrom && echo "/cdrom was Unmounted" || echo "Failed unmount /cdrom"
# umount $application_mountpoint && echo "$application_mountpoint was Unmounted" || echo "Failed unmount $application_mountpoint"
# sleep 1
mountroot()
{
echo "MOUNTROOT [FINISHED]"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment