F OpenNebula/one#6857: Add flattening support for Ceph incremental backups (#3699)

Signed-off-by: josepselga <jselga@opennebula.io>
Co-authored-by: Guillemo Ramos <gramos@opennebula.io>
This commit is contained in:
Josep SelgaandGuillemo Ramos authored and GitHub committed 2025-07-15 14:08:40 +02:00
1 parent d79f2c760b
commit 023924e008
8 files changed
+395 -36

No files matched your search

+6 -10
View File
@@ -159,12 +159,13 @@ script = <<~EOS
set -e -o pipefail; shopt -qs failglob
#{rds.resticenv_sh}
BKNAME=$(ls #{vm_dir}/disk.* | head -n 1)
RC=`#{ds.cmd_confinement(rcmd, vm_dir, ['SSH_AUTH_SOCK', 'RESTIC_PASSWORD', 'GOMAXPROCS'])}`
INFO=`echo "$RC" | jq -r '.snapshot_id + " " + (.total_bytes_processed|tostring) | select ( . != " null" )'`
echo $INFO
echo "$INFO $BKNAME"
EOS
rc = TransferManager::Action.ssh 'backup',
@@ -181,7 +182,7 @@ end
parts = rc.stdout.lines.last.split
if parts.length != 2
if parts.length != 3
STDERR.puts "Wrong format for backup action: #{rc.stdout}"
exit(-1)
end
@@ -191,14 +192,9 @@ end
id = parts[0]
short_id = id[0..7] # first 8 chars only
vm = REXML::Document.new(xml).root.elements['VM']
backup_format =
if vm.elements['TEMPLATE/TM_MAD_SYSTEM'].text == 'ceph' &&
vm.elements['BACKUPS/BACKUP_CONFIG/MODE']&.text == 'INCREMENT'
'rbd'
else
'raw'
end
disk_filepath = parts[2]
extension = File.extname(disk_filepath).delete_prefix('.')
backup_format = ['rbd2', 'rbdiff'].include?(extension) ? 'rbd' : 'raw'
STDOUT.puts "#{short_id} #{parts[1].to_i / (1024 * 1024)} #{backup_format}"
exit(0)
+7 -11
View File
@@ -159,10 +159,9 @@ script = <<~EOS
set -ex -o pipefail
BKSIZE=`du -sm #{vm_dir}`
BKNAME=$(ls #{vm_dir}/disk.* | head -n 1)
#{ds.cmd_confinement(cmd, vm_dir, ['SSH_AUTH_SOCK'])} > /dev/null
echo $BKSIZE
echo "$BKSIZE $BKNAME"
EOS
rc = TransferManager::Action.ssh 'backup',
@@ -177,14 +176,11 @@ if rc.code != 0 || rc.stdout.empty?
exit(-1)
end
vm = REXML::Document.new(xml).root.elements['VM']
backup_format =
if vm.elements['TEMPLATE/TM_MAD_SYSTEM'].text == 'ceph' &&
vm.elements['BACKUPS/BACKUP_CONFIG/MODE']&.text == 'INCREMENT'
'rbd'
else
'raw'
end
stdout_parts = rc.stdout.split(' ')
disk_filepath = stdout_parts[2]
extension = File.extname(disk_filepath).delete_prefix('.')
backup_format = ['rbd2', 'rbdiff'].include?(extension) ? 'rbd' : 'raw'
STDOUT.puts "#{backup_id} #{rc.stdout.lines.last.split[0]} #{backup_format}"
exit(0)
+21 -1
View File
@@ -17,6 +17,8 @@
#--------------------------------------------------------------------------- #
require 'CommandManager'
require 'rexml/document'
require_relative 'kvm'
require_relative 'shell'
@@ -42,14 +44,32 @@ module TransferManager
snap_cmd = ''
expo_cmd = ''
snap_clup = ''
expo_clup = ''
xml_vm = REXML::Document.new(@xml).root
bk_img_id_elem = xml_vm.elements['BACKUPS/BACKUP_IDS/ID']
bk_img_id = bk_img_id_elem&.text&.strip
if bk_img_id.nil? || bk_img_id.empty?
disk_format = 'qcow2'
else
client = OpenNebula::Client.new
bk_img = OpenNebula::Image.new_with_id(bk_img_id, client)
bk_img.info
disk_format = bk_img['FORMAT']&.strip
end
@disks.compact.each do |d|
did = d.id
next unless disks.include? did.to_s
cmds = d.backup_cmds(backup_dir, ds, live)
# Pass the format for this disk (or nil if not set)
cmds = d.backup_cmds(backup_dir, ds, live, disk_format)
return nil unless cmds
snap_cmd << cmds[:snapshot].to_s
+322
View File
@@ -0,0 +1,322 @@
#!/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. #
# ---------------------------------------------------------------------------- #
require 'fileutils'
require 'tempfile'
require 'json'
require 'English'
require 'tmpdir'
require 'open3'
def run_cmd(cmd, ignore_err: false)
stdout_str = `#{cmd} 2>&1`
exit_status = $CHILD_STATUS.exitstatus
if exit_status != 0 && !ignore_err
raise "Command error: #{cmd}\nExit status: #{exit_status}\nOutput: #{stdout_str}"
end
stdout_str
end
# --- Class to Parse .rdiff ---
class RbdDiffParser
MAGIC_HEADER_V1 = "rbd diff v1\n".freeze
MAGIC_HEADER_V2 = "rbd diff v2\n".freeze
attr_reader :version, :size, :records
def initialize
@records = []
@size = nil
@version = nil
end
def parse(file_path)
File.open(file_path, 'rb') do |file|
detect_version(file)
loop do
tag_byte = file.read(1)
break unless tag_byte
tag = tag_byte.chr
break if tag == 'e'
process_record(tag, file)
end
end
self
end
private
def detect_version(file)
header = file.read(MAGIC_HEADER_V1.bytesize)
@version =
case header
when MAGIC_HEADER_V1
1
when MAGIC_HEADER_V2
2
else
raise 'Unknown diff header. Not an rbd diff v1 or v2 file.'
end
end
def process_record(tag, file)
file.read(8) if @version == 2 && ['f', 't', 's'].include?(tag)
case tag
when 'f', 't'
len = file.read(4).unpack1('L<')
file.read(len)
when 's'
@size = file.read(8).unpack1('Q<')
when 'w'
read_write_record(file)
when 'z'
read_zero_record(file)
else
raise "Unknown diff tag '#{tag}' at position #{file.pos - 1}."
end
end
def read_write_record(file)
file.read(8) if @version == 2
offset = file.read(8).unpack1('Q<')
length = file.read(8).unpack1('Q<')
data = file.read(length)
@records << { :type => :write, :offset => offset, :length => length, :data => data }
end
def read_zero_record(file)
file.read(8) if @version == 2
offset = file.read(8).unpack1('Q<')
length = file.read(8).unpack1('Q<')
@records << { :type => :zero, :offset => offset, :length => length }
end
end
#-------------------------------------------------------------------------------
# Setup an NBD server to pull changes, an optional map can be provided
#-------------------------------------------------------------------------------
module Nbd
@server = nil
@socket = nil
def self.start_nbd(file, map = '', timeout_seconds = 10)
return unless @server.nil?
@socket = "#{File.realpath(file)}.socket"
@server = fork do
args = ['-k', @socket, '-f', 'qcow2', '-t']
args << '-B' << map unless map.empty?
args << file
exec('qemu-nbd', *args)
end
# Wait for the NBD socket to appear
start_time = Time.now
until File.exist?(@socket)
if Time.now - start_time > timeout_seconds
Process.kill('TERM', @server) rescue nil # Try to kill if it's still running
Process.waitpid(@server) rescue nil
raise "Timed out waiting for qemu-nbd socket to appear at #{@socket}"
end
sleep(0.1)
end
end
def self.stop_nbd
return unless @server # Ensure server was started
Process.kill('QUIT', @server)
Process.waitpid(@server) # Wait for the child process to terminate
# No sleep here, waitpid ensures the process is gone and socket should be released
File.unlink(@socket) if File.exist?(@socket)
@server = nil
@socket = nil
end
def self.uri
raise 'NBD server not started.' if @socket.nil?
"nbd+unix:///?socket=#{@socket}"
end
end
# --- Pull changes using qemu-io ---
def pull_changes(diff_parser, nbd_uri)
io_script = ''
temp_files = [] # Array to store Tempfile objects
begin
diff_parser.records.each do |extent|
offset = extent[:offset]
length = extent[:length]
next if length == 0
if extent[:type] == :write
tmp_file = Tempfile.new('rbd-diff', Dir.tmpdir)
temp_files << tmp_file # Store the Tempfile object
# Set permissions for security
File.chmod(0o600, tmp_file.path)
tmp_file.write(extent[:data])
tmp_file.flush
io_script << "write -s #{tmp_file.path} #{offset} #{length}\n"
elsif extent[:type] == :zero
io_script << "write -z #{offset} #{length}\n"
end
end
io_script << "flush\n"
io_script << "quit\n"
# Execute qemu-io.
status = Open3.popen3('qemu-io', '-f', 'raw', nbd_uri) do |stdin, _stdout, stderr, wait_thr|
stdin.write(io_script)
stdin.close
err_output = stderr.read
unless err_output.empty?
$stderr.puts "qemu-io STDERR: #{err_output}"
end
wait_thr.value
end
raise "qemu-io command failed with status #{status.exitstatus}" unless status.success?
ensure
# Clean up temporary files
temp_files.each do |tf|
tf.close
tf.unlink
end
end
end
# --- Main ---
if ARGV.length < 4
puts 'Error: Missing arguments.'
puts "Usage: #{$PROGRAM_NAME} <rbd_image> <start_snap_name> " \
'<end_snap_fullname> <output_filename>'
exit 1
end
ceph_user = ENV['CEPH_USER']
ceph_key = ENV['CEPH_KEY']
ceph_conf = ENV['CEPH_CONF']
rbd_image, start_snap_name, end_snap_fullname, filename = ARGV[0..3]
begin
if start_snap_name.upcase == 'NONE'
# Full Backup
output_file = filename.to_s
rbd_source_path = "rbd:#{end_snap_fullname}"
rbd_source_path += ":id=#{ceph_user}" unless ceph_user.to_s.empty?
rbd_source_path += ":keyfile=#{ceph_key}" unless ceph_key.to_s.empty?
rbd_source_path += ":conf=#{ceph_conf}" unless ceph_conf.to_s.empty?
FileUtils.rm(output_file) if File.exist?(output_file)
command = "qemu-img convert -f rbd -O qcow2 \"#{rbd_source_path}\" \"#{output_file}\""
run_cmd(command)
else
# Incremental Backup
output_qcow_path = filename.to_s
rbd_backing_path = "rbd:#{rbd_image}@#{start_snap_name}"
rbd_backing_path += ":id=#{ceph_user}" unless ceph_user.to_s.empty?
rbd_backing_path += ":keyfile=#{ceph_key}" unless ceph_key.to_s.empty?
rbd_backing_path += ":conf=#{ceph_conf}" unless ceph_conf.to_s.empty?
rbd_target_path = end_snap_fullname.to_s
temp_rdiff_path = nil
rbd_cmd = 'rbd'
rbd_cmd += " --id #{ceph_user}" if ceph_user && !ceph_user.empty?
rbd_cmd += " --keyfile #{ceph_key}" if ceph_key && !ceph_key.empty?
rbd_cmd += " --conf #{ceph_conf}" if ceph_conf && !ceph_conf.empty?
begin
temp_rdiff_path = File.join(Dir.tmpdir,
"rbd-diff-#{Process.pid}-#{Time.now.to_i}.rdiff")
#--- STEP 1: Generating temporary diff
diff_cmd = "#{rbd_cmd} export-diff --from-snap #{start_snap_name} " \
"\"#{rbd_target_path}\" \"#{temp_rdiff_path}\""
run_cmd(diff_cmd)
#--- STEP 2: Creating destination QCOW2 with backing file
diff_parser = RbdDiffParser.new
diff_parser.parse(temp_rdiff_path)
raise 'ERROR: Could not determine size from rdiff.' unless diff_parser.size
FileUtils.rm(output_qcow_path) if File.exist?(output_qcow_path)
create_cmd = "qemu-img create -f qcow2 -b \"#{rbd_backing_path}\" " \
"-F rbd \"#{output_qcow_path}\" #{diff_parser.size}"
run_cmd(create_cmd)
#--- STEP 3: Start NBD server and pull changes
Nbd.start_nbd(output_qcow_path)
begin
pull_changes(diff_parser, Nbd.uri)
ensure
Nbd.stop_nbd
end
# --- STEP 4: Changing backing file to empty
rebase_cmd = "qemu-img rebase -u -b \"\" -F qcow2 \"#{output_qcow_path}\""
run_cmd(rebase_cmd)
ensure
if temp_rdiff_path && File.exist?(temp_rdiff_path)
FileUtils.rm(temp_rdiff_path)
end
end
end
rescue StandardError => e
puts e.message
exit(-1)
end
+36 -9
View File
@@ -74,10 +74,21 @@ module TransferManager
end
end
ceph_user = disk_xml.elements['CEPH_USER']&.text
ceph_key = disk_xml.elements['CEPH_KEY']&.text
ceph_conf = disk_xml.elements['CEPH_CONF']&.text
@ceph_env = []
@ceph_env << "CEPH_USER=\"#{ceph_user}\"" if ceph_user && !ceph_user.empty?
@ceph_env << "CEPH_CONF=\"#{ceph_conf}\"" if ceph_conf && !ceph_conf.empty?
@ceph_env << "CEPH_KEY=\"#{ceph_key}\"" if ceph_key && !ceph_key.empty?
@ceph_env = @ceph_env.join(' ')
@rbd_cmd = 'rbd'
@rbd_cmd += Ceph.xml_opt(disk_xml, 'CEPH_USER', '--id')
@rbd_cmd += Ceph.xml_opt(disk_xml, 'CEPH_KEY', '--keyfile')
@rbd_cmd += Ceph.xml_opt(disk_xml, 'CEPH_CONF', '--conf')
@rbd_cmd += Ceph.xml_opt(disk_xml, 'CEPH_KEY', '--keyfile')
bc = @vm.elements['BACKUPS/BACKUP_CONFIG']
mode = bc.elements['MODE']&.text if bc
@@ -124,13 +135,17 @@ module TransferManager
# @param backup_dir [String]
# @param ds [TransferManager::Datastore]
# @param live [Boolean]
# @param format [String, nil] 'rbd' for rbdiff format, nil for raw
# @return [Disk]
def backup_cmds(backup_dir, ds, live)
def backup_cmds(backup_dir, ds, live, format = nil)
snap_cmd = ''
expo_cmd = ''
snap_clup = ''
expo_clup = ''
backup_util = '/var/tmp/one/tm/lib/backup_rbd.rb'
if @vm_backup_config[:mode] == :full
# Full backup
draw = "#{backup_dir}/disk.#{@id}.raw"
@@ -166,7 +181,7 @@ module TransferManager
# First incremental backup (similar to full but snapshot must be preserved)
incid = 0
dexp = "#{backup_dir}/disk.#{@id}.rbd2"
dexp = "#{backup_dir}/disk.#{@id}.#{incid}"
snapshot = "#{@rbd_image}@#{INC_SNAP_PREFIX}#{incid}"
snap_cmd << <<~EOF
@@ -176,14 +191,15 @@ module TransferManager
EOF
expo_cmd << ds.cmd_confinement(
"#{@rbd_cmd} export --export-format 2 #{snapshot} #{dexp}\n",
"env #{@ceph_env} " +
"ruby #{backup_util} #{@rbd_image} NONE #{snapshot} #{dexp}\n",
backup_dir
)
else
# Incremental backup
incid = @vm_backup_config[:last_increment] + 1
dinc = "#{backup_dir}/disk.#{@id}.#{incid}.rbdiff"
dinc = "#{backup_dir}/disk.#{@id}.#{incid}"
snapshot = "#{@rbd_image}@one_backup_#{incid}"
last_snap = "one_backup_#{@vm_backup_config[:last_increment]}"
@@ -191,10 +207,21 @@ module TransferManager
snap_cmd << "#{@rbd_cmd} snap create #{snapshot}\n"
snap_cmd << "#{@rbd_cmd} snap protect #{snapshot}\n"
expo_cmd << ds.cmd_confinement(
"#{@rbd_cmd} export-diff --from-snap #{last_snap} #{snapshot} #{dinc}\n",
backup_dir
)
if format == 'rbd'
dinc += '.rbdiff'
expo_cmd << ds.cmd_confinement(
"#{@rbd_cmd} export-diff " \
"--from-snap #{last_snap} #{snapshot} #{dinc}\n",
backup_dir
)
else
expo_cmd << ds.cmd_confinement(
"env #{@ceph_env} " +
"ruby #{backup_util} #{@rbd_image} #{last_snap} #{snapshot} #{dinc}\n",
backup_dir
)
end
old_snapshot = "one_backup_#{@vm_backup_config[:last_increment]}"
snap_clup << rm_snaps_sh({ :type => :eq, :text => old_snapshot })
+2 -1
View File
@@ -89,8 +89,9 @@ module TransferManager
# @param backup_dir [String]
# @param ds [TransferManager::Datastore]
# @param live [Boolean]
# @param _format [String, nil] Unused. Included for compatibility with ceph.rb.
# @return [Disk]
def backup_cmds(backup_dir, ds, live)
def backup_cmds(backup_dir, ds, live, _format = nil)
snap_cmd = ''
expo_cmd = ''
clup_cmd = ''
-4
View File
@@ -1719,10 +1719,6 @@ bool VirtualMachineDisks::backup_keep_last(bool do_volatile)
one_util::toupper(tm_mad);
if (tm_mad == "CEPH")
{
return false;
}
}
return true;