Perry Myers
2009-Jan-18 08:02 UTC
[Ovirt-devel] [PATCH node] Reworked logging for standalone mode
Entire firstboot is logged to both console/logfile using tee. Selected sections of the code are set to only log stdout/stderr to logfile to prevent data overload on console. In these cases log() function is used to write to the console. Signed-off-by: Perry Myers <pmyers at redhat.com> --- scripts/ovirt | 2 + scripts/ovirt-config-boot | 20 ++++++++++------- scripts/ovirt-config-collectd | 5 ++++ scripts/ovirt-config-hostname | 7 ++++++ scripts/ovirt-config-logging | 5 ++++ scripts/ovirt-config-networking | 28 +++++++++++++---------- scripts/ovirt-config-password | 15 +++++++++++- scripts/ovirt-config-setup | 6 +++- scripts/ovirt-config-storage | 45 +++++++++++++++++++++++++------------- scripts/ovirt-early | 2 + scripts/ovirt-firstboot | 11 ++++++--- scripts/ovirt-functions | 38 +++++++++++++++++++++++++++++++- scripts/ovirt-post | 2 + 13 files changed, 140 insertions(+), 46 deletions(-) diff --git a/scripts/ovirt b/scripts/ovirt index 6a48e47..f789d24 100755 --- a/scripts/ovirt +++ b/scripts/ovirt @@ -75,7 +75,9 @@ case "$1" in printf "Starting ovirt: " { + log "Starting ovirt" start + log "Completed ovirt" } >> $OVIRT_LOGFILE 2>&1 test $? == 0 && success || failure diff --git a/scripts/ovirt-config-boot b/scripts/ovirt-config-boot index 69e02d9..8c25d47 100755 --- a/scripts/ovirt-config-boot +++ b/scripts/ovirt-config-boot @@ -22,19 +22,21 @@ . /etc/init.d/functions . /etc/init.d/ovirt-functions +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 ovirt_boot_setup() { local disk=$1 local live=$2 local bootparams=$3 - printf "installing oVirt Node image ... " + log "installing oVirt Node image." mount_boot # check that /boot mounted ok and find partition number for GRUB BOOT=$(mount|awk '$3 == "/boot" {print $1}') BOOT=$(( ${BOOT#$disk} - 1 )) rc=$? if [ $rc -ne 0 -o $BOOT -lt 0 ]; then - printf "boot partition not available\n" + log "boot partition not available." return $rc fi mount_liveos @@ -54,7 +56,7 @@ ovirt_boot_setup() { && cp -p $live/$syslinux/vmlinuz0 /boot rc=$? if [ $rc -ne 0 ]; then - printf "image copy failed\n" + log "image copy failed." return $rc fi # append LVM support to the livecd initramfs @@ -113,7 +115,7 @@ setup --prefix=/grub (hd0) EOF rc=$? if [ $rc -ne 0 ]; then - printf "boot loader install failed\n" + log "boot loader install failed." return $rc fi umount /liveos @@ -121,14 +123,14 @@ EOF # caller decides when to reboot rm -rf $tmpdir - printf "done.\n" + log "done." # avoid reboot loops using Cobbler PXE only once # Cobbler XMLRPC post-install trigger (XXX is there cobbler SRV record?): # wget "http://192.168.50.2/cblr/svc/op/trig/mode/post/system/$(hostname)" # -O /dev/null } - +start_log disk=$1 live=$2 bootparams=$3 @@ -150,13 +152,15 @@ parted $disk -s "print all" > /dev/null if [ 0 -eq $? ]; then ovirt_boot_setup "$disk" "$live" "$bootparams" else - printf "Please configure $disk before attempting to install the node image.\n" - printf "Use the option 'Disk Partitioning' from the main menu.\n" + log "Please configure $disk before attempting to install the node image." + log "Use the option 'Disk Partitioning' from the main menu." doreboot="no" fi if [ -z "$doreboot" -o "$doreboot" = "yes" ]; then disable_firstboot ovirt_store_firstboot_config + stop_log reboot fi +stop_log diff --git a/scripts/ovirt-config-collectd b/scripts/ovirt-config-collectd index 6ece6be..bbc5a89 100755 --- a/scripts/ovirt-config-collectd +++ b/scripts/ovirt-config-collectd @@ -5,6 +5,9 @@ . /etc/init.d/functions . /etc/init.d/ovirt-functions +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 + collectd_conf="/etc/collectd.conf" # Creates the collectd file based on the following inputs @@ -15,6 +18,7 @@ ovirt_collectd() { local collectd_server=$1 local collectd_port=$2 + start_log if [ -f $collectd_conf.in ]; then sed -e "s/@COLLECTD_SERVER@/$collectd_server/" \ -e "s/@COLLECTD_PORT@/$collectd_port/" $collectd_conf.in \ @@ -26,6 +30,7 @@ ovirt_collectd() { chkconfig collectd on service collectd restart + stop_log } prompt_user() { diff --git a/scripts/ovirt-config-hostname b/scripts/ovirt-config-hostname index 2375675..bf60c70 100755 --- a/scripts/ovirt-config-hostname +++ b/scripts/ovirt-config-hostname @@ -5,18 +5,25 @@ . /etc/init.d/functions . /etc/init.d/ovirt-functions +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 + HOSTNAME_FILE="/etc/sysconfig/network" function set_hostname { + start_log augtool <<EOF set /files$HOSTNAME_FILE/HOSTNAME "$1" EOF + stop_log } function remove_hostname { + start_log augtool <<EOF rm /files$HOSTNAME_FILE/HOSTNAME EOF + stop_log } function prompt_user { diff --git a/scripts/ovirt-config-logging b/scripts/ovirt-config-logging index d6826c6..ba91468 100755 --- a/scripts/ovirt-config-logging +++ b/scripts/ovirt-config-logging @@ -5,6 +5,9 @@ . /etc/init.d/functions . /etc/init.d/ovirt-functions +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 + RSYSLOG_FILE="/etc/rsyslog.conf" # Creates the rsyslog file based on the following inputs @@ -62,7 +65,9 @@ local7.* /var/log/boot.log *.* $DELIM$1:$2 EOF +start_log /sbin/service rsyslog restart +stop_log } function is_numeric { diff --git a/scripts/ovirt-config-networking b/scripts/ovirt-config-networking index 6d55816..4b4ff10 100755 --- a/scripts/ovirt-config-networking +++ b/scripts/ovirt-config-networking @@ -13,11 +13,10 @@ WORKDIR=$(mktemp -d) || exit 1 # Remove $WORKDIR upon interrupt (and HUP, PIPE, TERM) and upon normal # termination, being careful not to change the exit status. -trap '__st=$?; rm -rf "$WORKDIR"; exit $__st' 0 +trap '__st=$?; rm -rf "$WORKDIR"; stop_log; exit $__st' 0 trap 'exit $?' 1 2 13 15 CONFIG_FILE_ROOT="/files/etc/sysconfig/network-scripts/ifcfg" -CONFIG_LOG_FILE="/var/log/ovirt-network-setup.log" function configure_interface { @@ -220,7 +219,7 @@ else setup_menu while true; do - printf "\n\n oVirt Node Networking Configuration\n\n" + printf "\n\n oVirt Node Networking Configuration\n\n" >&2 select NIC in $NICS do @@ -234,13 +233,18 @@ else done fi -{ - printf "Configuring network.\n" - if ls "$WORKDIR"/augtool-* > /dev/null 2>&1 ; then - config="$WORKDIR"/config-augtool - cat "$WORKDIR"/augtool-* > $config \ - && augtool $config \ - && service network restart +start_log +if ls "$WORKDIR"/augtool-* > /dev/null 2>&1 ; then + log "Configuring network" + config="$WORKDIR"/config-augtool + cat "$WORKDIR"/augtool-* > $config \ + && augtool $config \ + && service network restart + if [ $? = 0 ]; then + log "Network configured successfully" + else + log "Error configuring network, see $OVIRT_LOGFILE" fi - ovirt_store_config /etc/sysconfig/network-scripts/ifcfg* -} 2>&1 | tee $CONFIG_LOG_FILE +fi +ovirt_store_config /etc/sysconfig/network-scripts/ifcfg* +stop_log diff --git a/scripts/ovirt-config-password b/scripts/ovirt-config-password index 47be3f7..89b91da 100755 --- a/scripts/ovirt-config-password +++ b/scripts/ovirt-config-password @@ -1,12 +1,22 @@ #!/bin/bash # # Set the root password and others +# Source functions library +. /etc/init.d/functions +. /etc/init.d/ovirt-functions + +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 function sasl_password { user=$1 passwd=$2 - printf "adding user $user to the sasl list for libvirt\n" + + printf "\nadding user $user to the sasl list for libvirt\n" + + start_log printf '%s\n' "$passwd" | saslpasswd2 -a libvirt -p "$user" + stop_log } # Usage: set_sasl_password USER @@ -25,7 +35,9 @@ function set_sasl_password { local confirm=$REPLY if test "$passwd" = "$confirm"; then if [ "$user" = "root" ]; then + start_log printf '%s\n' "$passwd" | passwd --stdin "$user" + stop_log fi sasl_password "$user" "$passwd" return 0 @@ -53,4 +65,3 @@ set_sasl_password root printf "\nAdding new SASL users for libvirt" # TODO list existing users in /etc/libvirt/passwd.db while prompt_sasl_user; do :; done - diff --git a/scripts/ovirt-config-setup b/scripts/ovirt-config-setup index 380d79d..21e6b0e 100755 --- a/scripts/ovirt-config-setup +++ b/scripts/ovirt-config-setup @@ -2,6 +2,8 @@ # # Entry point for configuring an oVirt node when running in standalone mode. +. /etc/init.d/ovirt-functions + # symlinked scripts for menu options, link name is menu label CONFIG_DIR=/etc/ovirt-config-setup.d @@ -29,7 +31,7 @@ while true; do PS3="Please select an option: " while true ; do - printf "\n\n oVirt Node Standalone Configuration\n\n" + printf "\n\n oVirt Node Standalone Configuration\n\n" >&2 select OPTION in "${OPTIONS[@]}" do @@ -38,7 +40,7 @@ while true; do "$CONTINUE") exit 0 ;; *) clear; - $CONFIG_DIR/*"$OPTION"; + $CONFIG_DIR/*"$OPTION" 2>&1 | $TEE; break ;; esac done diff --git a/scripts/ovirt-config-storage b/scripts/ovirt-config-storage index 9928920..7489c68 100755 --- a/scripts/ovirt-config-storage +++ b/scripts/ovirt-config-storage @@ -12,6 +12,9 @@ ME=$(basename "$0") warn() { printf '%s: %s\n' "$ME" "$*" >&2; } die() { warn "$*"; exit 1; } +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 + default_boot_size=50 default_root_size=256 default_config_size=5 @@ -178,24 +181,25 @@ perform_partitioning() DRIVE_FILE=$(echo $DRIVE | tr '/' '_') if [ -f /tmp/ovirt-config-storage-$DRIVE_FILE ]; then printf "\n\nERROR:\n" - printf "\nStorage Configuration has already been run for $DRIVE_FILE.\n" - printf "To run it again, please reboot the Node from\n USB/cdrom/PXE\n" + printf "Storage Configuration has already been run for $DRIVE.\n" + printf "To run it again, please reboot the Node from USB/cdrom/PXE\n" printf "in stand-alone mode.\n\n\n" return 1 fi touch /tmp/ovirt-config-storage-$DRIVE_FILE - printf "Preparing local storage. Please wait..." + start_log + log "Starting partitioning of $DRIVE" - LOG=/var/log/ovirt-partition.log - { + log "Removing old LVM partitions" wipe_lvm_on_disk # Exit upon any failure. set -e # FIXME: save a backup copy, just in case? + log "Wiping old boot sector" dd if=/dev/zero of=$DRIVE bs=1024K count=1 blockdev --rereadpt $DRIVE partprobe -s $DRIVE @@ -204,6 +208,7 @@ perform_partitioning() # ensure minimal BOOT partition BOOT_SIZE=10 fi + log "Creating boot partition" parted $DRIVE -s "mklabel ${LABEL_TYPE}" parted $DRIVE -s "mkpartfs primary ext2 0M ${BOOT_SIZE}M" parted $DRIVE -s "mkpart primary ext2 ${BOOT_SIZE}M ${SPACE}M" @@ -214,57 +219,65 @@ perform_partitioning() # sync GPT to the legacy MBR partitions if [ "gpt" == "$LABEL_TYPE" ]; then - gptsync $DRIVE + log "Running gptsync to create legacy mbr" + gptsync $DRIVE fi + log "Creating physical volume" pvcreate "${DRIVE}2" pvck + log "Creating volume group" vgcreate /dev/HostVG "${DRIVE}2" + log "Creating boot filesystem" mke2fs -T ext3 "${DRIVE}1" -L "BOOT" tune2fs -c 0 -i 0 "${DRIVE}1" mkdir -p /dev/disk/by-label ln -snf "${DRIVE}1" /dev/disk/by-label/BOOT if [ "$SWAP_SIZE" -gt 0 ]; then + log "Creating swap partition" lvcreate --name Swap --size ${SWAP_SIZE}M /dev/HostVG mkswap -L "SWAP" /dev/HostVG/Swap echo "/dev/HostVG/Swap swap swap defaults 0 0" >> /etc/fstab fi if [ "$ROOT_SIZE" -gt 0 ]; then + log "Creating root partition" lvcreate --name Root --size ${ROOT_SIZE}M /dev/HostVG mke2fs -T ext3 /dev/HostVG/Root -L "ROOT" tune2fs -c 0 -i 0 /dev/HostVG/Root fi if [ "$CONFIG_SIZE" -gt 0 ]; then + log "Creating config partition" lvcreate --name Config --size ${CONFIG_SIZE}M /dev/HostVG mke2fs -T ext3 /dev/HostVG/Config -L "CONFIG" tune2fs -c 0 -i 0 /dev/HostVG/Config fi if [ "$LOGGING_SIZE" -gt 0 ]; then + log "Creating log partition" lvcreate --name Logging --size ${LOGGING_SIZE}M /dev/HostVG mke2fs -T ext3 /dev/HostVG/Logging -L "LOGGING" tune2fs -c 0 -i 0 /dev/HostVG/Logging echo "/dev/HostVG/Logging /var/log ext3 defaults 0 0" >> /etc/fstab - mount_logging fi + log "Creating data partition" lvcreate --name Data -l 100%FREE /dev/HostVG mke2fs -T ext3 /dev/HostVG/Data -L "DATA" tune2fs -c 0 -i 0 /dev/HostVG/Data echo "/dev/HostVG/Data /data ext3 defaults 0 0" >> /etc/fstab echo "/data/images /var/lib/libvirt/images bind bind 0 0" >> /etc/fstab + log "Mounting data partition" mount_data + log "Mounting config partition" if mount_config; then ovirt_store_config /etc/fstab fi - } 2>&1 | tee $LOG - if [ $? -eq 0 ]; then - printf "Completed!\n\n" - else - printf "FAILED! See $LOG\n\n" - fi + mount_logging + log "Completed!" + + stop_log } do_confirm() @@ -297,7 +310,7 @@ do_confirm() Y|y) check_partition_sizes perform_partitioning - exit 0 + exit 0 break ;; N|n) return ;; @@ -357,7 +370,7 @@ else PS3="Choose an option: " while true; do - printf "\n\n oVirt Node Storage Configuration\n\n" + printf "\n\n oVirt Node Storage Configuration\n\n" >&2 select OPTION in $OPTIONS do @@ -365,7 +378,7 @@ else "Configure") do_configure ; break ;; "Review") do_review ; break ;; "Partition") do_confirm ; break ;; - "Quit") exit ;; + "Quit") exit ;; esac done done diff --git a/scripts/ovirt-early b/scripts/ovirt-early index f31e977..6c7b29b 100755 --- a/scripts/ovirt-early +++ b/scripts/ovirt-early @@ -391,7 +391,9 @@ case "$1" in printf "Starting ovirt-early: " { + log "Starting ovirt-early" start + log "Completed ovirt-early" } >> $OVIRT_LOGFILE 2>&1 test $? == 0 && success || failure diff --git a/scripts/ovirt-firstboot b/scripts/ovirt-firstboot index c7ddfe4..575fa89 100755 --- a/scripts/ovirt-firstboot +++ b/scripts/ovirt-firstboot @@ -27,6 +27,9 @@ . /etc/init.d/functions . /etc/init.d/ovirt-functions +trap '__st=$?; stop_log; exit $__st' 0 +trap 'exit $?' 1 2 13 15 + start () { if ! is_firstboot && ! is_auto_install; then @@ -57,7 +60,9 @@ start () fi disable_firstboot - ovirt_store_firstboot_config >> $OVIRT_LOGFILE 2>&1 + start_log + ovirt_store_firstboot_config + stop_log } case "$1" in @@ -68,9 +73,7 @@ case "$1" in printf "Starting ovirt-firstboot: " - { - start - } + start test $? == 0 && success || failure echo diff --git a/scripts/ovirt-functions b/scripts/ovirt-functions index 814a8ae..13f9ad4 100644 --- a/scripts/ovirt-functions +++ b/scripts/ovirt-functions @@ -1,6 +1,8 @@ # -*-Shell-script-*- OVIRT_LOGFILE=/var/log/ovirt.log +TEE="tee -a $OVIRT_LOGFILE" +LOG_STATUS=0 # label of the oVirt partition OVIRT_LABEL=OVIRT @@ -28,6 +30,37 @@ OVIRT_CONFIG_FILES="\ /etc/collectd.conf " +# Save stdout to fd 6 and stderr to fd 7. Redirect normal stdout/stderr +# to log file +start_log() { + if [ $LOG_STATUS = 0 ]; then + exec 6>&1 + exec 7>&2 + exec 1>>$OVIRT_LOGFILE + exec 2>&1 + LOG_STATUS=1 + fi +} + +# Restore stdout/stderr from fd 6 and 7, respectively. Close fd 6 and 7 +stop_log() { + if [ $LOG_STATUS = 1 ]; then + exec 1>&6 6>&- + exec 2>&7 7>&- + LOG_STATUS=0 + fi +} + +log() { + printf "$(date +'%b %d %H:%M:%S') " + + if [ $LOG_STATUS = 1 ]; then + echo $@ >&6 + else + echo $@ + fi +} + ovirt_store_firstboot_config() { # persist config for standalone ovirt_store_config $OVIRT_CONFIG_FILES @@ -220,15 +253,16 @@ mount_logging() { fi if [ -e /dev/HostVG/Logging ] ; then + log "Mounting log partition" # backup the logs if [ -e /var/log.bak ]; then rm -rf /var/log.bak fi - cp -a /var/log /var/log.bak + cp -av /var/log /var/log.bak mount /dev/HostVG/Logging /var/log restorecon -rv /var/log - mv /var/log.bak/* /var/log + cp -av /var/log.bak/* /var/log rm -Rf /var/log.bak service rsyslog restart diff --git a/scripts/ovirt-post b/scripts/ovirt-post index 5c06927..3861973 100755 --- a/scripts/ovirt-post +++ b/scripts/ovirt-post @@ -34,7 +34,9 @@ case "$1" in printf "Starting ovirt-post: " { + log "Starting ovirt-post" start + log "Completed ovirt-post" } >> $OVIRT_LOGFILE 2>&1 test $? == 0 && success || failure -- 1.6.0.6
Alan Pevec
2009-Jan-18 11:31 UTC
[Ovirt-devel] Re: [PATCH node] Reworked logging for standalone mode
Perry Myers wrote:> Entire firstboot is logged to both console/logfile using tee. Selected > sections of the code are set to only log stdout/stderr to logfile to prevent > data overload on console. In these cases log() function is used to write > to the console.ACK just minor whitespace nits, which I'll fix and push> @@ -297,7 +310,7 @@ do_confirm() > Y|y) > check_partition_sizes > perform_partitioning > - exit 0 > + exit 0whitespace damage - and it's changed in "Checks the request partitions" anyway> - "Quit") exit ;; > + "Quit") exit ;;same here> @@ -220,15 +253,16 @@ mount_logging() { > fi > > if [ -e /dev/HostVG/Logging ] ; then > + log "Mounting log partition" > # backup the logs > if [ -e /var/log.bak ]; then > rm -rf /var/log.bak > fi > - cp -a /var/log /var/log.bak > + cp -av /var/log /var/log.bak > > mount /dev/HostVG/Logging /var/log > restorecon -rv /var/logmove to after cp, just in case> - mv /var/log.bak/* /var/log > + cp -av /var/log.bak/* /var/log > rm -Rf /var/log.bak > service rsyslog restart
Jim Meyering
2009-Jan-18 11:59 UTC
[Ovirt-devel] [PATCH node] Reworked logging for standalone mode
Perry Myers <pmyers at redhat.com> wrote:> Entire firstboot is logged to both console/logfile using tee. Selected > sections of the code are set to only log stdout/stderr to logfile to prevent > data overload on console. In these cases log() function is used to write > to the console.... ACK, but for some missing quotes and name tweaks> diff --git a/scripts/ovirt-functions b/scripts/ovirt-functions > index 814a8ae..13f9ad4 100644 > --- a/scripts/ovirt-functions > +++ b/scripts/ovirt-functions > @@ -1,6 +1,8 @@ > # -*-Shell-script-*- > > OVIRT_LOGFILE=/var/log/ovirt.log > +TEE="tee -a $OVIRT_LOGFILE" > +LOG_STATUS=0 > > # label of the oVirt partition > OVIRT_LABEL=OVIRT > @@ -28,6 +30,37 @@ OVIRT_CONFIG_FILES="\ > /etc/collectd.conf > " > > +# Save stdout to fd 6 and stderr to fd 7. Redirect normal stdout/stderr > +# to log file > +start_log() { > + if [ $LOG_STATUS = 0 ]; thenIt's better to quote all uses of LOG_STATUS, in case it's ever defined to something else, or empty.> + exec 6>&1 > + exec 7>&2 > + exec 1>>$OVIRT_LOGFILE > + exec 2>&1 > + LOG_STATUS=1 > + fi > +} > + > +# Restore stdout/stderr from fd 6 and 7, respectively. Close fd 6 and 7 > +stop_log() { > + if [ $LOG_STATUS = 1 ]; thenIt's slightly better to always use the same test. I.e., test against only one of 0 or 1, not both, and negate if needed. Otherwise, you have to wonder what happens if $LOG_STATUS is ever set to something like 2. E.g., you could test $LOG_STATUS != 1 in start_log, since there are two tests of $LOG_STATUS = 1 already. Or maybe you prefer "boolean" semantics and would want to test against 0.> + exec 1>&6 6>&- > + exec 2>&7 7>&- > + LOG_STATUS=0 > + fi > +} > + > +log() { > + printf "$(date +'%b %d %H:%M:%S') " > + > + if [ $LOG_STATUS = 1 ]; then > + echo $@ >&6 > + else > + echo $@Whoops. this needs double quotes around each "$@". Otherwise, logs lose TABs and sequences of spaces. E.g., $ bash -c 'foo() { echo $@; }; foo "a b"' a b> + fi > +}All names defined/used above impinge on the name space of each script that sources ovirt-functions, so it'd be nice to use names that make collisions less likely e.g., _TEE, _log and $_LOG_STATUS.