Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2019-10-04 19:45:28 +08:00
commit 3b3e2ab747
4 changed files with 416 additions and 435 deletions

View File

@ -1,225 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (luming)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to compile a Linux kernel in the 21st century)
[#]: via: (https://opensource.com/article/19/8/linux-kernel-21st-century)
[#]: author: (Seth Kenlon https://opensource.com/users/sethhttps://opensource.com/users/greg-p)
How to compile a Linux kernel in the 21st century
======
You don't have to compile the Linux kernel but you can with this quick
tutorial.
![and old computer and a new computer, representing migration to new software or hardware][1]
In computing, a kernel is the low-level software that handles communication with hardware and general system coordination. Aside from some initial firmware built into your computer's motherboard, when you start your computer, the kernel is what provides awareness that it has a hard drive and a screen and a keyboard and a network card. It's also the kernel's job to ensure equal time (more or less) is given to each component so that your graphics and audio and filesystem and network all run smoothly, even though they're running concurrently.
The quest for hardware support, however, is ongoing, because the more hardware that gets released, the more stuff a kernel must adopt into its code to make the hardware work as expected. It's difficult to get accurate numbers, but the Linux kernel is certainly among the top kernels for hardware compatibility. Linux operates innumerable computers and mobile phones, embedded system on a chip (SoC) boards for hobbyist and industrial uses, RAID cards, sewing machines, and much more.
Back in the 20th century (and even in the early years of the 21st), it was not unreasonable for a Linux user to expect that when they purchased a very new piece of hardware, they would need to download the very latest kernel source code, compile it, and install it so that they could get support for the device. Lately, though, you'd be hard-pressed to find a Linux user who compiles their own kernel except for fun or profit by way of highly specialized custom hardware. It generally isn't required these days to compile the Linux kernel yourself.
Here are the reasons why, plus a quick tutorial on how to compile a kernel when you need to.
### Update your existing kernel
Whether you've got a brand new laptop featuring a fancy new graphics card or WiFi chipset or you've just brought home a new printer, your operating system (called either GNU+Linux or just Linux, which is also the name of the kernel) needs a driver to open communication channels to that new component (graphics card, WiFi chip, printer, or whatever). It can be deceptive, sometimes, when you plug in a new device and your computer _appears_ to acknowledge it. But don't let that fool you. Sometimes that _is_ all you need, but other times your OS is just using generic protocols to probe a device that's attached.
For instance, your computer may be able to identify your new network printer, but sometimes that's only because the network card in the printer is programmed to identify itself to a network so it can gain a DHCP address. It doesn't necessarily mean that your computer knows what instructions to send to the printer to produce a page of printed text. In fact, you might argue that the computer doesn't even really "know" that the device is a printer; it may only display that there's a device on the network at a specific address and the device identifies itself with the series of characters _p-r-i-n-t-e-r_. The conventions of human language are meaningless to a computer; what it needs is a driver.
Kernel developers, hardware manufacturers, support technicians, and hobbyists all know that new hardware is constantly being released. Many of them contribute drivers, submitted straight to the kernel development team for inclusion in Linux. For example, Nvidia graphic card drivers are often written into the [Nouveau][2] kernel module and, because Nvidia cards are common, the code is usually included in any kernel distributed for general use (such as the kernel you get when you download [Fedora][3] or [Ubuntu][4]. Where Nvidia is less common, for instance in embedded systems, the Nouveau module is usually excluded. Similar modules exist for many other devices: printers benefit from [Foomatic][5] and [CUPS][6], wireless cards have [b43, ath9k, wl][7] modules, and so on.
Distributions tend to include as much as they reasonably can in their Linux kernel builds because they want you to be able to attach a device and start using it immediately, with no driver installation required. For the most part, that's what happens, especially now that many device vendors are now funding Linux driver development for the hardware they sell and submitting those drivers directly to the kernel team for general distribution.
Sometimes, however, you're running a kernel you installed six months ago with an exciting new device that just hit the stores a week ago. In that case, your kernel may not have a driver for that device. The good news is that very often, a driver for that device may exist in a very recent edition of the kernel, meaning that all you have to do is update what you're running.
Generally, this is done through a package manager. For instance, on RHEL, CentOS, and Fedora:
```
`$ sudo dnf update kernel`
```
On Debian and Ubuntu, first get your current kernel version:
```
$ uname -r
4.4.186
```
Search for newer versions:
```
$ sudo apt update
$ sudo apt search linux-image
```
Install the latest version you find. In this example, the latest available is 5.2.4:
```
`$ sudo apt install linux-image-5.2.4`
```
After a kernel upgrade, you must [reboot][8] (unless you're using kpatch or kgraft). Then, if the device driver you need is in the latest kernel, your hardware will work as expected.
### Install a kernel module
Sometimes a distribution doesn't expect that its users often use a device (or at least not enough that the device driver needs to be in the Linux kernel). Linux takes a modular approach to drivers, so distributions can ship separate driver packages that can be loaded by the kernel even though the driver isn't compiled into the kernel itself. This is useful, although it can get complicated when a driver isn't included in a kernel but is needed during boot, or when the kernel gets updated out from under the modular driver. The first problem is solved with an **initrd** (initial RAM disk) and is out of scope for this article, and the second is solved by a system called **kmod**.
The kmod system ensures that when a kernel is updated, all modular drivers installed alongside it are also updated. If you install a driver manually, you miss out on the automation that kmod provides, so you should opt for a kmod package whenever it is available. For instance, while Nvidia drivers are built into the kernel as the Nouveau driver, the official Nvidia drivers are distributed only by Nvidia. You can install Nvidia-branded drivers manually by going to the website, downloading the **.run** file, and running the shell script it provides, but you must repeat that same process after you install a new kernel, because nothing tells your package manager that you manually installed a kernel driver. Because Nvidia drives your graphics, updating the Nvidia driver manually usually means you have to perform the update from a terminal, because you have no graphics without a functional graphics driver.
![Nvidia configuration application][9]
However, if you install the Nvidia drivers as a kmod package, updating your kernel also updates your Nvidia driver. On Fedora and related:
```
`$ sudo dnf install kmod-nvidia`
```
On Debian and related:
```
$ sudo apt update
$ sudo apt install nvidia-kernel-common nvidia-kernel-dkms nvidia-glx nvidia-xconfig nvidia-settings nvidia-vdpau-driver vdpau-va-driver
```
This is only an example, but if you're installing Nvidia drivers in real life, you must also blacklist the Nouveau driver. See your distribution's documentation for the best steps.
### Download and install a driver
Not everything is included in the kernel, and not everything _else_ is available as a kernel module. In some cases, you have to download a special driver written and bundled by the hardware vendor, and other times, you have the driver but not the frontend to configure driver options.
Two common examples are HP printers and [Wacom][10] illustration tablets. If you get an HP printer, you probably have generic drivers that can communicate with your printer. You might even be able to print. But the generic driver may not be able to provide specialized options specific to your model, such as double-sided printing, collation, paper tray choices, and so on. [HPLIP][11] (the HP Linux Imaging and Printing system) provides options to manage jobs, adjust printing options, select paper trays where applicable, and so on.
HPLIP is usually bundled in package managers; just search for "hplip."
![HPLIP in action][12]
Similarly, drivers for Wacom tablets, the leading illustration tablet for digital artists, are usually included in your kernel, but options to fine-tune settings, such as pressure sensitivity and button functionality, are only accessible through the graphical control panel included by default with GNOME but installable as the extra package **kde-config-tablet** on KDE.
There are likely some edge cases that don't have drivers in the kernel but offer kmod versions of driver modules as an RPM or DEB file that you can download and install through your package manager.
### Patching and compiling your own kernel
Even in the futuristic utopia that is the 21st century, there are vendors that don't understand open source enough to provide installable drivers. Sometimes, such companies provide source code for a driver but expect you to download the code, patch a kernel, compile, and install manually.
This kind of distribution model has the same disadvantages as installing packaged drivers outside of the kmod system: an update to your kernel breaks the driver because it must be re-integrated into your kernel manually each time the kernel is swapped out for a new one.
This has become rare, happily, because the Linux kernel team has done an excellent job of pleading loudly for companies to communicate with them, and because companies are finally accepting that open source isn't going away any time soon. But there are still novelty or hyper-specialized devices out there that provide only kernel patches.
Officially, there are distribution-specific preferences for how you should compile a kernel to keep your package manager involved in upgrading such a vital part of your system. There are too many package managers to cover each; as an example, here is what happens behind the scenes when you use tools like **rpmdev** on Fedora or **build-essential** and **devscripts** on Debian.
First, as usual, find out which kernel version you're running:
```
`$ uname -r`
```
In most cases, it's safe to upgrade your kernel if you haven't already. After all, it's possible that your problem will be solved in the latest release. If you tried that and it didn't work, then you should download the source code of the kernel you are running. Most distributions provide a special command for that, but to do it manually, you can find the source code on [kernel.org][13].
You also must download whatever patch you need for your kernel. Sometimes, these patches are specific to the kernel release, so choose carefully.
It's traditional, or at least it was back when people regularly compiled their own kernels, to place the source code and patches in **/usr/src/linux**.
Unarchive the kernel source and the patch files as needed:
```
$ cd /usr/src/linux
$ bzip2 --decompress linux-5.2.4.tar.bz2
$ cd  linux-5.2.4
$ bzip2 -d ../patch*bz2
```
The patch file may have instructions on how to do the patch, but often they're designed to be executed from the top level of your tree:
```
`$ patch -p1 < patch*example.patch`
```
Once the kernel code is patched, you can use your old configuration to prepare the patched kernel config:
```
`$ make oldconfig`
```
The **make oldconfig** command serves two purposes: it inherits your current kernel's configuration, and it allows you to configure new options introduced by the patch.
You may need to run the **make menuconfig** command, which launches an ncurses-based, menu-driven list of possible options for your new kernel. The menu can be overwhelming, but since it starts with your old config as a foundation, you can look through the menu and disable modules for hardware that you know you do not have and do not anticipate needing. Alternately, if you know that you have some piece of hardware and see it is not included in your current configuration, you may choose to build it, either as a module or directly into the kernel. In theory, this isn't necessary because presumably, your current kernel was treating you well but for the missing patch, and probably the patch you applied has activated all the necessary options required by whatever device prompted you to patch your kernel in the first place.
Next, compile the kernel and its modules:
```
$ make bzImage
$ make modules
```
This leaves you with a file named **vmlinuz**, which is a compressed version of your bootable kernel. Save your old version and place the new one in your **/boot** directory:
```
$ sudo mv /boot/vmlinuz /boot/vmlinuz.nopatch
$ sudo cat arch/x86_64/boot/bzImage &gt; /boot/vmlinuz
$ sudo mv /boot/System.map /boot/System.map.stock
$ sudo cp System.map /boot/System.map
```
So far, you've patched and built a kernel and its modules, you've installed the kernel, but you haven't installed any modules. That's the final build step:
```
`$ sudo make modules_install`
```
The new kernel is in place, and its modules are installed.
The final step is to update your bootloader so that the part of your computer that loads before the kernel knows where to find Linux. The GRUB bootloader makes this process relatively simple:
```
`$ sudo grub2-mkconfig`
```
### Real-world compiling
Of course, nobody runs those manual commands now. Instead, refer to your distribution for instructions on modifying a kernel using the developer toolset that your distribution's maintainers use. This toolset will probably create a new installable package with all the patches incorporated, alert the package manager of the upgrade, and update your bootloader for you.
### Kernels
Operating systems and kernels are mysterious things, but it doesn't take much to understand what components they're built upon. The next time you get a piece of tech that appears to not work on Linux, take a deep breath, investigate driver availability, and go with the path of least resistance. Linux is easier than ever—and that includes the kernel.
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/8/linux-kernel-21st-century
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/sethhttps://opensource.com/users/greg-p
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/migration_innovation_computer_software.png?itok=VCFLtd0q (and old computer and a new computer, representing migration to new software or hardware)
[2]: https://nouveau.freedesktop.org/wiki/
[3]: http://fedoraproject.org
[4]: http://ubuntu.com
[5]: https://wiki.linuxfoundation.org/openprinting/database/foomatic
[6]: https://www.cups.org/
[7]: https://wireless.wiki.kernel.org/en/users/drivers
[8]: https://opensource.com/article/19/7/reboot-linux
[9]: https://opensource.com/sites/default/files/uploads/nvidia.jpg (Nvidia configuration application)
[10]: https://linuxwacom.github.io
[11]: https://developers.hp.com/hp-linux-imaging-and-printing
[12]: https://opensource.com/sites/default/files/uploads/hplip.jpg (HPLIP in action)
[13]: https://www.kernel.org/

View File

@ -1,210 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (amwps290 )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Adding themes and plugins to Zsh)
[#]: via: (https://opensource.com/article/19/9/adding-plugins-zsh)
[#]: author: (Seth Kenlon https://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/seth)
Adding themes and plugins to Zsh
======
Expand Z-shell's capabilities with themes and plugins installed with Oh
My Zsh.
![Someone wearing a hardhat and carrying code ][1]
In my [previous article][2], I explained how to get started with [Z-shell][2] (Zsh). For some users, the most exciting thing about Zsh is its ability to adopt new themes. It's so easy to theme Zsh both because of the active community designing visuals for the shell and also because of the [Oh My Zsh][3] project, which makes it trivial to install them.
Theming is one of those changes you notice immediately, so if you don't feel like you changed shells when you installed Zsh, you'll definitely feel it once you've adopted one of the 100+ themes bundled with Oh My Zsh. There's a lot more to Oh My Zsh than just pretty themes, though; there are also hundreds of plugins that add features to your Z-shell environment.
### Installing Oh My Zsh
The [ohmyz.sh][3] website encourages you to install the framework by running a script over the internet from your computer. While the Oh My Zsh project is almost certainly trustworthy, it's generally ill-advised to blindly run scripts on your system. If you want to run the install script, you can download it, read it, and run it after you're satisfied you understand what it's doing.
If you download the script and read it, you may notice that installation is only a three-step process:
#### 1\. Clone oh-my-zsh
First, clone the oh-my-zsh repository into a directory called **~/.oh-my-zsh**:
```
`% git clone http://github.com/robbyrussell/oh-my-zsh ~/.oh-my-zsh`
```
#### 2\. Switch the config file
Next, back up your existing **.zshrc** file and move the default one from the oh-my-zsh install into its place. You can do this in one command using the **-b** (backup) option for **mv**, as long as your version of the **mv** command includes that option:
```
% mv -b \
~/.oh-my-zsh/templates/zshrc.zsh-template \
~/.zshrc
```
#### 3\. Edit the config
By default, Oh My Zsh's configuration is pretty bland, so you might want to reintegrate your custom **~/.zshrc** into the **.oh-my-zsh** config. To do that, append your old config to the end of the new one using the [cat command][4]:
```
`% cat ~/.zshrc~ >> ~/.zshrc`
```
To see the default configuration and learn about some of the options it provides, open **~/.zshrc** in your favorite text editor. The file is well-commented, so it's a great way to get a good idea of what's possible.
For instance, you can change the location of your **.oh-my-zsh** directory. At installation, it resides at the base of your home directory, but modern Linux convention, as defined by the [Free Desktop][5] specification, is to place directories that extend the functionality of applications in the **~/.local/share** directory. You can change it in **~/.zshrc** by editing the line:
```
# Path to your oh-my-zsh installation.
export ZSH=$HOME/.local/share/oh-my-zsh
```
then moving the directory to that location:
```
% mv ~/.oh-my-zsh \
$HOME/.local/share/oh-my-zsh
```
If you're using MacOS, the specification is less clear, but arguably the most appropriate place for the directory is **$HOME/Library/Application\ Support**.
### Relaunching Zsh
After editing the config, you have to relaunch your shell. Before you do that, make sure you've finished any in-progress config changes; for instance, don't change the path of **.oh-my-zsh** then forget to move the directory to its new location. If you don't want to relaunch your shell, you can **source** the config file, just as you can with Bash:
```
% source ~/.zshrc
 .oh-my-zsh git:(master) ✗
```
You can ignore any warnings about missing update files; they will be resolved upon relaunch.
### Changing your theme
Installing Oh My Zsh sets your Z-shell theme to **robbyrussell**, a theme by the project's maintainer. This theme's changes are minimal, mostly involving the color of your prompt.
To view all the available themes, list the contents of the **.oh-my-zsh** theme directory:
```
 .oh-my-zsh git:(master) ✗ ls \
~/.local/share/oh-my-zsh/themes
3den.zsh-theme
adben.zsh-theme
af-magic.zsh-theme
afowler.zsh-theme
agnoster.zsh-theme
[...]
```
To see screenshots of themes before trying them, visit the Oh My Zsh [wiki][6]. For even more themes, visit the [External themes][7] wiki page.
Most themes are simple to set up and use. Just change the value of the theme name in **.zshrc** and reload the config:
```
➜ ~ sed -i \
's/_THEME=\"robbyrussel\"/_THEME=\"linuxonly\"/g' \
~/.zshrc
➜ ~ source ~/.zshrc
seth@darkstar:pts/0-&gt;/home/skenlon (0) ➜
```
Other themes require extra configuration. For example, to use the **agnoster** theme, you must first install the Powerline font. This is an open source font, and it's probably in your software repository if you're running Linux. Install it with:
```
`➜ ~ sudo dnf install powerline-fonts`
```
Set your theme in the config:
```
➜ ~ sed -i \
's/_THEME=\"linuxonly\"/_THEME=\"agnoster\"/g' \
~/.zshrc
```
and then relaunch (a simple **source** won't work). Upon relaunch, you will see the new theme:
![agnoster theme][8]
### Installing plugins
Over 200 plugins ship with Oh My Zsh, and you can see them by looking in **.oh-my-zsh/plugins**. Each plugin directory has a README file explaining what the plugin does.
Some plugins are relatively simple. For instance, the **dnf**, **ubuntu**, **brew**, and **macports** plugins are collections of aliases to simplify interactions with the DNF, Apt, Homebrew, and MacPorts package managers.
Others are more complex. The **git** plugin, active by default, detects when you're working in a [Git repository][9] and updates your shell prompt so that it lists the current branch and even indicates whether there are unmerged changes.
To activate a plugin, add it to the plugin setting in **~/.zshrc**. For example, to add the **dnf** and **pass** plugins, open **~/.zshrc** in your favorite text editor:
```
`plugins=(git dnf pass)`
```
Save your changes and reload your Zsh session:
```
`% source ~/.zshrc`
```
The plugins are now active. You can test the **dnf** plugin by using one of the aliases it provides:
```
% dnfs fop
====== Name Exactly Matched: fop ======
fop.noarch : XSL-driven print formatter
```
Different plugins do different things, so you may want to install only one or two at a time to help you learn the new capabilities of your shell.
#### Cheating
Some Oh My Zsh plugins are pretty generic. If you look at a plugin that claims to be a Z-shell plugin and the code is also compatible with Bash, then you can use it in your Bash shell. Some plugins require Z-shell-specific functions, so this won't work with all of them. But you can load plugins like **dnf**, **ubuntu**, **[firewalld][10]**, and others into a Bash shell by using **source** to load the plugin of your choice. For example:
```
if [ -d $HOME/.local/share/oh-my-zsh/plugins ]; then
        source $HOME/.local/share/oh-my-zsh/plugins/dnf/dnf.plugin.zsh
fi
```
### To Z or not to Z
Z-shell is a powerful shell both for its built-in features and the plugins contributed by its passionate community. Whether you use it as your primary shell or just as a shell you visit on weekends or holidays, you owe it to yourself to try it out.
What are your favorite Z-shell themes and plugins? Tell us in the comments!
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/9/adding-plugins-zsh
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/build_structure_tech_program_code_construction.png?itok=nVsiLuag (Someone wearing a hardhat and carrying code )
[2]: https://opensource.com/article/19/9/getting-started-zsh
[3]: https://ohmyz.sh/
[4]: https://opensource.com/article/19/2/getting-started-cat-command
[5]: http://freedesktop.org
[6]: https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
[7]: https://github.com/robbyrussell/oh-my-zsh/wiki/External-themes
[8]: https://opensource.com/sites/default/files/uploads/zsh-agnoster.jpg (agnoster theme)
[9]: https://opensource.com/resources/what-is-git
[10]: https://opensource.com/article/19/7/make-linux-stronger-firewalls

View File

@ -0,0 +1,222 @@
[#]: collector: (lujun9972)
[#]: translator: (luming)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to compile a Linux kernel in the 21st century)
[#]: via: (https://opensource.com/article/19/8/linux-kernel-21st-century)
[#]: author: (Seth Kenlon https://opensource.com/users/sethhttps://opensource.com/users/greg-p)
在 21 世纪编译你的 Linux 内核
======
也许你并不需要编译 Linux 内核,但你能通过这篇教程快速上手。
![and old computer and a new computer, representing migration to new software or hardware][1]
在计算机世界里,<ruby>内核<rt>kernel</rt><ruby>是处理硬件与一般系统之间通信的<ruby>低阶软件<rt>low-level software</rt></ruby>。除过一些烧录进计算机主板的初始固件,当你启动计算机时,内核让系统意识到它有一个硬盘驱动器、屏幕、键盘以及网卡。分配给每个部件(或多或少)相等时间使得图像、音频、文件系统和网络都流畅甚至并行地运行。
然而,对于硬件的需求是源源不断的,因为释放的硬件越多,内核就必须采用更多代码来保证硬件正常工作。得到具体的数字很困难,但是 Linux 内核无疑是硬件兼容性方面的顶级内核之一。Linux 操作着无数的计算机和移动电话工业用途和爱好者使用的板级嵌入式系统SoCRAID 卡,缝纫机等等。
回到 20 世纪甚至是21世纪初期对于 Linux 用户来说,在刚买到新的硬件后就需要下载最新的内核代码并编译安装才能使用这是不可理喻的。而现在你也很难见到 Linux 用户编译内核是除了好玩或通过高度专业化定制的硬件赚钱。现在,通常已经不需要再编译 Linux 内核了。
这里列出了一些原因以及快速编译内核的教程。
### 更新当前的内核
无论你买了配备新显卡或 Wifi 芯片集的新品牌电脑还是给家里配备一个新的打印机,你的操作系统(称为 GNU+Linux 或 Linux它也是内核的名字需要一个驱动来打开新部件显卡芯片集打印机和其他任何东西的信道。有时候当你插入某些新的设备时你可能会误认为电脑已经具备了驱动。但是别被骗到了有时候那就是你需要的驱动但更多的情况是你的操作系统仅仅使用了通用的协议来检测新的设备是否安装。
例如,你的计算机也许能够鉴别出新的网络打印机,但有时候那仅仅是因为打印机的网卡为了获得 DHCP 地址被设计成在网络上标识自己。它并不意味着你的计算机知道如何发送文档给打印机。事实上你可以认为计算机甚至不知道那台设备是一个打印机。它也许仅仅在网络上标识出自己的地址和一系列字符“p-r-i-n-t-e-r”。人类语言的便利性对于计算机毫无意义。计算机需要的是一个驱动。
内核开发者,硬件制造商,技术支持和爱好者都知道新的硬件会不断地发布。它们大多数都会贡献驱动,直接提交给内核开发团队以包含在 Linxu 中。例如,英伟达显卡驱动通常都会写入 [Nouveau][2] 内核模块中,并且因为英伟达显卡很常用,它的代码都包含在任一个日常使用的发行版内核中(例如当下载 [Fedora][3] 或 [Ubuntu][4] 得到的内核)。英伟达也有不常用的地方,例如嵌入式系统中 Nouveau 模块通常被移除。对其他设备来说也有类似的模块:打印机得益于 [Foomatic][5] 和 [CUPS][6],无线网卡有 [b43, ath9k, wl][7] 模块等等。
发行版往往会在它们 Linux 内核的构建中包含尽可能多合理的驱动,因为他们想让你在接入新设备时不用安装驱动能够立即使用。对于大多数情况来说都是非常令人开心的,尤其是现在很多设备厂商都在资助自己售卖硬件的 Linux 驱动开发,并且直接将这些驱动提交给内核团队以用在通常的发行版上。
有时候,或许你正在运行六个月之前安装的内核,并配备了上周刚刚上市令人兴奋的新设备。在这种情况下,你的内核也许没有那款设备的驱动。好消息是经常会出现那款设备的驱动在最近版本的内核中出现,意味着你只要更新运行的内核就可以了。
通常,这些都是通过安装包管理软件完成的。例如在 RHELCentOS 和 Fedora 上:
```
`$ sudo dnf update kernel`
```
在 Debian 和 Ubuntu 上,首先获取你当前内核的版本:
```
$ uname -r
4.4.186
```
搜索新的版本:
```
$ sudo apt update
$ sudo apt search linux-image
```
安装找到的最新版本。在这个例子中,最新的版本是 5.2.4
```
`$ sudo apt install linux-image-5.2.4`
```
内核更新后,你必须 [reboot][8] (除非你使用 kpatch 或 kgraft。这时如果你需要的设备驱动包含在最新的内核中你的硬件就会正常工作。
### 安装内核模块
有时候一个发行版没有预计到用户会使用某个设备(或者设备的驱动至少不足以包含在 Linux 内核中。Linux 对于驱动采用模块化方式因此尽管驱动没有编译进内核但发行版可以推送单独的驱动包让内核去加载。尽管有些复杂当驱动没有包含进内核中而是在引导过程中加载或是从模块驱动下得到更新时非常有用。第一个问题可以用“initrd”解决初始 RAM 磁盘这一点超出了本文的讨论范围第二点通过“kmod”系统解决。
kmod 系统保证了当内核更新后,所有与之安装的模块化驱动也得到更新。如果你手动安装一个驱动,你就体验不到 kmod 提供的自动化,因此只要当 kmod 安装包可用时,就应该选择它。例如,尽管英伟达驱动构建在内核中作为 Nouveau 模块,但官方的驱动仅由英伟达发布。你可以去网站上手动安装英伟达旗下的驱动,下载“.run”文件并运行提供的 shell 脚本,但在安装完新的内核之后你必须重复相同的过程,因为没有任何东西告诉包管理软件你手动安装了一个内核驱动。英伟达驱动着你的显卡,手动更新英伟达驱动通常意味着你需要通过终端来执行更新,因为没有显卡驱动将无法显示。
![Nvidia configuration application][9]
然而,如果你通过 kmod 包安装英伟达驱动,更新你的内核也会更新你的英伟达驱动。在 Fedora 和相关的发行版中:
```
`$ sudo dnf install kmod-nvidia`
```
在 Debian 和相关发行版上:
```
$ sudo apt update
$ sudo apt install nvidia-kernel-common nvidia-kernel-dkms nvidia-glx nvidia-xconfig nvidia-settings nvidia-vdpau-driver vdpau-va-driver
```
这仅仅是一个例子,但是如果你真的要安装英伟达驱动,你也必须屏蔽掉 Nouveau 驱动。参考你使用发行版的文档获取最佳的步骤吧。
### 下载并安装驱动
不是所有的东西都包含在内核中,也不是所有的东西都可以作为内核模块使用。在某些情况下,你需要下载一个由供应商编写并绑定好的特殊驱动程序,还有一些情况,你有驱动,但是没有前端配置驱动程序。
有两个常见的例子是 HP 打印机和 [Wacom][10] 数位板。如果你有一台 HP 打印机,你可能有通用的驱动和打印机通信,甚至能够打印出东西。但是通用的驱动却不能为特定型号的打印机提供定制化的选项,例如双面打印,校对,纸盒选择等等。[HPLIP][11]HP Linux 成像和打印系统)提供了选项来进行任务管理,调整打印设置,选择可用的纸盒等等。
HPLIP 通常包含在包管理软件中只要搜索“hplip”就行了。
![HPLIP in action][12]
同样的,电子艺术家主要使用的数位板 Wacom 驱动通常也包含在内核中,但是例如调整压感和按键功能等设置只能通过包含在 GNOME 的默认图形控制面板中访问。但也可以作为 KDE 上额外的程序包“kde-config-tablet”来访问。
这里也有几个类似的例子,例如内核中没有驱动,但可以作为 RPM 或 DEB 文件下载并且通过包管理软件安装来提供 kmod 版本的驱动。
### 打上补丁并编译你的内核
即使在 21 世纪的未来主义乌托邦里,仍有厂商不够了解开源,无法提供可安装的驱动。有时候,一些公司为驱动提供开源代码,希望你下载代码、修补内核、编译并手动安装。
这种发布方式和在 kmod 系统之外安装打包的驱动程序拥有同样的缺点:对内核的更新会破坏驱动,因为每次更换新的内核时都必须手动将其重新集成到内核中。
令人高兴的是,这一点变得少见了,因为 Linux 内核团队在高声呼唤公司与他们交流方面做得很好,并且公司最终接受了开源不会很快消失的事实。但仍有新奇的或高度专业的设备仅提供了内核补丁。
官方上,对于你如何编译内核以使包管理器参与到升级系统如此重要的部分中,发行版有特定的选项。这里有太多的包管理器,所以无法一一涵盖。举一个例子,当你使用 Fedora 上的工具例如“rpmdev”或“build-essential”Debian 上的“devscripts”。
首先,像通常那样,找到你正在运行内核的版本:
```
`$ uname -r`
```
在大多数情况下,如果你还没有升级过内核那么升级是安全的。搞定之后,也许你的问题就会在最新发布的内核中解决。如果你尝试后发现不起作用,那么你应该下载正在运行内核的源码。大多数发行版提供了特定的命令,但是手动操作的话,可以在 [kernel.org][13] 上找到它的源代码。
你必须下载内核所需的任何补丁。有时候,这些补丁对应具体的内核版本,因此请谨慎选择。
通常,或是至少回到人们习惯于编译内核的时候,都是替换源代码并在`/usr/src/linux` 打上补丁。
解压内核源码并打上需要的补丁:
```
$ cd /usr/src/linux
$ bzip2 --decompress linux-5.2.4.tar.bz2
$ cd  linux-5.2.4
$ bzip2 -d ../patch*bz2
```
补丁文件也许包含如何使用的教程,但通常它们都设计成在内核源码树的顶层可用来执行。
```
`$ patch -p1 < patch*example.patch`
```
当内核代码打上补丁后,你可以继续使用旧的配置。
```
`$ make oldconfig`
```
`make oldconfig` 命令有两个作用:它继承了当前的内核配置,并且允许你配置补丁带来的新的选项。
你或许需要运行 `make menuconfig` 命令,它启动了一个基于
ncurses 的菜单界面,列出了新的内核所有可能的选项。整个菜单可能看不过来,但是它是以旧的内核配置为基础的,你可以查看菜单并且禁用掉你不需要的硬件模块。另外,如果你知道自己有一些硬件没有包含在当前的配置中,你可以选择构建它,当作模块或者直接嵌入内核中。理论上,这些并不是必要的,因为你可以猜想,当前的内核运行良好只是缺少了补丁,当使用补丁的时候可能已经激活了所有设备所必要的选项。
下一步,编译内核和它的模块:
```
$ make bzImage
$ make modules
```
这会产生一个叫作 `vmlinuz` 的文件,它是可引导内核的压缩版本。保存旧的版本并在`/boot`文件夹下替换新的。
```
$ sudo mv /boot/vmlinuz /boot/vmlinuz.nopatch
$ sudo cat arch/x86_64/boot/bzImage &gt; /boot/vmlinuz
$ sudo mv /boot/System.map /boot/System.map.stock
$ sudo cp System.map /boot/System.map
```
到目前为止,你已经打上了补丁并且编译了内核和它的模块,你安装了内核,但你并没有安装任何模块。那就是最后的步骤:
```
`$ sudo make modules_install`
```
新的内核已经就位,并且它的模块也已经安装。
最后一步是更新你的 bootloader为了让你的计算机在加载 linux 内核之前知道它的位置。GRUB bootloader 使这一过程变得相当简单:
```
`$ sudo grub2-mkconfig`
```
### 现实生活中的编译
Of course, nobody runs those manual commands now. Instead, refer to your distribution for instructions on modifying a kernel using the developer toolset that your distribution's maintainers use. This toolset will probably create a new installable package with all the patches incorporated, alert the package manager of the upgrade, and update your bootloader for you.当然,现在没有人手动执行这些命令。相反的,参考你的发行版,寻找修改内核的维护人员使用的工具集。这些工具集可能会创建一个集成所有补丁的软件包,告诉你的包管理器来升级并更新你的 bootloader。
### 内核
操作系统和内核都是玄学,但要理解构成它们的组件并不难。下一次你看到某个技术无法应用在 Linux 上时深呼吸调查可用的驱动寻找一条捷径。Linux 比以前简单多了——包括内核。
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/8/linux-kernel-21st-century
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[LuMing](https://github.com/LuuMing)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/sethhttps://opensource.com/users/greg-p
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/migration_innovation_computer_software.png?itok=VCFLtd0q (and old computer and a new computer, representing migration to new software or hardware)
[2]: https://nouveau.freedesktop.org/wiki/
[3]: http://fedoraproject.org
[4]: http://ubuntu.com
[5]: https://wiki.linuxfoundation.org/openprinting/database/foomatic
[6]: https://www.cups.org/
[7]: https://wireless.wiki.kernel.org/en/users/drivers
[8]: https://opensource.com/article/19/7/reboot-linux
[9]: https://opensource.com/sites/default/files/uploads/nvidia.jpg (Nvidia configuration application)
[10]: https://linuxwacom.github.io
[11]: https://developers.hp.com/hp-linux-imaging-and-printing
[12]: https://opensource.com/sites/default/files/uploads/hplip.jpg (HPLIP in action)
[13]: https://www.kernel.org/

View File

@ -0,0 +1,194 @@
[#]: collector: (lujun9972)
[#]: translator: (amwps290)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Adding themes and plugins to Zsh)
[#]: via: (https://opensource.com/article/19/9/adding-plugins-zsh)
[#]: author: (Seth Kenlon https://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/seth)
给 Zsh 添加主题和插件
======
通过 Oh My Zsh 安装的主题和插件来扩展 Zsh 的功能
![Someone wearing a hardhat and carrying code ][1]
在我的[前文][2]中,我向大家展示了如何安装并使用 [Z-Shell][2] (Zsh)。对于某些用户来说Zsh 最令人激动的是它可以安装主题。Zsh 安装主题非常容易,一方面是因为有非常活跃的社区为 Z-Shell 设计主题,另一方面是因为有 [Oh My Zsh][3] 这个项目。这使得安装主题变得轻而易举。
主题的变化可能会立刻吸引你的注意力,因此如果你安装了 Zsh 并且将默认的 Shell 替换为 Zsh 时,你可能不喜欢 Shell 默认主题的样子,那么你可以立即更换 Oh My Zsh 自带的 100+ 的主题。Oh My Zsh 不仅拥有大量精美的主题,同时还有数以百计的扩展 Zsh 功能的插件。
### 安装 Oh My Zsh
Oh My Zsh 的[官网][3]建议你使用一个脚本在有网络的情况下来安装这个包。尽管 Oh My Zsh 项目几乎是可以令人信服的,但是盲目地在你的电脑上运行一个脚本这是一个不好的建议。如果你想运行这个脚本,你可以把它下载下来,看一下它实现了什么功能,在你确信你已经了解了它的所作所为之后,你就可以运行它了。
如果你下载了脚本并且阅读了它,你就会发现安装过程仅仅只有三步:
#### 1\. 克隆 oh-my-zsh
第一步,克隆 oh-my-zsh 库到 **~/.oh-my-zsh** 目录:
```
`% git clone http://github.com/robbyrussell/oh-my-zsh ~/.oh-my-zsh`
```
#### 2\. 切换配置文件
下一步,备份你已经存在的 **.zshrc** 文件,然后将 oh-my-zsh 自带的配置文件移动到这个地方。这两步操作可以一步完成,只需要你的 **mv** 命令支持 **-b** 这个选项。
```
% mv -b \
~/.oh-my-zsh/templates/zshrc.zsh-template \
~/.zshrc
```
#### 3\. 编辑配置文件
默认情况下Oh My Zsh 自带的配置文件是非常简陋的。如果你想将你自己的 **~/.zshrc** 文件合并到 **.oh-my-zsh** 的配置文件中。你可以使用 [cat][4] 命令将你的旧的配置文件添加到新文件的末尾。
```
`% cat ~/.zshrc~ >> ~/.zshrc`
```
看一下默认的配置文件以及它提供的一些选项。用你最喜欢的编辑器打开 **~/.zshrc** 文件。这个文件有非常良好的注释。这是了解它的一个非常好的方法。
例如,你可以更改 **.oh-my-zsh** 目录的位置。在安装的时候,它默认是位于你的家目录。但是,根据 [Free Desktop][5] 所定义的现代 Linux 规范。这个目录应当放置于 **~/.local/share** 。你可以在配置文件中进行修改。如下所示:
```
# Path to your oh-my-zsh installation.
export ZSH=$HOME/.local/share/oh-my-zsh
```
然后将 .oh-my-zsh 目录移动到你新配置的目录下
```
% mv ~/.oh-my-zsh \
$HOME/.local/share/oh-my-zsh
```
如果你使用的是 MacOS ,这个目录可能会有点含糊不清,但是最合适的位置可能是在 **$HOME/Library/Application\ Support** 。
### 重新启动 Zsh
编辑配置文件之后,你必须重新启动你的 Shell 。在这之前,你必须确定你的任何操作都已正确完成。例如,在你修改了 **.oh-my-zsh** 目录的路径之后。不要忘记将目录移动到新的位置。如果你不想重新启动你的 Shell 。你可以使用 **source** 命令来使你的配置文件生效。
```
% source ~/.zshrc
 .oh-my-zsh git:(master) ✗
```
你可以忽略任何丢失更新文件的警告;他们将会在重启的时候再次进行解析。
### 更换你的主题
安装好 oh-my-zsh 之后。你可以将你的 Zsh 的主题设置为 **robbyrussell**,这是一个项目维护者的主题。这个主题的更改是非常小的,仅仅是改变了提示符的颜色。
你可以通过列出 **.oh-my-zsh** 目录下的所有文件来查看所有安装的主题:
```
 .oh-my-zsh git:(master) ✗ ls \
~/.local/share/oh-my-zsh/themes
3den.zsh-theme
adben.zsh-theme
af-magic.zsh-theme
afowler.zsh-theme
agnoster.zsh-theme
[...]
```
想在切换主题之前查看一下它的样子。你可以查看 Oh My Zsh 的 [wiki][6] 页面。查看更多主题,可以查看 [External themes][7] wiki 页面。
大部分的主题是非常易于安装和使用的,仅仅需要改变 **.zshrc** 文件中的配置选项然后重新载入配置文件。
```
➜ ~ sed -i \
's/_THEME=\"robbyrussel\"/_THEME=\"linuxonly\"/g' \
~/.zshrc
➜ ~ source ~/.zshrc
seth@darkstar:pts/0-&gt;/home/skenlon (0) ➜
```
其他的主题可能需要一些额外的配置。例如,为了使用 **agnoster** 主题,你必须先安装 Powerline 字体。这是一个开源字体,如果你使用 Linux 操作系统的话,这个字体很可能在你的软件库中存在。使用下面的命令安装这个字体:
```
`➜ ~ sudo dnf install powerline-fonts`
```
在配置文件中更改你的主题:
```
➜ ~ sed -i \
's/_THEME=\"linuxonly\"/_THEME=\"agnoster\"/g' \
~/.zshrc
```
重新启动你的 Sehll一个简单的 **source** 并不会起作用)。一旦重启,你就可以看到新的主题:
![agnoster theme][8]
### 安装插件
Oh My Zsh 有超过 200 的插件,你可以在 **.oh-my-zsh/plugins** 中看到他们。每一个扩展目录下都有一个 README 文件解释了这个插件的作用。
一些插件相当简单。例如,**dnf**, **ubuntu**, **brew**, 和 **macports** 插件仅仅是为了简化与 DNF , Apt Homebres 和 MacPorts 的交互操作而定义的一些别名。
而其他的一些插件则较为复杂, **git** 插件默认是被激活使用的。当你的目录是一个 git 仓库的时候,这个扩展就会更新你的 Shell 提示符,以显示当前的分支和是否有未合并的更改。
为了激活这个扩展,你可以将这个扩展添加到你的配置文件 **~/.zshrc** 中。例如,你可以添加 **dnf****pass** 插件,按照如下的方式更改:
```
`plugins=(git dnf pass)`
```
保存修改,重新启动你的 Shell。
```
`% source ~/.zshrc`
```
这个扩展现在就可以使用了。你可以通过使用 **dnf** 提供的别名来测试一下:
```
% dnfs fop
====== Name Exactly Matched: fop ======
fop.noarch : XSL-driven print formatter
```
不同的插件做不同的事,因此你可以一次安装一两个插件来帮你学习新的特性和功能。
### 兼容性
一些 Oh My Zsh 插件具有通用性。如果你看到一个插件声称它可以与 Bash 兼容,那么它就可以在你自己的 Bash 中使用。另一些插件需要 Zsh 提供的特定功能。因此,他们并不是所有都能工作。但是你可以添加一些其他的插件,例如 **dnf****ubuntu** , **[firewalld][10]**,以及其他的一些插件。你可以使用 **source** 使你的选择生效。例如:
```
if [ -d $HOME/.local/share/oh-my-zsh/plugins ]; then
        source $HOME/.local/share/oh-my-zsh/plugins/dnf/dnf.plugin.zsh
fi
```
### 选择或者不选择 Zsh
Z-shell 的内置功能和它由社区贡献的扩展功能都非常强大。你可以把它当成你的主 Shell 使用,你也可以在你休闲娱乐的时候尝试一下。这取决于你的爱好。
什么是你最喜爱的主题和扩展可以在下方的评论告诉我们!
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/9/adding-plugins-zsh
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[amwps290](https://github.com/amwps290)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/sethhttps://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/build_structure_tech_program_code_construction.png?itok=nVsiLuag (Someone wearing a hardhat and carrying code )
[2]: https://opensource.com/article/19/9/getting-started-zsh
[3]: https://ohmyz.sh/
[4]: https://opensource.com/article/19/2/getting-started-cat-command
[5]: http://freedesktop.org
[6]: https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
[7]: https://github.com/robbyrussell/oh-my-zsh/wiki/External-themes
[8]: https://opensource.com/sites/default/files/uploads/zsh-agnoster.jpg (agnoster theme)
[9]: https://opensource.com/resources/what-is-git
[10]: https://opensource.com/article/19/7/make-linux-stronger-firewalls