WSL 2には/lib/modules/はありません。

WSL 2には/lib/modules/はありません。

私のラップトップのUbuntu 20で実行されているhello worldカーネルモジュールのソースコードがあります。私はUbuntu 20とWSL2で同じコードをコンパイルしようとしています。そのために私はこれを使用しています:

make -C /sys/modules/$(shell uname -r)/build M=$(PWD) modules

問題は/lib/modulesその場が空いているということだ。 WSL2は何もインポートできないようです。/lib/modules/4.19.104-microsoft-standard/build

私は以下を使ってタイトルを取得しようとします。

sudo apt search linux-headers-`uname -r`

Sorting... Done
Full Text Search... Done

しかし、モジュールフォルダには何も入力されません。

フォルダに必要なすべてのモジュールが含まれるようにするにはどうすればよいですか?

[編集する]

もっと近づいてくれた@HannahJに感謝します。

私がしていること:

> sudo make -C /home/<user>/WSL2-Linux-Kernel M=$(pwd) modules

SL2-Linux-Kernel M=$(pwd) modules
make: Entering directory '/home/<user>/WSL2-Linux-Kernel'
  CC [M]  /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.mod.o
  LD [M]  /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.ko
make: Leaving directory '/home/<user>/WSL2-Linux-Kernel'

いよいよlkm_example.koファイルを作成しました。

以来:

> sudo insmod lkm_example.ko
insmod: ERROR: could not insert module lkm_example.ko: Invalid module format

> dmesg
[200617.480635] lkm_example: no symbol version for module_layout
[200617.480656] lkm_example: loading out-of-tree module taints kernel.
[200617.481542] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 0000000074f1d70f, val ffffffffc0000158


> sudo modinfo lkm_example.ko
filename:       /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.ko
version:        0.01
description:    A simple example Linux module.
author:         Carlos Garcia
license:        GPL
srcversion:     F8B272146BAA2381B6332DE
depends:
retpoline:      Y
name:           lkm_example
vermagic:       4.19.84-microsoft-standard+ SMP mod_unload modversions

これは私のMakefileです。

obj-m += lkm_example.o
all:
    make -C /home/<usr>/WSL2-Linux-Kernel M=$(PWD) modules
clean:
    make -C /home/<usr>/WSL2-Linux-Kernel M=$(PWD) clean
test:
    # We put a — in front of the rmmod command to tell make to ignore
    # an error in case the module isn’t loaded.
    -sudo rmmod lkm_example
    # Clear the kernel log without echo
    sudo dmesg -C
    # Insert the module
    sudo insmod lkm_example.ko
    # Display the kernel log
    dmesg
unload:
    sudo rm /dev/lkm_example
    sudo rmmod lkm_example

[編集2] これは私のカーネルモジュールです。

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <asm/uaccess.h>
    #include <linux/init_task.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Carlos Garcia");
    MODULE_DESCRIPTION("A simple example Linux module.");
    MODULE_VERSION("0.01");
    
    /* Prototypes for device functions */
    static int device_open(struct inode *, struct file *);
    static int device_release(struct inode *, struct file *);
    static ssize_t device_read(struct file *, char *, size_t, loff_t *);
    static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
    static int major_num;
    static int device_open_count = 0;
    static char msg_buffer[MSG_BUFFER_LEN];
    static char *msg_ptr;
    
    /* This structure points to all of the device functions */
    static struct file_operations file_ops = {
        .read = device_read,
        .write = device_write,
        .open = device_open,
        .release = device_release
    };
    
    /* When a process reads from our device, this gets called. */
    static ssize_t device_read(struct file *flip, char *buffer, size_t len, loff_t *offset)
    {
     ...
    }
    
    /* Called when a process tries to write to our device */
    static ssize_t device_write(struct file *flip, const char *buffer, size_t len, loff_t *offset)
    {
       ...
    }
    
    /* Called when a process opens our device */
    static int device_open(struct inode *inode, struct file *file)
    {
        ...
        try_module_get(THIS_MODULE);
    
    }
    
    /* Called when a process closes our device */
    static int device_release(struct inode *inode, struct file *file)
    {
        ...
        module_put(THIS_MODULE);
    }
    
    static int __init lkm_example_init(void)
    {
        ...
        major_num = register_chrdev(0, "lkm_example", &file_ops);
        if (major_num < 0)
        {
            printk(KERN_ALERT "Could not register device: % d\n", major_num);
            return major_num;
        }
        else
        {
            printk(KERN_INFO "lkm_example module loaded with device major number % d\n", major_num);
            return 0;
        }
    }
    
    static void __exit lkm_example_exit(void)
    {
        /* Remember — we have to clean up after ourselves. Unregister the character device. */
        unregister_chrdev(major_num, DEVICE_NAME);
        printk(KERN_INFO "Goodbye, World !\n");
    }
    /* Register module functions */
    module_init(lkm_example_init);
    module_exit(lkm_example_exit);

ベストアンサー1

課題のためにこれを行う必要があるため、ここでソリューションを共有する必要があると思いました。

デフォルトのWSL2カーネルはモジュールのロードを許可しません。独自のカーネルバージョンをコンパイルして使用する必要があります。

WSL2でカーネルをコンパイルして使用する方法

  1. Ubuntu / WSLから:

    sudo apt install build-essential flex bison libssl-dev libelf-dev git dwarves
    git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
    cd WSL2-Linux-Kernel
    cp Microsoft/config-wsl .config
    make -j $(expr $(nproc) - 1)
    
  2. Windowsから\\wsl$\<DISTRO>\home\<USER>\WSL2-Linux-Kernel\arch\x86\boot\bzimageWindowsプロファイルにコピーします(%userprofile%C:\Users\<Windows_user>:)。

  3. 次の内容でファイルを作成します%userprofile%\.wslconfig

    [wsl2]
    kernel=C:\\Users\\WIN10_USER\\bzimage
    

    注:二重バックスラッシュ(\\)が必要です。また、潜在的な古いエラーを避けるために、行に末尾のスペースを残さないようにしてください。

  4. PowerShellで、次を実行します。wsl --shutdown

  5. WSL2バージョンを再起動してください

モジュールをコンパイルする方法

注:Makefileでこれらの操作を実行するか、/home/$USER/場所に合わせてMakefileを調整する必要があります。

  1. 以下を含むファイルを作成しますMakefile

    obj-m:=lkm_example.o
    
    all:
        make -C $(shell pwd)/WSL2-Linux-Kernel M=$(shell pwd) modules
    
    clean:
        make -C $(shell pwd)/WSL2-Linux-Kernel M=$(shell pwd) clean
    
  2. 走るmake

.wslconfigファイルステップソースここ

おすすめ記事