#!/bin/bash set -e echo "==========================================" echo " iSCSI Target Configuration Script" echo "==========================================" echo "" if [ "$EUID" -ne 0 ]; then echo "Error: This script must be run as root" exit 1 fi TGT_CONFIG_DIR="/etc/tgt/conf.d" ISCSI_IQN_BASE="iqn.2024-01.com.vtl-linux" echo "[1/4] Installing iSCSI target software..." apt-get update apt-get install -y tgt echo "[2/4] Configuring iSCSI targets..." mkdir -p "$TGT_CONFIG_DIR" cat > "$TGT_CONFIG_DIR/vtl-targets.conf" << 'EOF' backing-store /dev/sg1 initiator-address ALL incominguser vtl-user vtl-password write-cache on backing-store /dev/sg2 initiator-address ALL incominguser vtl-user vtl-password write-cache on backing-store /dev/sg3 initiator-address ALL incominguser vtl-user vtl-password write-cache on backing-store /dev/sg4 initiator-address ALL incominguser vtl-user vtl-password write-cache on backing-store /dev/sg0 initiator-address ALL incominguser vtl-user vtl-password device-type changer EOF echo "[3/4] Configuring firewall..." if command -v ufw &> /dev/null; then ufw allow 3260/tcp ufw reload elif command -v firewall-cmd &> /dev/null; then firewall-cmd --permanent --add-port=3260/tcp firewall-cmd --reload else iptables -A INPUT -p tcp --dport 3260 -j ACCEPT iptables-save > /etc/iptables/rules.v4 fi echo "[4/4] Starting iSCSI target service..." systemctl enable tgt systemctl restart tgt sleep 2 echo "" echo "==========================================" echo " iSCSI Target Configuration Complete!" echo "==========================================" echo "" echo "Available targets:" tgt-admin --show echo "" echo "Connection information:" echo " - Port: 3260" echo " - IQN Base: $ISCSI_IQN_BASE" echo " - Username: vtl-user" echo " - Password: vtl-password" echo "" echo "Client connection examples:" echo "" echo "Linux:" echo " iscsiadm -m discovery -t st -p :3260" echo " iscsiadm -m node --login" echo "" echo "Windows:" echo " iscsicli QAddTargetPortal " echo " iscsicli ListTargets" echo " iscsicli LoginTarget T * * * * * * * * * * * * * * * " echo ""