summaryrefslogtreecommitdiffstats
path: root/config.d/05diskimage_guestfish.defconf
blob: 7944430186ce8dd2e36ae01d2cff90fe1e770821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Hey Emacs, this is a -*- shell-script -*- !!!

# This file provides functions to implement access/update of disk
# images via guestfish.

######################################################################

diskimage_mount_guestfish ()
{
    echo "Using guestfish to update disk image ${disk}..."

    local mount_args=""
    local m d
    for m in $SYSTEM_DISK_MOUNTS ; do
	d="${m#*:}" # Device is after colon
	m="${m%:*}" # Mountpoint is before colon
	mount_args="${mount_args}${mount_args:+ } --mount ${d}:${m}"
	echo " mount ${m} from device ${d} configured"
    done
    [ -n "$mount_args" ] || mount_args="-i"

    eval $(guestfish --listen -a "$1" $mount_args)

    guestfish --remote ping-daemon || \
	die "Failed to initialise guestfish session"

    diskimage is_directory "/root" || {
	echo "Mounted directory does not look like a root filesystem"
	guestfish --remote ll /
	exit 1
    }

    echo $GUESTFISH_PID > tmp/guestfish.pid
    echo "To attach to guestfish image"
    echo "  export GUESTFISH_PID=$GUESTFISH_PID"
    echo "  guestfish --remote" 
}

# unmount a qemu image
diskimage_unmount_guestfish ()
{
    read GUESTFISH_PID < tmp/guestfish.pid
    export GUESTFISH_PID

    echo "Unmounting disk"
    guestfish --remote sync
    guestfish --remote exit
}

diskimage_mkdir_p_guestfish ()
{
    local t=$(guestfish --remote is-dir "$1/.")
    if [ "$t" = "false" ] ; then
	guestfish --remote mkdir-p "$1"
    fi
}

diskimage_substitute_vars_guestfish ()
{
    local t=$(mktemp)
    substitute_vars "$1" "$t"
    guestfish --remote upload "$t" "$2"
    rm "$t"
}

diskimage_chmod_guestfish ()
{
    local mode="$1" ; shift

    # For guestfish, octal mode must start with '0'.
    case "$mode" in
	(0*) : ;;
	(*) mode="0${mode}" ;;
    esac

    local i
    for i ; do
	guestfish --remote glob chmod "$mode" "$i"
    done
}

diskimage_chmod_reference_guestfish ()
{
    local mode=$(printf "%o\n" $(( 0x$(stat -c "%f" "$1") )) )
    mode="0${mode: -3}"
    shift

    local i
    for i ; do
	guestfish --remote glob chmod "$mode" "$i"
    done
}

diskimage_is_file_guestfish ()
{
    local t=$(guestfish --remote is-file "$1")
    [ "$t" = "true" ]
}

diskimage_is_directory_guestfish ()
{
    local t=$(guestfish --remote is-dir "$1/.")
    [ "$t" = "true" ]
}

diskimage_append_text_file_guestfish ()
{
    local t=$(mktemp)
    guestfish --remote download "$2" "$t"
    cat "$1" >> "$t"
    guestfish --remote upload "$t" "$2"
    rm "$t"
}

diskimage_append_text_guestfish ()
{
    local t=$(mktemp)
    guestfish --remote download "$2" "$t"
    echo "$1" >> "$t"
    guestfish --remote upload "$t" "$2"
    rm "$t"
}

diskimage_sed_guestfish ()
{
    local file="$1" ; shift

    local t=$(mktemp)
    guestfish --remote download "$file" "$t"
    sed -i "$@" "$t"
    guestfish --remote upload "$t" "$file"
    rm "$t"
}

diskimage_grep_guestfish ()
{
    local file="$1" ; shift

    # guestfish's grep doesn't support options like -f, so don't use it.
    local ret
    local t=$(mktemp)
    guestfish --remote download "$file" "$t"
    grep "$@" "$t"
    ret=$?
    rm "$t"

    return $ret
}

diskimage_put_guestfish ()
{
    if [ "$1" = "-" ] ; then
	local t=$(mktemp)
	cat > "$t"
	guestfish --remote upload "$t" "$2"
	rm "$t"
    else
	guestfish --remote upload "$1" "$2"
    fi
}

diskimage_ln_s_guestfish ()
{
    guestfish --remote ln-s "$1" "$2"
}

diskimage_command_guestfish ()
{
    # Yes, I mean "$*" and not "$@".  The command passed must be a
    # single string...  and, yes, quoting is lost.
    guestfish --remote command "$*"
}

diskimage_mv_guestfish ()
{
    guestfish --remote mv "$1" "$2"
}

diskimage_rm_rf_guestfish ()
{
    guestfish --remote rm-rf "$1"
}

######################################################################

diskimage_guestfish_sanity_check ()
{
    if [ "$SYSTEM_DISK_ACCESS_METHOD" = "guestfish" ] ; then
	check_command guestfish
    fi
}

register_hook post_config_hooks diskimage_guestfish_sanity_check