#!/usr/bin/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.                                             #
#--------------------------------------------------------------------------- #

# %%RUBYGEMS_SETUP_BEGIN%%
require 'load_opennebula_paths'
# %%RUBYGEMS_SETUP_END%%

###############################################################################
# This script creates an image on-the-fly bypassing the datastore/image workflow
###############################################################################
#
# ARGUMENTS: size format host:remote_system_ds/disk.i vmid dsid
#   - size in MB of the image
#   - format for the image
#   - remote_system_ds is the path for the system datastore in the host
#   - vmid is the id of the VM
#   - dsid is the target datastore (0 is the system datastore)
# STDIN: -
# RETURNS: -
#
###############################################################################

require_relative '../lib/tm_action'
require_relative '../../datastore/lvm'

_arg_size = ARGV[0]
arg_format = ARGV[1]
arg_dst = ARGV[2]
arg_vmid = ARGV[3]

dst = TransferManager::Action::Location.new(arg_dst)

lvmdisk = MAD::LVMVM.get_from_id(arg_vmid).disk(dst.disk_id)
lvmdisk.create(dst, arg_format == 'swap')
