@@ -19,11 +19,12 @@ import (
1919 "github.com/Microsoft/hcsshim/internal/uvm"
2020 "github.com/Microsoft/hcsshim/internal/wclayer"
2121 specs "github.com/opencontainers/runtime-spec/specs-go"
22+ "github.com/pkg/errors"
2223)
2324
24- func allocateWindowsResources (ctx context.Context , coi * createOptionsInternal , r * resources.Resources ) error {
25+ func allocateWindowsResources (ctx context.Context , coi * createOptionsInternal , r * resources.Resources , isSandbox bool ) error {
2526 if coi .Spec == nil || coi .Spec .Windows == nil || coi .Spec .Windows .LayerFolders == nil {
26- return fmt . Errorf ("field 'Spec.Windows.Layerfolders' is not populated" )
27+ return errors . New ("field 'Spec.Windows.Layerfolders' is not populated" )
2728 }
2829
2930 scratchFolder := coi .Spec .Windows .LayerFolders [len (coi .Spec .Windows .LayerFolders )- 1 ]
@@ -32,15 +33,15 @@ func allocateWindowsResources(ctx context.Context, coi *createOptionsInternal, r
3233 // Create the directory for the RW scratch layer if it doesn't exist
3334 if _ , err := os .Stat (scratchFolder ); os .IsNotExist (err ) {
3435 if err := os .MkdirAll (scratchFolder , 0777 ); err != nil {
35- return fmt . Errorf ( "failed to auto-create container scratch folder %s: %s " , scratchFolder , err )
36+ return errors . Wrapf ( err , "failed to auto-create container scratch folder %s" , scratchFolder )
3637 }
3738 }
3839
3940 // Create sandbox.vhdx if it doesn't exist in the scratch folder. It's called sandbox.vhdx
4041 // rather than scratch.vhdx as in the v1 schema, it's hard-coded in HCS.
4142 if _ , err := os .Stat (filepath .Join (scratchFolder , "sandbox.vhdx" )); os .IsNotExist (err ) {
4243 if err := wclayer .CreateScratchLayer (ctx , scratchFolder , coi .Spec .Windows .LayerFolders [:len (coi .Spec .Windows .LayerFolders )- 1 ]); err != nil {
43- return fmt . Errorf ( "failed to CreateSandboxLayer %s" , err )
44+ return errors . Wrap ( err , "failed to CreateSandboxLayer" )
4445 }
4546 }
4647
@@ -53,10 +54,10 @@ func allocateWindowsResources(ctx context.Context, coi *createOptionsInternal, r
5354 containerRootInUVM := r .ContainerRootInUVM ()
5455 containerRootPath , err := layers .MountContainerLayers (ctx , coi .Spec .Windows .LayerFolders , containerRootInUVM , coi .HostingSystem )
5556 if err != nil {
56- return fmt . Errorf ( "failed to mount container storage: %s" , err )
57+ return errors . Wrap ( err , "failed to mount container storage" )
5758 }
5859 coi .Spec .Root .Path = containerRootPath
59- layers := layers .NewImageLayers (coi .HostingSystem , containerRootInUVM , coi .Spec .Windows .LayerFolders )
60+ layers := layers .NewImageLayers (coi .HostingSystem , containerRootInUVM , coi .Spec .Windows .LayerFolders , isSandbox )
6061 r .SetLayers (layers )
6162 }
6263
@@ -136,37 +137,74 @@ func setupMounts(ctx context.Context, coi *createOptionsInternal, r *resources.R
136137 l .Debug ("hcsshim::allocateWindowsResources Hot-adding SCSI physical disk for OCI mount" )
137138 scsiMount , err := coi .HostingSystem .AddSCSIPhysicalDisk (ctx , mount .Source , uvmPath , readOnly )
138139 if err != nil {
139- return fmt . Errorf ( "adding SCSI physical disk mount %+v: %s " , mount , err )
140+ return errors . Wrapf ( err , "adding SCSI physical disk mount %+v" , mount )
140141 }
141142 coi .Spec .Mounts [i ].Type = ""
142143 r .Add (scsiMount )
143144 } else if mount .Type == "virtual-disk" {
144145 l .Debug ("hcsshim::allocateWindowsResources Hot-adding SCSI virtual disk for OCI mount" )
145146 scsiMount , err := coi .HostingSystem .AddSCSI (ctx , mount .Source , uvmPath , readOnly , uvm .VMAccessTypeIndividual )
146147 if err != nil {
147- return fmt . Errorf ( "adding SCSI virtual disk mount %+v: %s " , mount , err )
148+ return errors . Wrapf ( err , "adding SCSI virtual disk mount %+v" , mount )
148149 }
149150 coi .Spec .Mounts [i ].Type = ""
150151 r .Add (scsiMount )
151152 } else {
152153 if uvm .IsPipe (mount .Source ) {
153154 pipe , err := coi .HostingSystem .AddPipe (ctx , mount .Source )
154155 if err != nil {
155- return fmt . Errorf ( "failed to add named pipe to UVM: %s" , err )
156+ return errors . Wrap ( err , "failed to add named pipe to UVM" )
156157 }
157158 r .Add (pipe )
158159 } else {
159160 l .Debug ("hcsshim::allocateWindowsResources Hot-adding VSMB share for OCI mount" )
160161 options := coi .HostingSystem .DefaultVSMBOptions (readOnly )
161162 share , err := coi .HostingSystem .AddVSMB (ctx , mount .Source , options )
162163 if err != nil {
163- return fmt . Errorf ( "failed to add VSMB share to utility VM for mount %+v: %s " , mount , err )
164+ return errors . Wrapf ( err , "failed to add VSMB share to utility VM for mount %+v" , mount )
164165 }
165166 r .Add (share )
166167 }
167168 }
168169 }
169170 }
170171
172+ if cs , ok := coi .Spec .Windows .CredentialSpec .(string ); ok {
173+ // Only need to create a CCG instance for v2 containers
174+ if schemaversion .IsV21 (coi .actualSchemaVersion ) {
175+ hypervisorIsolated := coi .HostingSystem != nil
176+ ccgInstance , ccgResource , err := credentials .CreateCredentialGuard (ctx , coi .actualID , cs , hypervisorIsolated )
177+ if err != nil {
178+ return err
179+ }
180+ coi .ccgState = ccgInstance .CredentialGuard
181+ r .Add (ccgResource )
182+ if hypervisorIsolated {
183+ // If hypervisor isolated we need to add an hvsocket service table entry
184+ // By default HVSocket won't allow something inside the VM to connect
185+ // back to a process on the host. We need to update the HVSocket service table
186+ // to allow a connection to CCG.exe on the host, so that GMSA can function.
187+ // We need to hot add this here because at UVM creation time we don't know what containers
188+ // will be launched in the UVM, nonetheless if they will ask for GMSA. This is a workaround
189+ // for the previous design requirement for CCG V2 where the service entry
190+ // must be present in the UVM'S HCS document before being sent over as hot adding
191+ // an HvSocket service was not possible.
192+ hvSockConfig := ccgInstance .HvSocketConfig
193+ if err := coi .HostingSystem .UpdateHvSocketService (ctx , hvSockConfig .ServiceId , hvSockConfig .ServiceConfig ); err != nil {
194+ return errors .Wrap (err , "failed to update hvsocket service" )
195+ }
196+ }
197+ }
198+ }
199+
200+ if coi .HostingSystem != nil && coi .hasWindowsAssignedDevices () {
201+ windowsDevices , closers , err := handleAssignedDevicesWindows (ctx , coi .HostingSystem , coi .Spec .Annotations , coi .Spec .Windows .Devices )
202+ if err != nil {
203+ return err
204+ }
205+ r .Add (closers ... )
206+ coi .Spec .Windows .Devices = windowsDevices
207+ }
208+
171209 return nil
172210}
0 commit comments