-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget-rootfs.sh
executable file
·58 lines (46 loc) · 1.46 KB
/
get-rootfs.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
#!/bin/bash
# Command-line tool that simplifies the process of extracting Docker images
# and their root filesystem layers.
set -eux -o pipefail
if [ $# -ne 2 ]; then
echo "Usage: $0 <image_name> <output_directory>"
exit 1
fi
check_command_existence() {
# Function to check if a command exists
command -v "$1" >/dev/null 2>&1 || {
echo >&2 "ERROR: '$1' is required but not found. Please install '$1' before running this script."
exit 1
}
}
cleanup() {
# Function to clean up temporary files and directories
local exit_code=$?
# FIXME: add more safe checks before removing
if [ -n "$output_directory" ] && [ -d "$output_directory" ]; then
if [ $exit_code -gt 0 ]; then
rm -rf "$output_directory/rootfs"
fi
fi
if [ -n "$container_id" ]; then
# Delete created container
sudo docker rm "$container_id"
fi
}
# Check if docker and jq exist
check_command_existence "docker"
check_command_existence "jq"
image_name="$1"
output_directory="$(realpath "$2")"
trap cleanup 0 1 2 3 6 15
# Create a container based on the provided docker image
container_id="$(docker create "$image_name")"
# Copy container rootfs content to output directory
rm -rf "$output_directory/rootfs"
mkdir -p "$output_directory/rootfs"
cd "$output_directory/rootfs/"
docker export "$container_id" | tar xf -
# Cleanup useless files
for f in console pts shm; do
rm -rf "$output_directory/rootfs/dev/$f"
done