add on/off commands
[ktest-util.git] / vzt-pxe-boot
blob99a3a78615deef66a401cf2752cc0ad76771cd3e
1 #! /bin/sh -x
2 function show_usage
4 echo "$0: <host> <set/del> [BOOT_OPTS]"
6 . /etc/vzt/vzt-pxe-boot.cfg
7 EXIT_FAILURE=1
8 EXIT_NOENT=2
9 #PXE_KERNEL_DIR
10 #PXE_CONFIG_DIR
11 host=$1
12 action=$2
13 kernel_img=$3
14 initrd_img=$4
15 boot_opts=$5
16 title_msg=$6
18 ip=`gethostip $host | gawk '{ print \$1 }'` || exit 1
19 hex_ip=`gethostip $host | gawk '{ print \$3 }'` || exit 1
21 function change_pxe(){
22 local config="$1"
23 local fatal="$3"
24 PATT=$2
25 PATT="$(echo $PATT | sed -r 's/\//\\\//g')"
26 local default=$( cat $config | awk "{if (\$1 ~ /label/) point=\$2; if ((\$1 ~ /kernel/) && (\$2 ~ /$PATT/)) print point }")
28 [ -n "$default" ] || {
29 if [ $fatal -ne 0 ]; then
30 echo "No pattern found in $config"
31 exit $EXIT_NOENT
32 else
33 return $EXIT_NOENT
36 echo $default
37 [ $(echo "$default" | wc -w) -eq 1 ] || {
38 echo "Pattern found several times in $config"
39 exit $EXIT_FAILURE
42 sed -i "s/default .*/default $default/" $config
44 return $?
47 function add_entry_pxe()
49 local config=$1
50 local kernel_img="$2"
51 local kernel_opts="$3"
52 local initrd_img="$4"
53 local title_text="$5"
54 grep "label $title_text" $config
55 if [ $? -eq 0 ];then
56 echo "Err: label $title_text already present in: $config"
57 grep -n -A5 "label $title_text" $config
58 exit 1
60 echo " "
61 echo "#$title_text" >> $config
62 echo "label $title_text" >> $config
63 echo " kernel $kernel_img " >> $config
64 echo " append initrd=$initrd_img $kernel_opts" >> $config
65 return $?
68 function setup_pxe_config
70 local conf="$1"
71 local kernel_img="$2"
72 local initrd_img="$3"
73 local kernel_opts="$4"
74 local title_text="$5"
75 local kpattern=$kernel_img
77 if [ ! -f $PXE_ROOT/$kernel_img ]; then
78 echo "kernel image: $PXE_ROOT/$kernel_img not found "
79 exit 1
81 if [ ! -f $PXE_ROOT/$initrd_img ]; then
82 echo "initrd image: $PXE_ROOT/$initrd_img not found"
83 exit 1
86 change_pxe $conf $kpattern 0
87 if [ $? -eq 2 ]; then
88 echo "try to add new entry: $ $kpattern"
89 add_entry_pxe $conf "$kernel_img" "$kernel_opts" "$initrd_img" "$title_text"
90 change_pxe $conf $kpattern 1
91 if [ $? -eq 0 ]; then
92 grep default $conf
93 echo " "
94 tail $conf
95 echo "****************************************"
96 echo "*** Kernel was successfully installed ***"
97 echo "****************************************"
98 else
99 echo "*** Error while add new entry ***"
100 echo "config:$conf may be corrupted, plese check it"
101 exit 1
103 else
104 grep default $conf
105 echo "****************************************"
106 echo "*** Kernel was successfully selected ***"
107 echo "****************************************"
111 case "$action" in
112 del ) rm -rf $PXE_ROOT/$PXE_CONFIG_DIR/$hex_ip;;
113 add ) if [ ! -f $PXE_ROOT/$PXE_CONFIG_DIR/$ip ];then
114 echo "$PXE_ROOT/$PXE_CONFIG_DIR/$ip not exist,"
115 echo "create new config based on: $PXE_ROOT/$PXE_CONFIG_DIR/default"
116 cp $PXE_ROOT/$PXE_CONFIG_DIR/default $PXE_ROOT/$PXE_CONFIG_DIR/$ip
118 setup_pxe_config "$PXE_ROOT/$PXE_CONFIG_DIR/$ip" "$PXE_KERNEL_DIR/$kernel_img" "$PXE_KERNEL_DIR/$initrd_img" "$boot_opts" "$title_msg" || exit 1;
119 rm -rf $PXE_ROOT/$PXE_CONFIG_DIR/$hex_ip ;
120 ln -s $ip $PXE_ROOT/$PXE_CONFIG_DIR/$hex_ip ;;
121 * ) echo "Unknown action";
122 show_usage;
123 exit 1;;
124 esac