forked from goffinet/virt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattach-nic.sh
executable file
·64 lines (57 loc) · 1.63 KB
/
attach-nic.sh
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
#!/bin/bash
# This script attach a live guest present NIC to a bridged interface
guest=$1
bridge=$2
type=bridge
parameters=$#
check_parameters () {
# Check the numbers of parameters required
if [ "$parameters" -ne 2 ] ; then
echo "Description : This script attach a live guest present NIC to a bridge"
echo "Usage : $0 <guest name> <bridge_interface_name>"
echo "Example : $0 guest1 virbr0"
echo " to attach the live guest1 NIC to virbr0"
exit
fi
# Check if the guest name chosen is in live and display help to choose
guests_defined_live="$(virsh list --name)"
if grep -qvw "$guest" <<< ${guests_defined_live} ; then
echo "Please provide a live guest name : exit"
echo "Guests available :"
echo "$(virsh list --name)"
exit
fi
# Check if the bridge interface is available
if grep -qvw "$bridge" <<< $(ls /sys/class/net) ; then
echo "This interface ${bridge} is not available"
echo "Please create a valid bridge or choose between : "
echo $(ls /sys/class/net)
exit
fi
}
check_mac_address () {
mac=""
while grep -qvw "$mac" <<< $(virsh domiflist $guest | tail -n +3 | head -n -1 | awk '{ print $5; }') ; do
virsh domiflist $guest
read -p "Please choose a mac address to attach : " mac
done
}
define_nic () {
cat << EOF > /tmp/vnic.$mac.xml
<interface type="$type">
<mac address="$mac"/>
<source bridge="$bridge"/>
<model type='virtio'/>
</interface>
EOF
}
attach_nic () {
# Detach and attach the guest nic to the live guest
virsh detach-device $guest /tmp/vnic.$mac.xml --live --persistent
virsh attach-device $guest /tmp/vnic.$mac.xml --live --persistent
virsh domiflist $guest
}
check_parameters
check_mac_address
define_nic
attach_nic