Esempio n. 1
0
function build()
{
    $unix = new unix();
    $sock = new sockets();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . "pid";
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid)) {
        build_progress("{$GLOBALS["deflog_start"]} Already process exists {$pid}", 110);
        echo "{$GLOBALS["deflog_start"]} Already process exists {$pid}\n";
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $year = date('Y');
    $month = date('m');
    $EnableISCSI = intval($sock->GET_INFO("EnableISCSI"));
    $dd = $unix->find_program("dd");
    if ($EnableISCSI == 0) {
        build_progress("{$GLOBALS["deflog_start"]} {service_disabled}", 110);
        return;
    }
    $sql = "SELECT * FROM iscsi_params ORDER BY ID DESC";
    $q = new mysql();
    $c = 0;
    $dd = $unix->find_program("dd");
    $results = $q->QUERY_SQL($sql, 'artica_backup');
    if (!$q->ok) {
        build_progress("{$GLOBALS["deflog_start"]} MySQL error", 110);
        echo "{$GLOBALS["deflog_start"]} {$q->mysql_error}\n";
        return;
    }
    build_progress("{$GLOBALS["deflog_start"]} {building}...", 10);
    $max = mysql_num_rows($results);
    while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) {
        $hostname = $ligne["hostname"];
        $artica_type = $ligne["type"];
        $tbl = explode(".", $hostname);
        echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] ressource type:{$artica_type} {$ligne["dev"]}\n";
        build_progress("{$GLOBALS["deflog_start"]} {building} {$c}/{$max} {$artica_type} {$ligne["dev"]}", 20);
        if ($artica_type == "file") {
            if (!stat_system($ligne["dev"])) {
                echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] creating file {$ligne["dev"]} {$ligne["file_size"]}Go\n";
                $countsize = $ligne["file_size"] * 1000;
                $cmd = "{$dd} if=/dev/zero of={$ligne["dev"]} bs=1M count={$countsize}";
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] {$cmd}\n";
                }
                shell_exec($cmd);
                if (!stat_system($ligne["dev"])) {
                    build_progress("{$GLOBALS["deflog_start"]} {building} {$artica_type} {$ligne["dev"]} {failed}", 20);
                    echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] failed\n";
                    continue;
                }
            }
        }
        krsort($tbl);
        $newhostname = @implode(".", $tbl);
        $Params = unserialize(base64_decode($ligne["Params"]));
        if (!isset($Params["ImmediateData"])) {
            $Params["ImmediateData"] = 1;
        }
        if (!isset($Params["MaxConnections"])) {
            $Params["MaxConnections"] = 1;
        }
        if (!isset($Params["Wthreads"])) {
            $Params["Wthreads"] = 8;
        }
        if (!isset($Params["IoType"])) {
            $Params["IoType"] = "fileio";
        }
        if (!isset($Params["mode"])) {
            $Params["mode"] = "wb";
        }
        if (!is_numeric($Params["MaxConnections"])) {
            $Params["MaxConnections"] = 1;
        }
        if (!is_numeric($Params["ImmediateData"])) {
            $Params["ImmediateData"] = 1;
        }
        if (!is_numeric($Params["Wthreads"])) {
            $Params["Wthreads"] = 8;
        }
        if ($Params["IoType"] == null) {
            $Params["IoType"] = "fileio";
        }
        if ($Params["mode"] == null) {
            $Params["mode"] = "wb";
        }
        $EnableAuth = $ligne["EnableAuth"];
        $uid = trim($ligne["uid"]);
        echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] EnableAuth={$ligne["EnableAuth"]}\n";
        echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] uid=\"{$uid}\"\n";
        echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] Folder name=\"{$ligne["shared_folder"]} / {$ligne["type"]}\"\n";
        echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] Path=\"{$ligne["dev"]}\"\n";
        if ($ligne["type"] == "file") {
            if (is_dir($ligne["dev"])) {
                $newpath = FormatPath($ligne["dev"], $ligne["shared_folder"]);
                echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] Path is a directory assume {$newpath}\n";
                $ligne["dev"] = $newpath;
                $q->QUERY_SQL("UPDATE iscsi_params SET `dev`='{$newpath}' WHERE ID='{$ligne["ID"]}'", "artica_backup");
            }
        }
        if (is_link($ligne["dev"])) {
            $ligne["dev"] = @readlink($ligne["dev"]);
        }
        if ($ligne["type"] == "file") {
            $pathFile = $ligne["dev"];
            $pathDir = dirname($ligne["dev"]);
            if (!is_dir($pathDir)) {
                @mkdir($pathDir, 0755, true);
            }
            if (!stat_system($pathFile)) {
                echo "{$GLOBALS["deflog_start"]} [{$ligne["ID"]}] {$pathFile} no such file, create it\n";
                build_progress("{$GLOBALS["deflog_start"]} {building} {$pathFile}", 20);
                $countsize = $ligne["file_size"] * 1000;
                $cmd = "{$dd} if=/dev/zero of={$ligne["dev"]} bs=1M count={$countsize}";
                echo "{$cmd}\n";
                system($cmd);
            }
        }
        if ($Params["ImmediateData"] == 1) {
            $Params["ImmediateData"] = "Yes";
        } else {
            $Params["ImmediateData"] = "No";
        }
        $f[] = "Target iqn.{$year}-{$month}.{$newhostname}:{$ligne["shared_folder"]}";
        if ($EnableAuth == 1) {
            if (strlen($uid) > 2) {
                echo "{$GLOBALS["deflog_start"]} Authentication enabled for {$ligne["dev"]} with member {$ligne["uid"]}\n";
                $user = new user($ligne["uid"]);
                if ($user->password != null) {
                    $f[] = "\tIncomingUser {$ligne["uid"]} {$user->password}";
                }
            }
        }
        $f[] = "\tLun {$c} Path={$ligne["dev"]},Type={$Params["IoType"]},IOMode={$Params["mode"]}";
        $f[] = "\tMaxConnections {$Params["MaxConnections"]}";
        $f[] = "\tImmediateData {$Params["MaxConnections"]}";
        $f[] = "\tWthreads {$Params["Wthreads"]}";
        /*$f[]="\tMaxRecvDataSegmentLength 65536";
        		$f[]="\tMaxXmitDataSegmentLength 65536";
        		$f[]="\tMaxBurstLength          1048576";
        		$f[]="\tFirstBurstLength        262144";
        		$f[]="\tMaxOutstandingR2T       1";
        		$f[]="\tHeaderDigest            None";
        		$f[]="\tDataDigest              None";
        		$f[]="\tNOPInterval             60";
        		$f[]="\tNOPTimeout              180";
        		$f[]="\tQueuedCommands          64";
        		*/
        $f[] = "";
        $c++;
    }
    @mkdir("/etc/iet", true, 0600);
    $hostname = $unix->hostname_g();
    $tbl = explode(".", $hostname);
    krsort($tbl);
    $newhostname = @implode(".", $tbl);
    $sql = "SELECT * FROM users_containers WHERE created=1 AND onerror=0 AND iscsid=1";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    $count = mysql_num_rows($results);
    if ($count > 0) {
        $sock = new sockets();
        $sock->SET_INFO("EnableISCSI", 1);
    }
    build_progress("{checking_containers}", 30);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $directory = trim($ligne["directory"]);
        $ID = $ligne["container_id"];
        $container_time = $ligne["container_time"];
        if (!is_numeric($container_time)) {
            $container_time = 0;
        }
        if ($container_time == 0) {
            $container_time = time();
            $q->QUERY_SQL("UPDATE users_containers SET container_time={$container_time} WHERE container_id={$ID}", "artica_backup");
        }
        $year = date("Y", $container_time);
        $month = date("m", $container_time);
        if ($directory == null) {
            echo "{$GLOBALS["deflog_start"]} id:{$ID} No specified main directory...";
            continue;
        }
        $ContainerFullPath = $directory . "/{$ID}.disk";
        $f[] = "Target iqn.{$year}-{$month}.{$newhostname}:disk{$ID}";
        $webdav_creds = unserialize(base64_decode($ligne["webdav_creds"]));
        echo "{$GLOBALS["deflog_start"]} iqn.{$year}-{$month}.{$newhostname} {$ID}.disk LUN {$ContainerFullPath}\n";
        build_progress("iqn.{$year}-{$month}.{$newhostname} {$ID}.disk", 35);
        $f[] = "\tIncomingUser {$webdav_creds["username"]} {$webdav_creds["password"]}";
        $f[] = "\tLun {$c} Path={$ContainerFullPath},Type=fileio,IOMode=wb";
        $f[] = "\tMaxConnections 5";
        $f[] = "\tImmediateData Yes";
        $f[] = "\tWthreads 8";
        $f[] = "";
    }
    build_progress("{saving_configuration}", 40);
    echo "{$GLOBALS["deflog_start"]} ietd.conf done\n";
    @file_put_contents("/etc/iet/ietd.conf", @implode("\n", $f));
    @file_put_contents("/etc/ietd.conf", @implode("\n", $f));
    build_progress("{checking_startup_script}", 50);
    system($unix->LOCATE_PHP5_BIN() . " /usr/share/artica-postfix/exec.initslapd.php --iscsi");
    if ($GLOBALS["PROGRESS"]) {
        build_progress("{restarting}", 80);
        system("/etc/init.d/iscsitarget restart");
    }
    build_progress("{done}", 100);
}
Esempio n. 2
0
function build_dd($path, $size)
{
    $dir = dirname($path);
    if (!is_dir($dir)) {
        writelogs("{$dir} no such directory, create it", __FUNCTION__, __FILE__, __LINE__);
        @mkdir(dirname($path), 644, true);
    }
    if (!is_dir($dir)) {
        writelogs("{$dir} no such directory", __FUNCTION__, __FILE__, __LINE__);
        return false;
    }
    $unix = new unix();
    $dd = $unix->find_program("dd");
    $size = $size * 1024;
    $cmd = "{$dd} if=/dev/zero of={$path} bs=1024 count={$size} 2>&1";
    if ($GLOBALS["VERBOSE"]) {
        echo "{$cmd}\n";
    }
    exec($cmd, $results);
    echo "build_dd() {$cmd} " . count($results) . " rows\n";
    while (list($num, $ligne) = each($results)) {
        echo "build_dd() {$ligne}\n";
    }
    if (!stat_system($path)) {
        echo "build_dd() {$path} no such block\n";
        return false;
    }
    if (build_loop($path)) {
        return true;
    }
}
Esempio n. 3
0
function build()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . "pid";
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid)) {
        echo "Already process exists {$pid}\n";
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $year = date('Y');
    $month = date('m');
    $sql = "SELECT * FROM iscsi_params ORDER BY ID DESC";
    $q = new mysql();
    $c = 0;
    $dd = $unix->find_program("dd");
    $results = $q->QUERY_SQL($sql, 'artica_backup');
    if (!$q->ok) {
        echo "Starting......: ietd {$q->mysql_error}\n";
        return;
    }
    if (mysql_num_rows($results) == 0) {
        echo "Starting......: ietd no iSCSI disk scheduled\n";
        return;
    }
    while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) {
        $hostname = $ligne["hostname"];
        $artica_type = $ligne["type"];
        $tbl = explode(".", $hostname);
        echo "Starting......: ietd [{$ligne["ID"]}] ressource type:{$artica_type} {$ligne["dev"]}\n";
        if ($artica_type == "file") {
            if (!stat_system($ligne["dev"])) {
                echo "Starting......: ietd [{$ligne["ID"]}] creating file {$ligne["dev"]} {$ligne["file_size"]}Go\n";
                $countsize = $ligne["file_size"] * 1000;
                $cmd = "{$dd} if=/dev/zero of={$ligne["dev"]} bs=1M count={$countsize}";
                if ($GLOBALS["VERBOSE"]) {
                    echo "Starting......: ietd [{$ligne["ID"]}] {$cmd}\n";
                }
                shell_exec($cmd);
                if (!stat_system($ligne["dev"])) {
                    echo "Starting......: ietd [{$ligne["ID"]}] failed\n";
                    continue;
                }
            }
        }
        krsort($tbl);
        $newhostname = @implode(".", $tbl);
        $Params = unserialize(base64_decode($ligne["Params"]));
        if (!isset($Params["ImmediateData"])) {
            $Params["ImmediateData"] = 1;
        }
        if (!isset($Params["MaxConnections"])) {
            $Params["MaxConnections"] = 1;
        }
        if (!isset($Params["Wthreads"])) {
            $Params["Wthreads"] = 8;
        }
        if (!isset($Params["IoType"])) {
            $Params["IoType"] = "fileio";
        }
        if (!isset($Params["mode"])) {
            $Params["mode"] = "wb";
        }
        if (!is_numeric($Params["MaxConnections"])) {
            $Params["MaxConnections"] = 1;
        }
        if (!is_numeric($Params["ImmediateData"])) {
            $Params["ImmediateData"] = 1;
        }
        if (!is_numeric($Params["Wthreads"])) {
            $Params["Wthreads"] = 8;
        }
        if ($Params["IoType"] == null) {
            $Params["IoType"] = "fileio";
        }
        if ($Params["mode"] == null) {
            $Params["mode"] = "wb";
        }
        $EnableAuth = $ligne["EnableAuth"];
        $uid = trim($ligne["uid"]);
        if ($GLOBALS["VERBOSE"]) {
            echo "Starting......: ietd [{$ligne["ID"]}] EnableAuth={$ligne["EnableAuth"]}\n";
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "Starting......: ietd [{$ligne["ID"]}] uid=\"{$uid}\"\n";
        }
        if ($Params["ImmediateData"] == 1) {
            $Params["ImmediateData"] = "Yes";
        } else {
            $Params["ImmediateData"] = "No";
        }
        $f[] = "Target iqn.{$year}-{$month}.{$newhostname}:{$ligne["shared_folder"]}";
        if ($EnableAuth == 1) {
            if (strlen($uid) > 2) {
                echo "Starting......: ietd Authentication enabled for {$ligne["dev"]} with member {$ligne["uid"]}\n";
                $user = new user($ligne["uid"]);
                if ($user->password != null) {
                    $f[] = "\tIncomingUser {$ligne["uid"]} {$user->password}";
                }
            }
        }
        $f[] = "\tLun {$c} Path={$ligne["dev"]},Type={$Params["IoType"]},IOMode={$Params["mode"]}";
        $f[] = "\tMaxConnections {$Params["MaxConnections"]}";
        $f[] = "\tImmediateData {$Params["MaxConnections"]}";
        $f[] = "\tWthreads {$Params["Wthreads"]}";
        $f[] = "";
        $c++;
    }
    @mkdir("/etc/iet", true, 0600);
    @file_put_contents("/etc/iet/ietd.conf", @implode("\n", $f));
    @file_put_contents("/etc/ietd.conf", @implode("\n", $f));
    if (is_file("/etc/init.d/iscsitarget")) {
        shell_exec("/etc/init.d/iscsitarget restart");
    }
}