LX0-103 · Question #31
If the current directory is /root and the kernel source is located in /usr/src/linux, which of the following commands should be used to apply the patch /tmp/foopatch?
The correct answer is D. cd /usr/src/linux; patch -p1 < /tmp/foopatch. Applying a kernel patch requires using patch -p1 with input redirection from the patch file while the working directory is the kernel source tree.
Question
If the current directory is /root and the kernel source is located in /usr/src/linux, which of the following commands should be used to apply the patch /tmp/foopatch?
Options
- Acd /usr/src/linux; cat /tmp/foopatch | patch
- Bcd /usr/src/linux; cat /tmp/foopatch | patch -p0
- Ccd /usr/src/linux; patch -p1 > /tmp/foopatch
- Dcd /usr/src/linux; patch -p1 < /tmp/foopatch
- Ecat /tmp/foopatch | patch -p0
How the community answered
(25 responses)- A4% (1)
- C8% (2)
- D84% (21)
- E4% (1)
Why each option
Applying a kernel patch requires using patch -p1 with input redirection from the patch file while the working directory is the kernel source tree.
Piping through cat without -p1 uses the default -p0, which does not strip any directory prefix, causing patch to fail to locate the target files in the kernel source tree.
-p0 does not strip the leading directory component from patch paths, so the patched file paths will not match the actual file locations under /usr/src/linux.
Using > instead of < redirects output to /tmp/foopatch, overwriting or truncating the patch file rather than reading it as input.
The command cd /usr/src/linux; patch -p1 < /tmp/foopatch changes to the kernel source directory and uses patch with -p1 to strip one leading directory component from paths in the patch file, which is the standard convention for kernel patches generated with diff -u. The < operator correctly reads the patch file as input to the patch command.
Running from /root with -p0 means the working directory is wrong and no directory stripping is applied, so patch cannot find the kernel source files to modify.
Concept tested: Applying patches to kernel source with patch -p1
Source: https://man7.org/linux/man-pages/man1/patch.1.html
Topics
Community Discussion
No community discussion yet for this question.