@@ -848,14 +848,14 @@ func (d *Driver) Status() [][2]string {
848
848
// Metadata returns meta data about the overlay driver such as
849
849
// LowerDir, UpperDir, WorkDir and MergeDir used to store data.
850
850
func (d * Driver ) Metadata (id string ) (map [string ]string , error ) {
851
- dir := d .dir (id )
851
+ dir , _ , inAdditionalStore := d .dir2 (id , false )
852
852
if err := fileutils .Exists (dir ); err != nil {
853
853
return nil , err
854
854
}
855
855
856
856
metadata := map [string ]string {
857
857
"WorkDir" : path .Join (dir , "work" ),
858
- "MergedDir" : path . Join ( dir , "merged" ),
858
+ "MergedDir" : d . getMergedDir ( id , dir , inAdditionalStore ),
859
859
"UpperDir" : path .Join (dir , "diff" ),
860
860
}
861
861
@@ -1703,10 +1703,10 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO
1703
1703
}
1704
1704
}
1705
1705
1706
- mergedDir := path . Join ( dir , "merged" )
1706
+ mergedDir := d . getMergedDir ( id , dir , inAdditionalStore )
1707
1707
// Attempt to create the merged dir only if it doesn't exist.
1708
1708
if err := fileutils .Exists (mergedDir ); err != nil && os .IsNotExist (err ) {
1709
- if err := idtools .MkdirAs (mergedDir , 0o700 , rootUID , rootGID ); err != nil && ! os .IsExist (err ) {
1709
+ if err := idtools .MkdirAllAs (mergedDir , 0o700 , rootUID , rootGID ); err != nil && ! os .IsExist (err ) {
1710
1710
return "" , err
1711
1711
}
1712
1712
}
@@ -1856,7 +1856,9 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO
1856
1856
mountFunc = func (source string , target string , mType string , flags uintptr , label string ) error {
1857
1857
return mountOverlayFrom (d .home , source , target , mType , flags , label )
1858
1858
}
1859
- mountTarget = path .Join (id , "merged" )
1859
+ if ! inAdditionalStore {
1860
+ mountTarget = path .Join (id , "merged" )
1861
+ }
1860
1862
}
1861
1863
1862
1864
// overlay has a check in place to prevent mounting the same file system twice
@@ -1875,13 +1877,25 @@ func (d *Driver) get(id string, disableShifting bool, options graphdriver.MountO
1875
1877
return mergedDir , nil
1876
1878
}
1877
1879
1880
+ // getMergedDir returns the directory path that should be used as the mount point for the overlayfs.
1881
+ func (d * Driver ) getMergedDir (id , dir string , inAdditionalStore bool ) string {
1882
+ // If the layer is in an additional store, the lock we might hold only a reading lock. To prevent
1883
+ // races with other processes, use a private directory under the main store rundir, where we hold
1884
+ // a read/write lock.
1885
+ if inAdditionalStore {
1886
+ return path .Join (d .runhome , id , "merged" )
1887
+ }
1888
+ return path .Join (dir , "merged" )
1889
+ }
1890
+
1878
1891
// Put unmounts the mount path created for the give id.
1879
1892
func (d * Driver ) Put (id string ) error {
1880
1893
dir , _ , inAdditionalStore := d .dir2 (id , false )
1881
1894
if err := fileutils .Exists (dir ); err != nil {
1882
1895
return err
1883
1896
}
1884
- mountpoint := path .Join (dir , "merged" )
1897
+ mountpoint := d .getMergedDir (id , dir , inAdditionalStore )
1898
+
1885
1899
if count := d .ctr .Decrement (mountpoint ); count > 0 {
1886
1900
return nil
1887
1901
}
@@ -1938,7 +1952,15 @@ func (d *Driver) Put(id string) error {
1938
1952
}
1939
1953
}
1940
1954
1941
- if ! inAdditionalStore {
1955
+ if inAdditionalStore {
1956
+ // check the base name for extra safety
1957
+ if strings .HasPrefix (mountpoint , d .runhome ) && filepath .Base (mountpoint ) == "merged" {
1958
+ err := os .RemoveAll (filepath .Dir (mountpoint ))
1959
+ if err != nil {
1960
+ logrus .Warningf ("Failed to remove mountpoint %s overlay: %s: %v" , id , mountpoint , err )
1961
+ }
1962
+ }
1963
+ } else {
1942
1964
uid , gid := int (0 ), int (0 )
1943
1965
fi , err := os .Stat (mountpoint )
1944
1966
if err != nil {
@@ -1955,7 +1977,7 @@ func (d *Driver) Put(id string) error {
1955
1977
// rename(2) can be used on an empty directory, as it is the mountpoint after umount, and it retains
1956
1978
// its atomic semantic. In this way the "merged" directory is never removed.
1957
1979
if err := unix .Rename (tmpMountpoint , mountpoint ); err != nil {
1958
- logrus .Debugf ("Failed to replace mountpoint %s overlay: %s - %v" , id , mountpoint , err )
1980
+ logrus .Debugf ("Failed to replace mountpoint %s overlay: %s: %v" , id , mountpoint , err )
1959
1981
return fmt .Errorf ("replacing mount point %q: %w" , mountpoint , err )
1960
1982
}
1961
1983
}
0 commit comments