2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI Reloaded
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021-2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "controllers/controller_manager.h"
|
|
|
|
#include "controllers/scsi_controller.h"
|
|
|
|
#include "devices/device_factory.h"
|
|
|
|
#include "devices/primary_device.h"
|
|
|
|
#include "devices/disk.h"
|
|
|
|
#include "rascsi_service.h"
|
|
|
|
#include "rascsi_image.h"
|
|
|
|
#include "rascsi_exceptions.h"
|
|
|
|
#include "localizer.h"
|
2022-10-08 17:26:04 +00:00
|
|
|
#include "protobuf_util.h"
|
2022-10-06 14:15:19 +00:00
|
|
|
#include "command_context.h"
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "rasutil.h"
|
|
|
|
#include "rascsi_executor.h"
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace spdlog;
|
2022-10-08 17:26:04 +00:00
|
|
|
using namespace protobuf_util;
|
2022-10-04 15:23:42 +00:00
|
|
|
using namespace ras_util;
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
bool RascsiExecutor::ProcessDeviceCmd(const CommandContext& context, const PbDeviceDefinition& pb_device,
|
2022-10-04 15:23:42 +00:00
|
|
|
const PbCommand& command, bool dryRun)
|
|
|
|
{
|
|
|
|
PrintCommand(command, pb_device, dryRun);
|
|
|
|
|
|
|
|
const int id = pb_device.id();
|
|
|
|
const int lun = pb_device.unit();
|
|
|
|
|
|
|
|
if (!ValidateIdAndLun(context, id, lun)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PbOperation operation = command.operation();
|
|
|
|
|
|
|
|
// For all commands except ATTACH the device and LUN must exist
|
|
|
|
if (operation != ATTACH && !VerifyExistingIdAndLun(context, id, lun)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto device = controller_manager.GetDeviceByIdAndLun(id, lun);
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (!ValidateOperationAgainstDevice(context, *device, operation)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case START:
|
2022-11-02 06:36:25 +00:00
|
|
|
return Start(*device, dryRun);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
case STOP:
|
2022-11-02 06:36:25 +00:00
|
|
|
return Stop(*device, dryRun);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
case ATTACH:
|
|
|
|
return Attach(context, pb_device, dryRun);
|
|
|
|
|
|
|
|
case DETACH:
|
|
|
|
return Detach(context, device, dryRun);
|
|
|
|
|
|
|
|
case INSERT:
|
|
|
|
return Insert(context, pb_device, device, dryRun);
|
|
|
|
|
|
|
|
case EJECT:
|
2022-11-02 06:36:25 +00:00
|
|
|
return Eject(*device, dryRun);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
case PROTECT:
|
2022-11-02 06:36:25 +00:00
|
|
|
return Protect(*device, dryRun);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
case UNPROTECT:
|
2022-11-02 06:36:25 +00:00
|
|
|
return Unprotect(*device, dryRun);
|
2022-10-04 15:23:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CHECK_AUTHENTICATION:
|
|
|
|
case NO_OPERATION:
|
|
|
|
// Do nothing, just log
|
|
|
|
LOGTRACE("Received %s command", PbOperation_Name(operation).c_str())
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_OPERATION);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::ProcessCmd(const CommandContext& context, const PbCommand& command)
|
|
|
|
{
|
|
|
|
switch (command.operation()) {
|
|
|
|
case DETACH_ALL:
|
|
|
|
DetachAll();
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnStatus();
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
case RESERVE_IDS: {
|
|
|
|
const string ids = GetParam(command, "ids");
|
2022-10-08 17:26:04 +00:00
|
|
|
if (const string error = SetReservedIds(ids); !error.empty()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnStatus(false, error);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnStatus();
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case CREATE_IMAGE:
|
|
|
|
return rascsi_image.CreateImage(context, command);
|
|
|
|
|
|
|
|
case DELETE_IMAGE:
|
|
|
|
return rascsi_image.DeleteImage(context, command);
|
|
|
|
|
|
|
|
case RENAME_IMAGE:
|
|
|
|
return rascsi_image.RenameImage(context, command);
|
|
|
|
|
|
|
|
case COPY_IMAGE:
|
|
|
|
return rascsi_image.CopyImage(context, command);
|
|
|
|
|
|
|
|
case PROTECT_IMAGE:
|
|
|
|
case UNPROTECT_IMAGE:
|
|
|
|
return rascsi_image.SetImagePermissions(context, command);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// This is a device-specific command handled below
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the list of reserved files, than run the dry run
|
2022-10-23 19:51:39 +00:00
|
|
|
const auto& reserved_files = StorageDevice::GetReservedFiles();
|
2022-10-04 15:23:42 +00:00
|
|
|
for (const auto& device : command.devices()) {
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!ProcessDeviceCmd(context, device, command, true)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
// Dry run failed, restore the file list
|
2022-10-23 19:51:39 +00:00
|
|
|
StorageDevice::SetReservedFiles(reserved_files);
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the list of reserved files before proceeding
|
2022-10-23 19:51:39 +00:00
|
|
|
StorageDevice::SetReservedFiles(reserved_files);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
if (const string result = ValidateLunSetup(command); !result.empty()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnStatus(false, result);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& device : command.devices()) {
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!ProcessDeviceCmd(context, device, command, false)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ATTACH and DETACH return the device list
|
2022-10-06 14:15:19 +00:00
|
|
|
if (context.IsValid() && (command.operation() == ATTACH || command.operation() == DETACH)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
// A new command with an empty device list is required here in order to return data for all devices
|
|
|
|
PbCommand cmd;
|
|
|
|
PbResult result;
|
2022-11-04 07:22:32 +00:00
|
|
|
rascsi_response.GetDevicesInfo(controller_manager.GetAllDevices(), result, cmd,
|
|
|
|
rascsi_image.GetDefaultFolder());
|
2022-10-06 14:15:19 +00:00
|
|
|
serializer.SerializeMessage(context.GetFd(), result);
|
2022-10-04 15:23:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnStatus();
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::SetLogLevel(const string& log_level) const
|
|
|
|
{
|
|
|
|
if (log_level == "trace") {
|
|
|
|
set_level(level::trace);
|
|
|
|
}
|
|
|
|
else if (log_level == "debug") {
|
|
|
|
set_level(level::debug);
|
|
|
|
}
|
|
|
|
else if (log_level == "info") {
|
|
|
|
set_level(level::info);
|
|
|
|
}
|
|
|
|
else if (log_level == "warn") {
|
|
|
|
set_level(level::warn);
|
|
|
|
}
|
|
|
|
else if (log_level == "err") {
|
|
|
|
set_level(level::err);
|
|
|
|
}
|
|
|
|
else if (log_level == "off") {
|
|
|
|
set_level(level::off);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOGWARN("Invalid log level '%s'", log_level.c_str())
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGINFO("Set log level to '%s'", log_level.c_str())
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::Start(PrimaryDevice& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
if (!dryRun) {
|
2022-11-02 06:36:25 +00:00
|
|
|
LOGINFO("Start requested for %s ID %d, unit %d", device.GetTypeString(), device.GetId(), device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (!device.Start()) {
|
|
|
|
LOGWARN("Starting %s ID %d, unit %d failed", device.GetTypeString(), device.GetId(), device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::Stop(PrimaryDevice& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
if (!dryRun) {
|
2022-11-02 06:36:25 +00:00
|
|
|
LOGINFO("Stop requested for %s ID %d, unit %d", device.GetTypeString(), device.GetId(), device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
device.Stop();
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::Eject(PrimaryDevice& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
if (!dryRun) {
|
2022-11-02 06:36:25 +00:00
|
|
|
LOGINFO("Eject requested for %s ID %d, unit %d", device.GetTypeString(), device.GetId(), device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (!device.Eject(true)) {
|
|
|
|
LOGWARN("Ejecting %s ID %d, unit %d failed", device.GetTypeString(), device.GetId(), device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::Protect(PrimaryDevice& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
if (!dryRun) {
|
2022-11-02 06:36:25 +00:00
|
|
|
LOGINFO("Write protection requested for %s ID %d, unit %d", device.GetTypeString(), device.GetId(),
|
|
|
|
device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
device.SetProtected(true);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::Unprotect(PrimaryDevice& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
if (!dryRun) {
|
2022-11-02 06:36:25 +00:00
|
|
|
LOGINFO("Write unprotection requested for %s ID %d, unit %d", device.GetTypeString(), device.GetId(),
|
|
|
|
device.GetLun())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
device.SetProtected(false);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::Attach(const CommandContext& context, const PbDeviceDefinition& pb_device, bool dryRun)
|
|
|
|
{
|
|
|
|
const int id = pb_device.id();
|
|
|
|
const int lun = pb_device.unit();
|
|
|
|
const PbDeviceType type = pb_device.type();
|
|
|
|
|
|
|
|
if (lun >= ScsiController::LUN_MAX) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_INVALID_LUN, to_string(lun), to_string(ScsiController::LUN_MAX));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (controller_manager.GetDeviceByIdAndLun(id, lun) != nullptr) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_DUPLICATE_ID, to_string(id), to_string(lun));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reserved_ids.find(id) != reserved_ids.end()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_RESERVED_ID, to_string(id));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
const string filename = GetParam(pb_device, "file");
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
auto device = CreateDevice(context, type, lun, filename);
|
|
|
|
if (device == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// If no filename was provided the medium is considered not inserted
|
|
|
|
auto storage_device = dynamic_pointer_cast<StorageDevice>(device);
|
|
|
|
device->SetRemoved(storage_device != nullptr ? filename.empty() : false);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (!SetProductData(context, pb_device, *device)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!SetSectorSize(context, device, pb_device.block_size())) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string full_path;
|
2022-10-08 17:26:04 +00:00
|
|
|
if (device->SupportsFile()) {
|
|
|
|
// Only with removable media drives, CD and MO the medium (=file) may be inserted later
|
2022-10-04 15:23:42 +00:00
|
|
|
if (!device->IsRemovable() && filename.empty()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_MISSING_FILENAME, PbDeviceType_Name(type));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (!ValidateImageFile(context, *storage_device, filename, full_path)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only non read-only devices support protect/unprotect
|
|
|
|
// This operation must not be executed before Open() because Open() overrides some settings.
|
|
|
|
if (device->IsProtectable() && !device->IsReadOnly()) {
|
|
|
|
device->SetProtected(pb_device.protected_());
|
|
|
|
}
|
|
|
|
|
|
|
|
unordered_map<string, string> params = { pb_device.params().begin(), pb_device.params().end() };
|
|
|
|
if (!device->SupportsFile()) {
|
2022-10-23 19:51:39 +00:00
|
|
|
// Clients like rasctl might have sent both "file" and "interfaces"
|
2022-10-04 15:23:42 +00:00
|
|
|
params.erase("file");
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
if (!device->Init(params)) {
|
2022-10-29 16:10:00 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_INITIALIZATION, PbDeviceType_Name(device->GetType()),
|
2022-10-04 15:23:42 +00:00
|
|
|
to_string(id), to_string(lun));
|
|
|
|
}
|
|
|
|
|
2022-10-29 16:10:00 +00:00
|
|
|
if (storage_device != nullptr) {
|
2022-10-23 19:51:39 +00:00
|
|
|
storage_device->ReserveFile(full_path, id, lun);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the dry run here, before actually attaching
|
|
|
|
if (dryRun) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
if (!controller_manager.AttachToScsiController(id, device)) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SCSI_CONTROLLER);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string msg = "Attached ";
|
|
|
|
if (device->IsReadOnly()) {
|
|
|
|
msg += "read-only ";
|
|
|
|
}
|
|
|
|
else if (device->IsProtectable() && device->IsProtected()) {
|
|
|
|
msg += "protected ";
|
|
|
|
}
|
2022-10-23 19:51:39 +00:00
|
|
|
msg += string(device->GetTypeString()) + " device, ID " + to_string(id) + ", unit " + to_string(lun);
|
2022-10-04 15:23:42 +00:00
|
|
|
LOGINFO("%s", msg.c_str())
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::Insert(const CommandContext& context, const PbDeviceDefinition& pb_device,
|
2022-11-02 06:36:25 +00:00
|
|
|
const shared_ptr<PrimaryDevice>& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-10-23 19:51:39 +00:00
|
|
|
auto storage_device = dynamic_pointer_cast<StorageDevice>(device);
|
|
|
|
if (storage_device == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!storage_device->IsRemoved()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_EJECT_REQUIRED);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pb_device.vendor().empty() || !pb_device.product().empty() || !pb_device.revision().empty()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_DEVICE_NAME_UPDATE);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
const string filename = GetParam(pb_device, "file");
|
2022-10-04 15:23:42 +00:00
|
|
|
if (filename.empty()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_MISSING_FILENAME);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// Stop the dry run here, before modifying the device
|
2022-10-04 15:23:42 +00:00
|
|
|
if (dryRun) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGINFO("Insert %sfile '%s' requested into %s ID %d, unit %d", pb_device.protected_() ? "protected " : "",
|
2022-10-23 19:51:39 +00:00
|
|
|
filename.c_str(), storage_device->GetTypeString(), pb_device.id(), pb_device.unit())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!SetSectorSize(context, storage_device, pb_device.block_size())) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string full_path;
|
2022-11-02 06:36:25 +00:00
|
|
|
if (!ValidateImageFile(context, *storage_device, filename, full_path)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
storage_device->SetProtected(pb_device.protected_());
|
|
|
|
storage_device->ReserveFile(full_path, storage_device->GetId(), storage_device->GetLun());
|
2022-11-02 14:36:19 +00:00
|
|
|
storage_device->SetMediumChanged(true);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::Detach(const CommandContext& context, const shared_ptr<PrimaryDevice>& device, bool dryRun) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-10-08 17:26:04 +00:00
|
|
|
auto controller = controller_manager.FindController(device->GetId());
|
|
|
|
if (controller == nullptr) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_DETACH);
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
// LUN 0 can only be detached if there is no other LUN anymore
|
2022-10-08 17:26:04 +00:00
|
|
|
if (!device->GetLun() && controller->GetLunCount() > 1) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_LUN0);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dryRun) {
|
2022-10-25 08:29:57 +00:00
|
|
|
// Remember the ID before it gets invalid when removing the device
|
|
|
|
const int id = device->GetId();
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!controller->RemoveDevice(device)) {
|
2022-10-08 17:26:04 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_DETACH);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
// If no LUN is left also delete the controller
|
|
|
|
if (!controller->GetLunCount() && !controller_manager.DeleteController(controller)) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_DETACH);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (auto storage_device = dynamic_pointer_cast<StorageDevice>(device); storage_device != nullptr) {
|
|
|
|
storage_device->UnreserveFile();
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
LOGINFO("%s", ("Detached " + string(device->GetTypeString()) + " device with ID " + to_string(id)
|
|
|
|
+ ", unit " + to_string(device->GetLun())).c_str())
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RascsiExecutor::DetachAll()
|
|
|
|
{
|
|
|
|
controller_manager.DeleteAllControllers();
|
2022-10-23 19:51:39 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
LOGINFO("Detached all devices")
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::ShutDown(const CommandContext& context, const string& mode) {
|
|
|
|
if (mode.empty()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SHUTDOWN_MODE_MISSING);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PbResult result;
|
|
|
|
result.set_status(true);
|
|
|
|
|
|
|
|
if (mode == "rascsi") {
|
|
|
|
LOGINFO("RaSCSI shutdown requested")
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
serializer.SerializeMessage(context.GetFd(), result);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != "system" && mode != "reboot") {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SHUTDOWN_MODE_INVALID, mode);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The root user has UID 0
|
|
|
|
if (getuid()) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SHUTDOWN_PERMISSION);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == "system") {
|
|
|
|
LOGINFO("System shutdown requested")
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
serializer.SerializeMessage(context.GetFd(), result);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
DetachAll();
|
|
|
|
|
|
|
|
if (system("init 0") == -1) {
|
|
|
|
LOGERROR("System shutdown failed: %s", strerror(errno))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mode == "reboot") {
|
|
|
|
LOGINFO("System reboot requested")
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
serializer.SerializeMessage(context.GetFd(), result);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
DetachAll();
|
|
|
|
|
|
|
|
if (system("init 6") == -1) {
|
|
|
|
LOGERROR("System reboot failed: %s", strerror(errno))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string RascsiExecutor::SetReservedIds(string_view ids)
|
|
|
|
{
|
|
|
|
list<string> ids_to_reserve;
|
|
|
|
stringstream ss(ids.data());
|
|
|
|
string id;
|
|
|
|
while (getline(ss, id, ',')) {
|
|
|
|
if (!id.empty()) {
|
|
|
|
ids_to_reserve.push_back(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
set<int> reserved;
|
2022-10-08 17:26:04 +00:00
|
|
|
for (const string& id_to_reserve : ids_to_reserve) {
|
2022-10-04 15:23:42 +00:00
|
|
|
int res_id;
|
|
|
|
if (!GetAsInt(id_to_reserve, res_id) || res_id > 7) {
|
|
|
|
return "Invalid ID " + id_to_reserve;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (controller_manager.FindController(res_id) != nullptr) {
|
|
|
|
return "ID " + id_to_reserve + " is currently in use";
|
|
|
|
}
|
|
|
|
|
|
|
|
reserved.insert(res_id);
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
reserved_ids = { reserved.begin(), reserved.end() };
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
if (!reserved_ids.empty()) {
|
|
|
|
string s;
|
|
|
|
bool isFirst = true;
|
2022-11-02 14:36:19 +00:00
|
|
|
for (const auto& reserved_id : reserved) {
|
2022-10-04 15:23:42 +00:00
|
|
|
if (!isFirst) {
|
|
|
|
s += ", ";
|
|
|
|
}
|
|
|
|
isFirst = false;
|
|
|
|
s += to_string(reserved_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGINFO("Reserved ID(s) set to %s", s.c_str())
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOGINFO("Cleared reserved ID(s)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::ValidateImageFile(const CommandContext& context, StorageDevice& storage_device,
|
2022-10-04 15:23:42 +00:00
|
|
|
const string& filename, string& full_path) const
|
|
|
|
{
|
2022-10-23 19:51:39 +00:00
|
|
|
if (filename.empty()) {
|
2022-10-04 15:23:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
if (const auto [id1, lun1] = StorageDevice::GetIdsForReservedFile(filename); id1 != -1 || lun1 != -1) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_IMAGE_IN_USE, filename,
|
2022-10-25 08:29:57 +00:00
|
|
|
to_string(id1), to_string(lun1));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
string effective_filename = filename;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!StorageDevice::FileExists(filename)) {
|
|
|
|
// If the file does not exist search for it in the default image folder
|
|
|
|
effective_filename = rascsi_image.GetDefaultFolder() + "/" + filename;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
if (const auto [id2, lun2] = StorageDevice::GetIdsForReservedFile(effective_filename); id2 != -1 || lun2 != -1) {
|
2022-10-23 19:51:39 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_IMAGE_IN_USE, filename,
|
2022-10-25 08:29:57 +00:00
|
|
|
to_string(id2), to_string(lun2));
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (!StorageDevice::FileExists(effective_filename)) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_FILE_OPEN, effective_filename);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
storage_device.SetFilename(effective_filename);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (storage_device.IsReadOnlyFile()) {
|
2022-10-23 19:51:39 +00:00
|
|
|
// Permanently write-protected
|
2022-11-02 06:36:25 +00:00
|
|
|
storage_device.SetReadOnly(true);
|
|
|
|
storage_device.SetProtectable(false);
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-11-02 06:36:25 +00:00
|
|
|
storage_device.SetReadOnly(false);
|
|
|
|
storage_device.SetProtectable(true);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
storage_device.Open();
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
full_path = effective_filename;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RascsiExecutor::PrintCommand(const PbCommand& command, const PbDeviceDefinition& pb_device, bool dryRun) const
|
|
|
|
{
|
|
|
|
const map<string, string, less<>> params = { command.params().begin(), command.params().end() };
|
|
|
|
|
|
|
|
ostringstream s;
|
|
|
|
s << (dryRun ? "Validating" : "Executing");
|
|
|
|
s << ": operation=" << PbOperation_Name(command.operation());
|
|
|
|
|
|
|
|
if (!params.empty()) {
|
|
|
|
s << ", command params=";
|
|
|
|
bool isFirst = true;
|
|
|
|
for (const auto& [key, value]: params) {
|
|
|
|
if (!isFirst) {
|
|
|
|
s << ", ";
|
|
|
|
}
|
|
|
|
isFirst = false;
|
|
|
|
string v = key != "token" ? value : "???";
|
|
|
|
s << "'" << key << "=" << v << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s << ", device id=" << pb_device.id() << ", lun=" << pb_device.unit() << ", type="
|
|
|
|
<< PbDeviceType_Name(pb_device.type());
|
|
|
|
|
|
|
|
if (pb_device.params_size()) {
|
|
|
|
s << ", device params=";
|
|
|
|
bool isFirst = true;
|
|
|
|
for (const auto& [key, value]: pb_device.params()) {
|
|
|
|
if (!isFirst) {
|
|
|
|
s << ":";
|
|
|
|
}
|
|
|
|
isFirst = false;
|
|
|
|
s << "'" << key << "=" << value << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s << ", vendor='" << pb_device.vendor() << "', product='" << pb_device.product()
|
|
|
|
<< "', revision='" << pb_device.revision() << "', block size=" << pb_device.block_size();
|
|
|
|
LOGINFO("%s", s.str().c_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
string RascsiExecutor::ValidateLunSetup(const PbCommand& command) const
|
|
|
|
{
|
|
|
|
// Mapping of available LUNs (bit vector) to devices
|
|
|
|
unordered_map<uint32_t, uint32_t> luns;
|
|
|
|
|
|
|
|
// Collect LUN bit vectors of new devices
|
|
|
|
for (const auto& device : command.devices()) {
|
|
|
|
luns[device.id()] |= 1 << device.unit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect LUN bit vectors of existing devices
|
|
|
|
for (const auto& device : controller_manager.GetAllDevices()) {
|
|
|
|
luns[device->GetId()] |= 1 << device->GetLun();
|
|
|
|
}
|
|
|
|
|
|
|
|
// LUN 0 must exist for all devices
|
2022-10-08 17:26:04 +00:00
|
|
|
for (const auto& [id, lun]: luns) {
|
2022-10-04 15:23:42 +00:00
|
|
|
if (!(lun & 0x01)) {
|
|
|
|
return "LUN 0 is missing for device ID " + to_string(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::VerifyExistingIdAndLun(const CommandContext& context, int id, int lun) const
|
|
|
|
{
|
|
|
|
if (controller_manager.FindController(id) == nullptr) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_NON_EXISTING_DEVICE, to_string(id));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (controller_manager.GetDeviceByIdAndLun(id, lun) == nullptr) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_NON_EXISTING_UNIT, to_string(id), to_string(lun));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<PrimaryDevice> RascsiExecutor::CreateDevice(const CommandContext& context, const PbDeviceType type,
|
|
|
|
int lun, const string& filename) const
|
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
auto device = device_factory.CreateDevice(type, lun, filename);
|
2022-10-04 15:23:42 +00:00
|
|
|
if (device == nullptr) {
|
|
|
|
if (type == UNDEFINED) {
|
2022-10-06 14:15:19 +00:00
|
|
|
context.ReturnLocalizedError(LocalizationKey::ERROR_MISSING_DEVICE_TYPE, filename);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-10-06 14:15:19 +00:00
|
|
|
context.ReturnLocalizedError(LocalizationKey::ERROR_UNKNOWN_DEVICE_TYPE, PbDeviceType_Name(type));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
bool RascsiExecutor::SetSectorSize(const CommandContext& context, shared_ptr<PrimaryDevice> device, int block_size) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
if (block_size) {
|
|
|
|
auto disk = dynamic_pointer_cast<Disk>(device);
|
|
|
|
if (disk != nullptr && disk->IsSectorSizeConfigurable()) {
|
|
|
|
if (!disk->SetConfiguredSectorSize(device_factory, block_size)) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_BLOCK_SIZE, to_string(block_size));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2022-10-23 19:51:39 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_BLOCK_SIZE_NOT_CONFIGURABLE,
|
|
|
|
device->GetTypeString());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
bool RascsiExecutor::ValidateOperationAgainstDevice(const CommandContext& context, const PrimaryDevice& device,
|
|
|
|
const PbOperation& operation)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-11-02 06:36:25 +00:00
|
|
|
if ((operation == START || operation == STOP) && !device.IsStoppable()) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_OPERATION_DENIED_STOPPABLE, device.GetTypeString());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if ((operation == INSERT || operation == EJECT) && !device.IsRemovable()) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_OPERATION_DENIED_REMOVABLE, device.GetTypeString());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if ((operation == PROTECT || operation == UNPROTECT) && !device.IsProtectable()) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_OPERATION_DENIED_PROTECTABLE, device.GetTypeString());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if ((operation == PROTECT || operation == UNPROTECT) && !device.IsReady()) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_OPERATION_DENIED_READY, device.GetTypeString());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::ValidateIdAndLun(const CommandContext& context, int id, int lun)
|
|
|
|
{
|
|
|
|
// Validate the device ID and LUN
|
|
|
|
if (id < 0) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_MISSING_DEVICE_ID);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
if (id >= ControllerManager::DEVICE_MAX) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_INVALID_ID, to_string(id), to_string(ControllerManager::DEVICE_MAX - 1));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
if (lun < 0 || lun >= ScsiController::LUN_MAX) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_INVALID_LUN, to_string(lun), to_string(ScsiController::LUN_MAX - 1));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RascsiExecutor::SetProductData(const CommandContext& context, const PbDeviceDefinition& pb_device,
|
2022-11-02 06:36:25 +00:00
|
|
|
PrimaryDevice& device)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (!pb_device.vendor().empty()) {
|
2022-11-02 06:36:25 +00:00
|
|
|
device.SetVendor(pb_device.vendor());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
if (!pb_device.product().empty()) {
|
2022-11-02 06:36:25 +00:00
|
|
|
device.SetProduct(pb_device.product());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
if (!pb_device.revision().empty()) {
|
2022-11-02 06:36:25 +00:00
|
|
|
device.SetRevision(pb_device.revision());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const invalid_argument& e) {
|
2022-10-06 14:15:19 +00:00
|
|
|
return context.ReturnStatus(false, e.what());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|