MorphGuard / docs_archive /SETUP_GUIDE.md
juanquy's picture
Initial clean commit of modular MorphGuard
2978bba
|
Raw
History Blame Contribute Delete
10.4 kB

MorphGuard Development Environment Setup Guide

This guide will help you set up your MorphGuard development environment and optionally migrate to new storage.

Table of Contents

  1. Quick Start (Current Location)
  2. Setting Up New SSDs
  3. Migrating to New Storage
  4. Troubleshooting

Quick Start (Current Location)

If you want to start developing immediately at the current location (/home/juanquy/dev/MorphGuard):

Step 1: Make Scripts Executable

cd /home/juanquy/dev/MorphGuard
chmod +x setup_complete_dev_environment.sh
chmod +x migrate_to_new_storage.sh

Step 2: Run Complete Setup

sudo bash setup_complete_dev_environment.sh

This will:

  • Install all system dependencies
  • Install Python 3.12
  • Install PostgreSQL and TimescaleDB
  • Set up virtual environment with all packages
  • Download AI models
  • Install Grafana (optional)
  • Run validation tests

Estimated time: 30-60 minutes (depending on internet speed)

Step 3: Start Development

After setup completes:

./start_dev.sh

Then visit: http://localhost:5000


Setting Up New SSDs

When you install your 2x 500GB SSDs, follow these steps:

Step 1: Physical Installation

  1. Power off your computer
  2. Install the 2x 500GB SSDs
  3. Power on and boot into your OS

Step 2: Format and Mount SSDs

Option A: Single Large Volume (RAID 0 - Fastest, 1TB total)

⚠️ Warning: RAID 0 provides no redundancy. If one drive fails, all data is lost.

# Install mdadm
sudo apt install mdadm

# Identify your new drives (e.g., /dev/sdb and /dev/sdc)
lsblk

# Create RAID 0 array
sudo mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc

# Create filesystem
sudo mkfs.ext4 -F /dev/md0

# Create mount point
sudo mkdir -p /mnt/morphguard-storage

# Mount the array
sudo mount /dev/md0 /mnt/morphguard-storage

# Add to fstab for automatic mounting
echo "/dev/md0 /mnt/morphguard-storage ext4 defaults 0 2" | sudo tee -a /etc/fstab

# Save RAID configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u

Option B: Two Separate Volumes (Safe, 500GB each)

# Identify your new drives
lsblk

# Format first drive (adjust /dev/sdb to your actual device)
sudo mkfs.ext4 /dev/sdb
sudo mkdir -p /mnt/ssd1
sudo mount /dev/sdb /mnt/ssd1

# Format second drive
sudo mkfs.ext4 /dev/sdc
sudo mkdir -p /mnt/ssd2
sudo mount /dev/sdc /mnt/ssd2

# Get UUIDs for fstab
sudo blkid /dev/sdb
sudo blkid /dev/sdc

# Add to fstab (replace UUIDs with actual values from blkid)
echo "UUID=your-uuid-here /mnt/ssd1 ext4 defaults 0 2" | sudo tee -a /etc/fstab
echo "UUID=your-uuid-here /mnt/ssd2 ext4 defaults 0 2" | sudo tee -a /etc/fstab

Option C: Mirror (RAID 1 - Most Safe, 500GB usable)

Recommended for production data

# Install mdadm
sudo apt install mdadm

# Create RAID 1 array
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

# Format and mount (same as RAID 0)
sudo mkfs.ext4 -F /dev/md0
sudo mkdir -p /mnt/morphguard-storage
sudo mount /dev/md0 /mnt/morphguard-storage
echo "/dev/md0 /mnt/morphguard-storage ext4 defaults 0 2" | sudo tee -a /etc/fstab

# Save configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u

Step 3: Set Permissions

# For RAID setup
sudo chown -R $USER:$USER /mnt/morphguard-storage

# For separate drives
sudo chown -R $USER:$USER /mnt/ssd1
sudo chown -R $USER:$USER /mnt/ssd2

Migrating to New Storage

Before Migration

  1. Ensure new storage is mounted and accessible
  2. Verify sufficient space: df -h /mnt/morphguard-storage
  3. Backup important data (optional but recommended)

Run Migration Script

For RAID or Single Volume:

cd /home/juanquy/dev/MorphGuard
sudo bash migrate_to_new_storage.sh /mnt/morphguard-storage/MorphGuard

For Separate Drives:

# Use first drive for project
sudo bash migrate_to_new_storage.sh /mnt/ssd1/MorphGuard

Migration Process

The script will:

  1. βœ… Check disk space
  2. βœ… Copy all files (using rsync)
  3. βœ… Verify copy integrity
  4. βœ… Update configuration paths
  5. βœ… Rebuild virtual environment
  6. βœ… Run validation tests
  7. βœ… Create symbolic link (optional)

Estimated time: 15-30 minutes (for ~23GB project)

After Migration

  1. Test the new location:

    cd /mnt/morphguard-storage/MorphGuard  # or /mnt/ssd1/MorphGuard
    ./start_dev.sh
    
  2. Verify everything works:

    • Upload test images
    • Check GPU usage: watch -n 1 nvidia-smi
    • Test detection and demorphing
  3. Remove old directory (after confirming everything works):

    sudo rm -rf /home/juanquy/dev/MorphGuard.backup
    

Storage Layout Recommendations

Recommended: RAID 0 for Development

Best performance for AI workloads:

/mnt/morphguard-storage/
β”œβ”€β”€ MorphGuard/              # Main project
β”‚   β”œβ”€β”€ models/             # AI models (6GB)
β”‚   β”œβ”€β”€ data/               # Training data (6GB)
β”‚   β”œβ”€β”€ pSp/                # GAN models (6GB)
β”‚   └── venv/               # Virtual environment (5GB)
└── workspace/              # Additional space
    β”œβ”€β”€ experiments/        # Training experiments
    β”œβ”€β”€ datasets/           # Additional datasets
    └── backups/            # Project backups

Total project size: ~23GB Available after migration: ~977GB

Alternative: Separate Drives

For better organization:

/mnt/ssd1/                  # Drive 1 (500GB)
β”œβ”€β”€ MorphGuard/            # Main project (23GB)
└── workspace/             # Development space

/mnt/ssd2/                  # Drive 2 (500GB)
β”œβ”€β”€ datasets/              # Large datasets
β”œβ”€β”€ experiments/           # Training runs
└── backups/               # Backups

Troubleshooting

Issue: "PostgreSQL not installed"

Solution:

sudo apt update
sudo apt install postgresql-15
sudo systemctl start postgresql

Issue: "CUDA not available"

Check GPU:

nvidia-smi

Reinstall drivers if needed:

sudo apt install nvidia-driver-535
sudo reboot

Issue: "Python packages not found"

Rebuild virtual environment:

cd /your/morphguard/location
rm -rf venv
python3.12 -m venv venv
source venv/bin/activate
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
pip install -r requirements.txt

Issue: "Port 5000 already in use"

Find and kill the process:

sudo lsof -i :5000
sudo kill <PID>

Issue: "Permission denied" errors

Fix permissions:

sudo chown -R $USER:$USER /path/to/MorphGuard
chmod +x *.sh

Issue: "Database connection failed"

Check PostgreSQL status:

sudo systemctl status postgresql

Test connection:

PGPASSWORD=morphguard psql -h localhost -U morphguard -d morphguard -c "SELECT version();"

Issue: "Out of disk space during setup"

Check available space:

df -h

Clean up if needed:

# Remove old kernels
sudo apt autoremove

# Clean package cache
sudo apt clean

# Remove old logs
sudo journalctl --vacuum-time=7d

Quick Reference Commands

Start Development Server

cd /path/to/MorphGuard
./start_dev.sh

Check GPU Status

nvidia-smi
watch -n 1 nvidia-smi  # Live monitoring

Database Management

./manage_db.sh status   # Check database
./manage_db.sh backup   # Backup database
./manage_db.sh reset    # Reset database (careful!)

Activate Virtual Environment

cd /path/to/MorphGuard
source venv/bin/activate

Check Services

sudo systemctl status postgresql
sudo systemctl status grafana-server
sudo systemctl status morphguard  # If deployed as service

View Logs

# Application logs
tail -f logs/*.log

# System logs
sudo journalctl -u morphguard -f

# PostgreSQL logs
sudo tail -f /var/log/postgresql/postgresql-15-main.log

Environment Variables

Development (.env.development)

  • Used for local testing
  • Debug mode enabled
  • Localhost connections

Production (.env.production)

  • Used for deployment
  • Debug mode disabled
  • Network-accessible
  • Hardened security settings

Switch Environments

# Development
./start_morphguard.sh development

# Production
./start_morphguard.sh production

Best Practices

  1. Always use virtual environment

    • Prevents package conflicts
    • Isolates dependencies
  2. Regular backups

    • Database: ./manage_db.sh backup
    • Project: rsync -ah --progress MorphGuard/ backup/
  3. Monitor GPU temperature

    • Check: nvidia-smi --query-gpu=temperature.gpu --format=csv
    • Keep under 80Β°C during training
  4. Use version control

    • Initialize git: git init
    • Commit regularly: git commit -am "message"
  5. Test before deploying

    • Run tests: pytest
    • Check code: flake8 .
    • Verify imports work

Getting Help

Check Documentation

  • HOW_TO_START_MORPHGUARD.md - Startup guide
  • COMPREHENSIVE_DOCUMENTATION.md - Full documentation
  • DEPLOYMENT_STATUS.md - Deployment info

Check Logs

# Application logs
cat logs/error.log

# System messages
dmesg | grep -i error

# PostgreSQL
sudo grep ERROR /var/log/postgresql/postgresql-15-main.log

Verify System

# Python environment
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"

# Database
psql -h localhost -U morphguard -d morphguard -c "\dt"

# Services
systemctl list-units | grep -E 'postgres|grafana|morphguard'

Next Steps After Setup

  1. βœ… Run setup script
  2. βœ… Test with ./start_dev.sh
  3. βœ… Upload test image
  4. βœ… Verify detection works
  5. βœ… Check GPU utilization
  6. πŸ“ Start developing new features!

Support

For issues or questions:

  1. Check logs: logs/ directory
  2. Review documentation: *.md files
  3. Test components individually
  4. Check system resources: htop, nvidia-smi

Happy developing! πŸš€