summaryrefslogtreecommitdiff
path: root/notes/btrfs-test.sh
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2016-04-29 14:36:26 -0400
committerAndrew Cady <d@jerkface.net>2016-04-29 14:36:30 -0400
commit153d299a41b9be4e15dab1ca29bb93a74bd2445d (patch)
tree96fbfbe7c64f0b3f02f3d755e2b129917785bb98 /notes/btrfs-test.sh
parent5f41fb879ca830e5ad3345878e59072f3d6573bc (diff)
fix paths (in progress)
Diffstat (limited to 'notes/btrfs-test.sh')
-rw-r--r--notes/btrfs-test.sh282
1 files changed, 282 insertions, 0 deletions
diff --git a/notes/btrfs-test.sh b/notes/btrfs-test.sh
new file mode 100644
index 0000000..62cab9a
--- /dev/null
+++ b/notes/btrfs-test.sh
@@ -0,0 +1,282 @@
1#!/bin/sh
2
3. ./var.sh
4. ./btrfs-functions.sh
5
6create_image()
7{
8 $(ARGS_NE megs filename)
9 [ ! -e "$filename" ] || return
10 truncate -s "$megs"M "$filename"
11}
12
13make_seed_fs()
14{
15 $(ARGS_NE input_dir img)
16 local mountpoint dev seed_subvol root_subvol
17
18 mountpoint=$img.mnt
19 mkdir "$mountpoint" || die 'mkdir failed'
20 dev=$(lodev "$img") || die 'lodev failed'
21 mkfs.btrfs "$dev" || die 'mkfs failed'
22 mount "$dev" "$mountpoint" || die 'mount failed'
23
24 seed_subvol=$mountpoint/SEED
25 root_subvol=$mountpoint/ROOT
26 btrfs subvolume create "$root_subvol" || die
27 rsync -aP "$input_dir"/ "$root_subvol"/ || die 'rsync failed'
28 btrfs subvolume snapshot -r "$root_subvol" "$seed_subvol" || die
29 btrfs_subvolume_set_default "$root_subvol" || die
30
31 umount "$mountpoint" || die 'umount failed'
32 rmdir "$mountpoint"
33 btrfstune -S 1 "$dev" || die 'btrfstune failed'
34 unlodev "$dev"
35}
36
37btrfs_subvolume_set_default()
38{
39 $(ARGS_NE path)
40 local subvol_id
41 subvol_id=$(btrfs_show_subvolume_id "$path") || return
42 btrfs subvolume set-default "$subvol_id" "$path"
43}
44
45loop_is_attached()
46{
47 [ "$(losetup -l "$1" -O BACK-FILE -n 2>/dev/null)" ]
48}
49
50lodev() { lodev_helper false "$@"; }
51lodev_readonly() { lodev_helper true "$@"; }
52
53lodev_helper()
54{
55 $(ARGS_NE readonly img)
56 local link b dev opt_ro
57 link=$img.b
58 if $readonly; then
59 opt_ro=-r
60 else
61 opt_ro=
62 fi
63 if [ -L "$link" ]; then
64 b="$(readlink "$link")"
65 loop_is_attached "$b" || losetup $opt_ro "$b" "$img" || return
66 else
67 dev=$(unused_dev) || return
68 losetup $opt_ro "$dev" "$img" || return
69 ln -sf "$dev" "$img".b || return
70 fi >/dev/null
71 printf '%s\n' $link
72}
73
74unused_dev()
75{
76 local do_not_use f d no
77 do_not_use=$(for f in *.b; do [ -L "$f" ] || continue; readlink $f; done)
78 for n in $(seq 10 20); do
79 d=/dev/loop$n
80 for no in $do_not_use; do
81 [ "$d" != "$no" ] || continue 2
82 done
83 echo $d
84 return
85 done
86}
87
88unlodev()
89{
90 $(ARGS_NE link)
91 [ -L "$link" ] || return
92 losetup -d "$(readlink "$link")" # && rm -f "$link"
93}
94
95plant_seed()
96{
97 $(ARGS_NE seed_img overlay_img mountpoint)
98 local overlay_dev seed_dev
99
100 overlay_dev=$(lodev "$overlay_img") || die 'lodev failed'
101 seed_dev=$(lodev_readonly "$seed_img") || die 'lodev failed'
102
103 mount "$seed_dev" "$mountpoint" || die 'mount failed'
104
105 btrfs device add "$overlay_dev" "$mountpoint" || die 'btrfs-device-add failed'
106 mount -o rw,remount "$mountpoint" || die 'remount failed'
107 btrfs device delete "$seed_dev" "$mountpoint" || die 'btrfs-device-delete failed'
108
109 unlodev "$seed_dev"
110
111 umount "$mountpoint"
112 unlodev "$overlay_dev"
113}
114
115generate_seed_helper()
116{
117 $(ARGS_NE seed_layer keep_old_seeds size seed_img snapshot_mountpoint overlay_img_output)
118 local mountpoint overlay_dev seed_dev snapshot_default
119 mountpoint=${overlay_img_output}.mnt
120
121 mountpoint -q "$snapshot_mountpoint" || die
122 [ -d "$mountpoint" ] || mkdir "$mountpoint" || die
123
124 filemaker "$overlay_img_output" create_image "$size" || die
125
126 overlay_dev=$(lodev "$overlay_img_output") || die
127 seed_dev=$(lodev_read_only "$seed_img") || die
128
129 mount -o subvol=/ "$seed_dev" "$mountpoint" || die
130 btrfs device add "$overlay_dev" "$mountpoint" || die
131 mount -o rw,remount "$mountpoint" || die
132
133 snapshot_default=$(btrfs_show_default_path "$snapshot_mountpoint") || die
134
135 push_simple "$snapshot_mountpoint"/SEED_NEW "$snapshot_mountpoint"/SEED "$snapshot_default" "$mountpoint"
136
137 if $keep_old_seeds; then
138 sex mv -T --backup=numbered "$mountpoint"/SEED_NEW "$mountpoint"/SEED || die
139 else
140 btrfs subvolume delete "$mountpoint"/SEED || die
141 sex mv "$mountpoint"/SEED_NEW "$mountpoint"/SEED || die
142 fi
143
144 if ! $seed_layer; then
145 btrfs device remove "$seed_dev" "$mountpoint" || die
146 fi
147
148 umount "$mountpoint" || die
149 rmdir "$mountpoint"
150
151 btrfstune -S 1 "$overlay_img_output" || die 'btrfstune failed'
152}
153
154generate_seed_overlay() # NB. uses non-local variables set in <..>_helper
155{
156 generate_seed_helper true true "$@"
157}
158
159regenerate_toplevel_seed()
160{
161 generate_seed_helper false true "$@"
162}
163
164regenerate_from_master()
165{
166 regenerate_toplevel_seed 256 seed.img master overlay.img
167 remove_image seed.img || die
168 mv overlay.img seed.img || die
169 mv overlay.img.b seed.img.b || die
170}
171
172plant_seed_mount()
173{
174 plant_seed "$1" "$2" "$3"
175 mount "$(lodev "$2")" "$3"
176}
177
178plant_seed_mount_rootvol()
179{
180 plant_seed "$1" "$2" "$3"
181 mount -o subvol=/ "$(lodev "$2")" "$3"
182}
183
184remove_image()
185{
186 unlodev "$1.b"
187 rm -f "$1" "$1.b"
188}
189
190IMAGES='master.img slave.img seed.img overlay.img'
191MNTPOINTS='master slave/mnt slave seed.img.mnt overlay.img.mnt'
192
193mounter()
194{
195 $(ARGS_NE mp)
196 shift
197 mountpoint -q "$mp" && return
198 mkdir "$mp" 2>/dev/null
199 "$@" "$mp"
200}
201
202filemaker()
203{
204 $(ARGS_NE f)
205 shift
206 [ -f "$f" ] && return
207 ! [ -e "$f" ] || return
208 "$@" "$f"
209}
210
211in_dir()
212{
213 (set -e
214 cd "$1"
215 shift
216 "$@")
217}
218
219pull()
220{
221 $(ARGS_NE mnt src dst)
222 local src_subvol
223 src_subvol=$(btrfs_show_default_path "$src") || die
224
225 die 'unimplemented'
226}
227
228push_slave()
229{
230 local mnt img img_dev
231 mnt=slave/mnt
232 img=slave.img
233 mkdir "$mnt" 2>/dev/null
234 img_dev=$(lodev "$img") || die
235 mounter "$mnt" mount -o subvol=/ "$img_dev" || die
236
237 push "$mnt" slave master
238 umount "$mnt"
239 losetup -D
240}
241
242main()
243{
244 local img
245
246 mkdir inp
247 touch inp/file.txt
248
249 for img in $IMAGES; do
250 filemaker $img create_image 256 || die # Note: btrfs kernel task hangs if image is too small!
251 done
252
253 make_seed_fs inp seed.img || die
254
255 mounter slave plant_seed_mount seed.img slave.img
256 mounter master plant_seed_mount_rootvol seed.img master.img
257}
258
259cleanup()
260{
261 local img m
262
263 for img in $IMAGES; do
264 remove_image "$img"
265 done
266 for m in $MNTPOINTS; do
267 umount "$m" 2>/dev/null
268 rmdir "$m" 2>/dev/null
269 done
270 losetup -D 2>/dev/null
271 true
272}
273
274case "$1" in
275 clean|cleanup) cleanup ;;
276 testclean) main; cleanup ;;
277 cleantest) cleanup; main ;;
278 push) push_slave ;;
279 regen) regenerate_from_master ;;
280 '') main ;;
281 *) die "Unknown command: $1" ;;
282esac