# webvirtmgr **Repository Path**: 3092/webvirtmgr ## Basic Information - **Project Name**: webvirtmgr - **Description**: 从 webvirtmgr-4.8.9 迁移到Python3 环境 - **Primary Language**: Unknown - **License**: LGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-12-23 - **Last Updated**: 2026-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: kvm, Libvirt, webvirtmgr ## README # WebVirtMgr Python 3 迁移版 ## 原始项目介绍 WebVirtMgr is a libvirt-based Web interface for managing virtual machines. It allows you to create and configure new domains and adjust a domain's resource allocation. A VNC viewer presents a full graphical console to the guest domain. KVM is currently the only hypervisor supported. Technology: The application logic is written in Python & Django. The LIBVIRT Python bindings are used to interacting with the underlying hypervisor. This version has been migrated to Python 3 and adapted for Django 1.11+. ## 新功能 (What's New) - Added RPM specs (Thanks: `Edoardo Spadoni `_) - Added support SPICE SSH Tunnel fixed some bugs (Thanks: `brenard `_) - Responsive design (Thanks: `Michel Käser `_) - Added VNC WebSocket support (Thanks: `Tolbkni Kao `_) - Added novnc proxy supporting new websockify versions (Thanks: `casell `_) - Added support `TLS `_ connection (Thanks: `junkb `_) ## 屏幕截图 [Screenshots](https://github.com/retspen/webvirtmgr/wiki/Screenshots) ## 系统要求 - Python 3.6+ - Django 1.11.x - libvirt 5.0+ - 支持KVM的Linux发行版 ## 安装步骤 ### 1. 安装系统级依赖 ```bash # Ubuntu/Debian sudo apt-get update sudo apt-get install -r apt.txt # 或者手动安装 sudo apt-get install libvirt-daemon-system libvirt-dev python3-libvirt python3-lxml python3-dev pkg-config ``` ### 2. 创建Python虚拟环境 ```bash cd /home/dyna/Documents/Git/Webvirtmgr-4.8.9 python3 -m venv Python3.7_venv source Python3.7_venv/bin/activate ``` ### 3. 安装Python依赖 #### 3.1 安装/升级 pip 到最新的版本, 并配置镜像源 升级 pip 到最新的版本后进行配置: ```bash python -m pip install --upgrade pip pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ``` 如果您到 pip 默认源的网络连接较差,临时使用本镜像站来升级 pip: ```bash python -m pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --upgrade pip ``` 配置多个镜像源 如果您想配置多个镜像源平衡负载,可在已经替换 index-url 的情况下通过以下方式继续增加源站: ```bash pip config set global.extra-index-url " ..." ``` #### 3.2 安装项目依赖 ```bash pip install --upgrade pip pip install -r requirements.txt ``` 如果遇到rwlock模块问题,可以单独安装: ```bash pip install rwlock ``` ### 4. 数据库迁移 ```bash python3 manage.py migrate ``` ### 5. 创建超级用户 ```bash python3 manage.py createsuperuser ``` 按照提示输入用户名、邮箱和密码。 ### 6. 收集静态文件 ```bash python3 manage.py collectstatic ``` ### 7. 启动开发服务器 ```bash python3 manage.py runserver 0.0.0.0:8000 ``` 访问 http://localhost:8000 ## 常见问题解决 #### 1. Template Configuration Warnings 如果遇到以下警告: ``` WARNINGS: ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS, TEMPLATE_LOADERS. ``` 这是因为Django 1.8+版本引入了新的模板配置系统。请确保settings.py中只保留新的 `TEMPLATES`配置,并移除旧的 `TEMPLATE_LOADERS`和 `TEMPLATE_DIRS`配置: ```python TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(os.path.dirname(__file__), '..', 'templates'), ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] ``` #### 2. 静态文件问题 启动前请确保已收集静态文件: ```bash python3 manage.py collectstatic --no-input ``` ## 新版本新功能 - 新增对Python 3的全面支持 - 新增对Django 1.11+的全面支持 - 新增对libvirt 5.0+的全面支持 - 新增IP地址显示功能,在实例详情页中显示实例的IP地址 - 新增克隆页面自动生成随机MAC地址功能 # WebVirtMgr Python 3 迁移版 ## 生产环境部署 ### 使用Gunicorn部署 1. 安装Gunicorn(已在requirements.txt中): ```bash pip install gunicorn ``` 2. 启动Gunicorn服务器(推荐方式): ```bash gunicorn -c conf/gunicorn.conf.py webvirtmgr.wsgi:application ``` 或者使用Django管理命令(仅适用于旧版本Django): ```bash python manage.py run_gunicorn -c conf/gunicorn.conf.py ``` 注意:由于Django 1.11+版本中移除了BaseCommand.option_list属性,run_gunicorn命令可能无法正常工作。 如果您遇到以下错误: ``` AttributeError: type object 'BaseCommand' has no attribute 'option_list' ``` 请使用推荐的Gunicorn启动方式,或参考 `patches/GUNICORN_COMPATIBILITY_FIX.md`文件了解详细的修复方案。 ### 使用Nginx作为反向代理 配置示例: ```nginx server { listen 80; server_name your_domain.com; location /static/ { alias /path/to/webvirtmgr/static/; } location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` ### 配置ssh认证 Create SSH private key and ssh config options (On system where WebVirtMgr is installed): ```bash $ sudo su - nginx -s /bin/bash ``` (nginx default user might be different than "nginx", "www-data" might be used : check nginx.conf) ```bash $ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (path-to-id-rsa-in-nginx-home): Just hit Enter here! ```bash $ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config $ chmod 0600 ~/.ssh/config ``` Add webvirt user (on qemu-kvm/libvirt host server) and add it to the proper group : ```bash $ sudo adduser webvirtmgr $ sudo passwd webvirtmgr $ sudo usermod -G libvirtd -a webvirtmgr ``` on Debian the command looks like (groupname has no d appended) ```bash $ sudo usermod -G libvirt -a webvirtmgr ``` Back to webvirtmgr host and copy public key to qemu-kvm/libvirt host server: ```bash $ sudo su - nginx -s /bin/bash $ ssh-copy-id webvirtmgr@qemu-kvm-libvirt-host ``` Or if you changed the default SSH port use: ```bash $ ssh-copy-id -P YOUR_SSH_PORT webvirtmgr@qemu-kvm-libvirt-host ``` Now you can test the connection by entering: ```bash $ ssh webvirtmgr@qemu-kvm-libvirt-host ``` For a non-standard SSH port use: ```bash $ ssh -P YOUR_SSH_PORT webvirtmgr@qemu-kvm-libvirt-host ``` You should connect without entering a password. Set up permissions to manage libvirt (on qemu-kvm/libvirt host server): On Ubuntu: ```bash sudo adduser webvirtmgr libvirtd ``` Fedora, CentOS: Create file /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla (permissions for user webvirtmgr):# ```conf [Remote libvirt SSH access] Identity=unix-user:webvirtmgr Action=org.libvirt.unix.manage ResultAny=yes ResultInactive=yes ResultActive=yes ``` ## 已知问题和解决方案 ### 1. rwlock模块导入问题 **问题**:`ImportError: cannot import name 'ReadWriteLock' from 'rwlock'` **解决方案**:项目自带rwlock.py文件,已修改导入路径。 ### 2. Python 3语法兼容性问题 **问题**:`dict.has_key()`在Python 3中不可用 **解决方案**:已注释掉相关代码,使用Python 3兼容语法。 ### 3. Django URL配置问题 **问题**:Django 1.11+版本中视图引用语法变更 **解决方案**:已更新urls.py文件,使用正确的导入和引用方式。 ### 4. Python 3.6虚拟环境中libvirt模块问题 **问题**:在Python 3.6虚拟环境中无法导入libvirt模块,出现 `ModuleNotFoundError: No module named 'libvirtmod'`错误。 **原因分析**: - 系统中只安装了Python 3.7版本的libvirt模块文件(`libvirtmod.cpython-37m-x86_64-linux-gnu.so`) - 缺少Python 3.6版本对应的二进制扩展文件(`libvirtmod.cpython-36m-x86_64-linux-gnu.so`) - libvirt Python绑定需要特定于Python版本和架构的共享库文件 **解决方案**: 1. **安装Python 3.6对应的libvirt模块**: ```bash # 安装编译依赖 sudo apt-get install libvirt-dev pkg-config python3.6-dev # 在Python 3.6虚拟环境中安装libvirt-python pip install libvirt-python ``` 2. **使用系统站点包**: 创建虚拟环境时包含系统包: ```bash # 重新创建虚拟环境时使用--system-site-packages参数 python3.6 -m venv --system-site-packages new_env ``` 或者在现有环境中手动添加路径: ```python import sys sys.path.insert(0, '/usr/lib/python3/dist-packages') import libvirt ``` 3. **升级Python版本**: 考虑使用Python 3.7或更高版本,这样可以直接使用系统中已有的libvirt模块文件。 ## 文件修改记录 ### 核心修改文件 1. `vrtManager/connection.py`:修正rwlock模块导入路径 2. `vrtManager/IPy.py`:处理dict.has_key兼容性问题 3. `webvirtmgr/urls.py`:更新Django URL配置语法 4. `requirements.txt`:更新依赖列表 5. `apt.txt`:新增系统级依赖列表 ## 版本兼容性 - **Python版本**:3.6+ - **Django版本**:1.11.x - **Libvirt版本**:5.0+ - **操作系统**:Ubuntu 18.04+, Debian 10+, Rocky Linux 8+ ## 功能特性 - 虚拟机创建、启动、停止、重启 - 虚拟机资源监控(CPU、内存、网络、存储) - 存储池和卷管理 - 网络接口管理 - VNC/SPICE图形控制台 - 多主机管理 - 用户认证和权限管理 ## 故障排除 ### 1. 模板文件找不到 确保templates目录存在且包含必要的模板文件。 ### 2. 数据库迁移失败 检查SQLite数据库文件权限,必要时删除db.sqlite3文件并重新运行migrate命令。 ### 3. Libvirt连接问题 确保libvirtd服务正在运行: ```bash sudo systemctl status libvirtd ``` ## 许可证 WebVirtMgr is licensed under the [Apache Licence Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html). ## 贡献 欢迎提交Issue和Pull Request来改进此项目。