From ca10eb3320692392e5c6c501ba72f1bd73bced8a Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Wed, 5 Sep 2012 23:35:54 -0700 Subject: [PATCH] Device creation, editing and deletion pages --- iscsi-server/delete_devices.cgi | 62 +++++++++++++++++++++++++++++ iscsi-server/lang/en | 11 +++++- iscsi-server/list_devices.cgi | 4 +- iscsi-server/save_device.cgi | 69 +++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 iscsi-server/delete_devices.cgi create mode 100644 iscsi-server/save_device.cgi diff --git a/iscsi-server/delete_devices.cgi b/iscsi-server/delete_devices.cgi new file mode 100644 index 000000000..4908d3588 --- /dev/null +++ b/iscsi-server/delete_devices.cgi @@ -0,0 +1,62 @@ +#!/usr/local/bin/perl +# Delete multiple devices + +use strict; +use warnings; +require './iscsi-server-lib.pl'; +our (%text, %in, %config); +my $conf = &get_iscsi_config(); +&ReadParse(); +&error_setup($text{'devices_derr'}); + +# Get the devices +my @devices; +my @d = split(/\0/, $in{'d'}); +foreach my $d (@d) { + push(@devices, grep { $_->{'type'} eq 'device' && + $_->{'num'} eq $d } @$conf); + } +@devices || &error($text{'devices_denone'}); + +# Check if in use +foreach my $device (@devices) { + my @users = &find_extent_users($conf, $device); + if (@users) { + &error(&text('devices_einuse', + $device->{'type'}.$device->{'num'}, + join(", ", map { &describe_object($_) } @users))); + } + } + +if ($in{'confirm'}) { + # Do the deletion + &lock_file($config{'targets_file'}); + + foreach my $device (@devices) { + &save_directive($conf, $device, undef); + } + + &unlock_file($config{'targets_file'}); + if (@devices == 1) { + &webmin_log('delete', 'device', $devices[0]->{'device'}); + } + else { + &webmin_log('delete', 'devices', scalar(@devices)); + } + &redirect("list_devices.cgi"); + } +else { + # Ask first + &ui_print_header(undef, $text{'devices_title'}, ""); + + print &ui_confirmation_form( + "delete_devices.cgi", + &text('devices_drusure', + join(" ", map { $_->{'type'}.$_->{'num'} } @devices)), + [ map { [ "d", $_ ] } @d ], + [ [ 'confirm', $text{'devices_sure'} ] ], + ); + + &ui_print_footer("list_devices.cgi", $text{'devices_return'}); + } + diff --git a/iscsi-server/lang/en b/iscsi-server/lang/en index f662d2f27..ed8bbf5bf 100644 --- a/iscsi-server/lang/en +++ b/iscsi-server/lang/en @@ -24,7 +24,7 @@ extents_delete=Delete Selected Devices extents_return=list of devices to share extents_derr=Failed to delete devices extents_denone=None selected -extents_einuse=Device $1 cannot be deleted, as it is in use by $2 +extents_einuse=Device $1 cannot be deleted, as it is in use by : $2 extents_drusure=Are you sure you want to remove the following devices : $1? Their contents will be preserved, but they will no longer be accessible to iSCSI clients. extents_sure=Delete Devices @@ -52,7 +52,7 @@ extent_eother=File to share does not exist extent_estart=Start of extent must be a number extent_esize=Size of extent must be a number extent_esizemax=End of extent cannot be larger than the device size of $1 -extent_einuse=This device cannot be deleted, as it is in use by $1 +extent_einuse=This device cannot be deleted, as it is in use by : $1 devices_title=Devices Combinations devices_none=No device combinations have been created yet. @@ -64,6 +64,11 @@ devices_extents=Member devices devices_add=Add a new device combination. devices_delete=Delete Selected Combinations devices_return=list of device combinations +devices_derr=Failed to delete device combinations +devices_denone=None selected +devices_einuse=Combination $1 cannot be deleted, as it is in use by : $2 +devices_drusure=Are you sure you want to remove the following device combinations : $1? Their contents will be preserved, but they will no longer be accessible to iSCSI clients. +devices_sure=Delete Device Combinations device_create=Create Device Combination device_edit=Edit Device Combination @@ -78,6 +83,8 @@ device_err=Failed to save device combination device_derr=Failed to delete device combination device_rusure=Are you sure you want to delete the device combination $1? Its contents will be preserved, but it will no longer be accessible to iSCSI clients. device_sure=Delete Device Combination +device_einuse=This device combination cannot be deleted, as it is in use by : $1 +device_eextents=No member devices selected targets_title=Sharing Targets targets_none=No sharing targets have been created yet. diff --git a/iscsi-server/list_devices.cgi b/iscsi-server/list_devices.cgi index 8839cbebf..a64129e6a 100644 --- a/iscsi-server/list_devices.cgi +++ b/iscsi-server/list_devices.cgi @@ -20,13 +20,15 @@ if (@devices) { $text{'devices_name'}, $text{'devices_mode'}, $text{'devices_extents'} ], 100, 0, \@tds); + my %omap = map { $_->{'type'}.$_->{'num'}, $_ } @$conf; foreach my $e (@devices) { print &ui_checked_columns_row([ "". $e->{'type'}.$e->{'num'}."", $text{'devices_mode_'.$e->{'mode'}} || uc($e->{'mode'}), - join(" | ", @{$e->{'extents'}}), + join(" | ", + map { &describe_object($omap{$_}) } @{$e->{'extents'}}), ], \@tds, "d", $e->{'num'}); } print &ui_columns_end(); diff --git a/iscsi-server/save_device.cgi b/iscsi-server/save_device.cgi new file mode 100644 index 000000000..6c6d593a5 --- /dev/null +++ b/iscsi-server/save_device.cgi @@ -0,0 +1,69 @@ +#!/usr/local/bin/perl +# Create, update or delete an device + +use strict; +use warnings; +require './iscsi-server-lib.pl'; +our (%text, %in, %config); +&lock_file($config{'targets_file'}); +my $conf = &get_iscsi_config(); +&ReadParse(); +&error_setup($in{'delete'} ? $text{'device_derr'} : $text{'device_err'}); + +my ($device, $old_device); +if (!$in{'new'}) { + # Get the existing device + $device = &find($conf, "device", $in{'num'}); + $device || &text('device_egone', $in{'num'}); + $old_device = $device; + } +else { + # Creating a new one + $device = { 'num' => &find_free_num($conf, 'device'), + 'type' => 'device' }; + } + +if ($in{'delete'}) { + # Check if in use before deleting + my @users = &find_extent_users($conf, $device); + if (@users) { + &error(&text('device_einuse', + join(", ", map { &describe_object($_) } @users))); + } + + # Delete, after asking for confirmation + if ($in{'confirm'}) { + # Delete it + &save_directive($conf, $device, undef); + } + else { + # Ask first + &ui_print_header(undef, $text{'device_edit'}, ""); + + print &ui_confirmation_form( + "save_device.cgi", + &text('device_rusure', $device->{'type'}.$device->{'num'}), + [ [ 'num', $in{'num'} ], + [ 'delete', 1 ] ], + [ [ 'confirm', $text{'device_sure'} ] ], + ); + + &ui_print_footer("list_devices.cgi", $text{'devices_return'}); + return; + } + } +else { + # Validate and store inputs + $device->{'mode'} = $in{'mode'}; + my @extents = split(/\r?\n/, $in{'extents'}); + @extents || &error($text{'device_eextents'}); + $device->{'extents'} = \@extents; + + # Write out the config + &save_directive($conf, $old_device, $device); + } + +&unlock_file($config{'targets_file'}); +&webmin_log($in{'new'} ? 'create' : $in{'delete'} ? 'delete' : 'modify', + 'device', $device->{'device'}); +&redirect("list_devices.cgi");