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
|
#!/bin/sh
usage() {
cat << EOF
usage: tmpoverlay [OPTIONS] [SOURCE...] DEST
Create a tmpfs-backed overlayfs at DEST starting with SOURCEs. If no SOURCE is
specified, use DEST as the source. To free the memory, simply umount DEST.
options:
-c, --no-canonicalize don't canonicalize paths
-h, --help print this help
-o, --overlayfs OPTS add overlayfs mount options, e.g. redirect_dir/metacopy
-n, --no-mtab don't write to /etc/mtab
-N, --mount-name NAME source name for mount (default "overlay")
-t, --tmpfs OPTS add tmpfs mount options, e.g. size
-v, --verbose verbose mode
examples:
tmpoverlay / /new_root # make a thin copy of root
tmpoverlay /etc # make read-only /etc writable
tmpoverlay /a /b /c /merged # merge /a, /b, /c, and a fresh tmpfs
EOF
}
log() {
fmt=$1
shift
printf "tmpoverlay: $fmt\n" "$@" >&2
}
logv() {
if [ -n "$verbose" ]; then
log "$@"
fi
}
die() {
log "$@"
exit 1
}
canon() {
if [ -n "$no_canon" ]; then
printf '%s\n' "$1"
else
realpath "$1"
fi
}
my_getopt() {
getopt \
-l no-canonicalize \
-l help \
-l overlayfs: \
-l no-mtab \
-l mount-name: \
-l tmpfs: \
-l verbose \
-n tmpoverlay \
-- \
cho:nN:t:v \
"$@"
}
args=$(my_getopt "$@") || { usage >&2; exit 1; }
eval set -- "$args"
unset args
unset no_canon no_mtab extra_overlayfs_opts tmpfs_opts tmpdir verbose
while true; do
case "$1" in
-c|--no-canonicalize) no_canon=-c; shift;;
-h|--help) usage; exit 0;;
-o|--overlayfs)
[ -n "$2" ] && extra_overlayfs_opts="$extra_overlayfs_opts,$2"
shift 2
;;
-n|--no-mtab) no_mtab=-n; shift;;
-N|--mount-name) mount_name=$2; shift 2;;
-t|--tmpfs)
[ -n "$2" ] && tmpfs_opts="${tmpfs_opts:+$tmpfs_opts,}$2"
shift 2
;;
-v|--verbose) verbose=-v; shift;;
--) shift; break;;
*) die "getopt failure"
esac
done
[ $# != 0 ] || { log 'no paths specified'; usage >&2; exit 1; }
unset lowerdir
while [ -n "$2" ]; do
d=$(canon "$1")
if [ -h "$d" ] || ! [ -d "$d" ]; then
die 'source "%s" is not a directory' "$d"
fi
lowerdir=${lowerdir+:}$d
shift
done
dest=$(canon "$1")
echo lowerdir=$lowerdir dest=$dest
[ -d "$dest" ] || die 'destination "%s" is not a directory' "$dest"
[ "$dest" != / ] || log 'overmounting root, use with caution'
if [ -z "$lowerdir" ]; then
lowerdir=$dest
fi
err() {
r=$?
trap - INT TERM QUIT
logv 'an error occurred, cleaning up'
if [ -d "$tmpdir" ]; then
if mountpoint -q "$tmpdir"; then
logv 'unmounting tmpdir'
umount $no_mtab "$tmpdir"
fi
logv 'deleting tmpdir'
rmdir "$tmpdir"
fi
case $1 in
[A-Z]*) logv 'reraising SIG%s' $1; kill -$1 $$ && read _
esac
logv 'exiting'
if [ $r = 0 ]; then
exit 1
fi
exit $r
}
trap 'err INT' INT
trap 'err TERM' TERM
trap 'err QUIT' QUIT
if [ -z "$tmpdir" ]; then
logv 'creating tmpdir'
tmpdir=$(umask 077; mktemp -dt tmpoverlay.XXXXXXXXXX) || exit 1
logv 'created tmpdir: %s' "$tmpdir"
fi
mount -t tmpfs ${tmpfs_opts:+-o "$tmpfs_opts"} ${verbose:+-v} tmpfs "$tmpdir" || err
upperdir="$tmpdir/upper"
workdir="$tmpdir/work"
logv 'creating upper/work dirs'
mkdir "$upperdir" "$workdir" || err
# try to match perms/attrs. this is not race-free but it's impossible without
# atomic (CAS) chown/chmod/setfattr. chown --from is not atomic, not portable,
# and also doesn't cover chmod/setfattr.
lastlowerdir=${lowerdir##*:}
logv 'copying lowerdir owner to upperdir'
owner=$(stat -c %u:%g "$lastlowerdir") || err
chown $owner "$upperdir" || err
logv 'copying lowerdir perms to upperdir'
mode=$(stat -c %a "$lastlowerdir") || err
chmod $mode "$upperdir" || err
# -m - covers ACLs (system.posix_acl_access) and file caps
# (security.capability). theoretically someone might have get/setcap and/or
# get/setfacl but not get/setxattr, but this is unlikely since libcap/acl
# require attr.
logv 'copying root attrs'
if attr=$(cd "$lastlowerdir" && getfattr -d -m - . 2>/dev/null); then
if [ -n "$attr" ]; then
printf '%s\n' "$attr" | (cd "$upperdir"; setfattr --restore=-) || err
fi
else
log 'getfattr not found or failed, skipping xattrs'
fi
logv 'mounting overlay'
overlayfs_opts="lowerdir=$lowerdir,upperdir=$upperdir,workdir=$workdir,volatile$extra_overlayfs_opts"
mount -t overlay -o "$overlayfs_opts" $no_canon $no_mtab $verbose ${mount_name-overlay} "$dest" || err
logv 'cleaning up'
umount "$tmpdir" || exit
rmdir "$tmpdir" || exit
logv 'done, exiting'
|