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

Wanlong Gao
Sun, 04 Mar 2012 15:29:55 -0000

Hi Rich:
I just send a patch to request for your comments,
then will do further work about this.
things like this:

[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

Please comments.

Thanks
-Wanlong Gao

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 clone/virt-sysprep.in |   50 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/clone/virt-sysprep.in b/clone/virt-sysprep.in
index d505532..e4486d0 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)
--
1.7.9.2