summaryrefslogtreecommitdiffstats
path: root/config.d/05diskimage_guestmount.defconf
blob: 4c1757514a388e32c3ade680a01ae5d1dc7ed7aa (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
# Hey Emacs, this is a -*- shell-script -*- !!!

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

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

diskimage_mount_guestmount ()
{
    local disk="$1"

    echo "Using guestmount 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"

    mkdir -p mnt
    guestmount -a "$disk" $mount_args mnt || \
	die "Failed to mount $disk using guestmount"


    [ -d "mnt/root" ] || {
	echo "Mounted directory does not look like a root filesystem"
	ls -latr mnt
	exit 1
    }
}

# unmount a qemu image
diskimage_unmount_guestmount ()
{
    echo "Unmounting disk"
    sync; sync;

    fusermount -u mnt
}

# These are all the same as loopback.  We could do something cleverer,
# but this will suffice for now.

diskimage_mkdir_p_guestmount ()
{
    mkdir -p "mnt$1"
}

diskimage_substitute_vars_guestmount ()
{
    substitute_vars "$1" "mnt$2"
}

diskimage_chmod_guestmount ()
{
    local mode="$1" ; shift
    local i
    for i ; do
        # this should handle wildcards
	eval chmod "$mode" "mnt$i"
    done
}

diskimage_chmod_reference_guestmount ()
{
    local ref="$1" ; shift
    local i
    for i ; do
        # this should handle wildcards
	eval chmod --reference="$ref" "mnt$i"
    done
}

diskimage_is_file_guestmount ()
{
    [ -f "mnt$1" ]
}

diskimage_is_directory_guestmount ()
{
    [ -d "mnt$1" ]
}

diskimage_append_text_file_guestmount ()
{
    cat "$1" >> "mnt$2"
}

diskimage_append_text_guestmount ()
{
    echo "$1" >> "mnt$2"
}

diskimage_sed_guestmount ()
{
    local file="$1" ; shift
    sed -i.org "$@" "mnt$file"
}

diskimage_grep_guestmount ()
{
    local file="$1" ; shift
    grep "$@" "mnt$file"
}

diskimage_put_guestmount ()
{
    if [ "$1" = "-" ] ; then
	cat >"mnt$2"
    else
	cp "$1" "mnt$2"
    fi
}

diskimage_ln_s_guestmount ()
{
    ln -s "$1" "mnt$2"
}

diskimage_command_guestmount ()
{
    chroot mnt "$@"
}

diskimage_mv_guestmount ()
{
    mv "mnt$1" "mnt$2"
}

diskimage_rm_rf_guestmount ()
{
    rm -rf "mnt$1"
}

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

diskimage_guestmount_sanity_check ()
{
    if [ "$SYSTEM_DISK_FORMAT" = "qcow2" -a \
	"$SYSTEM_DISK_ACCESS_METHOD" = "guestmount" ] ; then
	check_command guestmount
	check_command fusermount
    fi
}

register_hook post_config_hooks diskimage_guestmount_sanity_check