1 # Hey Emacs, this is a -*- shell-script -*- !!!
3 # This file provides functions to implement access/update of disk
4 # images via guestfish.
6 ######################################################################
8 diskimage_mount_guestfish ()
10 echo "Using guestfish to update disk image ${disk}..."
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"
20 [ -n "$mount_args" ] || mount_args="-i"
22 eval $(guestfish --listen -a "$1" $mount_args)
24 guestfish --remote ping-daemon || \
25 die "Failed to initialise guestfish session"
27 diskimage is_directory "/root" || {
28 echo "Mounted directory does not look like a root filesystem"
29 guestfish --remote ll /
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"
39 # unmount a qemu image
40 diskimage_unmount_guestfish ()
42 read GUESTFISH_PID < tmp/guestfish.pid
45 echo "Unmounting disk"
46 guestfish --remote sync
47 guestfish --remote exit
50 diskimage_mkdir_p_guestfish ()
52 local t=$(guestfish --remote is-dir "$1/.")
53 if [ "$t" = "false" ] ; then
54 guestfish --remote mkdir-p "$1"
58 diskimage_substitute_vars_guestfish ()
61 substitute_vars "$1" "$t"
62 guestfish --remote upload "$t" "$2"
66 diskimage_chmod_guestfish ()
68 local mode="$1" ; shift
70 # For guestfish, octal mode must start with '0'.
73 (*) mode="0${mode}" ;;
78 guestfish --remote glob chmod "$mode" "$i"
82 diskimage_chmod_reference_guestfish ()
84 local mode=$(printf "%o\n" $(( 0x$(stat -c "%f" "$1") )) )
90 guestfish --remote glob chmod "$mode" "$i"
94 diskimage_is_file_guestfish ()
96 local t=$(guestfish --remote is-file "$1")
100 diskimage_is_directory_guestfish ()
102 local t=$(guestfish --remote is-dir "$1/.")
106 diskimage_append_text_file_guestfish ()
109 guestfish --remote download "$2" "$t"
111 guestfish --remote upload "$t" "$2"
115 diskimage_append_text_guestfish ()
118 guestfish --remote download "$2" "$t"
120 guestfish --remote upload "$t" "$2"
124 diskimage_sed_guestfish ()
126 local file="$1" ; shift
129 guestfish --remote download "$file" "$t"
131 guestfish --remote upload "$t" "$file"
135 diskimage_grep_guestfish ()
137 local file="$1" ; shift
139 # guestfish's grep doesn't support options like -f, so don't use it.
142 guestfish --remote download "$file" "$t"
150 diskimage_put_guestfish ()
152 if [ "$1" = "-" ] ; then
155 guestfish --remote upload "$t" "$2"
158 guestfish --remote upload "$1" "$2"
162 diskimage_ln_s_guestfish ()
164 guestfish --remote ln-s "$1" "$2"
167 diskimage_command_guestfish ()
169 # Yes, I mean "$*" and not "$@". The command passed must be a
170 # single string... and, yes, quoting is lost.
171 guestfish --remote command "$*"
174 diskimage_mv_guestfish ()
176 guestfish --remote mv "$1" "$2"
179 diskimage_rm_rf_guestfish ()
181 guestfish --remote rm-rf "$1"
184 ######################################################################
186 diskimage_guestfish_sanity_check ()
188 if [ "$SYSTEM_DISK_ACCESS_METHOD" = "guestfish" ] ; then
189 check_command guestfish
193 register_hook post_config_hooks diskimage_guestfish_sanity_check