Novell Home

Developing YaST Modules

Novell Cool Solutions: Feature
By Thomas Yeung

Rate This Page

Reader Rating  stars  from 1 ratings

Digg This - Slashdot This

Posted: 21 Jun 2005
 

What is YaST?

YaST stands for Yet another Setup Tool. It is a management tool for installing and administrating SUSE Linux. This tool is designed to simplified the complexity of managing different aspects of SUSE Linux. It presents a simple interface (both GUI or Text based) to administrators for a particular task.




YaST consists of a list of modules. Each module is usually written in a YaST specific language called YCP (YaST Control Language). YCP is an interpreted language which interact with the low level YaST components and the underlying hardware. This means you can write a YCP module once and it will run on all the YaST supported platforms. The modularity nature of YaST makes it highly extensible.

Why develop YaST modules?

Developing YaST modules using YCP is relatively quick and simple. The infrastructure provides strong separation between the following:

  • The user interface
  • The functional code doing the job
  • The data representing the current state of the system

It provides a consistent look and feel to all management tasks. In addition, YaST implemented a workflow system which guides administrators through the necessary steps to complete a specific task. During installation the target system is thoroughly analyzed with respect to its hardware components and in most cases YaST provides intelligent suggestions. If the administrator is satisfied with these settings, he/she can simply accept them. Otherwise, each of the system configuration categories can be activated to be changed manually. This is where the workflow comes in.

If the administrator decides to change a specific configuration, workflows will usually guide him/her through the steps necessary to accomplish a specific task. The steps are generally small to avoid an information overflow. Sometimes, a workflow has multiple branches for "novice" and "expert" modes. The novice mode fills in the default values and tries to determine as much information as possible. The expert mode offers full control to all the input values. It makes administration a whole lot easier.

YaST Architecture

A YaST module is comprised of three parts: User Interface, YaST Module and System Configuration Repository. As stated above, one the design goals with YaST is the logical and physical separation of user interface and functionality. Currently, all the UI related functions are all contained in the UI-API library. The API set defines dialogs, display data and get data from the user. Dialogs defined in UI-API can be displayed in the X11-based graphical user interface as well as in the console based Ncurses mode.

The actual logic will be responsible to interface user inputs, different installation sources (CD, FTP etc) and the SCR (System Configuration Repository). It also handles the workflow aspect and provides intelligent suggestions to administrator.

SCR consists of a conglomeration of various SCR agents for different tasks and a binary core component (the actual SCR) that implements the accessing logic for these agents. Agents are rather independent pieces of code that implement a specific functionality, e.g. accessing a configuration file. From a certain point of view they provide services that can be used from elsewhere in the system, mostly from YCP code. Since the services of the agents have to be used without knowing exactly how the underlying logic, there needs to be a standardized way to access those services. This is what the SCR core component does. It manages the agents that are part of the whole SCR layer and provides the SCR API to access them.

In simple terms, the agents "register" themselves with the SCR and identity an unique namespace between different agents. The following is a simple example:

<...>
usb_devices = SCR::Read(.probe.usb);
<...>
SCR::Write(.sysconfig.CHECK_INITTAB, "yes");
<...>
SCR::Execute(.target.bash, "mkdir -p /var/lib/foo");
<...>
inittab = SCR::Dir(.etc.inittab);
<...>

As was the case with the UI the SCR:: here again is a name space selector which directs the following command to the SCR. The first line gets a list of system's USB devices by using the hardware agent. The second line writes an identifier CHECK_INITTAB into /etc/sysconfig via the ini-agent. In the third line the system-agent executes a bash with a specific command and finally in the forth line the content of /etc/inittab is read into a local variable.

The YaST Language – YCP

YaST supports multiple programming languages. However, YCP is a scripting language and it is the preferred language for writing YaST modules. There is a compiler to compile YCP code into byte code. When an administrator runs a YCP program, the bytecode of the program is interpreted by the YaST engine. YCP has the usual features of procedural language and some from functional programming paradigm:

  • Control structures like if/then/else, foreach-loops
  • Compound data types like strings, lists and maps
  • Function definition
  • Variable scopes
  • Name spaces
  • Module import and include
  • UNIX command execution (via SCR)
  • Functions as parameters

Hello World! Sample

/**
 * File:    client/hello.ycp
 * Module:  hello world configuration
 * Summary: display hello world message
 * Author:  tyeung 
 * $Id: hello.ycp
 */
{
string message = "Hello, World!";

UI::OpenDialog(
		'Vbox(
	  'Label( message ),
 	  'PushButton("&OK")
 	  	)
);
UI::UserInput();
UI::CloseDialog();
}

The first part of the code is the comment part. It indicates the file name, details of the module, summary, author, email and ID information. The first line of the code defines a string variable called "message". In YCP, any variable definition must have a default value. This avoid any errors that might occur due to uninitialized variable.

The UI::{ defines the beginning of a dialog which needs to be displayed in the UI. The open curly bracket that follows immediately, opens a block in YCP. Blocks are used to "glue" several YCP statements together to form an entity that can be handled just like a single statement. Consequently, the leading UI:: directs the whole block with all the content to the UI where it is executed.

The OpenDialog command opens a dialog on screen. Since it is a UI specific command, it is valid only in the UI context, i.e. It must be proceeded with UI:: command. This parameter determines the content of the dialog.

'Vbox( is a statement related to the geometry of the dialog to be defined. As the name indicates it opens a (virtual) vertical box that display all content in a column wise manner. The leading back quote introduces a YCP feature that stems from the functional programming paradigm (http://en.wikipedia.org/wiki/Functional_programming). In YCP language, the 'VBox() is a term, i.e. a data object that can be executed. Terms can be passed to functions as parameters as is done here with Opendialog(). Since 'VBox() has to be evaluated in the UI, we must prevent the interpreter from evaluating this symbol. This is what the back quote does.

'Label( message ), displays strings in UI. This statement gets one parameter, the string variable we defined at the beginning. Because it is the first of two parameters passed to 'VBox() this line is not terminated with a semicolon but with a comma. As in most programming languages commas are used to separate parameters in YCP.

'PushButton("&OK") statement display a labeled push button. Since it is the next element in the enclosing 'VBox(), it is displayed immediately below the preceding label. The & in the label string is a YaST feature declaring the subsequent character to be a key shortcut. As a result, the button can not only be clicked with the mouse but also be activated by typing ALT-O.

) and ); first close the open 'VBox() and then the open OpenDialog(). Because 'VBox() is passed as a parameter to OpenDiaglog() there is no need to terminate the statement with a semicolon. OpenDialog() on the other hand is a statement in the UI and hence must be terminated with a semicolon.

UserInput(); hands over control to the UI which then awaits some sort of user input. In this case, it simply waits for the push button to be pressed by the user. Consequently, the above program halts at this point until the user really does it.

CloseDialog(); removes the dialog when UserInput() returns.

} indicates the end of the block passed to the UI, the closing curly bracket ends the program.

YaST Development environment setup

You can develop YaST module using different SUSE Linux distributions e.g. SUSE 9.2 Professional. The following are the required packages to be installed for YaST module development:

  • yast2-devtools – ycp compiler, automake/autoconf, helper script package
  • yast2-core-devel – documentation for system agents
  • yast2-devel – library documentation
  • yast2-testsuite – support for automatic testsuites

YCP Compiler

ycpc is a ycp bytecode compiler. It compiles YCP code into bytecode and perform syntax checking.

Usage:
 ycpc [-h] [--help]
  ycpc [-v] [--version]
  ycpc <command> [<option>]... <filename>...
  Commands:
     -c, --compile compile to bytecode
  -E, --fsyntax-only check syntax and print (unless -q)
  -f, --freshen freshen .ybc files
  -p, --print read and print bytecode
  -r, --run read and run bytecode
  Options:
  -l, --log-file <name> log file, - means stderr      
  -q, --quiet no output
  -t, --test more output (-tt, -ttt)
  -d, --no-implicit-imports don't preload implicit namespaces
  -F, --Force force recompilation of all dependant files
  -I, --include-path where to find include files
  -M, --module-path where to find module files
  --no-std-includes drop all built-in include paths
  --no-std-modules drop all built-in include paths
  -n, --no-std-paths no standard paths
  -o, --output output file for -c, -E, -p, -r
  -R, --recursive operate recursively
  -u, --ui {ncurses|qt} UI to start in combination with 'r'

Using the above example (hello.ycp), we can compile the YCP file into a ybc file. The following are some of switches for the ycpc compiler:

ycpc -Eq hello.ycp
   
  • The -E switch checkes YCP syntax only. It has the similar behaviour to the -E parameter for the GNU gcc compiler. -E is typically used together with -q to check for syntax but not to print out the progress report.
  •  
    ycpc -c hello.ycp
       
  • It compiles the YCP file into a bytecode file (ybc file). As a parameter, YCP file must be provided, the corresponding output file is automatically named with the ycp to ybc extension changed.
  • To test the YCP program, we can use the command line version of YaST called y2base. It is located in the /usr/lib/YaST2/bin directory. You can run y2base in both text based (using ncurses) or graphical UI (qt):

    By executing /usr/lib/YaST/bin/y2base hello.ycp qt, it will execute hello.ycp in UI format.

    Alternatively, by executing /usr/lib/YaST/bin/y2base hello.ycp ncurses, it will run hello.ycp in text based format.


    Creating a skeleton project using y2tool

    y2tool is a YaST developer tool which assists YaST developers with the following:

    • Create skeleton YaST project
    • Automake
    • Autoconfig
    • Create YaST rpm package
    • Checkin YaST package to CVS
    • Check YCP syntax
    • Compile YaST project

    Using the "create-new-package", developers can create new YaST projects with all the necessary templates and support files.

    Usage:
    y2tool create-new-package [-dsv] <skeleton> <name> <maintainer> <email>
    <skeleton> - Type of project to be created.
    <name> - Name of the YaST project
    <maintainer> - Maintainer's named
    <email> - Maintainer's email

    Here is an example:

    y2tool create-new-package config test tyeung tyeung@novell.com

    Once a skeleton project is created we can start to modify the source file in the src directory.

    References

    For more information on YCP syntax and YaST architecture, please visit the following websites:

    http://yast2.suse.com

    Eventually, YaST will be moved to the Novell Forge, http://forge.novell.com for YaST development and other YaST related projects.

    If you have further questions regarding the YaST development, please send an email to yast2-hack@suse.de.


    Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

    Novell® Making IT Work As One

    © 2009 Novell, Inc. All Rights Reserved.