Browse Source

[contrib] Add script to easily create copy-on-write SAN images

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
45d066d4cd
1 changed files with 48 additions and 0 deletions
  1. 48
    0
      contrib/vm/cow

+ 48
- 0
contrib/vm/cow View File

@@ -0,0 +1,48 @@
1
+#!/bin/sh
2
+
3
+set -e
4
+
5
+imgloop=
6
+tmpfile=
7
+tmploop=
8
+dmname=
9
+cowlink=
10
+
11
+function cleanup () {
12
+    set +e
13
+    [ -n "$cowlink" ] && rm $cowlink
14
+    [ -n "$dmname" ] && dmsetup remove $dmname
15
+    [ -n "$tmploop" ] && losetup -d $tmploop
16
+    [ -n "$tmpfile" ] && rm $tmpfile
17
+    [ -n "$imgloop" ] && losetup -d $imgloop
18
+}
19
+
20
+trap cleanup EXIT
21
+
22
+imgfile=$1
23
+if [ -z "$imgfile" ] ; then
24
+    echo Syntax: $0 /path/to/image/file
25
+    exit 1
26
+fi
27
+
28
+# Set up image loop device
29
+x=`losetup -f` ; losetup -r $x $imgfile ; imgloop=$x
30
+
31
+# Create temporary file and set up temporary loop device
32
+tmpfile=`mktemp $imgfile.XXXXXXXXXX`
33
+truncate -r $imgfile $tmpfile
34
+x=`losetup -f` ; losetup $x $tmpfile ; tmploop=$x
35
+
36
+# Create snapshot device
37
+imgsize=`blockdev --getsz $imgloop`
38
+x=`basename $imgfile` ; echo 0 $imgsize snapshot $imgloop $tmploop N 16 | \
39
+    dmsetup create $x ; dmname=$x
40
+chown --reference=$imgfile /dev/mapper/$dmname
41
+chmod --reference=$imgfile /dev/mapper/$dmname
42
+
43
+# Create symlink
44
+x=$imgfile.cow ; ln -s /dev/mapper/$dmname $x ; cowlink=$x
45
+
46
+# Wait until killed
47
+echo "Created $cowlink"
48
+while : ; do sleep 2147483647 ; done

Loading…
Cancel
Save