Skip to content

Commit

Permalink
fix the windows hyperv smb synced folder creation/destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
rgl committed Oct 3, 2022
1 parent c1fa7e4 commit a5a806b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
11 changes: 2 additions & 9 deletions plugins/hosts/windows/cap/smb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ def self.smb_cleanup(env, machine, opts)

if prune_shares.size > 0
machine.env.ui.warn("\n" + I18n.t("vagrant_sf_smb.uac.prune_warning") + "\n")
sleep UAC_PROMPT_WAIT
@@logger.info("remove shares: #{prune_shares}")
result = Vagrant::Util::PowerShell.execute(script_path, *prune_shares, sudo: true)
result = Vagrant::Util::PowerShell.execute(script_path, *prune_shares, sudo: false)
if result.exit_code != 0
failed_name = result.stdout.to_s.sub("share name: ", "")
raise SyncedFolderSMB::Errors::PruneShareFailed,
Expand Down Expand Up @@ -100,14 +99,8 @@ def self.smb_prepare(env, machine, folders, opts)
]
end
if !shares.empty?
uac_notified = false
shares.each_slice(10) do |s_shares|
if !uac_notified
machine.env.ui.warn("\n" + I18n.t("vagrant_sf_smb.uac.create_warning") + "\n")
uac_notified = true
sleep(UAC_PROMPT_WAIT)
end
result = Vagrant::Util::PowerShell.execute(script_path, *s_shares, sudo: true)
result = Vagrant::Util::PowerShell.execute(script_path, *s_shares, sudo: false)
if result.exit_code != 0
share_path = result.stdout.to_s.sub("share path: ", "")
raise SyncedFolderSMB::Errors::DefineShareFailed,
Expand Down
29 changes: 14 additions & 15 deletions plugins/hosts/windows/scripts/set_share.ps1
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# The names of the user are language dependent!
$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-1-0")
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])

$grant = "$objUser,Full"

for ($i=0; $i -le $args.length; $i = $i + 3) {
for ($i = 0; ($i+2) -lt $args.length; $i = $i + 3) {
$path = $args[$i]
$share_name = $args[$i+1]
$share_id = $args[$i+2]


if ($path -eq $null) {
Write-Warning "empty path argument encountered - complete"
exit 0
if (!$path) {
Write-Error "error - no share path provided"
exit 1
}

if ($share_name -eq $null) {
if (!$share_name) {
Write-Output "share path: ${path}"
Write-Error "error - no share name provided"
exit 1
}

if ($share_id -eq $null) {
if (!$share_id) {
Write-Output "share path: ${path}"
Write-Error "error - no share ID provided"
exit 1
}

$result = net share $share_id=$path /unlimited /GRANT:$grant /REMARK:"${share_name}"
if ($LastExitCode -ne 0) {
$host.ui.WriteLine("share path: ${path}")
$host.ui.WriteErrorLine("error ${result}")
exit 1
}
New-SmbShare `
-Name $share_id `
-Path $path `
-FullAccess $objUser `
-Description $share_name
}
exit 0
12 changes: 6 additions & 6 deletions plugins/hosts/windows/scripts/unset_share.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

ForEach ($share_name in $args) {
$result = net share $share_name /DELETE /YES
if ($LastExitCode -ne 0) {
Write-Output "share name: ${share_name}"
Write-Error "error - ${result}"
exit 1
}
Remove-SmbShare `
-Name $share_name `
-Force
}
Write-Output "share removal completed"
exit 0

0 comments on commit a5a806b

Please sign in to comment.