[Libguestfs] [PATCH V5] virt-sysprep:add ipconfig for preparation

Wanlong Gao gaowanlong at cn.fujitsu.com
Thu Mar 15 01:44:39 UTC 2012


V1->V2: add the documentation.
V2->V3: change the split sign from ":" to "@" for IPv6.
V3->V4: add the param quotes
V4->V5: add a missing pair of quotes

Add the the ipconfig for vir-prep.

Usage like below:

[root at Allen ~]# virt-sysprep --ipconfig="eth0 at 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  |   50 +++++++++++++++++++++++++++++++++++++++++++++++-
 clone/virt-sysprep.pod |   19 ++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/clone/virt-sysprep.in b/clone/virt-sysprep.in
index d505532..a57b5e4 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,48 @@ if [ "$hostname" = "yes" ]; then
     esac
 fi
 
+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..4278ab9 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 at ipaddr,netmask,gateway at device2@ipaddr,..."
+
+Change the IP configuration. See the L</ipconfig> operation below.
+If not given, defaults to C<dhcp>.
+
+For example:
+
+  --ipconfig "eth0 at 192.168.122.2,255.255.255.0,192.168.122.1 at 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




More information about the Libguestfs mailing list