Linux: add emergency cleanup for stale unmounts

When normal filesystem unmount fails, the Linux path could stop before cleaning VeraCrypt mapper, loop and FUSE objects. Add an explicit emergency dismount request that is only reached after interactive confirmation.

The recovery path lazy-detaches mounted filesystems, uses deferred dmsetup removal for VeraCrypt mapper devices, detaches loop devices, and keeps normal force/ignoreOpenFiles behavior unchanged.
This commit is contained in:
Mounir IDRASSI
2026-05-01 23:25:25 +09:00
parent 338fedc56c
commit abd089140b
60 changed files with 639 additions and 18 deletions

View File

@@ -44,6 +44,9 @@ namespace VeraCrypt
CommandLineInterface::CommandLineInterface (int argc, wchar_t** argv, UserInterfaceType::Enum interfaceType) :
ArgCommand (CommandId::None),
#ifdef TC_LINUX
ArgEmergencyUnmount (false),
#endif
ArgFilesystem (VolumeCreationOptions::FilesystemType::Unknown),
ArgNewPim (-1),
ArgNoHiddenVolumeProtection (false),
@@ -79,6 +82,9 @@ namespace VeraCrypt
parser.AddSwitch (L"", L"delete-token-keyfiles", _("Delete security token keyfiles"));
parser.AddSwitch (L"d", L"dismount", _("Unmount volume (deprecated: use 'unmount')"));
parser.AddSwitch (L"u", L"unmount", _("Unmount volume"));
#ifdef TC_LINUX
parser.AddSwitch (L"", L"emergency-unmount", _("Attempt emergency cleanup if normal Linux unmount fails"));
#endif
parser.AddSwitch (L"", L"display-password", _("Display password while typing"));
parser.AddOption (L"", L"encryption", _("Encryption algorithm"));
parser.AddSwitch (L"", L"explore", _("Open explorer window for mounted volume"));
@@ -405,6 +411,10 @@ namespace VeraCrypt
ArgForce = parser.Found (L"force");
#ifdef TC_LINUX
ArgEmergencyUnmount = parser.Found (L"emergency-unmount");
#endif
ArgDisableFileSizeCheck = parser.Found (L"no-size-check");
ArgUseLegacyPassword = parser.Found (L"legacy-password-maxlength");
#if defined(TC_LINUX ) || defined (TC_FREEBSD)
@@ -730,6 +740,11 @@ namespace VeraCrypt
if (param1IsMountedVolumeSpec)
ArgVolumes = GetMountedVolumes (parser.GetParamCount() > 0 ? parser.GetParam (0) : wxString());
#ifdef TC_LINUX
if (ArgEmergencyUnmount && ArgCommand != CommandId::DismountVolumes)
throw_err (L"--emergency-unmount is supported only with an unmount command");
#endif
if (ArgCommand == CommandId::None && Application::GetUserInterfaceType() == UserInterfaceType::Text)
parser.Usage();
}

View File

@@ -64,6 +64,9 @@ namespace VeraCrypt
CommandId::Enum ArgCommand;
bool ArgDisplayPassword;
shared_ptr <EncryptionAlgorithm> ArgEncryptionAlgorithm;
#ifdef TC_LINUX
bool ArgEmergencyUnmount;
#endif
shared_ptr <FilePath> ArgFilePath;
VolumeCreationOptions::FilesystemType::Enum ArgFilesystem;
bool ArgForce;

View File

@@ -66,6 +66,7 @@ namespace VeraCrypt
VC_CONVERT_EXCEPTION (MissingArgument);
VC_CONVERT_EXCEPTION (NoItemSelected);
VC_CONVERT_EXCEPTION (StringFormatterException);
VC_CONVERT_EXCEPTION (FilesystemDismountFailed);
VC_CONVERT_EXCEPTION (ExecutedProcessFailed);
VC_CONVERT_EXCEPTION (AlreadyInitialized);
VC_CONVERT_EXCEPTION (AssertionFailed);

View File

@@ -157,8 +157,11 @@ namespace VeraCrypt
DismountVolumes (volumes, ignoreOpenFiles, interactive);
}
void UserInterface::DismountVolumes (VolumeInfoList volumes, bool ignoreOpenFiles, bool interactive) const
void UserInterface::DismountVolumes (VolumeInfoList volumes, bool ignoreOpenFiles, bool interactive, bool emergencyCleanupRequested) const
{
#ifndef TC_LINUX
(void) emergencyCleanupRequested;
#endif
BusyScope busy (this);
volumes.sort (VolumeInfo::FirstVolumeMountedAfterSecond);
@@ -180,6 +183,7 @@ namespace VeraCrypt
VolumeInfoList volumesLeft;
foreach (shared_ptr <VolumeInfo> volume, volumes)
{
bool emergencyCleanupPerformed = false;
try
{
BusyScope busy (this);
@@ -207,6 +211,42 @@ namespace VeraCrypt
throw UserAbort (SRC_POS);
}
}
#ifdef TC_LINUX
catch (FilesystemDismountFailed&)
{
if (twoPassMode && firstPass)
{
volumesLeft.push_back (volume);
continue;
}
if (emergencyCleanupRequested)
{
{
BusyScope busy (this);
volume = Core->EmergencyDismountVolume (volume);
}
emergencyCleanupPerformed = true;
ShowWarning (StringFormatter (LangString["LINUX_EMERGENCY_UNMOUNTED"], wstring (volume->Path)));
}
else if (interactive)
{
if (AskYesNo (StringFormatter (LangString["LINUX_EMERGENCY_UNMOUNT_WARNING"], wstring (volume->Path)), false, true))
{
{
BusyScope busy (this);
volume = Core->EmergencyDismountVolume (volume);
}
emergencyCleanupPerformed = true;
ShowWarning (StringFormatter (LangString["LINUX_EMERGENCY_UNMOUNTED"], wstring (volume->Path)));
}
else
throw UserAbort (SRC_POS);
}
else
throw;
}
#endif
catch (...)
{
if (twoPassMode && firstPass)
@@ -220,9 +260,12 @@ namespace VeraCrypt
if (Preferences.Verbose)
{
if (!message.IsEmpty())
message += L'\n';
message += StringFormatter (LangString["LINUX_VOL_UNMOUNTED"], wstring (volume->Path));
if (!emergencyCleanupPerformed)
{
if (!message.IsEmpty())
message += L'\n';
message += StringFormatter (LangString["LINUX_VOL_UNMOUNTED"], wstring (volume->Path));
}
}
}
@@ -1133,7 +1176,11 @@ const FileManager fileManagers[] = {
return true;
case CommandId::DismountVolumes:
DismountVolumes (cmdLine.ArgVolumes, cmdLine.ArgForce, !Preferences.NonInteractive);
DismountVolumes (cmdLine.ArgVolumes, cmdLine.ArgForce, !Preferences.NonInteractive
#ifdef TC_LINUX
, cmdLine.ArgEmergencyUnmount
#endif
);
return true;
case CommandId::DisplayVersion:
@@ -1277,6 +1324,15 @@ const FileManager fileManagers[] = {
" Force mounting of a volume in use, unmounting of a volume in use, or\n"
" overwriting a file. Note that this option has no effect on some platforms.\n"
"\n"
#ifdef TC_LINUX
"--emergency-unmount\n"
" When used with --unmount on Linux, attempt emergency cleanup if normal\n"
" unmount fails. This recovery operation lazy-detaches the filesystem and\n"
" removes or schedules removal of VeraCrypt kernel objects. Pending writes may\n"
" already have failed, data may be lost, and cleanup may remain pending until\n"
" applications close open files. Use only for stale or removed-device states.\n"
"\n"
#endif
"--fs-options=OPTIONS\n"
" Filesystem mount options. The OPTIONS argument is passed to mount(8)\n"
" command with option -o when a filesystem on a VeraCrypt volume is mounted.\n"
@@ -1719,6 +1775,7 @@ const FileManager fileManagers[] = {
VC_CONVERT_EXCEPTION (MissingArgument);
VC_CONVERT_EXCEPTION (NoItemSelected);
VC_CONVERT_EXCEPTION (StringFormatterException);
VC_CONVERT_EXCEPTION (FilesystemDismountFailed);
VC_CONVERT_EXCEPTION (ExecutedProcessFailed);
VC_CONVERT_EXCEPTION (AlreadyInitialized);
VC_CONVERT_EXCEPTION (AssertionFailed);

View File

@@ -41,7 +41,7 @@ namespace VeraCrypt
virtual void DeleteSecurityTokenKeyfiles () const = 0;
virtual void DismountAllVolumes (bool ignoreOpenFiles = false, bool interactive = true) const;
virtual void DismountVolume (shared_ptr <VolumeInfo> volume, bool ignoreOpenFiles = false, bool interactive = true) const;
virtual void DismountVolumes (VolumeInfoList volumes, bool ignoreOpenFiles = false, bool interactive = true) const;
virtual void DismountVolumes (VolumeInfoList volumes, bool ignoreOpenFiles = false, bool interactive = true, bool emergencyCleanup = false) const;
virtual void DisplayVolumeProperties (const VolumeInfoList &volumes) const;
virtual void DoShowError (const wxString &message) const = 0;
virtual void DoShowInfo (const wxString &message) const = 0;