@@ -27,6 +27,9 @@ const (
2727 LmrInstanceFlagAllowGuestAuth = 0x8
2828 LmrInstanceFlagSupportsDirectmappedIo = 0x10
2929 SmbCeTransportTypeVmbus = 3
30+
31+ FileReadAttributes = 0x00000080
32+ FileFlagBackupSemantics = 0x02000000
3033)
3134
3235type IOStatusBlock struct {
@@ -166,13 +169,13 @@ func isLanmanWorkstationRunning() (bool, error) {
166169 return status .State == svc .Running , nil
167170}
168171
169- func VsmbMain () {
172+ func VsmbMain () (windows. Handle , error ) {
170173 logrus .Info ("Starting VSMB initialization..." )
171174
172175 logrus .Debug ("Configuring LanmanWorkstation service..." )
173176 if err := configureAndStartLanmanWorkstation (); err != nil {
174177 logrus .Errorf ("LanmanWorkstation setup failed: %v" , err )
175- return
178+ return windows . InvalidHandle , err
176179 }
177180
178181 time .Sleep (3 * time .Second ) // TODO: This needs to be better logic.
@@ -187,13 +190,13 @@ func VsmbMain() {
187190
188191 if err := winio .EnableProcessPrivileges ([]string {SeLoadDriverName }); err != nil {
189192 logrus .Errorf ("Failed to enable privilege: %v" , err )
190- return
193+ return windows . InvalidHandle , err
191194 }
192195 // Open LanmanRedirector
193196 namePtr , nerr := windows .UTF16PtrFromString (GlobalRdrDeviceName )
194197 if nerr != nil {
195198 logrus .WithError (nerr ).Errorf ("invalid device name %q" , GlobalRdrDeviceName )
196- return
199+ return windows . InvalidHandle , nerr
197200 }
198201
199202 lmrHandle , err := windows .CreateFile (
@@ -207,7 +210,7 @@ func VsmbMain() {
207210 )
208211 if err != nil {
209212 logrus .WithError (err ).Error ("Failed to open redirector" )
210- return
213+ return windows . InvalidHandle , err
211214 }
212215 defer func () {
213216 if derr := windows .CloseHandle (lmrHandle ); derr != nil {
@@ -221,7 +224,7 @@ func VsmbMain() {
221224 instanceNameUTF16 , nerr := windows .UTF16FromString (GlobalVsmbInstanceName )
222225 if nerr != nil {
223226 logrus .WithError (nerr ).Errorf ("invalid instance name %q" , GlobalVsmbInstanceName )
224- return
227+ return windows . InvalidHandle , nerr
225228 }
226229 structSize := int (unsafe .Sizeof (LMRStartInstanceRequest {}))
227230 bufferSize := structSize + (len (instanceNameUTF16 )- 1 )* 2
@@ -276,7 +279,7 @@ func VsmbMain() {
276279 namePtr , nerr = windows .UTF16PtrFromString (GlobalVsmbDeviceName )
277280 if nerr != nil {
278281 logrus .WithError (nerr ).Errorf ("invalid device name %q" , GlobalVsmbDeviceName )
279- return
282+ return windows . InvalidHandle , nerr
280283 }
281284 vmsmbHandle , err := windows .CreateFile (
282285 namePtr ,
@@ -286,7 +289,7 @@ func VsmbMain() {
286289 )
287290 if err != nil {
288291 logrus .Errorf ("Failed to open VMSMB device: %v" , err )
289- return
292+ return windows . InvalidHandle , err
290293 }
291294 defer func () {
292295 if derr := windows .CloseHandle (vmsmbHandle ); derr != nil {
@@ -297,7 +300,7 @@ func VsmbMain() {
297300 transportNameUTF16 , nerr := windows .UTF16FromString (GlobalVsmbTransportName )
298301 if nerr != nil {
299302 logrus .WithError (nerr ).Errorf ("invalid instance name %q" , GlobalVsmbTransportName )
300- return
303+ return windows . InvalidHandle , nerr
301304 }
302305
303306 bindStructSize := int (unsafe .Sizeof (LMRBindUnbindTransportRequest {}))
@@ -329,4 +332,31 @@ func VsmbMain() {
329332 } else {
330333 logrus .Errorf ("NtFsControlFile failed: 0x%08X" , status )
331334 }
335+
336+ const (
337+ device = `\\?\GLOBALROOT\Device\vmsmb\VSMB-{dcc079ae-60ba-4d07-847c-3493609c0870}\defaultEmptyShare`
338+ )
339+
340+ devicePtr , nerr := windows .UTF16PtrFromString (device )
341+ if nerr != nil {
342+ logrus .WithError (nerr ).Errorf ("invalid device name %q" , device )
343+ return windows .InvalidHandle , nerr
344+ }
345+ vsmbHandle , err := windows .CreateFile (
346+ devicePtr ,
347+ FileReadAttributes ,
348+ windows .FILE_SHARE_READ | windows .FILE_SHARE_WRITE | windows .FILE_SHARE_DELETE ,
349+ nil ,
350+ windows .OPEN_EXISTING ,
351+ FileFlagBackupSemantics ,
352+ 0 ,
353+ )
354+
355+ if err != nil {
356+ logrus .WithError (err ).Errorf ("Failed to open %s" , device )
357+ return windows .InvalidHandle , err
358+ }
359+
360+ logrus .Infof ("VSMB connection will be alive..." )
361+ return vsmbHandle , nil
332362}
0 commit comments