Merge pull request #30116 from vieux/cherry-pick-1.13.0-rc7

cherry pick 1.13.0-rc7
diff --git a/cli/command/formatter/image.go b/cli/command/formatter/image.go
index 594b2f3..5c7de82 100644
--- a/cli/command/formatter/image.go
+++ b/cli/command/formatter/image.go
@@ -226,8 +226,7 @@
 
 func (c *imageContext) Size() string {
 	c.AddHeader(sizeHeader)
-	//NOTE: For backward compatibility we need to return VirtualSize
-	return units.HumanSizeWithPrecision(float64(c.i.VirtualSize), 3)
+	return units.HumanSizeWithPrecision(float64(c.i.Size), 3)
 }
 
 func (c *imageContext) Containers() string {
@@ -253,8 +252,8 @@
 
 func (c *imageContext) UniqueSize() string {
 	c.AddHeader(uniqueSizeHeader)
-	if c.i.Size == -1 {
+	if c.i.VirtualSize == -1 || c.i.SharedSize == -1 {
 		return "N/A"
 	}
-	return units.HumanSize(float64(c.i.Size))
+	return units.HumanSize(float64(c.i.VirtualSize - c.i.SharedSize))
 }
diff --git a/daemon/images.go b/daemon/images.go
index 25fad4c..01dbced 100644
--- a/daemon/images.go
+++ b/daemon/images.go
@@ -210,7 +210,6 @@
 			rootFS := *img.RootFS
 			rootFS.DiffIDs = nil
 
-			newImage.Size = 0
 			newImage.SharedSize = 0
 			for _, id := range img.RootFS.DiffIDs {
 				rootFS.Append(id)
@@ -223,8 +222,6 @@
 
 				if layerRefs[chid] > 1 {
 					newImage.SharedSize += diffSize
-				} else {
-					newImage.Size += diffSize
 				}
 			}
 		}
@@ -323,7 +320,7 @@
 	newImage.ParentID = image.Parent.String()
 	newImage.ID = image.ID().String()
 	newImage.Created = image.Created.Unix()
-	newImage.Size = -1
+	newImage.Size = virtualSize
 	newImage.VirtualSize = virtualSize
 	newImage.SharedSize = -1
 	newImage.Containers = -1
diff --git a/distribution/config.go b/distribution/config.go
index 78cf053..bfea8b0 100644
--- a/distribution/config.go
+++ b/distribution/config.go
@@ -198,10 +198,18 @@
 	if p == nil {
 		return nil
 	}
-	return &storeLayer{
+	sl := storeLayer{
 		Layer: p,
 		ls:    l.ls,
 	}
+	if d, ok := p.(distribution.Describable); ok {
+		return &describableStoreLayer{
+			storeLayer:  sl,
+			describable: d,
+		}
+	}
+
+	return &sl
 }
 
 func (l *storeLayer) Open() (io.ReadCloser, error) {
diff --git a/integration-cli/docker_api_images_test.go b/integration-cli/docker_api_images_test.go
index cde8a77..b7617ea 100644
--- a/integration-cli/docker_api_images_test.go
+++ b/integration-cli/docker_api_images_test.go
@@ -127,3 +127,39 @@
 	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
 	c.Assert(res.Header.Get("Content-Type"), checker.Equals, "application/json")
 }
+
+// Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
+// This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
+func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
+	status, b, err := sockRequest("GET", "/images/json", nil)
+	c.Assert(err, checker.IsNil)
+	c.Assert(status, checker.Equals, http.StatusOK)
+	var images []types.ImageSummary
+	err = json.Unmarshal(b, &images)
+	c.Assert(err, checker.IsNil)
+	c.Assert(len(images), checker.Not(checker.Equals), 0)
+	for _, image := range images {
+		c.Assert(image.Size, checker.Not(checker.Equals), int64(-1))
+	}
+
+	type v124Image struct {
+		ID          string `json:"Id"`
+		ParentID    string `json:"ParentId"`
+		RepoTags    []string
+		RepoDigests []string
+		Created     int64
+		Size        int64
+		VirtualSize int64
+		Labels      map[string]string
+	}
+	status, b, err = sockRequest("GET", "/v1.24/images/json", nil)
+	c.Assert(err, checker.IsNil)
+	c.Assert(status, checker.Equals, http.StatusOK)
+	var v124Images []v124Image
+	err = json.Unmarshal(b, &v124Images)
+	c.Assert(err, checker.IsNil)
+	c.Assert(len(v124Images), checker.Not(checker.Equals), 0)
+	for _, image := range v124Images {
+		c.Assert(image.Size, checker.Not(checker.Equals), int64(-1))
+	}
+}