OS : 페도라 26 알파외부 모듈로 빌드하면 Linux Char 장치 드라이버가 작동하지 않지만 커널을 실행하지 않는 경우 빌드하면 작동합니다.하지만 그 이유는 무엇입니까?
커널 : 리눅스 4.11
커널은 수동으로 다음과 같은 명령으로 컴파일 :
1)
3) 모든 만들 menuconfig를 만드는)
2 oldconfig하기
4) make module_install
5)
성공적으로 커널 컴파일과 최신의 설치 후
, 내가 몇 가지 모듈을 빌드, 설치하게하지만make -C /root/linux-4.11/ M=$(pwd) modules
를 통해 외부 모듈로하지 커널을 실행에있다. 내가 개발 한 모든 기본 모듈은 예상대로 컴파일 및 설치되고 작동하지만 기본 문자 장치 드라이버를 코딩하지만 일부 이상한 문제가 발견되었습니다. 즉,
insmod
을 통해 모듈을 성공적으로 컴파일하고 삽입 한 후,이 모듈에 관한 커널 메시지를 얻지 못했고/dev/디렉토리에 문자 장치가 생성되지 않았지만이 문제에 관해서도 검색했지만 개선되지는 않았습니다. 이 같은 문제에 대한 대답.
갑자기 내가 컴파일하려고 시도하고 모듈을 삽입 한 후 make -C /lib/modules/$(uname -r)/build/ M=$(pwd) modules
을 통해 커널을 실행하면서 내 기대 결과를 얻는다. 하지만 외부 모듈로 빌드 할 때 다른 간단한 모듈이 제대로 작동하기 때문에 왜 이런 일이 발생하는지 파악하고 싶습니다.하지만 왜이 모듈을 사용하지 않을까요?
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/version.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Neeraj Pal <[email protected]>");
MODULE_DESCRIPTION("Sample Character Driver");
static dev_t first;
static struct device *dev_ret;
static struct cdev c_dev;
static struct class *cl;
static int dev_open(struct inode *,struct file *);
static int dev_release(struct inode *,struct file *);
static ssize_t dev_read(struct file *, char __user *,size_t count, loff_t *);
static ssize_t dev_write(struct file *, const char __user *,size_t count, loff_t *);
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release
};
static int __init chardev_init(void)
{
int ret;
printk(KERN_ALERT "char device registered\n");
if ((ret = alloc_chrdev_region(&first,0,1,"Namastey")) < 0)
{
return ret;
}
if (IS_ERR(cl = class_create(THIS_MODULE,"hello")))
{
unregister_chrdev_region(first,1);
return PTR_ERR(cl);
}
if (IS_ERR(dev_ret = device_create(cl,NULL,first,NULL,"mychar")))
{
class_destroy(cl);
unregister_chrdev_region(first,1);
return PTR_ERR(dev_ret);
}
cdev_init(&c_dev,&fops);
if ((ret = cdev_add(&c_dev, first, 1)) < 0)
{
device_destroy(cl, first);
class_destroy(cl);
unregister_chrdev_region(first, 1);
return ret;
}
return 0;
}
static void __exit chardev_exit(void)
{
cdev_del(&c_dev);
device_destroy(cl, first);
class_destroy(cl);
unregister_chrdev_region(first, 1);
printk(KERN_INFO " Unloaded \n");
}
static int dev_open(struct inode *i, struct file *f)
{
printk(KERN_INFO "Driver: open()\n");
return 0;
}
static int dev_release(struct inode *i, struct file *f)
{
printk(KERN_INFO "Driver: close()\n");
return 0;
}
static ssize_t dev_read(struct file *f, char __user *buf, size_t len, loff_t *off)
{
printk(KERN_INFO "Driver: read()\n");
return 0;
}
static ssize_t dev_write(struct file *f, const char __user *buf, size_t len,
loff_t *off)
{
printk(KERN_INFO "Driver: write()\n");
return len;
}
module_init(chardev_init);
module_exit(chardev_exit);
컴파일 & 커널 로그 & 커널, OS 정보 (외부 모듈로 모두 다음 커널을 실행에 대한 등) :
[[email protected] ldd]# uname -a
Linux localhost.localdomain 4.11.0 #1 SMP Tue May 2 14:34:09 IST 2017 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ldd]#
#NOW,BUILD as external module
[[email protected] ldd]# make -C /root/linux-4.11/ M=$(pwd) modules
make: Entering directory '/root/linux-4.11'
WARNING: Symbol version dump ./Module.symvers
is missing; modules will have no dependencies and modversions.
Building modules, stage 2.
MODPOST 1 modules
make: Leaving directory '/root/linux-4.11'
[[email protected] ldd]# insmod char
char_driver/ char.ko char.mod.o char.o
[[email protected] ldd]# insmod char.ko
insmod: ERROR: could not insert module char.ko: File exists
[[email protected] ldd]# rmmod char
[[email protected] ldd]# insmod char.ko
[[email protected] ldd]# journalctl -n 10
-- Logs begin at Tue 2017-05-02 11:28:36 IST, end at Fri 2017-05-05 12:14:01 IST. --
May 05 12:12:11 localhost.localdomain systemd[1]: Started Network Manager Script Dispatcher Service.
May 05 12:12:11 localhost.localdomain nm-dispatcher[1356]: req:1 'dhcp4-change' [ens3]: new request (4 scripts)
May 05 12:12:11 localhost.localdomain nm-dispatcher[1356]: req:1 'dhcp4-change' [ens3]: start running ordered scripts...
May 05 12:12:11 localhost.localdomain dhclient[564]: bound to xxx.xxx.xxx.xx-- renewal in 1401 seconds.
May 05 12:12:21 localhost.localdomain audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=NetworkManager-dispatcher comm="syst
May 05 12:14:01 localhost.localdomain anacron[1345]: Job `cron.daily' started
May 05 12:14:01 localhost.localdomain run-parts[1377]: (/etc/cron.daily) starting logrotate
May 05 12:14:01 localhost.localdomain run-parts[1382]: (/etc/cron.daily) finished logrotate
May 05 12:14:01 localhost.localdomain anacron[1345]: Job `cron.daily' terminated
May 05 12:14:01 localhost.localdomain anacron[1345]: Normal exit (1 job run)
[[email protected] ldd]# uname -a;cat /etc/redhat-release ;cat Makefile
Linux localhost.localdomain 4.11.0 #1 SMP Tue May 2 14:34:09 IST 2017 x86_64 x86_64 x86_64 GNU/Linux
Fedora release 26 (Twenty Six)
obj-m := char.o
[[email protected] ldd]# make -C /lib/modules
modules/ modules-load.d/
[[email protected] ldd]# make -C /lib/modules/4.11.0
4.11.0/ 4.11.0-0.rc3.git0.2.fc26.x86_64/
[[email protected] ldd]# make -C /lib/modules/4.11.0/
build/ kernel/ source/
#Now, Build against running kernel
[[email protected] ldd]# make -C /lib/modules/4.11.0/build/ M=$(pwd) modules
make: Entering directory '/usr/src/linux-4.11'
CC [M] /root/ldd/char.o
Building modules, stage 2.
MODPOST 1 modules
CC /root/ldd/char.mod.o
LD [M] /root/ldd/char.ko
make: Leaving directory '/usr/src/linux-4.11'
[[email protected] ldd]# rmmod char
[[email protected] ldd]# insmod char.ko
[[email protected] ldd]# journalctl -n 10
-- Logs begin at Tue 2017-05-02 11:28:36 IST, end at Fri 2017-05-05 12:33:04 IST. --
May 05 12:12:11 localhost.localdomain nm-dispatcher[1356]: req:1 'dhcp4-change' [ens3]: new request (4 scripts)
May 05 12:12:11 localhost.localdomain nm-dispatcher[1356]: req:1 'dhcp4-change' [ens3]: start running ordered scripts...
May 05 12:12:11 localhost.localdomain dhclient[564]: bound to xxx.xxx.xxx.xx-- renewal in 1401 seconds.
May 05 12:12:21 localhost.localdomain audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=NetworkManager-dispatcher comm="syst
May 05 12:14:01 localhost.localdomain anacron[1345]: Job `cron.daily' started
May 05 12:14:01 localhost.localdomain run-parts[1377]: (/etc/cron.daily) starting logrotate
May 05 12:14:01 localhost.localdomain run-parts[1382]: (/etc/cron.daily) finished logrotate
May 05 12:14:01 localhost.localdomain anacron[1345]: Job `cron.daily' terminated
May 05 12:14:01 localhost.localdomain anacron[1345]: Normal exit (1 job run)
May 05 12:33:04 localhost.localdomain kernel: char device registered
[[email protected] ldd]# ls -la /dev/mychar
crw------- 1 root root 244, 0 May 5 12:33 /dev/mychar
[[email protected] ldd]# echo "" > /dev/mychar
[[email protected] ldd]# cat /dev/mychar
[[email protected] ldd]# rmmod char
[[email protected] ldd]# journalctl -n 10
-- Logs begin at Tue 2017-05-02 11:28:36 IST, end at Fri 2017-05-05 12:33:57 IST. --
May 05 12:14:01 localhost.localdomain anacron[1345]: Job `cron.daily' terminated
May 05 12:14:01 localhost.localdomain anacron[1345]: Normal exit (1 job run)
May 05 12:33:04 localhost.localdomain kernel: char device registered
May 05 12:33:40 localhost.localdomain kernel: Driver: open()
May 05 12:33:40 localhost.localdomain kernel: Driver: write()
May 05 12:33:40 localhost.localdomain kernel: Driver: close()
May 05 12:33:51 localhost.localdomain kernel: Driver: open()
May 05 12:33:51 localhost.localdomain kernel: Driver: read()
May 05 12:33:51 localhost.localdomain kernel: Driver: close()
May 05 12:33:57 localhost.localdomain kernel: Unloaded
또한 위의 로그 및 출력 아래 가능한 스크린 샷을 볼 수 있습니다 자세한 내용은 (당신이 텍스트 정보에 만족하지 않는 경우, 링크를 클릭) :
Build against external module (not working,don't know why and Kernel & OS Details and Makefile
Build against running kernel (working , don't know why)
빌드 명령, 로그 및 질문 사항 *에 ** 텍스트 **로 있어야하며 이미지로 연결되지 않아야합니다. 게시물을 작성하고 수정하여 이러한 문제를 해결하십시오. – Tsyvarev
누락 된 Module.symvers에 대한 경고가 표시됩니다. 이것이 다른 커널에 대해 빌드 된 모듈을로드하는 것을 허용합니다. 내가 짓고있는 커널이 빌드되지 않았거나 CONFIG_MODVERSIONS가 비활성화되어 있다고 생각합니다 (해당 옵션에 대한 도움말 텍스트 참조). – fernan
@Tsyvarev 텍스트 형식으로 모든 정보를 업데이트하고 추가했습니다. 그러나 누군가 텍스트 형식을 이해하지 못하면 그 경우 스크린 샷 형식을 유지합니다. –