编辑
2024-01-15
记录知识
0

[Rust]安装环境

公司的虚拟化方案是用rust重构的,为了搭建环境,又开始起来的rust。哎,太菜了太菜了。以前学的东西脑子里全丢了。这里补一个rust环境配置。切记,rust不要想用ubuntu源里面的,直接根据文档使用最新的版本

必要工具

apt install curl rsync gdb-multiarch openocd cargo doxygen qemu-user-static \ build-essential libncurses5-dev libssl-dev libgtk2.0-dev libglib2.0-dev

国内源

vim ~/.bash_profile export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

官网安装

curl -L https://static.rust-lang.org/rustup.sh -O sh rustup.sh
Current installation options: default host triple: x86_64-unknown-linux-gnu default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation

选择自定义,配置最后如下:

default host triple: x86_64-unknown-linux-gnu default toolchain: nightly profile: complete modify PATH variable: yes

最后安装好了获得如下提示

nightly-x86_64-unknown-linux-gnu installed - rustc 1.77.0-nightly (30dfb9e04 2024-01-14)
source ~/.bashrc rustc --version rustc 1.59.0 rustup --version rustup 1.26.0 (5af9b9484 2023-04-05) info: This is the version for the rustup toolchain manager, not the rustc compiler. info: The currently active `rustc` version is `rustc 1.77.0-nightly (30dfb9e04 2024-01-14)`

这样,rust就安装好了

版本介绍

rust默认是安装stable版本,但是有nightly和beta版本可选,其用到了"火车发布模型",rust特地规定了stable无法使用打上feature flags的功能,而我们的仓库就是这种,所以我只好选择nightly。简单理解如下:

  • Nightly:正在开发的版本、每晚自动构建、自动发布
  • Beta:测试版本,每6周从Nightly生成一次,包含新特性
  • Stable:稳定版本,每6周从Beta生成一次

关于版本的详细了解如下链接:

https://rustwiki.org/zh-CN/book/appendix-07-nightly-rust.html

否则会出现如下错误:

`#![feature]` may not be used on the stable release channel

如果需要切换stable和nightly或beta,如下

rustup default stable/nightly/beta

交叉编译

rustc --print target-list
rustup target add aarch64-unknown-linux-gnu

下载编译工具链

wget https://developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz xz -d gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz tar xvf gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar

配置工具链

vim ~/.cargo/config [source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = 'ustc' [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index" [build] target = "aarch64-unknown-linux-gnu" [target.aarch64-unknown-linux-gnu] linker = "aarch64-none-linux-gnu-gcc"

使用

cargo new hello --bin cd hello && cargo build

如果想要编译x86的,如下即可

cargo build --target=x86_64-unknown-linux-gnu

参考链接

Rust文档还是很全的,如下:

https://www.kancloud.cn/thinkphp/rust/36040
https://kaisery.github.io/trpl-zh-cn/ch01-01-installation.html
https://forge.rust-lang.org/index.html
https://doc.rust-lang.org/book/second-edition/foreword.html

编辑
2024-01-15
记录知识
0

debian类系统的依赖问题

debian类系统通过apt和dpkg命令来安装系统软件包,但是通常会出现依赖问题,其主要原因来是dpkg这类命令太过细节和直接,对整体系统依赖关系的考量不够多,一旦出现问题只能依靠其他命令例如apt,aptitude来解决。这里介绍一下如何通过apt和aptitude来解决依赖问题。

制造依赖问题

为了制造出依赖的问题,通过dpkg可以强制卸载某个存在依赖关系的包。

dpkg -P --force-all xserver-xorg-core xserver-xorg-core是一个与系统显示强相关的软件包。它和系统许多应用强相关。在上述命令中,会强制卸载掉这个包。此时如果通过apt安装其他包的时候,就会因为依赖问题导致无法正常使用apt,如下:

root@kylin:~# apt install 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 您也许需要运行“apt --fix-broken install”来修正上面的错误。 下列软件包有未满足的依赖关系: xorgxrdp : 依赖: xorg-input-abi-24 依赖: xorg-video-abi-24 依赖: xserver-xorg-core (>= 2:1.18.99.901) 但是它还没有被安装 xserver-xorg : 依赖: xserver-xorg-core (>= 2:1.17.2-2) 但是它还没有被安装 xserver-xorg-input-libinput : 依赖: xorg-input-abi-24 依赖: xserver-xorg-core (>= 2:1.18.99.901) 但是它还没有被安装 E: 有未能满足的依赖关系。请尝试不指明软件包的名字来运行“apt --fix-broken install”(也可以指定一个解决办法)。

直接修复

当依赖问题出现时,最直接的办法就是按照提示修正错误。

apt --fix-broken install 通常是能够正常修复错误的(在系统源和网络正常的情况下),如下

root@kylin:~# apt --fix-broken install 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 正在修复依赖关系... 完成 将会同时安装下列软件: xserver-xorg-core 建议安装: xfonts-100dpi | xfonts-75dpi xfonts-scalable 下列【新】软件包将被安装: xserver-xorg-core 升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 546 个软件包未被升级。 需要下载 1,343 kB 的归档。 解压缩后会消耗 3,946 kB 的额外空间。 您希望继续执行吗? [Y/n]

寻找错误原因

如果需要追溯出来依赖产生原因,可以找找谁修改了dpkg列表
当某个包被破坏,其实意味着某人在不经意之间修改了dpkg的列表。查看dpkg的处理日志可以初步判断原因

root@kylin:~# grep "remove" /var/log/dpkg.log 2024-01-15 11:10:53 remove xserver-xorg-core:arm64 3:1.20.8-2rk7 <无>

找到原因是 2024-01-15 11:10:53 卸载了xserver-xorg-core 软件包导致依赖问题 那么接下来是找到3:1.20.4-1kord1版本的xorg即可

通过已有源找包

先从源里搜索

apt-cache show xserver-xorg-core | grep 3:1.20.8-2rk7

Version: 3:1.20.8-2rk7

这时候手动下载安装即可

apt download xserver-xorg-core=3:1.20.8-2rk7 dpkg -i xserver-xorg-core*.deb

试试aptitude修复

如果依赖问题一旦出现,可以第一步试试-f命令能否修复

apt install -f

如果不满足预期,可以使用aptitude工具,也可以修复

aptitude install

安装 下列软件包: 1) xserver-xorg-core [3:1.20.8-2rk7 (v101)] 是否接受该解决方案?[Y/n/q/?]

选择适合自己的方案即可

其他依赖问题

如果尝试上述方法无法解决,首先应该警觉起来,因为需要安装的包与当前系统和当前源存在不兼容的问题。你应该在心里做好即使安装好了,软件包仍会存在问题的最坏预期。如下分享一下判断手段

“E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系”

这种情况下,说明需要安装的包的依赖和系统要求的依赖存在版本之间的差异。 aptitude install pkgname 会找到最好的方案将系统的包进行升级和降级,从而适配需要安装的软件包。

如果aptitude无法解决这个问题,再去尝试修改需要安装包的依赖信息

dpkg -x pkgname.deb test (将pkgname.deb解包到test目录) cd test dpkg -e ../pkgname.deb (将pkgname.deb的control信息解开)

这里需要修改依赖描述文件的信息,使其符合系统当前要求
(注意:此操作仅修改依赖版本关系,保证安装包正常安装,不保证安装后运行正常)

vim DEBIAN/control

根据实际情况,修改包的依赖关系(降低包版本依赖要求,删除依赖信息,增加依赖信息)

Depends: xserver-common (>= 3:1.20.8-2rk7), keyboard-configuration, udev (>= 149), libegl1, libaudit1 (>= 1:2.2.1), libbsd0 (>= 0.7.0), libc6 (>= 2.29), libdbus-1-3 (>= 1.9.14), libdrm2 (>= 2.4.66), libepoxy0 (>= 1.5.4), libgcrypt20 (>= 1.8.0), libgl1, libpciaccess0 (>= 0.12.902), libpixman-1-0 (>= 0.30.0), librga2, libselinux1 (>= 2.0.82), libsystemd0, libudev1 (>= 183), libunwind8, libxau6, libxdmcp6, libxfont2 (>= 1:2.0.1), libxshmfence1

然后重新打包

cd .. && dpkg -b test/ pkgname.deb (重新打包)

不计后果的强制安装

在aptitude install无法正常解决依赖问题时,如果仅仅为了包的安装成功,通过强制命令可以将其安装成功,或许能够将其依赖问题解决。
(此操作可能损坏系统,执行操作的人需要为这样的操作承担后果)

dpkg -i --force-all pkgname.deb

安装之后,直接验证安装包内的程序是否正常使用。
如果安装之后,应用软件能够正常使用,可以修改包的安装信息,以匹配系统。如下所示:

vim /var/lib/dpkg/status (找到对应包的说明,修改成系统匹配的依赖状态)
vim /usr/share/doc/pkgname/changelog.gz(适当的修改changlog内容,用于明示此包是个人擅自修改过的)

如果安装成功后,应用软件不能正常使用或出现问题,请尝试从ld,patchelf等其他方式处理,通过安装包已无法正常处理。

下面将恢复软件包现状

dpkg -P pkgname.deb apt download pkgname.deb dpkg -i pkgname.deb apt install -f

与依赖相关的其他命令

apt-cache depends xserver-xorg-core # 查找安装包的正向依赖 apt-cache rdepends xserver-xorg-core # 查找安装包的反向依赖 apt-cache show xserver-xorg-core # 查看包的详细信息 apt-cache policy xserver-xorg-core # 查看包的优先级,以及哪个源提供的包 apt-cache showpkg xserver-xorg-core # 以更详细的方式查看包的依赖关系 echo "pkgname hold" | sudo dpkg --set-selections # 锁定某个包,让其他人无法升级和卸载 dpkg --get-selections | grep hold # 查看已经锁定的软件包 dpkg -S /usr/lib/xorg/Xorg # 查看文件输入哪个包 dpkg-deb -c pkgname.deb # 查看deb文件的内容 dpkg -i --force-overwrite B.deb # 强制覆盖某个安装包 apt install pkgname --download-only # 只想下载不想安装

锁定所有的包

#!/bin/bash dpkg --get-selections | awk -F ' ' '{print $1}' | while read name do echo $name echo "$name hold" | dpkg --set-selections done

提升某个包的优先级

vim /etc/apt/preferences.d/my.perf (优先级越高的源会首先安装) Package: pkgname Pin: origin "源地址" Pin-Priority: 1600

dpkg info被损坏 报错 “dpkg: unrecoverable fatal error, aborting: files list file for package 'xxxxxxx' is missing final newline”

mv /var/lib/dpkg/info/* /tmp/ dpkg --configure -a apt update apt-get install --reinstall linux-base apt install -f

警告 “dpkg: warning: files list file for package 'xxxxxxxx' missing; assuming package has no files currently installed”

for package in $(apt-get install -f 2>&1 |\ grep "warning: files list file for package '" |\ grep -Po "[^'\n ]+'" | grep -Po "[^']+"); do apt-get install --reinstall "$package"; done

总结

其实如果大家在安装包过程中存在问题,尝试apt和aptitude后仍无法解决,其实已经代表这个依赖很可能无法解决。换个思路,而不是一定要安装上这个包,或许能够解决你的问题。

参考链接

https://www.debian.org/doc/debian-policy/

编辑
2024-01-14
记录知识
0

从c程序来理解X86汇编

回顾x86汇编

简单的代码

#include <stdio.h> int main() { int a = 5; int b = a + 6; return 0; }

gcc -g -O0 simple.c -o simple gdb ./simple

(gdb) b main Breakpoint 1 at 0x1129: file simple.c, line 3. (gdb) r Starting program: /home/vivien/test/simple Breakpoint 1, main () at simple.c:3 3 { (gdb) n 2 5 int b = a + 6; (gdb) disass Dump of assembler code for function main: 0x0000555555555129 <+0>: endbr64 0x000055555555512d <+4>: push %rbp 0x000055555555512e <+5>: mov %rsp,%rbp 0x0000555555555131 <+8>: movl $0x5,-0x8(%rbp) => 0x0000555555555138 <+15>: mov -0x8(%rbp),%eax 0x000055555555513b <+18>: add $0x6,%eax 0x000055555555513e <+21>: mov %eax,-0x4(%rbp) 0x0000555555555141 <+24>: mov $0x0,%eax 0x0000555555555146 <+29>: pop %rbp 0x0000555555555147 <+30>: retq End of assembler dump.

这里disassemble命令默认以AT&T语法输出汇编指令 AT&T 语法中的指令格式为mnemonic source, destination 立即数为$符号,寄存器为%符号。 %eax是累加器,return的值也会放在这里 %ecx是计数器 %ebx是基地址寄存器 %edx是数据寄存器 %rbp是基指针,指向当前基址指针 %rsp是栈指针,指向当前堆栈指针

push %rbp mov %rsp,%rbp

这里将旧的基址指针压入栈保存备用,并将堆栈指针复制到基址指针上,这样%rbp指向main函数的栈帧底部。

movl $0x5,-0x8(%rbp) mov -0x8(%rbp),%eax add $0x6,%eax mov %eax,-0x4(%rbp)

这里将立即数5存储到 %rbp-8的局部变量中,再将%rbp-8移动到累加器中,然后对%eax自加6,然后将%eax的值存放在%rbp-4的局部变量

mov $0x0,%eax pop %rbp retq

这里将立即数0复制给%eax用作return的返回,并通过pop把旧的基指针从堆栈中取出并存回%rbp中。retq跳转回返回地址 在gdb中打印值来看看。

(gdb) x $rbp - 8 0x7fffffffe4f8: 0x00000005 (gdb) x &a 0x7fffffffe4f8: 0x00000005 (gdb) x &b 0x7fffffffe4fc: 0x0000000b (gdb) x $rbp - 4 0x7fffffffe4fc: 0x0000000b (gdb) x $rbp 0x7fffffffe500: 0x00000000

参考链接

https://www.recurse.com/blog/7-understanding-c-by-learning-assembly?utm_source=wechat_session&utm_medium=social&utm_oi=801824054439985152

编辑
2024-01-14
记录知识
0

Sar命令使用简介

Sar命令是一个分析系统性能的工具,这里简单介绍以下sar命令如何使用

sar -u 1

23时03分27秒 CPU %user %nice %system %iowait %steal %idle 23时03分28秒 all 2.76 0.00 2.01 0.00 0.00 95.23 %user 在用户模式下的百分比 %nice 如果通过renice调整了nice值,那么调整过的程序cpu占用的百分比 %system 在内核运行的百分比 %iowait 在io上阻塞时间的百分比,如果明显高,说明io压力很大 %steal 虚拟cpu等待实际cpu的时间百分比。说明虚拟机性能有问题 %idle 空闲的cpu百分比

sar -B 1

23时15分26秒 pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff 23时15分27秒 0.00 0.00 77.00 0.00 1126.00 0.00 0.00 0.00 0.00 pgpgin 页面换入计数 pgpgout 页面换出到磁盘的计数 fault 页面错误计数(major + minor) majflt major fault 的计数 pgfree 空闲链表的页面数 pgscank kswapd扫描的页数 pgscand 直接扫描的页数 pgsteal 扫描的页中每秒被回收的数 vmeff pgsteal / pgscan ,也就是回收效率,一直接近0%说明系统内存压力很大

sar -r 1

3时26分34秒 kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty 23时26分35秒 10114900 12329400 2527920 15.80 115168 2967592 9403528 55.63 1030508 4108996 124 kbmemfree 剩余内存,meminfo 的 MemFree kbavail 可用内存,meminfo的MemAvailable kbmemused 已用内存,free命令里面的used %memused 物理内存百分比 kbbuffers buffer使用,meminfo的Buffers kbcached cached使用,meminfo的Cached kbcommit 保证当前系统所需要的内存 %commit kbcommit/MemTotal kbactive 活跃内存数,meminfo的Active kbinact 非活跃内存数,meminfo的Inactive kbdirty 脏页数,meminfo的Dirty

sar -d 1

平均时间: DEV tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util 平均时间: dev7-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 tps 从物理磁盘读取IO的次数 rkB/s 读扇区的次数 wkB/s 写扇区的次数 dkB/s 丢弃IO的次数 areq-sz I/O 请求的平均大小 aqu-sz I/O请求的平均队列长度 await 每次请求IO的消耗时间,平均的 %util IO请求占用的cpu百分比

sar -b 1

17时36分27秒 tps rtps wtps dtps bread/s bwrtn/s bdscd/s 17时36分28秒 0.00 0.00 0.00 0.00 0.00 0.00 0.00 tps 从物理磁盘读取IO的总次数 rtps 读IO请求的次数 wtps 写IO请求的次数 dtps 丢弃IO请求的次数 bread/s 读取的数量 bwrtn/s 写入的数量 bdscd/s 丢弃的数量

sar -q 1

17时39分50秒 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked 17时39分51秒 0 697 0.10 0.18 0.10 0 runq-sz 等待运行队列长度 plist-sz 任务列表的任务数 ldavg-1 最近一分钟内的平均负载 ldavg-5 最近5分钟内的平均负载 ldavg-15 最近15分钟内的平均负载 blocked 因为IO原因在等待状态的任务数

sar -v 1

17时41分09秒 dentunusd file-nr inode-nr pty-nr 17时41分10秒 78017 10304 97509 2 dentunusd 目录缓存中的未使用的缓存数 file-nr 系统文件句柄使用数量 inode-nr 索引节点句柄的使用数量 pty-nr pty终端的使用数量

sar -w 1

17时41分39秒 proc/s cswch/s 17时41分40秒 0.00 431.00 proc/s 每秒创建的任务总数 cswch/s 每秒上下文切换的总数

sar -I 中断号 1

18时13分11秒 INTR intr/s 18时13分12秒 190 28.00 INTR 中断号 intr/s cpu对中断响应的时间

sar 电源统计

sar -m USB 1 总计: BUS idvendor idprod maxpower manufact product 总计: 3 24ae 4012 200 Rapoo V500RGB Gaming Keyboard BUS USB挂的总线号 idvendor USB的vendor ID idprod USB的product ID maxpower USB设置的最大电流值 manufact 制造商 product USB类型

sar -m CPU 1

18时26分54秒 CPU MHz 18时26分55秒 all 2223.78 CPU CPU号 MHz 频率

sar -m TEMP 1

平均时间: TEMP degC %temp DEVICE 平均时间: 1 29.00 29.00 coretemp-isa-0000 degC 当前摄氏度 %temp 相对设备温度,最大是temp_max

sar 网络监控

sar -n DEV 1

18时32分28秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil 18时32分29秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 IFACE 网卡接口 rxpck/s 接收包数量 txpck/s 发送包数量 rxkB/s 接收包数据 txkB/s 发送包数据 rxcmp/s 接收压缩包数量 txcmp/s 发送压缩包数量 rxmcst/s 接收多播包数量 %ifutil 网卡利用率

sar -n EDEV 1

平均时间: IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s 平均时间: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 rxerr/s 接收到的损坏包 txerr/s 发送包的损坏数 coll/s 当发送数据包时候,每秒钟发生的冲撞(collisions)数,这个是在半双工模式下才有 rxdrop/s 因为网络缓冲区满,接收时丢弃的包数量 txdrop/s 因为网络缓冲区满,发送时丢弃的包数量 txcarr/s carrier-errors发生的次数 rxfram/s 接收包时发生帧对齐错误的次数 rxfifo/s 接收包发生缓冲区溢出的次数 txfifo/s 发送包发生缓冲区溢出的次数

sar -n ICMP 1

18时46分51秒 imsg/s omsg/s iech/s iechr/s oech/s oechr/s itm/s itmr/s otm/s otmr/s iadrmk/s iadrmkr/s oadrmk/s oadrmkr/s 18时46分52秒 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 imsg/s 接收到的ICMP包数量 icmpInMsgs omsg/s 发送的ICMP包数量 icmpOutMsgs iech/s icmpInEchos的数量 iechr/s icmpInEchoReps的消息数 oech/s icmpOutEchos消息数 oechr/s icmpOutEchoReps消息数 itm/s icmpInTimestamps的数量 itmr/s icmpInTimestampReps的数量 otm/s icmpInTimestampReps的数量 otmr/s icmpOutTimestampReps的数量 iadrmk/s ICMP 地址掩码请求消息的数量 iadrmkr/s ICMP 地址掩码回复消息的数量 oadrmk/s ICMP 地址掩码请求消息的数量 oadrmkr/s 发送的 ICMP 地址掩码回复消息的数量

sar -n IP 1

19时11分13秒 irec/s fwddgm/s idel/s orq/s asmrq/s asmok/s fragok/s fragcrt/s irec/s 接收到的报文数 fwddgm/s 转发的报文数 idel/s 成功发送的报文数 orq/s 向IP层提供的报文数 asmrq/s 接收的IP分片数 asmok/s 成功重组的IP报文数 fragok/s 成功分片的报文数 fragcrt/s 产生的分片数

sar -n SOCK 1

19时12分15秒 totsck tcpsck udpsck rawsck ip-frag tcp-tw totsck 被使用的socket数 tcpsck TCP的socket数 udpsck UDP的socket数 rawsck RAW的socket数 if-frag IP分片的数 tcp-tw 处于TIME-WAIT状态的连接数量

sar -n SOFT 1

19时12分38秒 CPU total/s dropd/s squeezd/s rx_rps/s flw_lim/s total/s 网络帧数 dropd/s 溢出丢弃的网络帧数 squeezd/s 软中断的次数 rx_rps/s 唤醒cpu的次数 flw_lim/s 达到流量限制的次数

sar -n TCP 1

19时14分45秒 active/s passive/s iseg/s oseg/s active/s:主动连接数 passive/s:被动连接数 iseg/s:每秒接收的段总数 oseg/s:每秒发送的段总数

sar -n ETCP 1

19时20分22秒 atmptf/s estres/s retrans/s isegerr/s orsts/s atmptf/s 重试失败数 estres/s 断开连接数 retrans/s 重传数 isegerr/s 错误数 orsts/s 包含RST flag的tcp段数

sar -n UDP 1

19时16分14秒 idgm/s odgm/s noport/s idgmerr/s idgm/s 每次接收的 UDP 数据报总数 odgm/s 每秒发送的 UDP 数据报总数 noport/s 接收到但是却没有应用程序在指定目的端口的数据个数 idgmerr/s 本机接收到但却无法派发的数据个数

七七八八总算整理完了sar命令的这些用法,但实际上工作中用不到这么多。或者sar的一些监控命令也可以通过其他命令实现。 不过需要留一个印象的是,如果想监控内存,缓存,CPU,IO,网络,中断这些通常需要监控的信息,可以尝试一下sar命令。

参考链接

https://www.man7.org/linux/man-pages/man1/sar.1.html

编辑
2024-01-11
记录知识
0

git提交建议

git commit一直没有一个规范,但是经常翻阅大型仓库例如linux kernel的都能感觉到commit是有一定规范的。为了让自己提交代码更合规点,这里整理了规范和一些注意事项(不一定正确哦),如下分享

我的模板

Module: SubModule: Brief description of the patch ################################################################################# # Reference: https://www.kernel.org/doc/html/v5.4/process/submitting-patches.html # Notice: # 1. Only one problem was solved. # 2. Full description and justification # 3. Only one problem per patch # # Example 1: # Module: SubModule: Brief description of the patch # # Example 2: # Module: SubModule: Brief description of the patch # # Detailed description (if desired) # # Example 3: # [PATCH] Foo: Fix these things # or # [PATCH v2] Foo: Fix these things better # or # [PATCH v3 0/2] comedi: Fix these things # [PATCH v3 1/2] comedi: Fix the first thing # [PATCH v3 2/2] comedi: Fix the second thing #################################################################################

上述是.gitmessage文件,这个文件需要在自己的目录~/.gitmessage
为了使得这个模板生效,可以git config --global commit.template ~/.gitmessage
这样,以后提交就可以按照这个模板了。

介绍内核补丁提交

vim设置

vim ~/.vimrc

filetype plugin indent on syntax on set title set tabstop=8 set softtabstop=8 set shiftwidth=8 set noexpandtab

命令如下
:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab
update-alternatives --config editor

mutt设置

apt install esmtp touch ~/.esmtprc chmod g-rwx ~/.esmtprc chmod o-rwx ~/.esmtprc vim ~/.esmtprc identity "tangfengweny@gmail.com" hostname smtp.gmail.com:587 username "tangfengweny@gmail.com" password "邮箱密码" starttls required set sendmail="/usr/bin/esmtp" set envelope_from=yes set from="tangfeng <tangfengweny@gmail.com>" set use_from=yes set edit_headers=yes

git设置

vim ~/.gitconfig [user] name = tangfeng email = tangfengweny@example.com

更新内核

git fetch origin git rebase origin/yourbranch

checkpatch

apt-get install codespell python-ply python-git vim .git/hooks/post-commit #!/bin/sh exec git show --format=email HEAD | ./scripts/checkpatch.pl --strict --codespell chmod a+x .git/hooks/post-commit

这样提交时就默认检查补丁规范了./scripts/checkpatch.pl --strict --codespell

提交

确保提交的commit信息用 git log --oneline 能够清晰了解补丁内容

发送

了解对应提交的maintainer
git show HEAD | perl scripts/get_maintainer.pl --separator , -- nokeywords --nogit --nogit-fallback --norolestats 如果是特定文件 perl scripts/get_maintainer.pl --separator , --nokeywords --nogit -- nogit-fallback --norolestats -f drivers/staging/vt6655/baseband.c
不要直接把补丁发送到邮件列表,要先制作一封特殊的邮件。如下 git format-patch -o /tmp/ HEAD^ 先发给自己 mutt -H /tmp/0001-*.patch 也可以git发送

vim .gitconfig [sendemail] smtpserver = /usr/bin/esmtp git send-email --annotate HEAD^

对补丁进行版本控制

git format-patch --subject-prefix="PATCH v2"
如果需要对补丁额外修改,应该在补丁下添加---,然后开始解释,git工具会忽略---下面的内容 然后在补充内容后面加剪刀符>8-----8<这样文本会更加清晰 如下:

Signed-off-by: Your Name <my.email@gmail.com> --- Changes since v2: * Made commit message more clear * Corrected grammer in code comment * Used new API instead of depreciated API >8------------------------------------------------------8< drivers/staging/csr/bh.c

这里---下面的是补充的内容

制作补丁集

补丁集也就是对所有提交补丁的汇总,如下
git format-patch -n --subject-prefix="PATCH vY" --cover-letter
最后发送补丁集
git send-email --to <发给谁> --cc <addresses from get_maintainer.pl output> /tmp/*.patch
可以先--dry-run在本地实验后在发送
git send-email --to <发给谁> --cc <addresses from get_maintainer.pl output> -dry-run /tmp/*.patch

参考链接

https://kernelnewbies.org/FirstKernelPatch
https://kernelnewbies.org/PatchPhilosophy