如何配置Virt-Manager将快照存储至第四台远程服务器?
Got it, let's break this down into two practical approaches based on your needs—either saving snapshots directly to Server 4 as you create them, or syncing local snapshots over afterward. Both are straightforward to set up with Virt-Manager and standard Linux tools.
Approach 1: Store Snapshots Directly on Server 4 (Recommended for Real-Time Remote Storage)
This method configures Virt-Manager to use a remote shared storage pool on Server 4, so snapshots go straight there without hitting local storage first.
Step 1: Set Up Shared Storage on Server 4
We'll use NFS (most common for Linux environments) as an example—SMB works too if you prefer that.
On Server 4:
- Install NFS server:
sudo apt install nfs-kernel-server(Debian/Ubuntu) orsudo dnf install nfs-utils(RHEL/CentOS/Fedora) - Create a directory for snapshots:
sudo mkdir -p /srv/vm-snapshots - Edit the NFS exports file:
sudo nano /etc/exports
Add this line (replace192.168.1.0/24with your KVM hosts' subnet):/srv/vm-snapshots 192.168.1.0/24(rw,sync,no_root_squash) - Restart NFS to apply changes:
sudo systemctl restart nfs-server
- Install NFS server:
On each of your 3 KVM hosts:
- Install NFS client:
sudo apt install nfs-commonorsudo dnf install nfs-utils - Create a local mount point:
sudo mkdir -p /mnt/server4-snapshots - Mount the remote NFS share:
sudo mount -t nfs server4-ip:/srv/vm-snapshots /mnt/server4-snapshots - Make the mount permanent (add to
/etc/fstab):
Edit/etc/fstaband add:
Test it withserver4-ip:/srv/vm-snapshots /mnt/server4-snapshots nfs defaults 0 0sudo mount -ato ensure no errors.
- Install NFS client:
Step 2: Add the Remote Storage Pool to Virt-Manager
- Open Virt-Manager on your KVM host.
- Go to
Edit > Connection Details > Storagetab. - Click the
+button (Add Pool) in the bottom left. - Name the pool (e.g.,
Server4-Snapshots), selectDirectoryas the type, then click Forward. - For the
Target Path, browse to/mnt/server4-snapshotsand click Finish.
Step 3: Create Snapshots to the Remote Pool
- Open the virtual machine's details window.
- Switch to the
Snapshotstab. - Click the
+button (Create Snapshot). - In the popup window, use the
Storagedropdown to select yourServer4-Snapshotspool. - Name your snapshot and click Finish—your snapshot will be saved directly to Server 4.
Approach 2: Sync Local Snapshots to Server 4 Automatically
If you prefer keeping local snapshots as a backup and syncing them to Server 4 later, this method uses rsync and cron (or inotify for real-time sync).
Step 1: Set Up Passwordless SSH Access to Server 4
This lets your KVM hosts sync files without entering a password every time.
- On each KVM host, generate an SSH key pair (press enter for all prompts):
ssh-keygen -t ed25519 - Copy the public key to Server 4 (replace
userandserver4-ipwith your details):ssh-copy-id user@server4-ip - Test the connection to confirm it works without a password:
ssh user@server4-ip
Step 2: Create a Sync Script
- Create a script file (e.g.,
sync-snapshots.sh) with the following content:#!/bin/bash # Local directory where Virt-Manager stores snapshots (default below—adjust if you use custom pools) LOCAL_SNAP_DIR="/var/lib/libvirt/images" # Remote directory on Server 4 REMOTE_DIR="user@server4-ip:/srv/vm-snapshots" # Sync only snapshot files (qcow2 is the default format; adjust if you use raw) rsync -avz --delete --include='*.qcow2' --exclude='*' "$LOCAL_SNAP_DIR/" "$REMOTE_DIR" - Make the script executable:
chmod +x sync-snapshots.sh
Step 3: Automate the Sync
Option A: Scheduled Sync with Cron
- Open your crontab editor:
crontab -e - Add a line to run the script at your desired interval. For example, to sync every day at 2 AM:
Save and exit—cron will handle the rest.0 2 * * * /path/to/sync-snapshots.sh
Option B: Real-Time Sync with Inotify
If you want snapshots to sync immediately after creation, use inotify-tools to monitor the local snapshot directory:
- Install inotify-tools:
sudo apt install inotify-toolsorsudo dnf install inotify-tools - Modify your sync script to listen for file changes:
#!/bin/bash LOCAL_SNAP_DIR="/var/lib/libvirt/images" REMOTE_DIR="user@server4-ip:/srv/vm-snapshots" # Listen for new/modified files and sync qcow2 snapshots inotifywait -m -e create -e modify "$LOCAL_SNAP_DIR" | while read dir event file; do if [[ $file == *.qcow2 ]]; then rsync -avz "$dir$file" "$REMOTE_DIR" fi done - Set the script to run on boot (using systemd):
- Create a systemd service file:
sudo nano /etc/systemd/system/sync-snapshots.service[Unit] Description=Sync KVM snapshots to Server 4 After=network.target [Service] User=root ExecStart=/path/to/sync-snapshots.sh Restart=always [Install] WantedBy=multi-user.target - Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable --now sync-snapshots.service
- Create a systemd service file:
Quick Notes
- Make sure Server 4 has enough storage space—snapshots can be nearly as large as your VM disks, especially if you use full snapshots.
- For NFS, ensure your network is stable; a dropped connection during snapshot creation can corrupt the file.
- If you use SMB instead of NFS, adjust the mount command on KVM hosts to:
sudo mount -t cifs //server4-ip/snapshots /mnt/server4-snapshots -o username=your-user,password=your-pass
内容的提问来源于stack exchange,提问作者xav

