I wrote a simple kernel module and declared 3 parameters:
#include<linux/module.h>
int date = 1, month = 1, year = 2000;
module_param(date, int, 0);
MODULE_PARM_DESC(date, "date of time");
module_param(month, int, 0);
MODULE_PARM_DESC(month, "month of time");
module_param(year, int, 0);
MODULE_PARM_DESC(year, "year of time");
int init_module(void) {
printk(KERN_INFO "Today is %d/%d/%d.\n", year, month, date); // write to dmesg file
return 0;
}
When I execute this command:
sudo insmod hello.ko year=`date +%Y` month=`date +%m` date=`date +%d`
The terminal shows:
insmod: ERROR: could not insert module hello.ko: Invalid parameters
So I check the dmesg log:
[ 4283.774215] hello: `09' invalid for parameter `date'
But when I chang the command to:
sudo insmod hello.ko year=`date +%Y` month=`date +%m` date=9
It worked!
[ 4373.679843] Today is 2023/7/9.
Why date +%d
can't work but the others work?
You should rewrite your command on this way to avoid leading zeros in date
command:
sudo insmod hello.ko year=`date +%Y` month=`date +%-m` date=`date +%-d`
(the -
tell date
not to add leading zero)