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