Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VnH7e-0004lO-1b for mharc-grub-devel@gnu.org; Sun, 01 Dec 2013 19:12:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41976) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VnH7V-0004k5-EL for grub-devel@gnu.org; Sun, 01 Dec 2013 19:12:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VnH7P-0003UO-O8 for grub-devel@gnu.org; Sun, 01 Dec 2013 19:12:13 -0500 Received: from smtps.usherbrooke.ca ([132.210.244.51]:54864) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VnH7P-0003Tw-FL for grub-devel@gnu.org; Sun, 01 Dec 2013 19:12:07 -0500 Received: from xps (modemcable053.91-56-74.mc.videotron.ca [74.56.91.53]) (authenticated bits=0) by smtps.usherbrooke.ca (8.13.8/8.13.8) with ESMTP id rB20Bgq3019360 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Sun, 1 Dec 2013 19:11:42 -0500 Message-ID: <6ED1B54066274C3FB1D6D7611122D749@xps> From: "Michel Benoit" To: "The development of GNU GRUB" Subject: Proposition for Add-on Date: Sun, 1 Dec 2013 19:11:53 -0500 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_008A_01CEEEC9.323D04E0" X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Mailer: Microsoft Windows Live Mail 16.4.3505.912 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3505.912 X-UdeS-MailScanner-Information: X-MailScanner-ID: rB20Bgq3019360 X-UdeS-MailScanner: Aucun code suspect =?ISO-8859-1?Q?d=E9tect=E9?= X-MailScanner-SpamCheck: n'est pas un polluriel, SpamAssassin (not cached, score=-3.003, requis 5, ALL_TRUSTED -1.80, BAYES_00 -2.60, MIME_QP_LONG_LINE 1.40) X-UdeS-MailScanner-From: michel.benoit@usherbrooke.ca X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 132.210.244.51 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Dec 2013 00:12:19 -0000 Ce message est composi et au format MIME. ------=_NextPart_000_008A_01CEEEC9.323D04E0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by smtps.usherbrooke.ca id rB20Bgq3019360 Hi! I=E2=80=99ve d develop an internal command for grub to help the administr= ators of=20 pool of dual boot machine. The problem: We have class with near 100 computers in dual boot, under=20 windows we have a maintenance period in the night to automatically instal= l=20 update. For that, in grub we put Windows has the default menu-entry. The only time the computers are in linux, is when a student is using it.=20 It's not a good time to install update. To install update, security patch= ,=20 or other package, i was forced to pass computer by computer. To get a period for the maintenance in linux, i wrote a simple module for= =20 grub that select the default menu-entry depending of the time of the day. ex. : I set the period from 4am to 6am with the default boot to linux. So i can put a script in the crontab at 4h05 to install the update. (you have to put a shutdown -r command in Windows task scheduler at 4= .00=20 am. And use a wake on lan for computer that are down) Is it possible to integrate in in the distribution of grub, if you think = it=20 could help other user. -------------------------------------------------------------------------= -------------------------------------------------------------------------= ----------- The principal module grub-core/commands/select_by_time.c The configuration file, two option the simple version /etc/grub.d/98_select_by_time In 98_select_by_time, you select the period of time a= nd=20 the menuentry for that period, you can use many period of time. It use th= e=20 24 hours time format notation without colon. ex: # from 4 am to 6 am select item 3 as default select_by_time 0400 0600 3 # and from 11h25 pm to midnight select item 5 as default select_by_time 2325 2400 5 or the more complete /etc/grub.d/99_select_by_time, that use the con= fig=20 file /boot/grub/select_by_time.cfg With this version you don't have to know the position of the=20 operating system in grub.cfg (menuitem). exemple of select_by_time.cfg select_default Windows select_by_time 5:00 6:00 ubuntu # maintenance perio= d select_by_time 13:00 15:00 ubuntu # linux course select_by_time 19:00 2308 "Linux 3.2.0-23-generic" # speci= fic =E2=80=93----------------------------------------------------------------= ---------------------- Right now, we are using select_by_time with 98_select_by_time, and we sav= e a=20 lot of days of work. If you think it could be helpful for other administrator, could you put i= t=20 in a future grub distribution (the source code is very simple). The 99_select_by_time is optional, it's just more easy to use. Thank's ------------------------------------------------------ Michel Benoit (morph1853) Technicien Syst=C3=A8mes Ordin=C3=A9s Dep. Informatique Universit=C3=A9 de Sherbooke michel.benoit@usherbrooke.ca ------=_NextPart_000_008A_01CEEEC9.323D04E0 Content-Type: application/octet-stream; name="98_select_by_time" Content-Disposition: attachment; filename="98_select_by_time" Content-Transfer-Encoding: quoted-printable #!/bin/sh=0A= exec awk '$1 ~ /select_by_time/ {print $1,$2,$3,$4;}' $0=0A= =0A= # select_by_time start_time stop_time menu choice=0A= #=0A= # exemple between 4h00am to 6h00am select the third menu in grub = (menuitem start at 0) =0A= # select_by_time 400 600 2=0A= # Module to select default entry by time=0A= =0A= select_by_time 0400 0600 2 # linux maintenance period =0A= select_by_time 2000 2200 4=0A= =0A= ------=_NextPart_000_008A_01CEEEC9.323D04E0 Content-Type: application/octet-stream; name="99_select_by_time" Content-Disposition: attachment; filename="99_select_by_time" Content-Transfer-Encoding: quoted-printable #!/bin/sh=0A= # Module to select default entry by time=0A= =0A= =0A= # Validation and Transformation of the time format=0A= decript () { =0A= # remove : h AM PM=0A= value=3D` echo $1 | tr -d '[:a-zA-Z]'`=0A= pm=3D` echo $1 | tr '[:upper:]' '[:lower:]' | tr -d '[:h0-9]' = ` =0A= =0A= # data format validation =0A= to_validate=3D` echo $1 | tr '[:upper:]' '[:lower:]' `=0A= case "$to_validate" in # valide format=0A= [0-9][0-5][0-9]) ;; # 700=0A= [0,1][0-9][0-5][0-9]) ;; # 1700=0A= [2][0-4][0-5][0-9]) ;; # 1700=0A= [0-9][h:][0-5][0-9]) ;; # 7:00 7h00=0A= [0,1][0-9][h:][0-5][0-9]) ;; # 19:00 19h00=0A= [2][0-4][h:][0-5][0-9]) ;; # 21:00 22h00=0A= =0A= [0-9][0-5][0-9][a,p]m) ;; # 700am=0A= [0,1][0-9][0-5][0-9][a,p]m) ;; # 1700pm=0A= [2][0-4][0-5][0-9][a,p]m) ;; # 2300pm=0A= [0-9][h:][0-5][0-9][a,p]m) ;; # 7:00pm 7h00am=0A= [0,1][0-9][h:][0-5][0-9][a,p]m) ;; # 19:00pm 19h00pm=0A= [2][0-4][h:][0-5][0-9][a,p]m) ;; # 21:00pm 22h00pm=0A= =0A= *) # echo "Invalide format" >&2=0A= return 1 =0A= ;;=0A= esac=0A= if [ "$pm" =3D "pm" ]=0A= then=0A= if [ "$value" -lt "1200" ] ; then value=3D`expr $value + 1200 ` ; fi =0A= fi =0A= if [ "$value" -gt "2400" ] ; then return 1 ; fi #echo not in range, = only 24h by day=0A= echo $value # echo OK=0A= return 0=0A= }=0A= =0A= # find choice of menuentry in grub.cfg=0A= # by item number=0A= # or by description (ex.: Ubuntu, Debian, Windows, ... )=0A= find_menu_item () {=0A= if [ ! -z "$1" ] ; then=0A= validate=3D` echo $1 | sed 's/[^0-9]*//g' `=0A= if [ "$1" =3D "$validate" ] ; then=0A= # the choice is a number=0A= if [ $1 -gt $last_menu ] ; then return 1 ; fi=0A= choice=3D$1=0A= else=0A= # the choice in alphanumeric, patch choice = beginning by -=0A= search=3D`echo $1 | sed "s/^-/\\\\\-/" `=0A= choice=3D`egrep "^menuentry" = $GRUB_PREFIX/$MENU_FILE_CONVERT | nl -v0 -w1 -s\: | egrep -m1 -i = "$search" | cut -d\: -f1 `=0A= # text selected not in menuentrys=0A= if [ "$choice" =3D "" ] ; then return 1 ; fi=0A= fi=0A= echo $choice # first entry corresponding=0A= return 0=0A= fi=0A= }=0A= =0A= select_by_time () { =0A= =0A= if [ $# !=3D 3 ] ; then =0A= echo "select_by_time $* : syntaxe error " >&2 =0A= echo " Select_by_time start_time end_time menu_choice " >&2 =0A= return=0A= fi=0A= start_time=3D`decript "$1" `=0A= if [ $? =3D 1 ] ; then echo "Select_by_time $* \t: Invalid format $1 " = >&2 ; return ; fi =0A= stop_time=3D`decript "$2" `=0A= if [ $? =3D 1 ] ; then echo "Select_by_time $* \t: Invalid format $2 " = >&2 ; return ; fi =0A= menu_item=3D`find_menu_item "$3" `=0A= if [ $? =3D 1 ] ; then echo "Select_by_time $* \t: $3 not available " = >&2 ; return ; fi =0A= =0A= if [ $start_time -ge $stop_time ] ; then echo "Select_by_time $* \t: = Stop time is before start time " >&2 ; return ; fi=0A= =0A= echo select_by_time $start_time $stop_time $menu_item=0A= }=0A= =0A= =0A= select_default () { =0A= =0A= if [ $# !=3D 1 ] ; then =0A= echo "select_default $* : syntaxe error " >&2 =0A= echo " select_default menu_choice " >&2 =0A= return=0A= fi=0A= menu_item=3D`find_menu_item "$1" `=0A= if [ $? =3D 1 ] ; then return ; fi =0A= echo default=3D$menu_item=0A= }=0A= =0A= # extract menuentry from grub.cfg.new, taking care of "previous version" = item=0A= convert_grub_cfg () {=0A= if [ -f $GRUB_PREFIX/$MENU_FILE_CONVERT ] ; then rm -f = $GRUB_PREFIX/$MENU_FILE_CONVERT ; fi =0A= menu_count=3D0=0A= bracket_count=3D0=0A= sed "s/[{}]/\n&\n/" "$GRUB_PREFIX/$MENU_FILE" | egrep = "}|{|menuentry|submenu" | while read line =0A= do=0A= bcount_open=3D`echo $line | grep -o '{' | wc -w`=0A= bcount_close=3D`echo $line | grep -o '}' | wc -w`=0A= bracket_count=3D` expr $bracket_count + $bcount_open - = $bcount_close `=0A= tmp=3D`echo $line | egrep -ic "^menuentry"`=0A= if [ $? =3D 0 ] ; then=0A= if [ $bracket_count =3D 0 ] ; then=0A= sub_count=3D0=0A= echo "$line" >> = $GRUB_PREFIX/$MENU_FILE_CONVERT=0A= else=0A= if [ $bracket_count =3D 1 ] ; then # inside a = submenu=0A= if [ $sub_count =3D 0 ] ; then=0A= sub_count=3D1=0A= echo "$line" >> = $GRUB_PREFIX/$MENU_FILE_CONVERT=0A= fi=0A= fi=0A= fi=0A= fi=0A= done=0A= }=0A= =0A= =0A= # main()=0A= =0A= MENU_FILE=3D"grub.cfg.new"=0A= MENU_FILE_CONVERT=3D"select_by_time_menu_grub"=0A= USER_CHOICE=3D"select_by_time.cfg"=0A= if [ ! -e "$GRUB_PREFIX/$MENU_FILE" ] ; then DEBUG=3D1 ; fi=0A= if [ $DEBUG ] ; then =0A= # set -x =0A= GRUB_PREFIX=3D/boot/grub=0A= MENU_FILE=3Dgrub.cfg=0A= fi=0A= convert_grub_cfg # extract menuentry from grub.cfg.new, taking care = of "previous version" item=0A= =0A= last_menu=3D`egrep "^menuentry" $GRUB_PREFIX/$MENU_FILE_CONVERT | nl = -v0 -w1 -s\: |tail -1 |cut -d\: -f1`=0A= if [ "$last_menu" =3D "" ] ; then return; fi # file not containing = menuentry=0A= =0A= if [ -f "$GRUB_PREFIX/$USER_CHOICE" ] ; then=0A= # traitement=0A= # read user file=0A= =0A= egrep -i "select_by_time|select_default" = "$GRUB_PREFIX/$USER_CHOICE" | awk -F"#" '{print $1}' | tr '[:upper:]' = '[:lower:]' | while read line=0A= do=0A= command=3D`echo $line | awk -F" " '{print $1}'`=0A= argument=3D`echo $line | awk -F" " '{$1=3D""; print }'`=0A= eval set -- "$argument"=0A= if [ "$command" =3D "select_by_time" ] ; then select_by_time "$@" ; fi=0A= if [ "$command" =3D "select_default" ] ; then select_default "$@" ; fi =0A= done=0A= fi =0A= =0A= ###################################################=0A= # Clean temporary file=0A= ###################################################=0A= if [ -f $GRUB_PREFIX/$MENU_FILE_CONVERT ] ; then rm -f = $GRUB_PREFIX/$MENU_FILE_CONVERT ; fi =0A= =0A= =0A= #########################################################################= ##################################=0A= # time selection in /boot/grub/select_by_time.cfg=0A= #########################################################################= ##################################=0A= # select_default must be before select_by_time=0A= # exemple=0A= # select_default Windows=0A= # select_by_time 5:00 6:00 ubuntu # maintenance period=0A= # select_by_time 13:00 15:00 ubuntu # linux course=0A= # select_by_time 19:00 2308 "Linux 3.2.0-23-generic" # specific =0A= # select_by_time 19:00 2302 "recovery mode" =0A= =0A= =0A= =0A= =0A= ------=_NextPart_000_008A_01CEEEC9.323D04E0 Content-Type: application/octet-stream; name="select_by_time.c" Content-Disposition: attachment; filename="select_by_time.c" Content-Transfer-Encoding: quoted-printable /* select_by_time.c - command to choice menu item depanding of the time = of the day. */=0A= /*=0A= * idea of: Michel.benoit@USherbrooke.ca (morph1853)=0A= */=0A= =0A= #include =0A= #include =0A= #include =0A= #include =0A= #include =0A= =0A= GRUB_MOD_LICENSE ("GPLv3+");=0A= =0A= static grub_err_t=0A= grub_cmd_select_by_time (grub_command_t cmd __attribute__ ((unused)),=0A= int argc, char **args)=0A= {=0A= struct grub_datetime datetime;=0A= =0A= if (argc !=3D 3)=0A= {=0A= grub_printf (" Select default boot by time of the day \n");=0A= grub_printf (" usage: select_by_time hhmm HHMM number_of_menuitem \n");=0A= grub_printf (" between the time hhmm and HHMM use number_of_menuitem as = default choice \n");=0A= =0A= return 0;=0A= }=0A= =0A= char *s;=0A= int start_time, stop_time, current_time ;=0A= if (grub_get_datetime (&datetime))=0A= return grub_errno;=0A= current_time=3D (datetime.hour * 100) + datetime.minute;=0A= =0A= /* convert argument, and validate that they contain only number, arg0 = -> start_time, arg1 -> stop_time, arg2 -> menu choice */ =0A= start_time =3D grub_strtoul (args[0], &s, 10);=0A= if ( grub_errno !=3D 0 || *s)=0A= return grub_error (GRUB_ERR_BAD_ARGUMENT, "start time invalid format"); =0A= =0A= stop_time =3D grub_strtoul (args[1], &s, 10);=0A= if ( grub_errno !=3D 0 || *s)=0A= return grub_error (GRUB_ERR_BAD_ARGUMENT, "stop time invalid format"); =0A= =0A= grub_strtoul (args[2], &s, 10);=0A= if ( grub_errno !=3D 0 || *s)=0A= return grub_error (GRUB_ERR_BAD_ARGUMENT, "menu choice not valid"); =0A= =0A= if ((current_time >=3D start_time) && (current_time <=3D stop_time))=0A= grub_env_set ("default",args[2]);=0A= return 0;=0A= }=0A= =0A= static grub_command_t cmd;=0A= =0A= GRUB_MOD_INIT(select_by_time)=0A= {=0A= cmd =3D=0A= grub_register_command ("select_by_time", grub_cmd_select_by_time,=0A= N_("hourminute hourminute menuitem"),=0A= N_("Set the default menu item by time."));=0A= }=0A= =0A= GRUB_MOD_FINI(select_by_time)=0A= {=0A= grub_unregister_command (cmd);=0A= }=0A= =0A= ------=_NextPart_000_008A_01CEEEC9.323D04E0 Content-Type: application/octet-stream; name="select_by_time.cfg" Content-Disposition: attachment; filename="select_by_time.cfg" Content-Transfer-Encoding: quoted-printable #########################################################################= ##################################=0A= # select_by_time.cfg: time selection =0A= #########################################################################= ##################################=0A= # select_default must be before select_by_time=0A= #=0A= # exemple=0A= # select_default Windows=0A= # select_by_time 5:00 6:00 ubuntu # maintenance period=0A= # select_by_time 13:00 15:00 ubuntu # linux course=0A= # select_by_time 19:00 2308 "Linux 3.2.0-23-generic" # specific =0A= # select_by_time 19:00 2302 "recovery mode" =0A= =0A= ------=_NextPart_000_008A_01CEEEC9.323D04E0--