This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
/
image_extract.go
149 lines (128 loc) · 3.99 KB
/
image_extract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"github.com/coreos/rkt/pkg/fileutil"
"github.com/coreos/rkt/pkg/tar"
"github.com/coreos/rkt/pkg/uid"
"github.com/coreos/rkt/store"
"github.com/spf13/cobra"
)
var (
cmdImageExtract = &cobra.Command{
Use: "extract IMAGE OUTPUT_DIR",
Short: "Extract a stored image to a directory",
Long: `IMAGE should be a string referencing an image: either a hash or an image name.
Note that in order to make cleaning up easy (just rm -rf), extract does not use
overlayfs or any other mechanism.`,
Run: runWrapper(runImageExtract),
}
flagExtractRootfsOnly bool
flagExtractOverwrite bool
)
func init() {
cmdImage.AddCommand(cmdImageExtract)
cmdImageExtract.Flags().BoolVar(&flagExtractRootfsOnly, "rootfs-only", false, "extract rootfs only")
cmdImageExtract.Flags().BoolVar(&flagExtractOverwrite, "overwrite", false, "overwrite output directory")
}
func runImageExtract(cmd *cobra.Command, args []string) (exit int) {
if len(args) != 2 {
cmd.Usage()
return 1
}
outputDir := args[1]
s, err := store.NewStore(getDataDir())
if err != nil {
stderr.PrintE("cannot open store", err)
return 1
}
key, err := getStoreKeyFromAppOrHash(s, args[0])
if err != nil {
stderr.Error(err)
return 1
}
aci, err := s.ReadStream(key)
if err != nil {
stderr.PrintE("error reading ACI from the store", err)
return 1
}
// ExtractTar needs an absolute path
absOutputDir, err := filepath.Abs(outputDir)
if err != nil {
stderr.PrintE("error converting output to an absolute path", err)
return 1
}
if _, err := os.Stat(absOutputDir); err == nil {
if !flagExtractOverwrite {
stderr.Print("output directory exists (try --overwrite)")
return 1
}
// don't allow the user to delete the root filesystem by mistake
if absOutputDir == "/" {
stderr.Print("this would delete your root filesystem. Refusing.")
return 1
}
if err := os.RemoveAll(absOutputDir); err != nil {
stderr.PrintE("error removing existing output dir", err)
return 1
}
}
// if the user only asks for the rootfs we extract the image to a temporary
// directory and then move/copy the rootfs to the output directory, if not
// we just extract the image to the output directory
extractDir := absOutputDir
if flagExtractRootfsOnly {
rktTmpDir, err := s.TmpDir()
if err != nil {
stderr.PrintE("error creating rkt temporary directory", err)
return 1
}
tmpDir, err := ioutil.TempDir(rktTmpDir, "rkt-image-extract-")
if err != nil {
stderr.PrintE("error creating temporary directory", err)
return 1
}
defer os.RemoveAll(tmpDir)
extractDir = tmpDir
} else {
if err := os.MkdirAll(absOutputDir, 0755); err != nil {
stderr.PrintE("error creating output directory", err)
return 1
}
}
if err := tar.ExtractTar(aci, extractDir, false, uid.NewBlankUidRange(), nil); err != nil {
stderr.PrintE("error extracting ACI", err)
return 1
}
if flagExtractRootfsOnly {
rootfsDir := filepath.Join(extractDir, "rootfs")
if err := os.Rename(rootfsDir, absOutputDir); err != nil {
if e, ok := err.(*os.LinkError); ok && e.Err == syscall.EXDEV {
// it's on a different device, fall back to copying
if err := fileutil.CopyTree(rootfsDir, absOutputDir, uid.NewBlankUidRange()); err != nil {
stderr.PrintE("error copying ACI rootfs", err)
return 1
}
} else {
stderr.PrintE("error moving ACI rootfs", err)
return 1
}
}
}
return 0
}