Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
| ... | ... |
@@ -19,6 +19,9 @@ if [[ "${buildkit_ref}" == *-*-* ]]; then
|
| 19 | 19 |
buildkit_ref=$(curl -s "https://api.github.com/repos/${buildkit_repo}/commits/${buildkit_ref}" | jq -r .sha)
|
| 20 | 20 |
fi |
| 21 | 21 |
|
| 22 |
+# https://github.com/moby/buildkit/pull/6278 |
|
| 23 |
+buildkit_ref="1030099b27bd3455bf7e5d5fe73b6be5dbec3c1f" |
|
| 24 |
+ |
|
| 22 | 25 |
cat << EOF |
| 23 | 26 |
BUILDKIT_REPO=$buildkit_repo |
| 24 | 27 |
BUILDKIT_REF=$buildkit_ref |
| 25 | 28 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,127 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "log" |
|
| 4 |
+ "os" |
|
| 5 |
+ "path/filepath" |
|
| 6 |
+ "strings" |
|
| 7 |
+ |
|
| 8 |
+ "github.com/moby/buildkit/identity" |
|
| 9 |
+ "github.com/moby/sys/mountinfo" |
|
| 10 |
+ "github.com/pkg/errors" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+const volumePath = "/abc/a" |
|
| 14 |
+ |
|
| 15 |
+var rootfs string |
|
| 16 |
+ |
|
| 17 |
+func main() {
|
|
| 18 |
+ if err := run(); err != nil {
|
|
| 19 |
+ log.Printf("error: %+v", err)
|
|
| 20 |
+ } |
|
| 21 |
+} |
|
| 22 |
+ |
|
| 23 |
+func run() error {
|
|
| 24 |
+ infos, err := mountinfo.GetMounts(nil) |
|
| 25 |
+ if err != nil {
|
|
| 26 |
+ return err |
|
| 27 |
+ } |
|
| 28 |
+ hasVolume := false |
|
| 29 |
+ for _, info := range infos {
|
|
| 30 |
+ if info.Mountpoint == "/" {
|
|
| 31 |
+ v, err := getOverlayRootfs(info) |
|
| 32 |
+ if err != nil {
|
|
| 33 |
+ return err |
|
| 34 |
+ } |
|
| 35 |
+ rootfs = v |
|
| 36 |
+ } |
|
| 37 |
+ |
|
| 38 |
+ if info.Mountpoint == volumePath {
|
|
| 39 |
+ hasVolume = true |
|
| 40 |
+ } |
|
| 41 |
+ log.Printf("mount: %+v", info)
|
|
| 42 |
+ } |
|
| 43 |
+ if !hasVolume {
|
|
| 44 |
+ return errors.Errorf("volume not found: %s", volumePath)
|
|
| 45 |
+ } |
|
| 46 |
+ |
|
| 47 |
+ log.Printf("rootfs: %s", rootfs)
|
|
| 48 |
+ |
|
| 49 |
+ base := filepath.Base(rootfs) |
|
| 50 |
+ if err := os.Symlink("/", filepath.Join(volumePath, base)); err != nil {
|
|
| 51 |
+ return err |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ // create duplicate volume path with symlink target |
|
| 55 |
+ p := "/" |
|
| 56 |
+ var volumeRoot string |
|
| 57 |
+ for _, c := range strings.Split(filepath.Dir(volumePath), string(filepath.Separator)) {
|
|
| 58 |
+ if c == "" {
|
|
| 59 |
+ continue |
|
| 60 |
+ } |
|
| 61 |
+ if volumeRoot == "" {
|
|
| 62 |
+ volumeRoot = "/" + c |
|
| 63 |
+ c += "_target" |
|
| 64 |
+ } |
|
| 65 |
+ p = filepath.Join(p, c) |
|
| 66 |
+ if err := os.Mkdir(p, 0755); err != nil {
|
|
| 67 |
+ if os.IsExist(err) {
|
|
| 68 |
+ continue |
|
| 69 |
+ } |
|
| 70 |
+ return err |
|
| 71 |
+ } |
|
| 72 |
+ log.Printf("created: %s", p)
|
|
| 73 |
+ } |
|
| 74 |
+ if err := os.Symlink(filepath.Dir(rootfs), filepath.Join(p, filepath.Base(volumePath))); err != nil {
|
|
| 75 |
+ return err |
|
| 76 |
+ } |
|
| 77 |
+ |
|
| 78 |
+ if err := os.Rename(volumeRoot, volumeRoot+"_old"); err != nil {
|
|
| 79 |
+ return err |
|
| 80 |
+ } |
|
| 81 |
+ |
|
| 82 |
+ for {
|
|
| 83 |
+ if _, err := os.Stat(volumeRoot); err != nil {
|
|
| 84 |
+ if os.IsNotExist(err) {
|
|
| 85 |
+ continue |
|
| 86 |
+ } |
|
| 87 |
+ return err |
|
| 88 |
+ } |
|
| 89 |
+ // log.Printf("detected: %s", volumeRoot)
|
|
| 90 |
+ break |
|
| 91 |
+ } |
|
| 92 |
+ |
|
| 93 |
+ for {
|
|
| 94 |
+ if err := os.Rename(volumeRoot+"_target", volumeRoot); err != nil {
|
|
| 95 |
+ if os.IsExist(err) {
|
|
| 96 |
+ if err := os.Rename(volumeRoot, volumeRoot+"_"+identity.NewID()); err != nil {
|
|
| 97 |
+ return err |
|
| 98 |
+ } |
|
| 99 |
+ continue |
|
| 100 |
+ } |
|
| 101 |
+ log.Printf("failed to rename: %s", err)
|
|
| 102 |
+ } |
|
| 103 |
+ break |
|
| 104 |
+ } |
|
| 105 |
+ |
|
| 106 |
+ return nil |
|
| 107 |
+} |
|
| 108 |
+ |
|
| 109 |
+func getOverlayRootfs(info *mountinfo.Info) (string, error) {
|
|
| 110 |
+ if info.FSType != "overlay" {
|
|
| 111 |
+ return "", errors.Errorf("not overlay: %s", info.FSType)
|
|
| 112 |
+ } |
|
| 113 |
+ for _, opt := range strings.Split(info.VFSOptions, ",") {
|
|
| 114 |
+ parts := strings.SplitN(opt, "=", 2) |
|
| 115 |
+ if parts[0] == "workdir" {
|
|
| 116 |
+ return filepath.Join(filepath.Dir(parts[1]), "merged"), nil |
|
| 117 |
+ } |
|
| 118 |
+ } |
|
| 119 |
+ return "", errors.Errorf("workdir not found: %s", info.VFSOptions)
|
|
| 120 |
+} |
|
| 121 |
+ |
|
| 122 |
+// /var/lib/docker/overlay2/86fcb18db0e3774cfe3b9ed6fa526d4dffcb45ac3b26cbe99db6c2d08e6dfc0e/merged |
|
| 123 |
+ |
|
| 124 |
+// merged/<sym>/dir1/dir2/ |
|
| 125 |
+ |
|
| 126 |
+// volume |