#!/bin/perl use strict; use integer; use warnings; sub write_file ($$); if ( not -d "/mnt/data/mysql" ) { warn "No database created\n"; if ($ENV{_auto_create} == 0) { warn "Appliance property auto_create is set to 0\n"; } exit 0; } # determine the log file that we are to pass to mysqld my $ip = "0.255.255.255" ; my $logfile = $ENV{_error_log_filename}; my $logdir ; my $loglevel = $ENV{_error_log_level }; my $ret ; my $i = 0 ; my ($name, $aliases, $addrtype, $length, @addr) = gethostbyname ("log"); $ip = (sprintf "%vd", $addr[0]) if exists $addr[0]; if ($logfile ne "") { # write message to log file if log terminal not connected if ($ip eq "0.255.255.255") { warn "Failed to start mysql due to error_log_filename set and log terminal not connected\n"; exit 1; } # disable unix extensions $ret = system "/sbin/modprobe cifs && echo 0 > /proc/fs/cifs/LinuxExtensionsEnabled"; if ($ret) { warn "Failed to disable cifs unix extensions.\n"; exit 1; } # mount the share and die if fails system "rm -f /var/cache/samba/gencache.tdb"; for ($i=0; $i<18; $i++) { $ret = system "mount -t cifs -o uid=mysql,rw,guest,file_mode=0777,dir_mode=0777 //log/share /mnt/log > /dev/null"; last if (!$ret); system "sleep 10"; } if ($i >= 18) { warn "Failed to start mysql due to failure to mount share through log terminal.\n"; exit 1; } # ensure that the share is writeable if ( not -w "/mnt/log" ) { warn "Failed to start mysql because the share through the log terminal is not writeable."; system "umount /mnt/log"; exit 1; } # strip any leading slash from the file name $logfile =~ s/^\///g; $logfile = "/mnt/log/$logfile"; # extract any directories ($logdir) = ($logfile =~ /^([\w\$\/.-]+?)\/([\w\$.-]+?)$/); # create the directory if it doesn't exist if ( not -e "$logdir") { $ret = system "mkdir -p $logdir"; if ($ret) { warn "Failed to start mysql due to failure to create directory $logdir for mysql error log.\n"; system "umount /mnt/log"; exit 1; } } # ensure that the directory is writeable if ( not -w $logdir ) { warn "Failed to start mysql because the log directory $logdir is not writeable."; system "umount /mnt/log"; exit 1; } # create the file if it doesn't exist if ( not -f $logfile ) { my @lines; # create the log file push @lines, ""; $ret = write_file ("$logfile", \@lines); if ($ret) { warn "Failed to start mysql due to failure to create log file $logfile."; system "umount /mnt/log"; exit 1; } } # make sure that the file is writeable if ( not -w $logfile ) { warn "Failed to start mysql because the log file $logfile is not writeable."; system "umount /mnt/log"; exit 1; } } # start mysql my $options; # determine mysql command line options if ($logfile eq "") { $options = "--skip-log-warnings" } else { $options = "--log-error=$logfile"; $options .= " --skip-log-warnings" if ((uc $loglevel) eq "ERROR"); $options .= " --log-warnings" if ((uc $loglevel) eq "WARN"); } $ret = system "/usr/bin/mysqld_safe $options & >/dev/null 2>&1"; if ($ret) { warn "Failed to start mysql deamon.\n"; system "umount /mnt/log"; exit 1; } for ($i=0; $i<=10; $i++ ) { sleep 1; -f '/var/run/mysqld/mysqld.pid' && last } if ($i >= 10) { warn "Failed to start mysql deamon.\n"; system "umount /mnt/log"; exit 1; } exit 0; sub write_file ($$) # (,\@) { my ($fname, $lref) = @_; return 1 if (not open F, "> $fname"); foreach (@$lref) { print F "$_\n"; } close F; return 0; } # write_file