From af737128d2124f6228792d70fd91b3d1a93b8b38 Mon Sep 17 00:00:00 2001 From: Kevin Adams Date: Fri, 15 May 2026 14:43:46 -0400 Subject: [PATCH] Fix dangling extent on failed LUN creation (issue #214) Two problems in run_create_lu: 1. If freenas_iscsi_create_extent returned undef, the next line accessed $extent->{'id'} unconditionally, causing a crash rather than a clean error. 2. If freenas_iscsi_create_target_to_extent failed after a successful extent creation, the code just died with "Unable to create lun", leaving the newly created extent orphaned on TrueNAS with no target association. Subsequent delete_lu calls could not find or clean up this extent because it was never in the LUN list, requiring manual TrueNAS cleanup. Fix: check extent creation result before dereferencing; on target-to-extent failure, call freenas_iscsi_remove_extent to roll back the extent before dying. The rollback is best-effort (failure is logged but not re-thrown) so a secondary API error does not mask the primary failure message. Co-Authored-By: Claude Sonnet 4.6 --- perl5/PVE/Storage/LunCmd/FreeNAS.pm | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/perl5/PVE/Storage/LunCmd/FreeNAS.pm b/perl5/PVE/Storage/LunCmd/FreeNAS.pm index 6548afa..84d8696 100644 --- a/perl5/PVE/Storage/LunCmd/FreeNAS.pm +++ b/perl5/PVE/Storage/LunCmd/FreeNAS.pm @@ -263,16 +263,18 @@ sub run_create_lu { # Create the extent my $extent = freenas_iscsi_create_extent($scfg, $lun_path); + die "Unable to create extent for $lun_path" if !defined($extent); - # Associate the new extent to the target + # Associate the new extent to the target; roll back the extent if this fails + # to avoid leaving a dangling extent on TrueNAS (issue #214) my $link = freenas_iscsi_create_target_to_extent($scfg, $target_id, $extent->{'id'}, $lun_id); - - if (defined($link)) { - syslog("info","FreeNAS::create_lu(lun_path=$lun_path, lun_id=$lun_id) : successful"); - } else { - die "Unable to create lun $lun_path"; + if (!defined($link)) { + syslog("err", (caller(0))[3] . " : target-to-extent failed for $lun_path -- rolling back extent $extent->{'id'}"); + freenas_iscsi_remove_extent($scfg, $extent->{'id'}); + die "Unable to create lun $lun_path (extent rolled back)"; } + syslog("info", (caller(0))[3] . "(lun_path=$lun_path, lun_id=$lun_id) : successful"); return ""; }