@@ -21,6 +21,7 @@ import (
2121 "fmt"
2222 "path/filepath"
2323 "regexp"
24+ "sort"
2425 "strings"
2526
2627 "github.com/hashicorp/go-multierror"
@@ -57,7 +58,7 @@ func RunMount(cfg *v1.RunConfig, spec *v1.MountSpec) error {
5758 }
5859
5960 cfg .Logger .Debug ("Mounting volumes" )
60- if err = MountVolumes (cfg , spec . Sysroot , spec . Volumes ); err != nil {
61+ if err = MountVolumes (cfg , spec ); err != nil {
6162 cfg .Logger .Errorf ("Error mounting volumes: %s" , err .Error ())
6263 return err
6364 }
@@ -69,7 +70,7 @@ func RunMount(cfg *v1.RunConfig, spec *v1.MountSpec) error {
6970 }
7071
7172 cfg .Logger .Debugf ("Mounting persistent directories" )
72- if err = MountPersistent (cfg , spec .Sysroot , spec .Persistent , spec . Volumes ); err != nil {
73+ if err = MountPersistent (cfg , spec .Sysroot , spec .Persistent ); err != nil {
7374 cfg .Logger .Errorf ("Error mounting persistent overlays: %s" , err .Error ())
7475 return err
7576 }
@@ -84,28 +85,42 @@ func RunMount(cfg *v1.RunConfig, spec *v1.MountSpec) error {
8485 return nil
8586}
8687
87- func MountVolumes (cfg * v1.RunConfig , sysroot string , volumes [] * v1.VolumeMount ) error {
88+ func MountVolumes (cfg * v1.RunConfig , spec * v1.MountSpec ) error {
8889 var errs error
8990
90- for _ , vol := range volumes {
91+ volumes := map [string ]* v1.VolumeMount {}
92+ keys := []string {}
93+ if spec .HasPersistent () {
94+ volumes [spec .Persistent .Volume .Mountpoint ] = & spec .Persistent .Volume
95+ keys = append (keys , spec .Persistent .Volume .Mountpoint )
96+ }
97+
98+ for _ , v := range spec .Volumes {
99+ volumes [v .Mountpoint ] = v
100+ keys = append (keys , v .Mountpoint )
101+ }
102+
103+ sort .Strings (keys )
104+
105+ for _ , k := range keys {
91106 var dev string
92107 switch {
93- case strings .HasPrefix (vol .Device , labelPref ):
94- dev = filepath .Join (diskByLabel , strings .TrimPrefix (vol .Device , labelPref ))
95- case strings .HasPrefix (vol .Device , partLabelPref ):
96- dev = filepath .Join (diskByPartLabel , strings .TrimPrefix (vol .Device , partLabelPref ))
97- case strings .HasPrefix (vol .Device , uuidPref ):
98- dev = filepath .Join (diskByUUID , strings .TrimPrefix (vol .Device , uuidPref ))
99- case strings .HasPrefix (vol .Device , devPref ):
100- dev = vol .Device
108+ case strings .HasPrefix (volumes [ k ] .Device , labelPref ):
109+ dev = filepath .Join (diskByLabel , strings .TrimPrefix (volumes [ k ] .Device , labelPref ))
110+ case strings .HasPrefix (volumes [ k ] .Device , partLabelPref ):
111+ dev = filepath .Join (diskByPartLabel , strings .TrimPrefix (volumes [ k ] .Device , partLabelPref ))
112+ case strings .HasPrefix (volumes [ k ] .Device , uuidPref ):
113+ dev = filepath .Join (diskByUUID , strings .TrimPrefix (volumes [ k ] .Device , uuidPref ))
114+ case strings .HasPrefix (volumes [ k ] .Device , devPref ):
115+ dev = volumes [ k ] .Device
101116 default :
102117 cfg .Logger .Errorf ("Unknown device reference, it should be LABEL, PARTLABEL, UUID or a /dev/* path" )
103- errs = multierror .Append (errs , fmt .Errorf ("Unkown device reference: %s" , vol .Device ))
118+ errs = multierror .Append (errs , fmt .Errorf ("Unkown device reference: %s" , volumes [ k ] .Device ))
104119 continue
105120 }
106- mountpoint := vol .Mountpoint
121+ mountpoint := volumes [ k ] .Mountpoint
107122 if ! strings .HasPrefix (mountpoint , runPath ) {
108- mountpoint = filepath .Join (sysroot , mountpoint )
123+ mountpoint = filepath .Join (spec . Sysroot , mountpoint )
109124 }
110125
111126 err := utils .MkdirAll (cfg .Fs , mountpoint , constants .DirPerm )
@@ -115,7 +130,13 @@ func MountVolumes(cfg *v1.RunConfig, sysroot string, volumes []*v1.VolumeMount)
115130 continue
116131 }
117132
118- err = cfg .Mounter .Mount (dev , mountpoint , "auto" , vol .Options )
133+ fstype := volumes [k ].FSType
134+ if fstype == "" {
135+ fstype = "auto"
136+ }
137+
138+ cfg .Logger .Debugf ("Mounting %s to %s" , dev , mountpoint )
139+ err = cfg .Mounter .Mount (dev , mountpoint , fstype , volumes [k ].Options )
119140 if err != nil {
120141 cfg .Logger .Errorf ("failed mounting device %s to %s" , dev , mountpoint )
121142 errs = multierror .Append (errs , err )
@@ -165,29 +186,21 @@ func MountEphemeral(cfg *v1.RunConfig, sysroot string, overlay v1.EphemeralMount
165186 return nil
166187}
167188
168- func MountPersistent (cfg * v1.RunConfig , sysroot string , persistent v1.PersistentMounts , volumes []* v1.VolumeMount ) error {
169- var vol * v1.VolumeMount
170-
189+ func MountPersistent (cfg * v1.RunConfig , sysroot string , persistent v1.PersistentMounts ) error {
171190 mountFunc := MountOverlayPath
172191 if persistent .Mode == "bind" {
173192 mountFunc = MountBindPath
174193 }
175194
176- for _ , v := range volumes {
177- if v .Persistent {
178- vol = v
179- break
180- }
181- }
182- if vol == nil {
195+ if persistent .Volume .Device == "" || persistent .Volume .Mountpoint == "" {
183196 cfg .Logger .Debug ("No persistent device defined, omitting persistent paths mounts" )
184197 return nil
185198 }
186199
187200 for _ , path := range persistent .Paths {
188201 cfg .Logger .Debugf ("Mounting path %s into %s" , path , sysroot )
189202
190- target := filepath .Join (vol .Mountpoint , constants .PersistentStateDir )
203+ target := filepath .Join (persistent . Volume .Mountpoint , constants .PersistentStateDir )
191204 if err := mountFunc (cfg , sysroot , target , path ); err != nil {
192205 cfg .Logger .Errorf ("Error mounting path %s: %s" , path , err .Error ())
193206 return err
@@ -268,38 +281,30 @@ func MountOverlayPath(cfg *v1.RunConfig, sysroot, overlayDir, path string) error
268281
269282func WriteFstab (cfg * v1.RunConfig , spec * v1.MountSpec , data string ) error {
270283 var errs error
271- var persistentVol * v1.VolumeMount
272284
273285 if ! spec .WriteFstab {
274286 cfg .Logger .Debug ("Skipping writing fstab" )
275287 return nil
276288 }
277289
278- data += fstab ("tmpfs" , constants .OverlayDir , "tmpfs" , []string {"defaults" , fmt .Sprintf ("size=%s" , spec .Ephemeral .Size )})
279-
280290 for _ , vol := range spec .Volumes {
281- if vol .Persistent {
282- persistentVol = vol
283- }
284-
285- data = data + fstab (vol .Device , vol .Mountpoint , "auto" , vol .Options )
291+ data += fstab (vol .Device , vol .Mountpoint , vol .FSType , vol .Options )
286292 }
287293
288- for _ , rw := range spec .Ephemeral . Paths {
289- data += overlayLine ( rw , constants . OverlayDir , constants . OverlayDir )
290- }
294+ if spec .HasPersistent () {
295+ pVol := spec . Persistent . Volume
296+ data += fstab ( pVol . Device , pVol . Mountpoint , pVol . FSType , pVol . Options )
291297
292- if persistentVol != nil {
293298 for _ , path := range spec .Persistent .Paths {
294299 if spec .Persistent .Mode == constants .OverlayMode {
295- data += overlayLine (path , filepath .Join (persistentVol .Mountpoint , constants .PersistentStateDir ), constants .PersistentDir )
300+ data += overlayLine (path , filepath .Join (pVol .Mountpoint , constants .PersistentStateDir ), constants .PersistentDir )
296301 continue
297302 }
298303
299304 if spec .Persistent .Mode == constants .BindMode {
300305 trimmed := strings .TrimPrefix (path , "/" )
301306 pathName := strings .ReplaceAll (trimmed , "/" , "-" ) + ".bind"
302- stateDir := filepath .Join (persistentVol .Mountpoint , constants .PersistentStateDir , pathName )
307+ stateDir := filepath .Join (pVol .Mountpoint , constants .PersistentStateDir , pathName )
303308
304309 data = data + fstab (stateDir , path , "none" , []string {"defaults" , "bind" })
305310 continue
@@ -308,6 +313,11 @@ func WriteFstab(cfg *v1.RunConfig, spec *v1.MountSpec, data string) error {
308313 }
309314 }
310315
316+ data += fstab ("tmpfs" , constants .OverlayDir , "tmpfs" , []string {"defaults" , fmt .Sprintf ("size=%s" , spec .Ephemeral .Size )})
317+ for _ , rw := range spec .Ephemeral .Paths {
318+ data += overlayLine (rw , constants .OverlayDir , constants .OverlayDir )
319+ }
320+
311321 return cfg .Config .Fs .WriteFile (filepath .Join (spec .Sysroot , "/etc/fstab" ), []byte (data ), 0644 )
312322}
313323
@@ -322,11 +332,11 @@ func InitialFstabData(runner v1.Runner, sysroot string) (string, error) {
322332 if mnt .Mountpoint == sysroot {
323333 data += fstab (mnt .Device , "/" , "auto" , mnt .Options )
324334 } else if strings .HasPrefix (mnt .Mountpoint , sysroot ) {
325- data += fstab (mnt .Device , strings .TrimPrefix (mnt .Mountpoint , sysroot ), "auto" , mnt .Options )
335+ data += fstab (mnt .Device , strings .TrimPrefix (mnt .Mountpoint , sysroot ), mnt . FSType , mnt .Options )
326336 } else if strings .HasPrefix (mnt .Mountpoint , constants .RunElementalDir ) {
327- data += fstab (mnt .Device , mnt .Mountpoint , "auto" , mnt .Options )
337+ data += fstab (mnt .Device , mnt .Mountpoint , mnt . FSType , mnt .Options )
328338 } else if mnt .Mountpoint == constants .RunningStateDir {
329- data += fstab (mnt .Device , mnt .Mountpoint , "auto" , mnt .Options )
339+ data += fstab (mnt .Device , mnt .Mountpoint , mnt . FSType , mnt .Options )
330340 }
331341 }
332342
@@ -337,6 +347,10 @@ func fstab(device, path, fstype string, flags []string) string {
337347 if len (flags ) == 0 {
338348 flags = []string {"defaults" }
339349 }
350+
351+ if fstype == "" {
352+ fstype = "auto"
353+ }
340354 return fmt .Sprintf ("%s\t %s\t %s\t %s\t 0\t 0\n " , device , path , fstype , strings .Join (flags , "," ))
341355}
342356
0 commit comments