summaryrefslogtreecommitdiff
path: root/conf/tcos-generation-functions.sh
blob: c89d5dee4fd228d81b89df2a89728aa6af3f387c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# vars



# functions used by tcos hooks

_echo () {
 # wrapper to echo in scripts
    echo "$@"
}

_debug() {
  if [ ${TCOS_DEBUG} ]; then
    echo "   DEBUG :: $@" >&2
  fi
}

read_template() {
  [ ! -e "$1" ] && return
  tpl=$(awk -F"=" '/^TCOS_TEMPLATE=/ {print $2}' $1)
  if [ "$tpl" = "" ]; then
    #_debug "Not found TEMPLATE in $1, searching BASED_TEMPLATE"
    tpl=$(awk -F"=" '/^TCOS_BASED_TEMPLATE=/ {print $2}' $1)
  fi
  [ "$tpl" = "" ] && _debug "$tpl not found" && return
  #_debug "Template $tpl"
  for tdir in /usr/share/initramfs-tools-tcos/templates /etc/tcos/templates; do
    #_debug "Searching in dir $tdir"
    [ -f $tdir/$tpl ] && echo "$tdir/$tpl" && break
  done
}

tcos_get_templates() {
  # read template from tcos.conf
  tpl1=$(read_template /etc/tcos/tcos.conf)
  tpl2=""
  personalized=""
  _debug "Default template $tpl"
  for tfile in $(find /etc/tcos/templates -type f -name "*.conf"); do
    if [ -f $tfile ]; then
      #_debug "$tfile file exists, adding to tpl2"
      tpl2="$tpl2 $(read_template $tfile)"
      personalized=$tfile
    else
      _debug "Based template $tfile not found, ignoring"
    fi
  done
  if [ "$( echo $tpl2 | sed 's/[[:blank:]]//g' )" != "" ]; then
    _debug "return tpl2='base.conf $tpl2 $personalized'"
    echo "/usr/share/initramfs-tools-tcos/templates/base.conf $tpl2 $personalized"
  else
    _debug "return tpl1='base.conf $tpl1'"
    echo "/usr/share/initramfs-tools-tcos/templates/base.conf $tpl1"
  fi
}


convert_links2files () {
# convert all links in $DESTDIR to linked files
# needed to calculate initramfs space
 #_echo "Converting links to files..."
 subdir=$1
 if [ "$subdir" = "" ]; then
   subdir="usr"
 fi
 dir=$(pwd)
 cd $DESTDIR
 # copy binary not links
 for xfile in $(find "$subdir" -type l); do
   _link=$(readlink $xfile)
   if [ $(echo $_link | grep -c ${TCOS_PKG_CACHE}) != 0 ]; then
     rm -f $DESTDIR/$xfile
     cp -f $_link $DESTDIR/$xfile
     #_echo " ******** convert_link2files() CACHED FILE => cp -f $_link $DESTDIR/$xfile"

   elif [ -f /$xfile ] ;then
     rm -f $DESTDIR/$xfile
     cp -f /$xfile $DESTDIR/$xfile
     #_echo "  :::::::  convert_link2files() :::::A:::: cp /$xfile $DESTDIR/$xfile"

   else
     #_echo "    convert_links2files() "
     rm -f $DESTDIR/$xfile
     cp -f $_link $DESTDIR/$xfile
     #_echo "  :::::::  convert_link2files() :::::R:::: cp -f $_link $DESTDIR/$xfile"
   fi
 done
 cd $dir
}


stat_before () {
  # read space in $DESTDIR (this functions is a checkpoint)
  size1=$(du -s ${DESTDIR} 2>/dev/null| awk '{print $1}')
}
stat_after () {
  # read size after checkpoint and prints diff between disk space
  # this give what space need an app
  convert_links2files
  size2=$(du -s ${DESTDIR} 2>/dev/null| awk '{print $1}')
  #diff_size=$(echo $((size2 -size1)) )
  diff_size=$(echo $size2 $size1 | awk '{print $1 - $2}' )
  if [ ${TCOS_DEBUG} ]; then
     _echo "  Package $1 got ${diff_size} Kb."
  fi
}

pathof() {
  # give absolute path of $1 binary (if found)
    location=$(which $1 | awk '{print $1}')
    if [ -z $location ]; then
        location=$(whereis $1 | awk '{print $2}')
    fi
    if [ ! -e $location ]; then
        echo "pathof() ERROR $1 not found !!!" 1>&2
    fi
    echo $location
}

cpifexists () {
 orig=$1
 dest=$2
  if [ -f $DESTDIR/$orig ]; then
    return 1
  fi
  
  
  if [ $# != 2 ]; then
   echo "    cpifexists(): ERROR: Need 2 arguments: \$1=$orig \$2=$dest"
   return 1
 fi
 
 if [ ! -f $orig ]; then
   echo "    cpifexits() WARNING: $orig not found, searching with pathof()..."
   _file=$(pathof $(basename $orig) )
   if [ "$_file" = "" ]; then
     echo "    cpifexists(): ERROR $orig not found in PATH, please install package that contain $orig."
     return 1
   elif [ -f "$_file" ]; then
     echo "    cpifexists(): FIXED: $(basename $orig) found in other path: $_file, using it!!!"
     orig=$_file
   else
     echo "   cpifexists(): ERROR: $(basename $orig) no found in PATH, please package that contain $orig"
     return 1
   fi   
 fi

 
 if [ ! -d $DESTDIR/$dest ]; then
   echo "  cpifexits() WARNING: $DESTDIR/$dest don't exists"
 fi


copy_exec "${orig}" "${dest}"
return 0
}

copydir () {
 if [ $# != 2 ]; then
   echo "copydir() ERROR Need 2 arguments: \$1=$1 \$2=$2"
   #_echo "     DEBUG: \$1=$1 \$2=$2"
   return 1
 fi
 if [ ! -d $1 ]; then
   echo "copydir() WARNING $1 dir not exits"
   #_echo "     DEBUG: \$1=$1 \$2=$2"
   return 1
 fi
 
#_echo "cp -ra \"${1}\" \"${DESTDIR}/${2}\""
cp -ra "${1}" "${DESTDIR}/${2}"
return 0
}

read_env_var() {
# read from env var
tmpvar=$(env| grep "^$1=" | awk -F "=" '{print $2}')
if [ "$tmpvar" = "" ]; then
  echo "$2"
else
  echo $tmpvar
fi
}

# ubuntu dapper have very old manual_add_modules that don't work correctly
tcos_manual_add_modules()
{
	for mam_x in $(modprobe --set-version="${version}" --ignore-install \
	--show-depends "${1}" 2>/dev/null | awk '/^insmod/ { print $2 }'); do
		# Prune duplicates
		if [ -e "${DESTDIR}/${mam_x}" ]; then
			continue
		fi

		mkdir -p "${DESTDIR}/$(dirname "${mam_x}")"
		ln -s "${mam_x}" "${DESTDIR}/$(dirname "${mam_x}")"
		if [ -n "${verbose}" ] && [ "${verbose}" = "y" ]; then
			echo "Adding module ${mam_x}"
		fi
	done
}


force_add_module() {
        # force copy of module modprobe dont show as dependency
        this_module=$(modprobe --set-version="${version}" -l "${1}")
        if [ ! -e "${DESTDIR}/${this_module}" ]; then
                #echo "  WARNING, adding module $(basename ${this_module}), see Debian bug: #384043"
                mkdir -p "${DESTDIR}/$(dirname "${this_module}")"
                ln -s "${this_module}" "${DESTDIR}/$(dirname "${this_module}")"
        fi
        # end of force copy
}

function parse_pxelinuxcfg(){
        if [ $TCOS_NETBOOT_MENU ]; then
          TEMPLATE=${TCOS_CONF}/pxelinux.menu.cfg.tpl
        else
          TEMPLATE=${TCOS_CONF}/pxelinux.cfg.tpl
        fi

        if [ $TCOS_NETBOOT_MENU_VESA ]; then
          TCOS_MENU_TYPE=vesamenu.c32
        else
          TCOS_MENU_TYPE=menu.c32
        fi

        if [ $TCOS_NETBOOT_HIDE_INSTALL ]; then
          TCOS_NETBOOT_HIDE_INSTALL_STRING=""
        else
          TCOS_NETBOOT_HIDE_INSTALL_STRING="#"
        fi

        if [ "$1" != "" ]; then
           DESTFILE=$1
        else
           DESTFILE=${TFTP_DIR}/pxelinux.cfg/default
        fi
        echo "## Generated file don't edit, edit $TEMPLATE instead" > ${DESTFILE}
        echo "## File generated by gentcos on $(date)"                               >> ${DESTFILE}
        sed -e "s/__TCOS_KERNEL__/${TCOS_KERNEL}/g"\
            -e "s/__DATE__/$(date)/g" \
            -e "s/__TCOS_MENU_TYPE__/${TCOS_MENU_TYPE}/g" \
            -e "s/__TCOS_MENU_HIDE_INSTALL__/${TCOS_NETBOOT_HIDE_INSTALL_STRING}/g" \
            -e "s/__TCOS_MENU_PASSWD__/${TCOS_ROOT_PASSWD}/g" \
            -e "s/__TFTP_MSG__/${TFTP_MSG}/g" ${TEMPLATE}                            >> ${DESTFILE}
}