Wanlong Gao
2012-Mar-14 12:41 UTC
[Libguestfs] [PATCH V3] virt-sysprep:add ipconfig for preparation
V1->V2: add the documentation. V2->V3: change the split sign from ":" to "@" for IPv6. 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 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
Wanlong Gao
2012-Mar-14 12:53 UTC
[Libguestfs] [PATCH V3] virt-sysprep:add ipconfig for preparation
On 03/14/2012 08:41 PM, Wanlong Gao wrote:> V1->V2: add the documentation. > V2->V3: change the split sign from ":" to "@" for IPv6. > > 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-6u1sorry, this should be: [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 Thanks, Wanlong Gao> > 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 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.
Richard W.M. Jones
2012-Mar-14 22:37 UTC
[Libguestfs] [PATCH V3] virt-sysprep:add ipconfig for preparation
On Wed, Mar 14, 2012 at 08:41:01PM +0800, Wanlong Gao wrote:> + if [ -z $ipconfig_param ]; thenThe quoting here is still (not actually) wrong, but I think we should quote it. See my previous comment: https://www.redhat.com/archives/libguestfs/2012-March/msg00048.html "Apparently 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." But, if you add the right quoting, this patch will be fine and I will push it. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v
Richard W.M. Jones
2012-Mar-16 18:31 UTC
[Libguestfs] virt-sysprep future (was: Re: [PATCH V5] virt-sysprep:add ipconfig for preparation)
What's the plan for virt-sysprep? It's an interesting proof-of-concept. People love it! But it has several shortcomings. As I said before, I think it should be spun off into a separate project, and possibly be rewritten (/bin/bash is a terrible language for writing complicated things). It really needs to support at least some Windows guests to some degree. Any thoughts on this? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/