rsyslog: Turn off rate limiting
[amitay/autocluster.git] / config.d / 05diskimage_guestfish.defconf
1 # Hey Emacs, this is a -*- shell-script -*- !!!
2
3 # This file provides functions to implement access/update of disk
4 # images via guestfish.
5
6 ######################################################################
7
8 diskimage_mount_guestfish ()
9 {
10     echo "Using guestfish to update disk image ${disk}..."
11
12     local mount_args=""
13     local m d
14     for m in $SYSTEM_DISK_MOUNTS ; do
15         d="${m#*:}" # Device is after colon
16         m="${m%:*}" # Mountpoint is before colon
17         mount_args="${mount_args}${mount_args:+ } --mount ${d}:${m}"
18         echo " mount ${m} from device ${d} configured"
19     done
20     [ -n "$mount_args" ] || mount_args="-i"
21
22     eval $(guestfish --listen -a "$1" $mount_args)
23
24     guestfish --remote ping-daemon || \
25         die "Failed to initialise guestfish session"
26
27     diskimage is_directory "/root" || {
28         echo "Mounted directory does not look like a root filesystem"
29         guestfish --remote ll /
30         exit 1
31     }
32
33     echo $GUESTFISH_PID > tmp/guestfish.pid
34     echo "To attach to guestfish image"
35     echo "  export GUESTFISH_PID=$GUESTFISH_PID"
36     echo "  guestfish --remote" 
37 }
38
39 # unmount a qemu image
40 diskimage_unmount_guestfish ()
41 {
42     read GUESTFISH_PID < tmp/guestfish.pid
43     export GUESTFISH_PID
44
45     echo "Unmounting disk"
46     guestfish --remote sync
47     guestfish --remote exit
48 }
49
50 diskimage_mkdir_p_guestfish ()
51 {
52     local t=$(guestfish --remote is-dir "$1/.")
53     if [ "$t" = "false" ] ; then
54         guestfish --remote mkdir-p "$1"
55     fi
56 }
57
58 diskimage_substitute_vars_guestfish ()
59 {
60     local t=$(mktemp)
61     substitute_vars "$1" "$t"
62     guestfish --remote upload "$t" "$2"
63     rm "$t"
64 }
65
66 diskimage_chmod_guestfish ()
67 {
68     local mode="$1" ; shift
69
70     # For guestfish, octal mode must start with '0'.
71     case "$mode" in
72         (0*) : ;;
73         (*) mode="0${mode}" ;;
74     esac
75
76     local i
77     for i ; do
78         guestfish --remote glob chmod "$mode" "$i"
79     done
80 }
81
82 diskimage_chmod_reference_guestfish ()
83 {
84     local mode=$(printf "%o\n" $(( 0x$(stat -c "%f" "$1") )) )
85     mode="0${mode: -3}"
86     shift
87
88     local i
89     for i ; do
90         guestfish --remote glob chmod "$mode" "$i"
91     done
92 }
93
94 diskimage_is_file_guestfish ()
95 {
96     local t=$(guestfish --remote is-file "$1")
97     [ "$t" = "true" ]
98 }
99
100 diskimage_is_directory_guestfish ()
101 {
102     local t=$(guestfish --remote is-dir "$1/.")
103     [ "$t" = "true" ]
104 }
105
106 diskimage_append_text_file_guestfish ()
107 {
108     local t=$(mktemp)
109     guestfish --remote download "$2" "$t"
110     cat "$1" >> "$t"
111     guestfish --remote upload "$t" "$2"
112     rm "$t"
113 }
114
115 diskimage_append_text_guestfish ()
116 {
117     local t=$(mktemp)
118     guestfish --remote download "$2" "$t"
119     echo "$1" >> "$t"
120     guestfish --remote upload "$t" "$2"
121     rm "$t"
122 }
123
124 diskimage_sed_guestfish ()
125 {
126     local file="$1" ; shift
127
128     local t=$(mktemp)
129     guestfish --remote download "$file" "$t"
130     sed -i "$@" "$t"
131     guestfish --remote upload "$t" "$file"
132     rm "$t"
133 }
134
135 diskimage_grep_guestfish ()
136 {
137     local file="$1" ; shift
138
139     # guestfish's grep doesn't support options like -f, so don't use it.
140     local ret
141     local t=$(mktemp)
142     guestfish --remote download "$file" "$t"
143     grep "$@" "$t"
144     ret=$?
145     rm "$t"
146
147     return $ret
148 }
149
150 diskimage_put_guestfish ()
151 {
152     if [ "$1" = "-" ] ; then
153         local t=$(mktemp)
154         cat > "$t"
155         guestfish --remote upload "$t" "$2"
156         rm "$t"
157     else
158         guestfish --remote upload "$1" "$2"
159     fi
160 }
161
162 diskimage_ln_s_guestfish ()
163 {
164     guestfish --remote ln-s "$1" "$2"
165 }
166
167 diskimage_command_guestfish ()
168 {
169     # Yes, I mean "$*" and not "$@".  The command passed must be a
170     # single string...  and, yes, quoting is lost.
171     guestfish --remote command "$*"
172 }
173
174 diskimage_mv_guestfish ()
175 {
176     guestfish --remote mv "$1" "$2"
177 }
178
179 diskimage_rm_rf_guestfish ()
180 {
181     guestfish --remote rm-rf "$1"
182 }
183
184 ######################################################################
185
186 diskimage_guestfish_sanity_check ()
187 {
188     if [ "$SYSTEM_DISK_ACCESS_METHOD" = "guestfish" ] ; then
189         check_command guestfish
190     fi
191 }
192
193 register_hook post_config_hooks diskimage_guestfish_sanity_check