#!/bin/bash set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" INSTALL_DIR="/opt/adastra-vtl" WEB_DIR="/var/www/html" SYSTEMD_DIR="/etc/systemd/system" CONFIG_DIR="/etc/mhvtl" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' print_header() { echo -e "${BLUE}╔════════════════════════════════════════╗${NC}" echo -e "${BLUE}║ Adastra VTL Installer v1.0 ║${NC}" echo -e "${BLUE}║ Virtual Tape Library System ║${NC}" echo -e "${BLUE}╚════════════════════════════════════════╝${NC}" echo "" } print_success() { echo -e "${GREEN}✓${NC} $1" } print_error() { echo -e "${RED}✗${NC} $1" } print_info() { echo -e "${YELLOW}➜${NC} $1" } check_root() { if [ "$EUID" -ne 0 ]; then print_error "Please run as root or with sudo" exit 1 fi } detect_distro() { if [ -f /etc/os-release ]; then . /etc/os-release DISTRO=$ID VERSION=$VERSION_ID elif [ -f /etc/redhat-release ]; then DISTRO="rhel" elif [ -f /etc/debian_version ]; then DISTRO="debian" else print_error "Unable to detect Linux distribution" exit 1 fi print_info "Detected: $DISTRO $VERSION" } install_dependencies_debian() { print_info "Installing dependencies for Debian/Ubuntu..." apt-get update -qq DEBIAN_PACKAGES=( "build-essential" "git" "zlib1g-dev" "lsscsi" "mt-st" "mtx" "lsof" "sg3-utils" "apache2" "php" "libapache2-mod-php" "tgt" "open-iscsi" ) apt-get install -y "${DEBIAN_PACKAGES[@]}" systemctl enable apache2 systemctl start apache2 systemctl enable tgt systemctl start tgt print_success "Dependencies installed (Debian/Ubuntu)" } install_dependencies_rpm() { print_info "Installing dependencies for RHEL/CentOS/Fedora..." if command -v dnf &> /dev/null; then PKG_MGR="dnf" else PKG_MGR="yum" fi RPM_PACKAGES=( "gcc" "gcc-c++" "make" "git" "zlib-devel" "lsscsi" "mt-st" "mtx" "lsof" "sg3_utils" "httpd" "php" "scsi-target-utils" "iscsi-initiator-utils" ) $PKG_MGR install -y "${RPM_PACKAGES[@]}" systemctl enable httpd systemctl start httpd if systemctl list-unit-files | grep -q "tgtd.service"; then systemctl enable tgtd systemctl start tgtd fi if command -v firewall-cmd &> /dev/null; then firewall-cmd --permanent --add-service=http firewall-cmd --reload fi if command -v setenforce &> /dev/null; then setenforce 0 || true sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config 2>/dev/null || true fi print_success "Dependencies installed (RHEL/CentOS/Fedora)" } install_mhvtl() { print_info "Installing mhvtl from source..." cd /tmp if [ -d "mhvtl" ]; then rm -rf mhvtl fi git clone https://github.com/markh794/mhvtl.git cd mhvtl make make install if [ ! -d "$CONFIG_DIR" ]; then mkdir -p "$CONFIG_DIR" fi if [ -f "kernel/mhvtl.ko" ]; then cp kernel/*.ko /lib/modules/$(uname -r)/kernel/drivers/scsi/ 2>/dev/null || true depmod -a fi cd /tmp rm -rf mhvtl print_success "mhvtl installed" } install_adastra_vtl() { print_info "Installing Adastra VTL..." if [ -d "$INSTALL_DIR" ]; then print_info "Removing old installation..." rm -rf "$INSTALL_DIR" fi mkdir -p "$INSTALL_DIR" print_info "Copying scripts..." if [ -d "$SCRIPT_DIR/scripts" ]; then cp -r "$SCRIPT_DIR/scripts" "$INSTALL_DIR/" chmod +x "$INSTALL_DIR/scripts"/*.sh print_success "Scripts copied" else print_error "Scripts directory not found: $SCRIPT_DIR/scripts" fi print_info "Copying documentation..." if [ -d "$SCRIPT_DIR/docs" ]; then cp -r "$SCRIPT_DIR/docs" "$INSTALL_DIR/" print_success "Documentation copied" fi if [ -f "$SCRIPT_DIR/README.md" ]; then cp "$SCRIPT_DIR/README.md" "$INSTALL_DIR/" fi print_info "Copying configuration templates..." if [ -d "$SCRIPT_DIR/config" ]; then mkdir -p "$CONFIG_DIR" for file in "$SCRIPT_DIR/config"/*; do if [ -f "$file" ]; then filename=$(basename "$file") if [ ! -f "$CONFIG_DIR/$filename" ]; then cp "$file" "$CONFIG_DIR/" fi fi done print_success "Configuration templates copied" fi print_info "Deploying Web UI..." if [ -d "$SCRIPT_DIR/web-ui" ]; then mkdir -p "$WEB_DIR" cp -r "$SCRIPT_DIR/web-ui"/* "$WEB_DIR/" if [ "$DISTRO" = "debian" ] || [ "$DISTRO" = "ubuntu" ]; then chown -R www-data:www-data "$WEB_DIR" else chown -R apache:apache "$WEB_DIR" fi chmod -R 755 "$WEB_DIR" print_success "Web UI deployed to $WEB_DIR" else print_error "Web UI directory not found: $SCRIPT_DIR/web-ui" fi print_info "Installing systemd services..." if [ -d "$SCRIPT_DIR/systemd" ]; then cp "$SCRIPT_DIR/systemd"/*.service "$SYSTEMD_DIR/" systemctl daemon-reload print_success "Systemd services installed" else print_error "Systemd directory not found: $SCRIPT_DIR/systemd" fi print_info "Configuring sudoers for web UI..." if [ -f "$SCRIPT_DIR/config/mhvtl-sudoers" ]; then cp "$SCRIPT_DIR/config/mhvtl-sudoers" /etc/sudoers.d/mhvtl chmod 440 /etc/sudoers.d/mhvtl print_success "Sudoers configured" fi print_success "Adastra VTL installed to $INSTALL_DIR" } configure_system() { print_info "Configuring system..." if ! grep -q "^vtl:" /etc/group; then groupadd vtl fi if ! id -u vtl &>/dev/null; then useradd -r -g vtl -s /bin/bash -d /var/lib/mhvtl vtl fi mkdir -p /var/lib/mhvtl chown -R vtl:vtl /var/lib/mhvtl mkdir -p /opt/mhvtl chown -R vtl:vtl /opt/mhvtl chmod 775 /opt/mhvtl mkdir -p "$CONFIG_DIR/backups" chown -R vtl:vtl "$CONFIG_DIR" chmod 775 "$CONFIG_DIR" chmod 775 "$CONFIG_DIR/backups" chmod 664 "$CONFIG_DIR"/*.conf 2>/dev/null || true if [ "$DISTRO" = "debian" ] || [ "$DISTRO" = "ubuntu" ]; then usermod -a -G vtl www-data # Initialize users file securely if not exists if [ ! -f "/etc/mhvtl/users.json" ]; then echo '[{"username":"admin","password":"$2y$10$b7994UTOrF9YHQv1osnhwO06GsqP.grtqbT3AX7Y.jde11nTToAS2","role":"admin","created":"'$(date '+%Y-%m-%d %H:%M:%S')'","enabled":true}]' > /etc/mhvtl/users.json fi chown www-data:www-data /etc/mhvtl/users.json chmod 600 /etc/mhvtl/users.json systemctl restart apache2 2>/dev/null || true else usermod -a -G vtl apache # Initialize users file securely if not exists if [ ! -f "/etc/mhvtl/users.json" ]; then echo '[{"username":"admin","password":"$2y$10$b7994UTOrF9YHQv1osnhwO06GsqP.grtqbT3AX7Y.jde11nTToAS2","role":"admin","created":"'$(date '+%Y-%m-%d %H:%M:%S')'","enabled":true}]' > /etc/mhvtl/users.json fi chown apache:apache /etc/mhvtl/users.json chmod 600 /etc/mhvtl/users.json systemctl restart httpd 2>/dev/null || true fi if [ -f "$INSTALL_DIR/scripts/vtl" ]; then ln -sf "$INSTALL_DIR/scripts/vtl" /usr/local/bin/vtl chmod +x /usr/local/bin/vtl fi if [ -f "$INSTALL_DIR/scripts/load-mhvtl.sh" ]; then ln -sf "$INSTALL_DIR/scripts/load-mhvtl.sh" /usr/local/bin/mhvtl-load fi if [ -f "$INSTALL_DIR/scripts/unload-mhvtl.sh" ]; then ln -sf "$INSTALL_DIR/scripts/unload-mhvtl.sh" /usr/local/bin/mhvtl-unload fi print_success "System configured" } print_completion() { echo "" echo -e "${GREEN}╔════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ Installation Complete! ║${NC}" echo -e "${GREEN}╚════════════════════════════════════════╝${NC}" echo "" echo -e "${BLUE}Installation Details:${NC}" echo -e " • Install directory: ${YELLOW}$INSTALL_DIR${NC}" echo -e " • Web UI: ${YELLOW}http://$(hostname -I | awk '{print $1}')/${NC}" echo -e " • Config directory: ${YELLOW}$CONFIG_DIR${NC}" echo "" echo -e "${BLUE}Quick Start:${NC}" echo -e " 1. Load mhvtl kernel module:" echo -e " ${YELLOW}mhvtl-load${NC}" echo "" echo -e " 2. Configure via Web UI or edit:" echo -e " ${YELLOW}$CONFIG_DIR/device.conf${NC}" echo "" echo -e " 3. Start mhvtl service:" echo -e " ${YELLOW}systemctl start mhvtl${NC}" echo "" echo -e " 4. Enable on boot:" echo -e " ${YELLOW}systemctl enable mhvtl${NC}" echo "" echo -e "${BLUE}Useful Commands:${NC}" echo -e " • Load modules: ${YELLOW}mhvtl-load${NC}" echo -e " • Unload modules: ${YELLOW}mhvtl-unload${NC}" echo -e " • Check status: ${YELLOW}systemctl status mhvtl${NC}" echo -e " • View devices: ${YELLOW}lsscsi -g${NC}" echo -e " • VTL CLI Tool: ${YELLOW}vtl status${NC}" echo -e " • Default Web Login: ${YELLOW}admin / admin123${NC}" echo "" } main() { print_header check_root detect_distro case $DISTRO in ubuntu|debian|linuxmint|pop) install_dependencies_debian ;; rhel|centos|fedora|rocky|almalinux) install_dependencies_rpm ;; *) print_error "Unsupported distribution: $DISTRO" print_info "Supported: Debian, Ubuntu, RHEL, CentOS, Fedora, Rocky, AlmaLinux" exit 1 ;; esac install_mhvtl install_adastra_vtl configure_system print_info "Fixing mhvtl configuration permissions..." chmod 664 "$CONFIG_DIR"/*.conf 2>/dev/null || true print_success "Permissions fixed" print_completion } main "$@"