ACC SHELL

Path : /usr/share/YaST2/modules/
File Upload :
Current File : //usr/share/YaST2/modules/SystemSettings.ycp

/**
 *
 * Module:	Set Kernel and System Settings
 *
 * Author:	Lukas Ocilka <locilka@suse.cz>
 *
 * $Id: SystemSettings.ycp 51831 2008-10-03 11:06:09Z lslezak $
 *
 * This module manages the System and Kernel settings such as I/O Scheduler type,
 * Serial Console, SysRq Keys...
 */

{
    module "SystemSettings";
    textdomain "tune";

    import "Service";
    import "Mode";

    /* Internal Data */
    string ENABLE_SYSRQ = nil;

    string elevator = nil;
    /* Internal Data */

    boolean modified = false;

    global list<string> GetPossibleElevatorValues () {
	    // here are listed all known values of the 'elevator' variable
	    return ["cfq","as","noop","deadline"];
    }

    global boolean Modified()
    {
	y2milestone("Modified: %1", modified);
	return modified;
    }

    global define boolean Read () {
	ENABLE_SYSRQ = (string) SCR::Read(.sysconfig.sysctl.ENABLE_SYSRQ);
	y2milestone("SysRq enabled: %1", ENABLE_SYSRQ);

	string current_sysrq = (string)SCR::Read(.target.string, "/proc/sys/kernel/sysrq");

	// read just the first line
	current_sysrq = splitstring(current_sysrq, "\n")[0]:"";

	if (current_sysrq != ENABLE_SYSRQ)
	{
	    y2warning("SysRq mismatch: sysconfig value: '%1', current: '%2'", ENABLE_SYSRQ, current_sysrq);
	}

        /*
	 * I have to admit that this is very ugly but it is here
	 * to avoid of the very long starting time of the yast module
	 * because the Storage module (which is imported by the Bootloader (imported by the SystemSettings module))
	 * has a Read() function call in its constructor.
	 */
	import "Bootloader";

	if (Mode::normal())
	{
	    // runtime - read the settings
	    Bootloader::Read();
	}

	// get 'elevator' option from the default section
	any elevator_parameter = Bootloader::getKernelParam(Bootloader::getDefaultSection(), "elevator");

	y2milestone("elevator_parameter: %1", elevator_parameter);

	// Variable is not set
	if (elevator_parameter == false || elevator_parameter == "false") {
	    elevator = "";
	// Variable is set but has not parameter
	} else if (elevator_parameter == true || elevator_parameter == "true") {
	    y2warning("'elevator' variable has to have some value");
	    elevator = "";
	// Variable is set but hasn't any known value
	} else if (!contains(GetPossibleElevatorValues(), (string) elevator_parameter)) {
	    y2warning("'elevator' variable has to have a value from %1 instead of being set to %2", GetPossibleElevatorValues(), elevator_parameter);
	    elevator = "";
	// Variable is OK
	} else {
	    elevator = (string) elevator_parameter;
	}

	y2milestone("Global IO scheduler: %1", elevator);

	// FIXME: probe available serial line devices

	return true;
    }

    global boolean Activate()
    {
	// activate the SysRq setting
	string proc_value = "";

	if (ENABLE_SYSRQ == "yes")
	{
	    proc_value = "1";
	}
	else if (ENABLE_SYSRQ == "no")
	{
	    proc_value = "0";
	}
	else if (regexpmatch(ENABLE_SYSRQ, "^[0-9]*$"))
	{
	    proc_value = ENABLE_SYSRQ;
	}
	else
	{
	    y2warning("Unknown ENABLE_SYSRQ value: %1, not activating", ENABLE_SYSRQ);
	}

	if (proc_value != "")
	{
	    y2milestone("Activating SysRq config: %1", proc_value);
	    SCR::Execute(.target.bash, sformat("echo '%1' > /proc/sys/kernel/sysrq", proc_value));
	}

	if (elevator != nil)
	{
	    import "Bootloader";
	    string new_elevator = (elevator == "") ? "false" : elevator;

	    y2milestone("Activating scheduler: %1", new_elevator);
	    // set the scheduler
	    Bootloader::setKernelParam(Bootloader::getDefaultSection(), "elevator", new_elevator);

	    // TODO FIXME: set the scheduler for all disk devices,
	    // reboot is required to activate the new scheduler now
	}

	return true;
    }


    global define boolean Write () {
	// writing SysRq settings
	if (ENABLE_SYSRQ != nil && (ENABLE_SYSRQ == "yes" || ENABLE_SYSRQ == "no" || regexpmatch(ENABLE_SYSRQ, "^[0-9]*$")))
	{
	    // save the SysRq setting
	    y2milestone("Saving ENABLE_SYSRQ: %1", ENABLE_SYSRQ);
	    SCR::Write(.sysconfig.sysctl.ENABLE_SYSRQ, ENABLE_SYSRQ);
	    SCR::Write(.sysconfig.sysctl, nil);
	}
	else
	{
	    y2warning("Not writing invalid ENABLE_SYSRQ value: %1", ENABLE_SYSRQ);
	}

	// enable boot.proc service which sets the value after boot
	Service::Enable("boot.proc");

	// the bootloader configuration is written at the end of the first stage
	if (Mode::normal())
	{
	    // write the elevator setting
	    import "Bootloader";
	    Bootloader::Write();
	}

	// FIXME: write serial console...
	return true;
    }

    // Kernel param 'elevator'
    global define string GetIOScheduler () {
	return elevator;
    }

    global define void SetIOScheduler (string io_scheduler) {
	// empty string = use the default scheduler
	if (contains(GetPossibleElevatorValues(), io_scheduler) || io_scheduler == "")
	{
	    if (elevator != io_scheduler)
	    {
		modified = true;
	    }

	    elevator = io_scheduler;
	}
	else
	{
	    y2error("unknown IO scheduler '%1', use: %2", io_scheduler, GetPossibleElevatorValues());
	}
    }

    global define boolean GetSysRqKeysEnabled () {
	if (ENABLE_SYSRQ == "yes")
	    return true;
	else
	    return false;
    }

    global define void SetSysRqKeysEnabled (boolean enable_sysrq) {
	if (enable_sysrq != true && enable_sysrq != false)
	{
	    y2warning("enable_sysrq should be 'true' or 'false'");
	    return;
	}

	string enable_sysrq_string = enable_sysrq ? "yes" : "no";

	if (ENABLE_SYSRQ != enable_sysrq_string)
	{
	    modified = true;
	}

	ENABLE_SYSRQ = enable_sysrq_string;
	y2milestone("SysRq was set to %1", ENABLE_SYSRQ);
    }

    /**
     * Default settings for serial console
     */
    map <string, any> default_sc_settings = $[
	"enabled" : false,
	"baud_rate" : 9600,
	"serial_device" : "/dev/ttyS0",
    ];

    global define map <string, any> GetSerialConsoleSettings () {
	return $[
	    "enabled" : true,
	    "baud_rate" : 9600,
	    "serial_device" : "/dev/ttyS0",
	];
    }

    global define void SetSerialDeviceConsoleSettings (map <string, any> settings) {
	return nil;
    }
}

ACC SHELL 2018