forked from projectatomic/atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.sh
executable file
·211 lines (179 loc) · 6.78 KB
/
migrate.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# bash script to migrate containers from one backend storage to another.
set -e
ATOMIC_LIBEXEC="${ATOMIC_LIBEXEC-/usr/libexec/atomic}"
GOTAR="$ATOMIC_LIBEXEC/gotar"
main() {
if [ $(id -u) != 0 ];then
echo "Run 'migrate' as root user"
exit
fi
NUMARGS=$#
if [ $NUMARGS -eq 0 ] || [ "$1" = "--help" ];then
echo "Usage: migrate COMMAND [ARGS] [OPTIONS]
migrate [--help]
A self-sufficient tool for migrating docker containers from one backend storage to another
Commands:
export Export a container from an existing storage
import Import a container into a new storage"
exit
fi
if [ "$1" = "export" ];then
if [ -z "$2" ]; then
echo "migrate: "export" requires a minimum of 1 argument.
See 'migrate export --help'
Usage: migrate export CONTAINER-ID [OPTIONS]
Export a container from an existing storage"
exit
elif [ "$2" = "--help" ];then
echo "
Usage: migrate export CONTAINER-ID [OPTIONS]
Export a container from an existing storage
--graph Root of the Docker runtime (Default: /var/lib/docker)
--export-location Path for exporting the container (Default: /var/lib/atomic/migrate/containers)"
exit
else
container_export $2 $3 $4
fi
fi
if [ "$1" = "import" ];then
if [ -z "$2" ]; then
echo "migrate: "import" requires a minimum of 1 argument.
See 'migrate import --help'
Usage: migrate import CONTAINER-ID [OPTIONS]
Import a container into a new storage"
exit
elif [ "$2" = "--help" ];then
echo "
Usage: migrate import CONTAINER-ID [OPTIONS]
Import a container into a new storage
--graph Root of the Docker runtime (Default: /var/lib/docker)
--import-location Path for importing the container (Default: /var/lib/atomic/migrate/containers)"
exit
else
container_import $2 $3 $4
fi
fi
}
get_docker_pid() {
if ! systemctl is-active docker >/dev/null; then
echo "Docker daemon is not running"
exit 1
fi
pid=$(systemctl show -p MainPID docker.service)
echo ${pid#*=}
}
container_export(){
for arg in "$@"
do
flag=$(echo $arg|cut -d'=' -f 1)
val=$(echo $arg|cut -d'=' -f 2)
case "$flag" in
--container-id)
containerID=$val
;;
--graph)
dockerRootDir=$val
;;
--export-location)
exportPath=$val
;;
esac
done
if [ -z "$containerID" ]; then
echo "--container-id cannot be null"
exit 1
fi
if [ -z "$exportPath" ]; then
exportPath="/var/lib/atomic/migrate"
fi
dockerPid=$(get_docker_pid)
dockerCmdline=$(cat /proc/$dockerPid/cmdline)||exit 1
if [[ $dockerCmdline =~ "-g=" ]] || [[ $dockerCmdline =~ "-g/" ]] || [[ $dockerCmdline =~ "--graph" ]];then
if [ -z "$dockerRootDir" ] || [ $dockerRootDir = "/var/lib/docker" ];then
echo "Docker is not located at the default (/var/lib/docker) root location."
echo "Please provide the new root location of the docker runtime in --graph option."
exit 1
fi
else
dockerRootDir="/var/lib/docker"
fi
notruncContainerID=$(docker ps -aq --no-trunc|grep $containerID)||exit 1
tmpDir=$exportPath/containers/migrate-$containerID
mkdir -p $tmpDir
cd $tmpDir
containerBaseImageID=$(docker inspect --format '{{.Image}}' $containerID)||exit 1
echo $dockerRootDir>containerInfo.txt
echo $containerBaseImageID>>containerInfo.txt
echo $notruncContainerID>>containerInfo.txt
"$GOTAR" -cf container-metadata.tar $dockerRootDir/containers/$notruncContainerID 2> /dev/null
imageID=$(docker commit $containerID)||exit 1
mkdir -p $tmpDir/temp
docker save $imageID > $tmpDir/temp/image.tar||exit 1
$(cd $tmpDir/temp; "$GOTAR" -xf image.tar)
cd $tmpDir/temp/$imageID
cp layer.tar $tmpDir/container-diff.tar
cd $tmpDir
rm -rf temp
docker rmi -f $imageID 1>/dev/null||exit 1
}
container_import(){
for arg in "$@"
do
flag=$(echo $arg|cut -d'=' -f 1)
val=$(echo $arg|cut -d'=' -f 2)
case "$flag" in
--container-id)
containerID=$val
;;
--graph)
dockerRootDir=$val
;;
--import-location)
importPath=$val
;;
esac
done
if [ -z "$containerID" ]; then
echo "--container-id cannot be null"
exit
fi
if [ -z "$importPath" ]; then
importPath="/var/lib/atomic/migrate"
fi
dockerPid=$(get_docker_pid)
dockerCmdline=$(cat /proc/$dockerPid/cmdline)||exit 1
if [[ $dockerCmdline =~ "-g=" ]] || [[ $dockerCmdline =~ "-g/" ]] || [[ $dockerCmdline =~ "--graph" ]];then
if [ -z "$dockerRootDir" ] || [ $dockerRootDir = "/var/lib/docker" ];then
echo "Docker is not located at the default (/var/lib/docker) root location."
echo "Please provide the new root location of the docker runtime in --graph option."
exit 1
fi
else
dockerRootDir="/var/lib/docker"
fi
cd $importPath/containers/migrate-$containerID
dockerBaseImageID=$(sed -n '2p' containerInfo.txt)||exit 1
cat container-diff.tar|docker run -i -v "$GOTAR:/dev/shm/gotar" $dockerBaseImageID /dev/shm/gotar -xf -
newContainerID=$(docker ps -lq)||exit 1
newContainerName=$(docker inspect -f '{{.Name}}' $newContainerID)||exit 1
newNotruncContainerID=$(docker ps -aq --no-trunc|grep $newContainerID)||exit 1
cd $dockerRootDir/containers/$newNotruncContainerID
rm -rf *
cp $importPath/containers/migrate-$containerID/container-metadata.tar .
"$GOTAR" -xf container-metadata.tar
rm container-metadata.tar
oldDockerRootDir=$(sed -n '1p' $importPath/containers/migrate-$containerID/containerInfo.txt)||exit 1
oldNotruncContainerID=$(sed -n '3p' $importPath/containers/migrate-$containerID/containerInfo.txt)||exit 1
cp -r ${oldDockerRootDir:1}/containers/$oldNotruncContainerID/* .
baseDir=$(echo $oldDockerRootDir|cut -d"/" -f 2)
rm -rf $baseDir
oldStorageDriver=$(sed -n '1p' $importPath/info.txt)||exit 1
newStorageDriver=$(docker info|grep "Storage Driver"|cut -d" " -f 3)
sed -i "s|\"Driver\":\"$oldStorageDriver\"|\"Driver\":\"$newStorageDriver\"|g" config.json
sed -i "s|$oldDockerRootDir/containers/$oldNotruncContainerID|$dockerRootDir/containers/$oldNotruncContainerID|g" config.json
cd $dockerRootDir
find . -name "*$newNotruncContainerID*" -type d -exec rename $newNotruncContainerID $oldNotruncContainerID {} +
find . -name "*$newNotruncContainerID*" -type f -exec rename $newNotruncContainerID $oldNotruncContainerID {} +
}
main "$@"