summaryrefslogtreecommitdiffstats
path: root/clone/virt-sysprep.in
blob: b73acfb84bfdeae09cd35404209f97bdf6f83354 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash -
# @configure_input@
# libguestfs virt-sysprep tool
# Copyright (C) 2011 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

unset CDPATH
program="virt-sysprep"
version="@PACKAGE_VERSION@"

TEMP=`getopt \
        -o a:c:d:vVx \
        --long help,add:,connect:,domain:,enable:,format::,hostname:,list-operations,verbose,version \
        -n $program -- "$@"`
if [ $? != 0 ]; then
    echo "$program: problem parsing the command line arguments"
    exit 1
fi
eval set -- "$TEMP"

# This array accumulates the arguments we pass through to guestfish.
declare -a guestfish
guestfish[0]="guestfish"
guestfish[1]="--rw"
guestfish[2]="--listen"
guestfish[3]="-i"
i=4

verbose=
add_params=0
enable=
hostname_param=localhost.localdomain

usage ()
{
    echo "Usage:"
    echo "  $program [--options] -d domname"
    echo "  $program [--options] -a disk.img [-a disk.img ...]"
    echo
    echo "Read $program(1) man page for more information."
    echo
    echo "NOTE: $program modifies the guest or disk image *in place*."
    exit $1
}

while true; do
    case "$1" in
        -a|--add)
            guestfish[i++]="-a"
            guestfish[i++]="$2"
            ((add_params++))
            shift 2;;
        -c|--connect)
            guestfish[i++]="-c"
            guestfish[i++]="$2"
            shift 2;;
        -d|--domain)
            guestfish[i++]="-d"
            guestfish[i++]="$2"
            ((add_params++))
            shift 2;;
        --enable)
            if [ -n "$enable" ]; then
                echo "error: --enable option can only be given once"
                exit 1
            fi
            enable="$2"
            shift 2;;
        --format)
            if [ -n "$2" ]; then
                guestfish[i++]="--format=$2"
            else
                guestfish[i++]="--format"
            fi
            shift 2;;
        --help)
            usage 0;;
        --hostname)
            hostname_param="$2"
            shift 2;;
        --list-operations)
            enable=list
            shift;;
        -v|--verbose)
            guestfish[i++]="-v"
            verbose=yes
            shift;;
        -V|--version)
            echo "$program $version"
            exit 0;;
        -x)
            guestfish[i++]="-x"
            shift;;
        --)
            shift
            break;;
        *)
            echo "Internal error!"
            exit 1;;
    esac
done

# Different sysprep operations that can be enabled.  Default is to
# enable all of these, although some of them are only done on certain
# guest types (see details below).
if [ -z "$enable" ]; then
    hostname=yes
    net_hwaddr=yes
    ssh_hostkeys=yes
    udev_persistent_net=yes
elif [ "$enable" = "list" ]; then
    echo "hostname"
    echo "net-hwaddr"
    echo "ssh-hostkeys"
    echo "udev-persistent-net"
    exit 0
else
    for opt in $(echo "$enable" | sed 's/,/ /g'); do
        case "$opt" in
            hostname)              hostname=yes ;;
            net-hwaddr)            net_hwaddr=yes ;;
            ssh-hostkeys)          ssh_hostkeys=yes ;;
            udev-persistent-net)   udev_persistent_net=yes ;;
            *)
                echo "error: unknown --enable feature: $opt"
                exit 1
        esac
    done
fi

# Make sure there were no extra parameters on the command line.
if [ $# -gt 0 ]; then
    echo "error: $program: extra parameters on the command line"
    echo
    usage 1
fi

# Did the user specify at least one -a or -d option?
if [ $add_params -eq 0 ]; then
    echo "error: $program: you need at least one -a or -d option"
    echo
    usage 1
fi

# end of command line parsing
#----------------------------------------------------------------------

set -e

if [ "$verbose" = "yes" ]; then
    echo command: "${guestfish[@]}"
fi

# Create a temporary directory for general purpose use during operations.
tmpdir="$(mktemp -d)"

# Call guestfish.
GUESTFISH_PID=
eval $("${guestfish[@]}")
if [ -z "$GUESTFISH_PID" ]; then
    echo "$program: guestfish didn't start up, see error messages above"
    exit 1
fi

cleanup ()
{
    kill $GUESTFISH_PID >/dev/null 2>&1 ||:
    rm -rf "$tmpdir" ||:
}
trap cleanup EXIT

# Launch back-end, inspect for operating systems, and get the guest
# root disk.
root=$(guestfish --remote inspect-get-roots)

if [ "$root" = "" ]; then
    echo "$program: no operating system was found on this disk"
    exit 1
fi

if [ "$verbose" = "yes" ]; then
    echo root: "$root"
fi

# Get the guest type.
type="$(guestfish --remote -- -inspect-get-type $root)"

if [ "$type" = "linux" ]; then
    distro="$(guestfish --remote -- -inspect-get-distro $root)"
fi

if [ "$type" = "windows" ]; then
    systemroot="$(guestfish --remote -- -inspect-get-windows-systemroot $root)"
fi

#----------------------------------------------------------------------
# Useful functions.

# erase_line filename regex
#
# Erase line(s) in a file that match the given regex.
erase_line ()
{
    guestfish --remote -- download "$1" "$tmpdir/file"
    sed "/$2/d" < "$tmpdir/file" > "$tmpdir/file.1"
    guestfish --remote -- upload "$tmpdir/file.1" "$1"
}

# rm_files wildcard
#
# Remove files.  Doesn't fail if no files exist.  Note the wildcard
# parameter cannot contain spaces or characters that need special
# quoting.
rm_files ()
{
    files=$(guestfish --remote -- glob-expand "$1")
    for f in $files; do
        guestfish --remote -- rm "$f"
    done
}

# rm_file filename
#
# Remove a single file.  No error if the file doesn't exist or is not
# a file.
rm_file ()
{
    t=$(guestfish --remote -- is-file "$1")
    if [ "$t" = "true" ]; then
        guestfish --remote -- rm "$1"
    fi
}

#----------------------------------------------------------------------
# The sysprep operations.

if [ "$hostname" = "yes" ]; then
    case "$type/$distro" in
        linux/fedora)
            guestfish --remote -- \
                download /etc/sysconfig/network "$tmpdir/network"
            echo "HOSTNAME=$hostname_param" > "$tmpdir/network.1"
            sed '/^HOSTNAME=/d' < "$tmpdir/network" >> "$tmpdir/network.1"
            guestfish --remote -- \
                upload "$tmpdir/network.1" /etc/sysconfig/network ;;
        linux/debian|linux/ubuntu)
            guestfish --remote -- write /etc/hostname "$hostname_param"
    esac
fi

if [ "$net_hwaddr" = "yes" ]; then
    case "$type/$distro" in
        linux/fedora)
            # XXX these filenames can have spaces and untrusted chars in them!
            files=$(guestfish --remote -- glob-expand '/etc/sysconfig/network-scripts/ifcfg-*')
            for f in $files; do
                erase_line "$f" "^HWADDR="
            done
    esac
fi

if [ "$ssh_hostkeys" = "yes" -a "$type" != "windows" ]; then
    rm_files "/etc/ssh/*_host_*"
fi

if [ "$udev_persistent_net" = "yes" -a "$type" = "linux" ]; then
    rm_file /etc/udev/rules.d/70-persistent-net.rules
fi

# Clean up and close down.

guestfish --remote umount-all
guestfish --remote sync
guestfish --remote exit

exit 0