Wanlong Gao
2012-Mar-09 06:50 UTC
[Libguestfs] [PATCH V2] virt-sysprep:add ipconfig for preparation
Hi Rich: V1->V2: add the documentation. Any comments? Thanks, Wanlong Gao --------------------------------------------------------------------------------------------------- Add the the ipconfig for vir-prep. Usage like below: [root at Allen ~]# virt-sysprep --ipconfig="eth0:192.168.1.2,255.255.255.0,192.168.1.1" --enable=ipconfig -d clone-6u1 OR [root at Allen ~]# virt-sysprep -d clone-6u1 Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> --- clone/virt-sysprep.in | 51 +++++++++++++++++++++++++++++++++++++++++++++++- clone/virt-sysprep.pod | 19 ++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/clone/virt-sysprep.in b/clone/virt-sysprep.in index d505532..557d46a 100644 --- a/clone/virt-sysprep.in +++ b/clone/virt-sysprep.in @@ -26,7 +26,7 @@ version="@PACKAGE_VERSION@" TEMP=`getopt \ -o a:c:d:vVx \ - --long help,add:,connect:,domain:,enable:,format::,hostname:,list-operations,selinux-relabel,no-selinux-relabel,verbose,version \ + --long help,add:,connect:,domain:,enable:,format::,hostname:,ipconfig:,list-operations,selinux-relabel,no-selinux-relabel,verbose,version \ -n $program -- "$@"` if [ $? != 0 ]; then echo "$program: problem parsing the command line arguments" @@ -91,6 +91,9 @@ while true; do --hostname) hostname_param="$2" shift 2;; + --ipconfig) + ipconfig_param="$2" + shift 2;; --list-operations) enable=list shift;; @@ -135,6 +138,7 @@ if [ -z "$enable" ]; then dhcp_client_state=yes dhcp_server_state=yes hostname=yes + ipconfig=yes logfiles=yes mail_spool=yes net_hwaddr=yes @@ -150,6 +154,7 @@ elif [ "$enable" = "list" ]; then echo "dhcp-client-state" echo "dhcp-server-state" echo "hostname" + echo "ipconfig" echo "logfiles" echo "mail-spool" echo "net-hwaddr" @@ -168,6 +173,7 @@ else dhcp-client-state) dhcp_client_state=yes ;; dhcp-server-state) dhcp_server_state=yes ;; hostname) hostname=yes ;; + ipconfig) ipconfig=yes ;; logfiles) logfiles=yes ;; mail-spool) mail_spool=yes ;; net-hwaddr) net_hwaddr=yes ;; @@ -286,6 +292,49 @@ if [ "$hostname" = "yes" ]; then esac fi +#FIXME: This may not work OK for IPv6 ? +if [ "$ipconfig" = "yes" ]; then + case "$type/$distro" in + linux/fedora|linux/rhel) + if [ -d $mnt/etc/sysconfig/network-scripts ]; then + if [ -z $ipconfig_param ]; then + rm_ipconfig () + { + sed '/^IPADDR=/d;/^BOOTPROTO=/d;/^NETMASK=/d;/^GATEWAY=/d;/^DNS/d' \ + < "$1" > "$1.new" + echo "BOOTPROTO=dhcp" >> "$1.new" + mv -f "$1.new" "$1" + } + export -f rm_ipconfig + find $mnt/etc/sysconfig/network-scripts \ + -name 'ifcfg-*' -type f \ + -exec bash -c 'rm_ipconfig "$0"' {} \; + else + for (( i=1; i<8 ; i+=2 )); do + __device=$(echo "$ipconfig_param" | awk -v j=$i -F: '{print $j}') + if [ -z $__device ]; then + break + fi + __ipconfig=$(echo "$ipconfig_param" | awk -v j=$((i+1)) -F: '{print $j}') + __config_file=$(echo "$mnt/etc/sysconfig/network-scripts/ifcfg-$__device") + if [ -e $__config_file ]; then + __ip=$(echo "$__ipconfig" | awk -F, '{print $1}') + __mask=$(echo "$__ipconfig" | awk -F, '{print $2}') + __gw=$(echo "$__ipconfig" | awk -F, '{print $3}') + sed '/^IPADDR=/d;/^BOOTPROTO=/d;/^NETMASK=/d;/^GATEWAY=/d;/^DNS/d' \ + < "$__config_file" > "$__config_file.new" + echo "IPADDR=$__ip" >> "$__config_file.new" + echo "BOOTPROTO=static" >> "$__config_file.new" + echo "NETMASK=$__mask" >> "$__config_file.new" + echo "GATEWAY=$__gw" >> "$__config_file.new" + mv -f "$__config_file.new" "$__config_file" + fi + done + fi + fi + esac +fi + if [ "$logfiles" = "yes" ]; then case "$type" in linux) diff --git a/clone/virt-sysprep.pod b/clone/virt-sysprep.pod index 5cab3eb..c699887 100755 --- a/clone/virt-sysprep.pod +++ b/clone/virt-sysprep.pod @@ -121,6 +121,17 @@ security problem with malicious guests (CVE-2010-3851). Change the hostname. See the L</hostname> operation below. If not given, defaults to C<localhost.localdomain>. +=item B<--ipconfig> "device1:ipaddr,netmask,gateway:device2:ipaddr,..." + +Change the IP configuration. See the L</ipconfig> operation below. +If not given, defaults to C<dhcp>. + +For example: + + --ipconfig "eth0:192.168.122.2,255.255.255.0,192.168.122.1:eth1:192.168.122.3,255.255.255.0,192.168.122.1" + +You can configure the IP of any existent net device in the guest. + =item B<--list-operations> List the operations supported by the virt-sysprep program. @@ -191,6 +202,14 @@ I<--hostname> parameter. If the I<--hostname> parameter is not given, then the hostname is changed to C<localhost.localdomain>. +=head2 ipconfig + +Changes the IP configuration of the guest to the value given in +the I<--ipconfig> parameter. + +If the I<--ipconfig> parameter is not given, then the IP configuration +all changed to C<dhcp>. + =head2 logfiles Remove many log files. -- 1.7.10.rc0
Richard W.M. Jones
2012-Mar-09 15:48 UTC
[Libguestfs] [PATCH V2] virt-sysprep:add ipconfig for preparation
On Fri, Mar 09, 2012 at 02:50:17PM +0800, Wanlong Gao wrote:> +#FIXME: This may not work OK for IPv6 ?:-) Does it work or not? Anyone??> +if [ "$ipconfig" = "yes" ]; then > + case "$type/$distro" in > + linux/fedora|linux/rhel) > + if [ -d $mnt/etc/sysconfig/network-scripts ]; then > + if [ -z $ipconfig_param ]; thenApparently you don't need to quote parameters in bash, but while this is technically correct, I think it is safer to quote it. There are a few other similar ones later in the code.> + for (( i=1; i<8 ; i+=2 )); do > + __device=$(echo "$ipconfig_param" | awk -v j=$i -F: '{print $j}') > + if [ -z $__device ]; then > + break > + fi > + __ipconfig=$(echo "$ipconfig_param" | awk -v j=$((i+1)) -F: '{print $j}') > + __config_file=$(echo "$mnt/etc/sysconfig/network-scripts/ifcfg-$__device") > + if [ -e $__config_file ]; then > + __ip=$(echo "$__ipconfig" | awk -F, '{print $1}') > + __mask=$(echo "$__ipconfig" | awk -F, '{print $2}') > + __gw=$(echo "$__ipconfig" | awk -F, '{print $3}') > + sed '/^IPADDR=/d;/^BOOTPROTO=/d;/^NETMASK=/d;/^GATEWAY=/d;/^DNS/d' \ > + < "$__config_file" > "$__config_file.new" > + echo "IPADDR=$__ip" >> "$__config_file.new" > + echo "BOOTPROTO=static" >> "$__config_file.new" > + echo "NETMASK=$__mask" >> "$__config_file.new" > + echo "GATEWAY=$__gw" >> "$__config_file.new" > + mv -f "$__config_file.new" "$__config_file" > + fi > + done > + fi > + fiI can't see any fault in this code. I wonder if there's an easier way, but maybe not, this is shell script after all ... Seems OK in general. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones New in Fedora 11: Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 70 libraries supprt'd http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
Reasonably Related Threads
- [RFC PATCH] virt-sysprep:add ipconfig for preparation
- [PATCH V3] virt-sysprep:add ipconfig for preparation
- [PATCH] sysprep: remove crash data generated by kexec-tools
- [PATCH] sysprep: remove the process accounting log files
- [PATCH 2/2] x86, paravirt: Fix bool return type for PVOP_CALL