包含 Linux 标签的文章

Ubuntu 安装 PHP

安装: sudo apt-get install php Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline Suggested packages: php-pear The following NEW packages will be installed: php php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline 0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded. Need to get 3,532 kB of archives. After this operation, 14.1 MB of additional disk space will be used. Do you want to continue? [Y/n] 除了自动安装的 php7.0-cli php7.0-common php7.0-fpm 等,再安装一些模块: sudo apt-get install php7.0-mysql php7.0-curl php7.0-mcrypt php7.0-mbstring php7.0-xml 测试: php -v PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies php7.0-fpm 服务控制: sudo service php7.0-fpm stop sudo service php7.0-fpm start sudo service php7.0-fpm restart sudo service php7.0-fpm statu...

Ubuntu 安装 Nginx

apt-get install nginx # 安装 service nginx status # 查看服务运行状态 netstat -nap |grep nginx # 查看服务所使用的端口 网页浏览器访问服务器 IP 显示: Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx...

Ubuntu 下载内核源码

方法 1 uname -r apt-cache search linux-source //搜索内核版本 sudo apt-get install linux-source-4.15.0 会将内核源码包下载到 /usr/src 方法 2 apt-get source linux-image-$(uname -r) 可以下载到当前目录 我的测试结果是获取不到内核源码(源的配置没有问题)。 方法 3 git clone git://kernel.ubuntu.com/ubuntu/ubuntu-<release>.git git clone git://kernel.ubuntu.com/ubuntu/ubuntu-xenial.git (16.04) git tag -l Ubuntu-* 下载的源码很大,速度很慢(git 浅克隆可以解决太大的问题)。 安装编译内核可能依赖的库或工具 sudo apt-get install libncurses5-dev libssl-dev sudo apt-get install build-essential openssl sudo apt-get install zlibc minizip sudo apt-get install libidn11-dev libidn11 编译 #sudo make mrproper sudo make menuconfig sudo make -j4 安装内核 sudo make modules_install #安装内核模块 sudo make install #安装内核 内核用户手册 man sudo apt-get install xmlto  make mandocs -j4 #编译用户手册 sudo make installmandocs man print...

Ubuntu 驱动开发简介

在 Ubuntu 上开发驱动通常并不需要下载内核的源码(不需要改内核源码的情况下),下载内核头文件就可以了。下载的头文件中自带内核的 Makefile 文件。 查询系统对应的内核头文件包: dpkg-query -s linux-headers-$(uname -r) 安装: sudo apt-get install linux-headers-$(uname -r) 内核头文件会被安装在 /usr/src 驱动的 Makefile 简例: obj-m := helloworld.o KDIR := /usr/src/linux-headers-4.15.0-36-generic PWD := $(shell pwd) all: modules modules: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)...

Linux 内核打印 printk

内核中没有 C 库,所以不能使用 printf 打印。但它提供了类似的 printk 函数,printk 函数输出的字符串前加一个带尖括号的整数来控制打印级别,如 printk("<6>Hello, world!\n")。级别数值越小,优先级越高,其紧急和严重程度就越高。然而,需要注意的是,并不是所有级别的消息都会进行输出,而是根据 printk 的打印级别进行过滤。 printk 打印级别宏定义: #define KERN_EMERG KERN_SOH "0" /* system is unusable */ #define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */ #define KERN_CRIT KERN_SOH "2" /* critical conditions */ #define KERN_ERR KERN_SOH "3" /* error conditions */ #define KERN_WARNING KERN_SOH "4" /* warning conditions */ #define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */ #define KERN_INFO KERN_SOH "6" /* informational */ #define KERN_DEBUG KERN_SOH "7" /* debug-level messages */ #define KERN_DEFAULT KERN_SOH "d" /* the default kernel loglevel */ 内核日志打印级别查看: cat /proc/sys/kernel/printk 4 4 1 7 该文件包含四个数字值,分别表示: 控制台日志级别(Console log level):只有级别高于设定值的日志消息才会被显示在控制台上(值要小于该值)。 默认的消息日志级别(Default message log level):用于打印没有优先级的消息,即当 printk 没有指定消息级别时,将使用该默认级别。 最低的控制台日志级别(Minimum console log level):控制台日志级别可被设置的最小值,也就是最高的优先级。 默认的控制台日志级别(Default console log level):控制台日志级别的缺省值。 修改内核日志打印级别: echo "6 4 1 7" | sudo tee /proc/sys/kernel/printk 修改后的值重启后会恢复,如果要保持,可以将它添加到启动脚本中,以便在系统启动时重新设置。 printk 打印的是控制台,也就是 /dev/console。而图形界面中的终端,其实是把 stdin,stdout,stderr 三个文件重定向了一下。所以 printk 是不能在图形界面中的终端中显示的,当然可以在 /var/log/syslog 或者用 dmesg 查看。 不够打印级别的信息会被写到日志中可通过 dmesg 命令来查看。 dmesg 命令: 实时监控 dmesg 的日志输出: watch "dmesg | tail -20"...

Linux 内核模块的相关操作命令

lsmod 查看已经安装的模块 lsmod Module Size Used by binfmt_misc 20480 1 vmw_vsock_vmci_transport 28672 2 vsock 36864 3 vmw_vsock_vmci_transport snd_ens1371 28672 2 snd_ac97_codec 131072 1 snd_ens1371 gameport 16384 1 snd_ens1371 ac97_bus 16384 1 snd_ac97_codec ... modinfo 显示模块信息 modinfo nfs.ko filename: /home/matt/test/kernel/linux-3.2.0-psp04.06.00.11/fs/nfs/nfs.ko license: GPL author: Olaf Kirch <okir@monad.swb.de> depends: sunrpc,lockd intree: Y vermagic: 3.2.0 mod_unload modversions ARMv7 p2v8 parm: cache_getent:Path to the client cache upcall program (string) parm: cache_getent_timeout:Timeout (in seconds) after which the cache upcall is assumed to have failed (ulong) parm: enable_ino64:bool parm: nfs4_disable_idmapping:Turn off NFSv4 idmapping when using 'sec=sys' (bool) depends 显示出了模块的依赖项 insmod 加载指定位置的模块 insmod /path-to-file/nfs.ko 如果依赖模块没有安装会提示 Unknown symbol... 使用时要指定模块的绝对路径。 rmmod 卸载驱动模块 rmmod <module_name> 注意其中 ”module_name” 是 lsmod 显示的模块名称,而不是对应的 ko 文件名 modprobe 用于挂载内核模块,挂载模块时不用指定模块文件的路径,也不用带文件的后缀。 modprobe nfs 相比 insmod 更智能些,不过限定了 ko 文件的位置。 实例: modprobe nfs [ 207.024183] RPC: Registered named UNIX socket transport module. [ 207.030461] RPC: Registered udp transport module. [ 207.035397] RPC: Registered tcp transport module. [ 207.040317] RPC: Registered tcp NFSv4.1 backchannel transport module. 卸载模块: modprobe -r nfs modprobe: remove[ 145.687621] RPC: Unregistered named UNIX socket transport module. 'sunrpc': Resou[ 145.694428] RPC: Unregistered udp transport module. rce temporarily [ 145.700954] RPC: Unregistered tcp transport module. unavailable [ 145.707460] RPC: Unregistered tcp NFSv4.1 backchannel transport module. depmod 用于分析可载入模块的相依性,供 modprobe 在安装模块时使用。 depmod 通过读取 /lib/modules/$(uname -r) 目录下的每一个模块来创建一个记录模块相依性的列...

以动态模块 ko 的形式使用 nfs 客户端

ARM 设备的的内核中没有编进去 NFS,所以打算采用 ko 的方式加载进内核。 编译内核源程序,产生 Makefile。 make menuconfig 配置内核: [*] Network File Systems ---> <M> NFS client support [*] NFS client support for NFS version 3 把 NFS client support 选做模块(M)。 make modules 生成 nfs.ko、sunrpc.ko、lockd.ko 拷贝到 /lib/modules/$(uname -r) 执行 depmod 执行 modprobe nfs [ 207.024183] RPC: Registered named UNIX socket transport module. [ 207.030461] RPC: Registered udp transport module. [ 207.035397] RPC: Registered tcp transport module. [ 207.040317] RPC: Registered tcp NFSv4.1 backchannel transport module...

创建一个 Linux 内核模块实例

源代码: #include <linux/init.h> //包含模块初始化、清除函数 #include <linux/module.h> //包含许多符号和函数的定义,模块的定义 #include <linux/kernel.h> static int __init hello_init(void) { printk(KERN_ALERT "Hello World!\n"); //内核中的打印只有printk,而且分打印级别 return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "Goodbye, see you again!\n"); } module_init(hello_init); //函数声明,把代码段放到init段;module_init是该程序的入口函数 //当系统启动init程序的时候,执行module_init()里的函数 module_exit(hello_exit); //当系统启动exit程序的时候,执行module_exi()里的函数 //没有module声明的函数(普通函数)会放到代码段(文本段) MODULE_AUTHOR("Matt <matt@mculoop.com>"); //声明作者 MODULE_DESCRIPTION("Linux Kernel Hello World Module (C) 2018"); //模块描述 MODULE_LICENSE("GPL"); //声明LICENSE Makefile: # To build modules outside of the kernel tree, we run "make" # in the kernel source tree; the Makefile these then includes this # Makefile once again. # This conditional selects whether we are being included from the # kernel Makefile or not. # called from kernel build system: just declare what our modules are obj-m := helloworld.o # Assume the source tree is where the running kernel was built # You should set KERNELDIR in the environment if it's elsewhere KERNELDIR ?= /home/matt/test/kernel/linux-3.2.0-psp04.06.00.11 # The current directory is passed to sub-makes as argument PWD := $(shell pwd) all: modules modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET) 编译: 编译前应当确保已经编译过内核,因为要使用内核的Makefile。 mak...

AM335X 内核编译

内核编译: sudo apt-get install u-boot-tools #uImage 生成时依赖的 mkimage 工具 make mrproper make am335x_evm_defconfig make menuconfig make -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage 模块编译: make modules make modules_install INSTALL_MOD_PATH=./module...

解决错误 modinfo can't open '...modules.dep' No such file or directory

在 ARM 开发板上查看模块信息提示题中的错误: modinfo: can't open '/lib/modules/3.2.0/modules.dep': No such file or directory 解决办法是: 1、创建文件夹:/lib/modules/$(uname -r) 2、cp xx.ko /lib/modules/3.2.0/ 3、depmod 4、mv /lib/modules/3.2.0/modules.dep.bb /lib/modules/3.2.0/modules.dep 5、modinfo xx.ko 这下就正常了 又发现: 其它模块不用重复这样做 为什么其它模块不需要这样做的?是不是只要有 modules.dep 这么一个文件名的文件存在于这里就可以来了? 再次发现: 在这里创建一个空文件就行了! touch modules.dep 那么,在 /lib/modules/3.2.0/ 中没有 ko 文件的时候,直接执行 depmod 命令是不是也可以产生空文件?答案是:是的,不过产生的是 modules.dep.bb 。为什么不直接产生 modules.dep 呢?还不清楚。有一点是可以肯定的,modinfo 使用前提是存在 modules.dep,空的就行,模块信息是从 ko 文件中获取的,跟 modules.dep 没关系。不过,为什么非要存在 modules.dep 文件呢?这也还不清楚。 我在 Ubuntu 18.04 中做了测试,内核版本是 4.15.0-36,执行 depmod 没有 modules.dep.bb 产生,直接产生 modules.dep,并且文件的内容格式与 3.2.0 中的也不一...