F OpenNebula/one#6912: Thin LVM - incremental backups (#3734)

Signed-off-by: josepselga <jselga@opennebula.io>
Co-authored-by: Ruben S. Montero <rsmontero@opennebula.org>
This commit is contained in:
Josep SelgaandRuben S. Montero authored and GitHub committed 2025-07-24 13:55:13 +02:00
1 parent 89ba30f1b1
commit 78638dab05
7 files changed
+276 -9

No files matched your search

+1
View File
@@ -1314,6 +1314,7 @@ TM_LIB_FILES="src/tm_mad/lib/kvm.rb \
src/tm_mad/lib/shell.rb \
src/tm_mad/lib/tm_action.rb \
src/tm_mad/lib/backup_qcow2.rb \
src/tm_mad/lib/backup_lvmthin.rb \
src/tm_mad/lib/backup_rbd.rb \
src/tm_mad/lib/datastore.rb \
src/tm_mad/lib/tm_cache.rb \
+1 -1
View File
@@ -3,7 +3,7 @@ Defaults:oneadmin secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Cmnd_Alias ONE_CEPH = /usr/bin/rbd
Cmnd_Alias ONE_HA = /usr/bin/systemctl start opennebula-flow, /usr/bin/systemctl stop opennebula-flow, /usr/bin/systemctl start opennebula-gate, /usr/bin/systemctl stop opennebula-gate, /usr/bin/systemctl start opennebula-hem, /usr/bin/systemctl stop opennebula-hem, /usr/bin/systemctl start opennebula-showback.timer, /usr/bin/systemctl stop opennebula-showback.timer, /usr/sbin/service opennebula-flow start, /usr/sbin/service opennebula-flow stop, /usr/sbin/service opennebula-gate start, /usr/sbin/service opennebula-gate stop, /usr/sbin/service opennebula-hem start, /usr/sbin/service opennebula-hem stop, /usr/sbin/arping, /usr/sbin/ip address *
Cmnd_Alias ONE_LVM = /usr/sbin/lvcreate, /usr/sbin/lvremove, /usr/sbin/lvs, /usr/sbin/vgdisplay, /usr/sbin/lvchange, /usr/sbin/lvscan, /usr/sbin/lvextend
Cmnd_Alias ONE_LVM = /usr/sbin/lvcreate, /usr/sbin/lvremove, /usr/sbin/lvs, /usr/sbin/vgdisplay, /usr/sbin/lvchange, /usr/sbin/lvscan, /usr/sbin/lvextend, /usr/sbin/dmsetup
Cmnd_Alias ONE_NFS = /usr/bin/mount, /usr/bin/umount, /usr/bin/sed -i -f /proc/self/fd/0 /etc/fstab
Cmnd_Alias ONE_LXC = /usr/bin/mount, /usr/bin/umount, /usr/bin/bindfs, /usr/sbin/losetup, /usr/bin/qemu-nbd, /usr/bin/lxc-attach, /usr/bin/lxc-config, /usr/bin/lxc-create, /usr/bin/lxc-destroy, /usr/bin/lxc-info, /usr/bin/lxc-ls, /usr/bin/lxc-start, /usr/bin/lxc-stop, /usr/bin/lxc-console, /usr/sbin/e2fsck, /usr/sbin/resize2fs, /usr/sbin/xfs_growfs, /usr/bin/rbd-nbd
Cmnd_Alias ONE_MARKET = /usr/lib/one/sh/create_container_image.sh
+1 -1
View File
@@ -3,7 +3,7 @@ Defaults:oneadmin secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Cmnd_Alias ONE_CEPH = /usr/bin/rbd
Cmnd_Alias ONE_HA = /usr/bin/systemctl start opennebula-flow, /usr/bin/systemctl stop opennebula-flow, /usr/bin/systemctl start opennebula-gate, /usr/bin/systemctl stop opennebula-gate, /usr/bin/systemctl start opennebula-hem, /usr/bin/systemctl stop opennebula-hem, /usr/bin/systemctl start opennebula-showback.timer, /usr/bin/systemctl stop opennebula-showback.timer, /usr/sbin/service opennebula-flow start, /usr/sbin/service opennebula-flow stop, /usr/sbin/service opennebula-gate start, /usr/sbin/service opennebula-gate stop, /usr/sbin/service opennebula-hem start, /usr/sbin/service opennebula-hem stop, /usr/bin/arping, /usr/sbin/ip address *
Cmnd_Alias ONE_LVM = /usr/sbin/lvcreate, /usr/sbin/lvremove, /usr/sbin/lvs, /usr/sbin/vgdisplay, /usr/sbin/lvchange, /usr/sbin/lvscan, /usr/sbin/lvextend
Cmnd_Alias ONE_LVM = /usr/sbin/lvcreate, /usr/sbin/lvremove, /usr/sbin/lvs, /usr/sbin/vgdisplay, /usr/sbin/lvchange, /usr/sbin/lvscan, /usr/sbin/lvextend, /usr/sbin/dmsetup
Cmnd_Alias ONE_NFS = /usr/bin/mount, /usr/bin/umount, /usr/bin/sed -i -f /proc/self/fd/0 /etc/fstab
Cmnd_Alias ONE_LXC = /usr/bin/mount, /usr/bin/umount, /usr/bin/bindfs, /usr/sbin/losetup, /usr/bin/qemu-nbd, /usr/bin/lxc-attach, /usr/bin/lxc-config, /usr/bin/lxc-create, /usr/bin/lxc-destroy, /usr/bin/lxc-info, /usr/bin/lxc-ls, /usr/bin/lxc-start, /usr/bin/lxc-stop, /usr/bin/lxc-console, /usr/sbin/e2fsck, /usr/sbin/resize2fs, /usr/sbin/xfs_growfs, /usr/bin/rbd-nbd
Cmnd_Alias ONE_MARKET = /usr/lib/one/sh/create_container_image.sh
+2 -1
View File
@@ -47,7 +47,8 @@ class Sudoers
'/var/tmp/one/vnm/ip_netns_exec ip route *'
],
:LVM => [
'lvcreate', 'lvremove', 'lvs', 'vgdisplay', 'lvchange', 'lvscan', 'lvextend'
'lvcreate', 'lvremove', 'lvs', 'vgdisplay', 'lvchange', 'lvscan', 'lvextend',
'dmsetup'
],
:OVS => ['ovs-ofctl', 'ovs-vsctl'],
:CEPH => ['rbd'],
+221
View File
@@ -0,0 +1,221 @@
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
$LOAD_PATH.unshift('/var/tmp/one')
require 'fileutils'
require 'tempfile'
require 'open3'
require 'rexml/document'
require 'English'
require 'CommandManager'
require 'DriverLogger'
# A helper module for running shell commands and handling LVM operations.
module LVMHelper
# Gets a specific attribute for logical volumes.
def self.get_lv_attr(attr, lv_path)
LocalCommand.run("sudo lvs --noheadings -o #{attr} #{lv_path}").stdout.strip
end
# Constructs the device mapper path for a thin pool.
def self.get_tpool_device(vg_name, pool_name)
"/dev/mapper/#{vg_name.gsub('-', '--')}-#{pool_name.gsub('-', '--')}-tpool"
end
# Constructs the device mapper path for a thin pool's metadata.
def self.get_tmeta_device(vg_name, pool_name)
"/dev/mapper/#{vg_name.gsub('-', '--')}-#{pool_name.gsub('-', '--')}_tmeta"
end
# Gets the full /dev/mapper path for a logical volume.
def self.get_mapper_path(lv_path)
vg_name = get_lv_attr('vg_name', lv_path)
lv_name = get_lv_attr('lv_name', lv_path)
"/dev/mapper/#{vg_name.gsub('-', '--')}-#{lv_name.gsub('-', '--')}"
end
end
# Parses the XML output from the 'thin_delta' to find changed blocks.
class ThinDeltaParser
attr_reader :records
def initialize
@records = []
end
def parse(xml_path)
doc = REXML::Document.new(File.read(xml_path))
block_size_sectors = doc.elements['//superblock'].attributes['data_block_size'].to_i
block_size_bytes = block_size_sectors * 512
raise 'Invalid block size detected in thin_delta XML' if block_size_bytes <= 0
# Selects ranges that are different or exist only in the newer snapshot.
doc.elements.each('//diff/different | //diff/right_only') do |range|
offset = range.attributes['begin'].to_i
length = range.attributes['length'].to_i
@records << {
:offset => offset * block_size_bytes,
:length => length * block_size_bytes
}
end
end
end
# Manages writing changed extents to a file.
class ExtentWriter
# Writes changed blocks from a source device to a target qcow2 file.
#
# @param source_dev [String] The source block device (/dev/mapper/vg-snap).
# @param target_file [String] The final destination for the qcow2.
# @param records [Array<Hash>] Array of hashes describing changed extents.
def self.write(source_dev, target_file, records)
unless File.blockdev?(source_dev)
raise "Source device #{source_dev} is not a block device or does not exist."
end
# Use temporary file for the intermediate raw file.
temp_raw_file = Tempfile.new(['inc_backup', '.raw'])
temp_raw_path = temp_raw_file.path
temp_raw_file.close!
# Write to a temporary qcow2 file first.
temp_qcow2_file = target_file + ".tmp.#{Process.pid}"
begin
# 1. Get the size from the source device.
size = LocalCommand.run("blockdev --getsize64 #{source_dev}").stdout.strip
# 2. Create a raw file with the virtual size.
LocalCommand.run("truncate -s #{size} #{temp_raw_path}")
# 3. dd to copy each changed block.
records.each do |r|
offset = r[:offset]
length = r[:length]
LocalCommand.run(
"dd if=#{source_dev} of=#{temp_raw_path} bs=4M " \
'iflag=skip_bytes,count_bytes oflag=seek_bytes ' \
"skip=#{offset} seek=#{offset} count=#{length} conv=notrunc"
)
end
# 4. Convert the temporary raw file to temporary qcow2.
LocalCommand.run(
"qemu-img convert -f raw -O qcow2 #{temp_raw_path} " \
"#{temp_qcow2_file}"
)
# 5. Rename the temp file to final destination.
FileUtils.mv(temp_qcow2_file, target_file)
rescue StandardError => e
FileUtils.rm_f(temp_qcow2_file)
raise e
ensure
# Cleanup
FileUtils.rm_f(temp_raw_path)
end
end
end
# --- Main ---
def main(origin_lv, from_snap_lv, to_snap_lv, output_file)
delta_file = Tempfile.new(['thin_delta', '.xml'])
delta_file_path = delta_file.path
# Activate LV
LocalCommand.run("sudo lvchange -K -ay #{to_snap_lv}")
# LVM metadata
vg_name = LVMHelper.get_lv_attr('vg_name', origin_lv)
pool_name = LVMHelper.get_lv_attr('pool_lv', origin_lv)
tpool_dev = LVMHelper.get_tpool_device(vg_name, pool_name)
tmeta_dev = LVMHelper.get_tmeta_device(vg_name, pool_name)
from_id = LVMHelper.get_lv_attr('thin_id', from_snap_lv)
to_id = LVMHelper.get_lv_attr('thin_id', to_snap_lv)
source_dev = LVMHelper.get_mapper_path(to_snap_lv)
# Reserve and release snapshot metadata
begin
OpenNebula::DriverLogger.log_info "Getting changed blocks from #{from_id} to #{to_id}"
LocalCommand.run("sudo dmsetup message #{tpool_dev} 0 reserve_metadata_snap")
LocalCommand.run(
"thin_delta --metadata-snap --thin1 #{from_id} " \
"--thin2 #{to_id} #{tmeta_dev} > #{delta_file_path}"
)
ensure
# Release the snapshot
LocalCommand.run("sudo dmsetup message #{tpool_dev} 0 release_metadata_snap", nil, nil,
nil, nil)
end
# Parse delta file (get changed blocks)
parser = ThinDeltaParser.new
parser.parse(delta_file_path)
# No changes
if parser.records.empty?
OpenNebula::DriverLogger.log_info 'No changes between snapshots. Empty qcow2 incremental.'
size = LocalCommand.run("blockdev --getsize64 #{source_dev}").stdout.strip
LocalCommand.run("qemu-img create -f qcow2 #{output_file} #{size}")
return
end
# Write extents to final file
OpenNebula::DriverLogger.log_info "Changed blocks found: #{parser.records.size}, writing..."
ExtentWriter.write(source_dev, output_file, parser.records)
OpenNebula::DriverLogger.log_info "LVM Thin Backup completed successfully: #{output_file}"
ensure
# Deactivate LV
LocalCommand.run("sudo lvchange -an #{to_snap_lv}", nil, nil, nil, nil)
# Cleanup
delta_file.close
delta_file.unlink
end
# --- Entry ---
if __FILE__ == $PROGRAM_NAME
if ARGV.length != 4
puts "Usage: #{$PROGRAM_NAME} <origin_lv> <from_snap> <to_snap> <qcow2_output>"
exit 1
end
begin
main(*ARGV)
rescue StandardError => e
OpenNebula::DriverLogger.report "LVM Thin Backup failed: #{e.message}"
OpenNebula::DriverLogger.report e.backtrace.join("\n")
exit 1
end
end
+41 -6
View File
@@ -40,6 +40,9 @@ module TransferManager
# LVM disks
class Disk
# DON'T CHANGE THIS CONSTANT; will break existing incremental backups
INC_SNAP_PREFIX = 'one_backup_'
attr_reader :id, :vmid, :lv
# @param vm_xml [String, REXML::Element]
@@ -92,15 +95,16 @@ module TransferManager
# @param _format [String, nil] Unused. Included for compatibility with ceph.rb.
# @return [Disk]
def backup_cmds(backup_dir, ds, live, _format = nil)
snap_cmd = ''
expo_cmd = ''
snap_clup = ''
snap_cmd = ''
expo_cmd = ''
snap_clup = ''
backup_util = '/var/tmp/one/tm/lib/backup_lvmthin.rb'
# Supported configurations
# Legend: (T)hin, (F)at
# | | Live | Poweroff |
# | Full | T | TF |
# | Incr | - | - |
# | Incr | T | T |
# rubocop:disable Style/GuardClause
if @vm_backup_config[:mode] == :full
ddst = "#{backup_dir}/disk.#{@id}.0"
@@ -127,10 +131,41 @@ module TransferManager
sudo lvchange -K -ay #{orig}
qemu-img convert -m 4 -O qcow2 #{orig} #{ddst}
EOF
elsif @vm_backup_config[:last_increment] == -1
# First incremental backup (initial full backup)
return unless @is_thin
incid = 0
dexp = "#{backup_dir}/disk.#{@id}.#{incid}"
snap_curr = "#{@lv}_#{INC_SNAP_PREFIX}#{incid}"
snap_path = path(snap_curr)
snap_cmd << "sudo lvcreate -s -n #{snap_curr} #{qual(@lv)}\n"
expo_cmd << ds.cmd_confinement(<<~EOF, backup_dir)
sudo lvchange -K -ay #{snap_path}
qemu-img convert -m 4 -O qcow2 #{snap_path} #{dexp}
EOF
else
# Incremental: not implemented
return
# Incremental backup
return unless @is_thin
incid = @vm_backup_config[:last_increment] + 1
dinc = "#{backup_dir}/disk.#{@id}.#{incid}"
snap_curr = "#{@lv}_#{INC_SNAP_PREFIX}#{incid}"
snap_prev = "#{@lv}_#{INC_SNAP_PREFIX}#{@vm_backup_config[:last_increment]}"
snap_cmd << "sudo lvchange --refresh #{qual(@pool)}\n"
snap_cmd << "sudo lvremove -y #{qual(snap_curr)} || true\n"
snap_cmd << "sudo lvcreate -s -n #{snap_curr} #{qual(@lv)}\n"
expo_cmd << ds.cmd_confinement(
"ruby #{backup_util} #{qual(@lv)} " \
"#{qual(snap_prev)} #{qual(snap_curr)} #{dinc}\n",
backup_dir
)
snap_clup << "sudo lvremove -y #{qual(snap_prev)}\n"
end
# rubocop:enable Style/GuardClause
+9
View File
@@ -1691,6 +1691,15 @@ bool VirtualMachineDisks::backup_increment(bool do_volatile)
one_util::toupper(format);
string lvm_thin_enable = disk->vector_value("LVM_THIN_ENABLE");
one_util::toupper(lvm_thin_enable);
if (format == "RAW" && lvm_thin_enable == "YES")
{
continue;
}
if (format != "QCOW2" || disk->has_snapshots())
{
return false;