[58828de] | 1 | #@file ogfunctions.lib |
---|
| 2 | #@brief Librería o clase para la generación del 1erFS |
---|
| 3 | #@class client |
---|
| 4 | #@brief Funciones para la generación del primers sistema (initrd) |
---|
| 5 | #@version 0.91 |
---|
| 6 | #@warning License: GNU GPLv3+ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | ogExportKernelParameters () |
---|
| 12 | { |
---|
| 13 | GLOBAL="cat /proc/cmdline" |
---|
| 14 | for i in `${GLOBAL}` |
---|
| 15 | do |
---|
| 16 | echo $i | grep "=" > /dev/null && export $i |
---|
| 17 | done |
---|
| 18 | return 0 |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | ogExportVarEnvironment () |
---|
| 22 | { |
---|
| 23 | #puntos de accesos al servidor ogprotocol=nfs |
---|
| 24 | export NFSROOTBOOT="/var/lib/tftpboot" |
---|
| 25 | export NFSCLIENTDIR="/opt/opengnsys/client" |
---|
| 26 | export NFSLOGDIR="/opt/opengnsys/log/clients" |
---|
| 27 | export NFSIMGDIR="/opt/opengnsys/images" |
---|
| 28 | #puntos de accesos al servidor ogprotocol=smb |
---|
| 29 | export SMBROOTBOOT="tftpboot" |
---|
| 30 | export SMBCLIENTDIR="ogclient" |
---|
| 31 | export SMBLOGDIR="oglog" |
---|
| 32 | export SMBIMGDIR="ogimages" |
---|
| 33 | |
---|
| 34 | #puntos de montaje local ram o cache |
---|
| 35 | export LOCALCLIENTDIR="/opt/opengnsys" |
---|
| 36 | export LOCALLOGDIR="/opt/opengnsys/log" |
---|
| 37 | export LOCALIMGDIR="/opt/opengnsys/images" |
---|
| 38 | export LOCALROOTBOOT="/opt/og2fs/tftpboot" #punto de montaje del contendor tftpboot |
---|
| 39 | export LOCALROOTIMG="/opt/og2fs/2ndfs" #punto de montaje donde se accede al 2nd FS mediante loop |
---|
| 40 | export LOCALROOTRAM="/opt/og2fs/1stfs" #punto de montaje para unionfs |
---|
| 41 | export LOCALROOTUNION="/opt/og2fs/unionfs" #punto de union entreo LOCALROOTIMG y LOCALROOTRAM |
---|
| 42 | ##INFORMACION DE OTRAS VARIABLES OBTENDIAS EN OTRAS FUNCIONES. |
---|
| 43 | #ogGetROOTSERVER() ip del servidor pxe, valor obtenido automaticamente desde dhcpd. |
---|
| 44 | #IPV4DDR |
---|
| 45 | #IPV4BROADCAST |
---|
| 46 | #IPV4NETMASK |
---|
| 47 | #IPV4GATEWAY |
---|
| 48 | #DNS0 DNS1 |
---|
| 49 | #HOSTNAME |
---|
| 50 | #ROOTSERVER #ip del servidor pxe que ha servido el kernel |
---|
| 51 | #REPOSERVER=ogrepo -> ogConectROOTSERVER() ip del servidor de images para separar serviicios. |
---|
| 52 | return 0 |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | ogConfigureRamfs () |
---|
| 56 | { |
---|
| 57 | mkdir -p $LOCALROOTBOOT |
---|
| 58 | mkdir -p $LOCALROOTIMG |
---|
| 59 | mkdir -p $LOCALROOTRAM |
---|
| 60 | mkdir -p $LOCALROOTUNION |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | ogLoadNetModule () |
---|
| 64 | { |
---|
| 65 | #cargando netmodule |
---|
| 66 | if [ -n "$ognetmodule" ] |
---|
| 67 | then |
---|
| 68 | echo "Cargando modulo de red $netmodule" |
---|
| 69 | insmod `find /lib/modules/ -name ${netmodule}*` |
---|
| 70 | fi |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | ogPostConfigureFS() |
---|
| 75 | { |
---|
| 76 | # configuramos el /etc/hostname. |
---|
| 77 | echo $HOSTNAME > /etc/hostname |
---|
| 78 | |
---|
| 79 | #configuramos el /etc/hosts |
---|
| 80 | echo "127.0.0.1 localhost" > /etc/hosts |
---|
| 81 | echo "$IPV4ADDR $HOSTNAME" >> /etc/hosts |
---|
| 82 | |
---|
| 83 | #configuramos el host.conf |
---|
| 84 | echo "order hosts,bind" > /etc/host.conf |
---|
| 85 | echo "multi on" >> /etc/host.conf |
---|
| 86 | |
---|
| 87 | # configuramos el /etc/networks |
---|
| 88 | #read -e NETIP NETDEFAULT <<<$(route -n | grep eth0 | awk -F" " '{print $1}') |
---|
| 89 | NETIP=$(route -n | grep eth0 | awk -F" " '{print $1}') && NETIP=$(echo $NETIP | cut -f1 -d" ") |
---|
| 90 | echo "default 0.0.0.0" > /etc/networks |
---|
| 91 | echo "loopback 127.0.0.0" >> /etc/networks |
---|
| 92 | echo "link-local 169.254.0.0" >> /etc/networks |
---|
| 93 | echo "localnet $NETIP" >> /etc/networks |
---|
| 94 | #route |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | ogGetROOTSERVER () |
---|
| 101 | { |
---|
| 102 | # get nfs root from dhcp |
---|
| 103 | if [ "x${NFSROOT}" = "xauto" ]; then |
---|
| 104 | # check if server ip is part of dhcp root-path |
---|
| 105 | if [ "${ROOTPATH#*:}" = "${ROOTPATH}" ]; then |
---|
| 106 | NFSROOT=${ROOTSERVER}:${ROOTPATH} |
---|
| 107 | else |
---|
| 108 | NFSROOT=${ROOTPATH} |
---|
| 109 | fi |
---|
| 110 | |
---|
| 111 | # nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>] |
---|
| 112 | elif [ -n "${NFSROOT}" ]; then |
---|
| 113 | # nfs options are an optional arg |
---|
| 114 | if [ "${NFSROOT#*,}" != "${NFSROOT}" ]; then |
---|
| 115 | NFSOPTS="-o ${NFSROOT#*,}" |
---|
| 116 | fi |
---|
| 117 | NFSROOT=${NFSROOT%%,*} |
---|
| 118 | if [ "${NFSROOT#*:}" = "$NFSROOT" ]; then |
---|
| 119 | NFSROOT=${ROOTSERVER}:${NFSROOT} |
---|
| 120 | fi |
---|
| 121 | fi |
---|
| 122 | export ROOTSERVER |
---|
| 123 | return 0 |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | ogConectROOTSERVER () |
---|
| 127 | { |
---|
| 128 | local OPTIONS |
---|
| 129 | #params a detectar |
---|
| 130 | if [ $ogrepo ] |
---|
| 131 | then |
---|
| 132 | # Validar si la ip es correcta |
---|
| 133 | ROOTREPO=$ogrepo |
---|
| 134 | else |
---|
| 135 | ROOTREPO=$ROOTSERVER |
---|
| 136 | fi |
---|
| 137 | |
---|
| 138 | case "$ogprotocol" in |
---|
| 139 | cdrom) |
---|
| 140 | echo "Montar imagen de CD-ROM" |
---|
| 141 | blkid /dev/s* |
---|
| 142 | mount -t iso9660 LABEL=ogClient $LOCALROOTBOOT |
---|
| 143 | ;; |
---|
| 144 | httfs) |
---|
| 145 | echo "protocolo httfs aun no soportado" |
---|
| 146 | ;; |
---|
| 147 | sshfs) |
---|
| 148 | echo "protocolo sshfs aun no soportado" |
---|
| 149 | ;; |
---|
| 150 | smb) |
---|
| 151 | echo "Preparando conexión con el Repositorio $ROOTSERVER por $ogprotocol" |
---|
| 152 | OPTIONS=" -o user=opengnsys,pass=og" |
---|
| 153 | mount.cifs //${ROOTSERVER}/${SMBCLIENTDIR} $LOCALCLIENTDIR $OPTIONS |
---|
| 154 | mount.cifs //${ROOTSERVER}/${SMBLOGDIR} $LOCALLOGDIR $OPTIONS |
---|
| 155 | mount.cifs //${ROOTSERVER}/${SMBROOTBOOT} $LOCALROOTBOOT $OPTIONS |
---|
| 156 | mount.cifs //${ROOTREPO}/${SMBIMGDIR} $LOCALIMGDIR ${OPTIONS},ro |
---|
| 157 | ;; |
---|
| 158 | nfs) |
---|
| 159 | echo "Preparando conexión con el Repositorio $ROOTSERVER por $ogprotocol" |
---|
| 160 | nfsmount -o nolock,ro $ROOTSERVER:$NFSCLIENTDIR $LOCALCLIENTDIR |
---|
| 161 | nfsmount -o nolock $ROOTSERVER:$NFSLOGDIR $LOCALLOGDIR |
---|
| 162 | nfsmount -o nolock $ROOTSERVER:$NFSROOTBOOT $LOCALROOTBOOT |
---|
| 163 | nfsmount -o nolock,ro $ROOTREPO:$NFSIMGDIR $LOCALIMGDIR |
---|
| 164 | ;; |
---|
| 165 | esac |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | ogMerge2ndFile() |
---|
| 169 | { |
---|
| 170 | if [ -f $LOCALROOTBOOT/ogclient/ogclient.sqfs ] |
---|
| 171 | then |
---|
| 172 | cat /proc/mounts > /tmp/mtab.preunion |
---|
| 173 | if [ "$og2nd" == "img" ] |
---|
| 174 | then |
---|
| 175 | #para acceder al img |
---|
| 176 | losetup /dev/loop0 $LOCALROOTBOOT/ogclient/ogclient.img -o 32256 |
---|
| 177 | mount /dev/loop0 $LOCALROOTIMG |
---|
| 178 | else |
---|
| 179 | ## para acceder al squashfs |
---|
| 180 | mount $LOCALROOTBOOT/ogclient/ogclient.sqfs $LOCALROOTIMG -t squashfs -o loop |
---|
| 181 | fi |
---|
| 182 | for i in etc var lib bin sbin usr root boot; do |
---|
| 183 | unionmount $i |
---|
| 184 | done |
---|
| 185 | cat /tmp/mtab.preunion > /etc/mtab |
---|
| 186 | else |
---|
| 187 | echo "Fichero imagen del cliente no encontrado" |
---|
| 188 | return 1 |
---|
| 189 | fi |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | unionmount() |
---|
| 194 | { |
---|
| 195 | tmpdir=/$1 #dir |
---|
| 196 | FUSE_OPT="-o default_permissions -o allow_other -o use_ino -o nonempty -o suid" |
---|
| 197 | UNION_OPT="-o cow -o noinitgroups" |
---|
| 198 | UBIN="unionfs-fuse" |
---|
| 199 | #UPATH="/unionfs" |
---|
| 200 | #LOCALROOTIMG="/opt/og2fs/2ndfs" |
---|
| 201 | #LOCALROOTRAM="/opt/og2fs/1stfs" #/unionfs/host #punto de montaje para unionfs |
---|
| 202 | #LOCALROOTUNION=/opt/og2fs/unionfs/" #/unionfs/union #punto de union entreo LOCALROOTIMG y LOCALROOTRAM |
---|
| 203 | #mkdir -p $LOCALROOTRAM #/unionfs/host |
---|
| 204 | #mkdir -p $LOCALROOTUNION #/unionfs/union |
---|
| 205 | mkdir -p $LOCALROOTRAM$tmpdir |
---|
| 206 | #mount --bind /$tmpdir $LOCALROOTRAM$tmpdir |
---|
| 207 | U1STDIR="${LOCALROOTRAM}${tmpdir}=RW" |
---|
| 208 | U2NDDIR="${LOCALROOTIMG}${tmpdir}=RO" |
---|
| 209 | UNIONDIR=$LOCALROOTUNION$tmpdir |
---|
| 210 | mkdir -p $UNIONDIR |
---|
| 211 | $UBIN $FUSE_OPT $UNION_OPT ${U1STDIR}:${U2NDDIR} $UNIONDIR |
---|
| 212 | mount --bind $UNIONDIR $tmpdir |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | unionmountOLD() |
---|
| 217 | { |
---|
| 218 | FUSE_OPT="-o default_permissions -o allow_other -o use_ino -o nonempty -o suid" |
---|
| 219 | UNION_OPT="-o cow -o noinitgroups" |
---|
| 220 | UPATH="/unionfs" |
---|
| 221 | UBIN="unionfs-fuse" |
---|
| 222 | mkdir -p /unionfs/host |
---|
| 223 | mkdir -p /unionfs/union |
---|
| 224 | dir=$1 |
---|
| 225 | mkdir -p /unionfs/host/$dir |
---|
| 226 | #mount --bind /$dir /unionfs/host/$dir |
---|
| 227 | mkdir -p /unionfs/union/$dir |
---|
| 228 | host="/unionfs/host/${dir}=RW" |
---|
| 229 | common="/opt/og2fs/${dir}=RO" |
---|
| 230 | $UBIN $FUSE_OPT $UNION_OPT ${host}:${common} /unionfs/union/$dir |
---|
| 231 | mount --bind /unionfs/union/$dir /$dir |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | ogconfigure_lo() |
---|
| 235 | { |
---|
| 236 | # for the portmapper we need localhost |
---|
| 237 | ifconfig lo 127.0.0.1 |
---|
| 238 | #/etc/init.d/portmap start |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | ogconfigure_networking() |
---|
| 242 | { |
---|
| 243 | IP=$IPOPTS |
---|
| 244 | # http://paste.ubuntu.com/427631/ Paste from yofel at Tue, 4 May 2010 13:49:56 +0000 |
---|
| 245 | if [ -n "${BOOTIF}" ]; then |
---|
| 246 | # pxelinux sets BOOTIF to a value based on the mac address of the |
---|
| 247 | # network card used to PXE boot, so use this value for DEVICE rather |
---|
| 248 | # than a hard-coded device name from initramfs.conf. this facilitates |
---|
| 249 | # network booting when machines may have multiple network cards. |
---|
| 250 | # pxelinux sets BOOTIF to 01-$mac_address |
---|
| 251 | |
---|
| 252 | # strip off the leading "01-", which isn't part of the mac |
---|
| 253 | # address |
---|
| 254 | temp_mac=${BOOTIF#*-} |
---|
| 255 | |
---|
| 256 | # convert to typical mac address format by replacing "-" with ":" |
---|
| 257 | bootif_mac="" |
---|
| 258 | IFS='-' |
---|
| 259 | for x in $temp_mac ; do |
---|
| 260 | if [ -z "$bootif_mac" ]; then |
---|
| 261 | bootif_mac="$x" |
---|
| 262 | else |
---|
| 263 | bootif_mac="$x:$bootif_mac" |
---|
| 264 | fi |
---|
| 265 | done |
---|
| 266 | unset IFS |
---|
| 267 | |
---|
| 268 | # look for devices with matching mac address, and set DEVICE to |
---|
| 269 | # appropriate value if match is found. |
---|
| 270 | for device in /sys/class/net/* ; do |
---|
| 271 | if [ -f "$device/address" ]; then |
---|
| 272 | current_mac=$(cat "$device/address") |
---|
| 273 | if [ "$bootif_mac" = "$current_mac" ]; then |
---|
| 274 | DEVICE=${device##*/} |
---|
| 275 | break |
---|
| 276 | fi |
---|
| 277 | fi |
---|
| 278 | done |
---|
| 279 | fi |
---|
| 280 | |
---|
| 281 | # networking already configured thus bail out |
---|
| 282 | [ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ] && return 0 |
---|
| 283 | |
---|
| 284 | # support ip options see linux sources |
---|
| 285 | # Documentation/filesystems/nfsroot.txt |
---|
| 286 | # Documentation/frv/booting.txt |
---|
| 287 | |
---|
| 288 | for ROUNDTTT in 2 3 4 6 9 16 25 36 64 100; do |
---|
| 289 | |
---|
| 290 | # The NIC is to be configured if this file does not exist. |
---|
| 291 | # Ip-Config tries to create this file and when it succeds |
---|
| 292 | # creating the file, ipconfig is not run again. |
---|
| 293 | if [ -e /tmp/net-"${DEVICE}".conf ]; then |
---|
| 294 | break; |
---|
| 295 | fi |
---|
| 296 | |
---|
| 297 | case ${IP} in |
---|
| 298 | none|off) |
---|
| 299 | # Do nothing |
---|
| 300 | ;; |
---|
| 301 | ""|on|any) |
---|
| 302 | # Bring up device |
---|
| 303 | echo "Setting $DEVICE with kernel params $IP: ipconfig -t ${ROUNDTTT} ${DEVICE} " |
---|
| 304 | ipconfig -t ${ROUNDTTT} ${DEVICE} |
---|
| 305 | ;; |
---|
| 306 | dhcp|bootp|rarp|both) |
---|
| 307 | echo "Setting $DEVICE with (dhcp) kernel params $IP: ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} " |
---|
| 308 | ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} |
---|
| 309 | ;; |
---|
| 310 | *) |
---|
| 311 | echo "Setting $DEVICE with kernel params $IP: ipconfig -t ${ROUNDTTT} -d $IP " |
---|
| 312 | ipconfig -t ${ROUNDTTT} -d $IP |
---|
| 313 | |
---|
| 314 | # grab device entry from ip option |
---|
| 315 | NEW_DEVICE=${IP#*:*:*:*:*:*} |
---|
| 316 | if [ "${NEW_DEVICE}" != "${IP}" ]; then |
---|
| 317 | NEW_DEVICE=${NEW_DEVICE%:*} |
---|
| 318 | else |
---|
| 319 | # wrong parse, possibly only a partial string |
---|
| 320 | NEW_DEVICE= |
---|
| 321 | fi |
---|
| 322 | if [ -n "${NEW_DEVICE}" ]; then |
---|
| 323 | DEVICE="${NEW_DEVICE}" |
---|
| 324 | fi |
---|
| 325 | ;; |
---|
| 326 | esac |
---|
| 327 | done |
---|
| 328 | |
---|
| 329 | # source ipconfig output |
---|
| 330 | if [ -n "${DEVICE}" ]; then |
---|
| 331 | # source specific bootdevice |
---|
| 332 | . /tmp/net-${DEVICE}.conf |
---|
| 333 | else |
---|
| 334 | # source any interface as not exaclty specified |
---|
| 335 | . /tmp/net-*.conf |
---|
| 336 | fi |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | ##################################################################### |
---|
| 340 | # Ask yesno question. |
---|
| 341 | # |
---|
| 342 | # Usage: yesno OPTIONS QUESTION |
---|
| 343 | # |
---|
| 344 | # Options: |
---|
| 345 | # --timeout N Timeout if no input seen in N seconds. |
---|
| 346 | # --default ANS Use ANS as the default answer on timeout or |
---|
| 347 | # if an empty answer is provided. |
---|
| 348 | # |
---|
| 349 | # Exit status is the answer. 0=yes 1=no |
---|
| 350 | |
---|
| 351 | ogYesNo() |
---|
| 352 | { |
---|
| 353 | local ans |
---|
| 354 | local ok=0 |
---|
| 355 | local timeout=0 |
---|
| 356 | local default |
---|
| 357 | local t |
---|
| 358 | |
---|
| 359 | while [[ "$1" ]] |
---|
| 360 | do |
---|
| 361 | case "$1" in |
---|
| 362 | --default) |
---|
| 363 | shift |
---|
| 364 | default=$1 |
---|
| 365 | if [[ ! "$default" ]]; then error "Missing default value"; fi |
---|
| 366 | t=$(echo $default | tr '[:upper:]' '[:lower:]') |
---|
| 367 | |
---|
| 368 | if [[ "$t" != 'y' && "$t" != 'yes' && "$t" != 'n' && "$t" != 'no' ]]; then |
---|
| 369 | error "Illegal default answer: $default" |
---|
| 370 | fi |
---|
| 371 | default=$t |
---|
| 372 | shift |
---|
| 373 | ;; |
---|
| 374 | |
---|
| 375 | --timeout) |
---|
| 376 | shift |
---|
| 377 | timeout=$1 |
---|
| 378 | if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi |
---|
| 379 | #if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value: $timeout"; fi |
---|
| 380 | shift |
---|
| 381 | ;; |
---|
| 382 | |
---|
| 383 | -*) |
---|
| 384 | error "Unrecognized option: $1" |
---|
| 385 | ;; |
---|
| 386 | |
---|
| 387 | *) |
---|
| 388 | break |
---|
| 389 | ;; |
---|
| 390 | esac |
---|
| 391 | done |
---|
| 392 | |
---|
| 393 | if [[ $timeout -ne 0 && ! "$default" ]]; then |
---|
| 394 | error "Non-zero timeout requires a default answer" |
---|
| 395 | fi |
---|
| 396 | |
---|
| 397 | if [[ ! "$*" ]]; then error "Missing question"; fi |
---|
| 398 | |
---|
| 399 | while [[ $ok -eq 0 ]] |
---|
| 400 | do |
---|
| 401 | if [[ $timeout -ne 0 ]]; then |
---|
| 402 | if ! read -t $timeout -p "$*" ans; then |
---|
| 403 | ans=$default |
---|
| 404 | else |
---|
| 405 | # Turn off timeout if answer entered. |
---|
| 406 | timeout=0 |
---|
| 407 | if [[ ! "$ans" ]]; then ans=$default; fi |
---|
| 408 | fi |
---|
| 409 | else |
---|
| 410 | read -p "$*" ans |
---|
| 411 | if [[ ! "$ans" ]]; then |
---|
| 412 | ans=$default |
---|
| 413 | else |
---|
| 414 | ans=$(echo $ans | tr '[:upper:]' '[:lower:]') |
---|
| 415 | fi |
---|
| 416 | fi |
---|
| 417 | |
---|
| 418 | if [[ "$ans" == 'y' || "$ans" == 'yes' || "$ans" == 'n' || "$ans" == 'no' ]]; then |
---|
| 419 | ok=1 |
---|
| 420 | fi |
---|
| 421 | |
---|
| 422 | if [[ $ok -eq 0 ]]; then warning "Valid answers are: yes y no n"; fi |
---|
| 423 | done |
---|
| 424 | [[ "$ans" = "y" || "$ans" == "yes" ]] |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | |
---|