-
Notifications
You must be signed in to change notification settings - Fork 5
/
shocker-image-create
executable file
·50 lines (38 loc) · 1.26 KB
/
shocker-image-create
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
#!/bin/sh
dirname=$(dirname "$(readlink -f "$0")")
#shellcheck disable=SC1090
. "$dirname"/utils.sh
usage () {
cat << USAGE
shocker-image-create - create an image
Usage: shocker image create <dir|image|container> [tar-archive [...]]
Options:
-h, --help output usage information
Examples:
$ shocker image create . # create a new image from current dir
$ shocker image create . archive.tgz # create a new image from current dir
# with archive.tgz layed over it
USAGE
}
[ "$#" -eq 0 ] && { usage; exit 1; }
case "$1" in
-h|--help ) usage && exit 1 ;;
esac
# generate image id
while true; do
uuid="$(mktemp -u img_XXXXX)"
[ ! -e "${image_path}/${uuid}" ] && break
done
# create new empty subvolume
btrfs subvolume create "${image_path}/${uuid}" > /dev/null
# copy (using reflink) all files in source dir to image dir
find "$1" -mindepth 1 -maxdepth 1 -print0 | xargs -0 cp -prf --reflink=auto -t "${image_path}/${uuid}" >/dev/null
# save image meta data
readlink -f "$1" > "${image_path}/${uuid}/img.source"
# extract all tarballs over image directory
shift
for tarball in "$@"; do
[ -r "$tarball" ] || break;
tar xaf "$tarball" -C "${image_path}/${uuid}"
done
printf "Created: %s\n" "$uuid"