2022-06 Monthly Archive
Git tags
git tag A # create a tag
git push --tags # push all tags
git push origin A # push to remote a specific tag
kernel’s Command-line parameters
Q: We already have .config, why another configurations option?
A: The former is config in compile time and cannot change at runtime, the latter is more flexible and can be configured at runtime.
内核命令行参数可以在 grub cmdline 里进行配置。grub 是一个 bootloader,它可以指定应当如何启动内核。常用的 kernel 启动参数有:
- ibt=off: Indirect Branch Tracking security feature
You can add kernel command line using:
vim /etc/default/grub
sudo update-grub # Ubuntu
sudo grub2-mkconfig # CentOS
Logical Volume Manager (LVM)
10 Steps about How to create LVM Volume - UX Techno
Q: What's the relationship/difference between LVM and disk partationing?
A: The core concept in LVM is pooling (池化), With traditional storage, three 1 TB disks are handled individually. With LVM, those same three disks are considered to be 3 TB of aggregated storage capacity. that means you can change the capacity of each volumn, which makes it more flexible.
Some principles for note-taking
- keep the note atomatic;
- do not copy, use your own words;
- do not forget the references.
Gitignore doesn't work on commited files
if the files are few, simply:
git rm --cached /path/to/file
if the number of files is large, just:
git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
git - Applying .gitignore to committed files - Stack Overflow
Microsoft snipping tool not working
Restart Windows File Explorer.
Is Your Snipping Tool Not Working? Here's How to Fix It
Microsoft keeps asking me to change my password
Enable or Disable Password Expiration for Your Microsoft Account | Tutorials
BTW: How to reuse an old password?
I had the same problem but I found out that it only stores your last 5 or so passwords. If you change your password 6 times it will let you reuse the old one. At least that’s how long it took me maybe it was more but you get the idea. Now I have my old password again.
I want to reuse an old password for my Microsoft Account but you will - Microsoft Community
INVLPG
The source operand is a memory address. The processor determines the page that contains that address and flushes the TLB entry for that page.
The INVLPG instruction normally flushes the TLB entry only for the specified page; however, in some cases, it flushes the entire TLB.
INVLPG: Invalidate TLB Entry (x86 Instruction Set Reference)
TAS/CAS
gcc has built-in functions for atomic memory access:
__sync_lock_test_and_set
__sync_val_compare_and_swap
Atomic Builtins - Using the GNU Compiler Collection
Memory barrier (membar, memory fence, 内存屏障)
在单处理器的情况下,不需要任何额外的操作便能保持正确的顺序。 但是在多处理器下 由于每一个处理器都有自己的缓存,这样就可能出现一个 CPU 上的缓存数据与另一个上的缓存数据不一致的问题。
几乎所有的处理器都至少支持一个粗粒度的屏障指令(通常称为 Fence,也叫全屏障),这通常都是最耗时的指令之一(它的开销通常接近甚至超过原子操作指令)。
内存屏障的作用:阻止屏障两边的指令重排序,在内存屏障之前的指令和之后的指令不会由于系统优化等原因而导致乱序。
语义上,内存屏障之前的所有写操作都要写入内存;内存屏障之后的读操作都可以获得同步屏障之前的写操作的结果。因此,对于敏感的程序块,写操作之后、读操作之前可以插入内存屏障。
-
lfence
:load fence(读内存屏障,it combines LoadLoad and LoadStore barriers)。对于 Load Barrier 来说,在指令前插入 Load Barrier,可以让高速缓存中的数据失效,强制从新从主内存加载数据;保证不会将读屏障之后的代码排在读屏障之前(因此在读屏障执行结束时,后面的 load 的代码不会重排到读屏障之前(所以挡得是后面),从而读到了 cache 里 stale 的数据,因为此时我们还没有 invalid cache)。 -
sfence
:store fence(写内存屏障,it is a StoreStore barrier)。对于 Store Barrier 来说,在指令后插入 Store Barrier,能让写入缓存中的最新数据更新写入主内存,让其他线程可见;保证不会将写屏障之前的代码排在写屏障之后(所以挡得是前面)(因此在写屏障执行结束时,前面的代码可以保证已经完全执行完毕了,从而不会在此屏障结束时才开始写,那我们的屏障刷 cache 到 memory 就没有意义了)。 -
mfence
:memory fence(全屏障,读写屏障),前面两者的性质都有。MFENCE = SFENCE + LFENCE。
所以说,lfence/sfence
应该是做了两件事?
- 保证指令顺序不重排,同时
- 进行刷 cache / invalid cache 的操作。
扩展阅读:(8 封私信 / 80 条消息) intel x86系列CPU既然是strong order的,不会出现loadload乱序,为什么还需要lfence指令? - 知乎
金阶之路:内存屏障(Memory Barriers) - 知乎
Memory barrier & Lfence Sfence - 知乎
https://www.kernel.org/doc/Documentation/memory-barriers.txt
乱序执行
低能耗是另一种难以用乱序执行设计所实现的目标。
注意:仅当所有在该指令之前的指令都将他们的结果写入寄存器后,这条指令的结果才会被写入寄存器中。这个过程被称为毕业或者退休周期。
Object code
An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable.
Oculus Quest2 科学激活
(68) 无需软路由 轻松激活oculus quest2 升级固件 oculus商店下载、升级软件(clash TUN模式+共享wifi给其他设备代理所有流量) - YouTube
CORS and mix-content (they are not the same)
Why same origin policy(同源策略)?
值得注意的是同源策略仅适用于脚本,这意味着某网站可以通过相应的 HTML 标签访问不同来源网站上的 图像、CSS 和 动态加载脚本 等资源。
The origin of a JavaScript file is defined by the domain of the HTML page which includes it. So if you include the Google Analytics code with a <script>
-tag, it can do anything to your website but does not have same origin permissions on the Google website.
Mixed content occurs when initial HTML is loaded over a secure HTTPS connection, but other resources (such as images, videos, stylesheets, scripts) are loaded over an insecure HTTP connection.
Where to install software in Linux? /usr/local or /opt?
/usr/local
is for installing files built by the administrator, typically by using the make
command (e.g., ./configure; make; make install
).
/opt
is a directory for installing unbundled packages (i.e. packages not part of the Operating System distribution, but provided by an independent source), each one in its own subdirectory.
Fish shell startup profiling and optimization
Make fish start faster by avoiding ‘alias’ · bsago.me
profiling - How can I profile the Fish shell init? - Stack Overflow
Git core.autocrlf
If you’re on a Windows machine, set it to true
— auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem:
git config --global core.autocrlf true
If you’re on a Linux or macOS machine, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf
to input:
git config --global core.autocrlf input
If you’re a Windows programmer doing a Windows-only project, then you can turn off this functionality:
git config --global core.autocrlf false
Todesk, parsec
可以作为向日葵的替代品。
How to locate Weasel/RIME build error/如何定位小狼毫部署错误?
AppData\Local\Temp has build log.
Type-C USB-C
Type-C is USB-C.
卡片盒笔记法 Zettelkasten
Getting Started • Zettelkasten Method
Trailing characters after a file name
@
such as vmlinux@
in /boot/: Put a slash (/) after each filename if the file is a directory, an asterisk (*) if the file is executable, a vertical bar (|) if it is a FIFO, and an at sign (@) if the file is a symbolic link. ls
tilde such as foo.bar~
: backups created by editors shell - What does the tilde mean at the end of a filename? - Unix & Linux Stack Exchange
asterisk such as mksdcard*
: the file is executable.
ELF (Executable and Linkable Format)
A common standard file format for executable files, object code, shared libraries, and core dumps.
Backport
Backporting is the action of taking parts from a newer version of a software system or software component and porting them to an older version of the same software. It forms part of the maintenance step in a software development process, and it is commonly used for fixing security issues in older versions of the software and also for providing new features to older versions.
Thunderbird grammar check
There is a plugin Language Tool
that can help check the English grammar and spelling, you can check it.
Can a VM vmexit actively?
Yes, it can. it can vmexit by a hypercall: hypercall is like syscall which can switch from no-root mode to root mode. I think the name "hyper" indicate that this call is operate at a higher/meta level, it will change the operation mode rather than the privilege.
If the guest wants to hypercall/vmexit, it means it will know it in a virtualization environment, which is known as the Paravirtualization
Hypercall · Kernelgo's KVM学习笔记
CVE
CVE, short for Common Vulnerabilities and Exposures.
MSR intercept, passthrough, emulation and feature expose
feature exposed | feature NOT exposed | |
---|---|---|
intercept | emulate the MSR r/w | r/w will raise #GP |
passthrough | use the physical MSR to r/w | wrong state |
Reviewer and maintainer
Although everyone can give a reviewed-by if he/she has thoughroughly reviewed the patch. There maybe someone whose reviewed-by is more convincible, and can reduce a lot of work for maintainer.
Difference between ENTRYPOINT and CMD in docker
Dot in struct initialization
Designated Initializers
c - What does dot mean in a struct initializer? - Stack Overflow
IVT(Interrupt Vector Table) and IDT(Interrupt Descriptor Table)
- In the real addressing mode the structure just contains addresses of ISRs. This format of it is known as IVT.
- In the protected mode the structure is more complex and is called IDT.
The IVT is only necessary when the OS boots, because that happens in real mode.
BTW, although we call it vector, actually it is just a number…
x86 - what is the difference between IVT and IDT? - Stack Overflow
2-way Merge and 3-way merge
version control - Why is a 3-way merge advantageous over a 2-way merge? - Stack Overflow
Then how does vscode diff two files without git?
TUN/TAP
tap/tun 提供了一台主机内用户空间的数据传输机制。它虚拟了一套网络接口,这套接口和物理的接口无任何区别,可以配置 IP,可以路由流量,不同的是,它的流量只在主机内流通。
应用场景:Linux 中的 Tun/Tap 介绍 | 学习笔记
其实可以从应用场景里看出来,Clash 的 Tun/Tap 和 Proxifier 的作用一样,都是劫持 App 的流量并且导向到代理软件。
tap/tun 有些许的不同,tun 只操作三层的 IP 包,而 tap 操作二层的以太网帧。
TUN 与 TAP 是操作系统内核中的虚拟网络 设备。不同于普通靠硬件 网络适配器 实现的设备,这些虚拟的网络设备全部用软件实现,并向运行于 操作系统 上的软件提供与硬件的网络设备完全相同的功能。
举个例子,著名代理软件 clash 可以通过开启 TUN 模式,在操作系统中虚拟出一个网卡。
TAP 等同于一个 以太网 设备,它操作 第二层 数据包如 以太网 数据帧。TUN 模拟了 网络层 设备,操作 第三层 数据包比如 IP 数据封包。
网卡工作在第一层与第二层。
Kernel development process
It is imperative that all code contributed to the kernel be legitimately free software. For that reason, code from anonymous (or pseudonymous) contributors will not be accepted. All contributors are required to “sign off” on their code, stating that the code can be distributed with the kernel under the GPL.
A new major kernel release happening every ltwo or three months.
At the beginning of each development cycle, the “merge window” is said to be open.
The changes integrated during the merge window do not come out of thin air; they have been collected, tested, and staged ahead of time.
The merge window lasts for approximately two weeks. At the end of this time, Linus Torvalds will declare that the window is closed and release the first of the “rc” kernels. For the kernel which is destined to be 5.6, for example, the release which happens at the end of the merge window will be called 5.6-rc1. The -rc1 release is the signal that the time to merge new features has passed, and that the time to stabilize the next kernel has begun.
Linus releases new -rc kernels about once a week; a normal series will get up to somewhere between -rc6 and -rc9 before the kernel is considered to be sufficiently stable and the final release is made.
Once a stable release is made, its ongoing maintenance is passed off to the “stable team,” currently Greg Kroah-Hartman. The stable team will release occasional updates to the stable release using the 5.x.y numbering scheme.
Design work is often done without involving the community, but it is better to do this work in the open if at all possible; it can save a lot of time redesigning things later.
There is exactly one person who can merge patches into the mainline kernel repository: Linus Torvalds. But, for example, of the over 9,500 patches which went into the 2.6.38 kernel, only 112 (around 1.3%) were directly chosen by Linus himself.
Please note that most maintainers also have day jobs, so merging your patch may not be their highest priority.
When the patch is getting close to ready for mainline inclusion, it should be accepted by a relevant subsystem maintainer - though this acceptance is not a guarantee that the patch will make it all the way to the mainline. The patch will show up in the maintainer’s subsystem tree and into the -next trees (described below). When the process works, this step leads to more extensive review of the patch and the discovery of any problems resulting from the integration of this patch with work being done by others.
Since each maintainer in the chain trusts those managing lower-level trees, this process is known as the “chain of trust.”
When the merge window opens, top-level maintainers will ask Linus to “pull” the patches they have selected for merging from their repositories. If Linus agrees, the stream of patches will flow up into his repository, becoming part of the mainline kernel.
What if somebody wants to look at all of the patches which are being prepared for the next merge window? The answer comes in the form of -next trees, where subsystem trees are collected for testing and review.
The primary tree for next-cycle patch merging is linux-next, maintained by Stephen Rothwell. The linux-next tree is, by design, a snapshot of what the mainline is expected to look like after the next merge window closes.
Linux-next has become an integral part of the kernel development process; all patches merged during a given merge window should really have found their way into linux-next some time before the merge window opens.
For example, net.git is David Miller's kernel tree.
net-next.git is the tree containing all patches submitted to Linus for the Next kernel merge window.
Some major subsystem maintainers use quilt to manage patches intended to go upstream.
Developers may start to generate reformatting patches as a way of gaining familiarity with the process, or as a way of getting their name into the kernel changelogs - or both. But pure coding style fixes are seen as noise by the development community; they tend to get a chilly reception. So this type of patch is best avoided.
#ifdef use should be confined to **header files **whenever possible.
If you are tempted to define a macro, consider creating an inline function instead. The code which results will be the same, but inline functions are easier to read, do not evaluate their arguments multiple times, and allow the compiler to perform type checking on the arguments and return value.
Each patch should yield a kernel which builds and runs properly; if your patch series is interrupted in the middle, the result should still be a working kernel.
Acked-by: indicates an agreement by another developer (often a maintainer of the relevant code) that the patch is appropriate for inclusion into the kernel.
If a patch is considered to be a good thing to add to the kernel, and once most of the review issues have been resolved, the next step is usually entry into a subsystem maintainer’s tree. How that works varies from one subsystem to the next;
Dual function keys on Linux DE
https://gitlab.com/interception/linux/plugins/dual-function-keys
What is webkit?
Every browser is backed by a rendering engine to draw the HTML/CSS web page.
-
IE → Trident(discontinued) - Edge →
EdgeHTML (clean-up fork of Trident)(Edge switched to Blink in 2019) - Firefox → Gecko
-
Opera → Presto(no longer uses Presto since Feb 2013, consider Opera = Chrome, therefore Blink nowadays) - Safari → WebKit
- Chrome → Blink (a fork of Webkit).
https://stackoverflow.com/a/3468236/18644471
kvm_cpu_cap_has And guest_cpuid_has #virt #kvm #code
kvm_cpu_cap_has
represents this feature can be used by KVM, but if your want it to be actually used, you need to do ioctl to enble it in the guest, that's where guest_cpuid_has
will fit in.
kvm_cpu_cap_has
is set by the hypervisor, it cannot be changed by userspace.
Power distribution unit (PDU)
A power distribution unit (PDU) is a device fitted with multiple outputs designed to distribute electric power, especially to racks of computers and networking equipment located within a data center.
Basic PDUs simply provide a means of distributing power from the input to a plurality of outlets. Intelligent PDUs normally have an intelligence module which allow the PDU for remote management of power metering information, power outlet on/off control, and/or alarms. Some advanced PDUs allow users to manage external sensors such as temperature, humidity, airflow, etc.
Power distribution unit - Wikipedia
Two beautiful fonts for web
Lora, Istok Web
海外网站测速
https://www.boce.com/
.img Image img and .iso iso file extension
store raw disk images of floppy disks, hard drives, and optical discs
so it can be used as a disk for virtual machine.
ISO images are another type of optical disc image files.
They are similar.
Git reset --mixed, --soft, and --hard
TLDR:
-
--soft
will only have influence on HEAD; -
--mixed
will have influence on HEAD and index; -
--hard
will have influence on HEAD, index and working directory.
The difference between --mixed
and --soft
is whether or not your index is also modified. So, if we're on branch master
with this series of commits:
- A - B - C (master)
HEAD
points to C
and the index matches C
.
- When we run
git reset --soft B
,master
(and thusHEAD
) now points toB
, but the index still has the changes fromC
, i.e., the change is staged; - When we run
git reset --mixed B
. (Note:--mixed
is the default option). Once again,master
andHEAD
point to B, but this time the index is also modified to matchB
, we still have the unstaged changes in the working directory.
And finally, --hard
is the same as --mixed
(it changes your HEAD
and index), except that --hard
also modifies your working directory.
It should be noted that git reset --hard
and git clean -f
are always used together, to force the workspace to be consistent with a specific revision.
Q: Why git reset
can unstage changes?
A: --mixed
is the default behavior, it only have influence on HEAD and index (stage area) and doesn't influence working directory, so we can use it to unstage changes;
https://stackoverflow.com/a/3528483/18644471
Q: Why sometimes git reset --hard
cannot remove the changes (whether it is staged or unstaged)?
A: They are untracked
files, the untracked files will still present even after the reset command is used.
Another condition maybe this repo has submodules, so its hard to restore to the clean status, you can
git clean -xfdf
git submodule foreach --recursive git clean -xfdf
git reset --hard
git submodule foreach --recursive git reset --hard
git submodule update --init --recursive
git clean -xfdf # delete the untracked submodule
git submodule foreach --recursive git clean -xfdf
to force it to be clean.
Cleans and resets a git repo and its submodules
Q: git reset
on a pushed branch?
A: You can do this (by git push -f
), but it is not recommended. If you don't want to change the branch name, and the branch is not shared to other people yet, you can do this to force a branch change.
SSL
How to apply a free SSL?
Tencent cloud can apply a 1-year free TrustAsia ssl cert.
when applied, download type select "other/其它 ".
How to install a SSL on typesense?
Server Configuration | Typesense
use .crt and .key.
Vim repeat last command
@:
then
@@
Macro in C
An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants.
You may continue the definition onto multiple lines, if necessary, using backslash-newline. When the macro is expanded, however, it will all come out on one line.
The C preprocessor scans your program sequentially. Macro definitions take effect at the place you write them.
When the preprocessor expands a macro name, the macro’s expansion replaces the macro invocation, then the expansion is examined for more macros to expand. For example,
#define TABLESIZE BUFSIZE
#define BUFSIZE 1024
TABLESIZE
→ BUFSIZE
→ 1024
If the expansion of a macro contains its own name, either directly or via intermediate macros, it is not expanded again when the expansion is examined for more macros. This prevents infinite recursion.
To define a function-like macro, you put a pair of parentheses immediately after the macro name. For example,
#define lang_init() c_init()
A function-like macro is only expanded if its name appears with a pair of parentheses after it.
extern void foo(void);
#define foo() /* optimized inline version */ …
foo(); // the macro one for it appears with a pair of parentheses after it
funcptr = foo; // the extern one
You insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers.
macro (array[x = y, x + 1])
passes two arguments to macro
: array[x = y
and x + 1]
. If you want to supply array[x = y, x + 1]
as an argument, you can write it as array[(x = y, x + 1)]
, which is equivalent C code.
All arguments to a macro are completely macro-expanded before they are substituted into the macro body. After substitution, the complete text is scanned again for macros to expand, including the arguments.
min (min (a, b), c)
min (((a) < (b) ? (a) : (b)), (c))
((((a) < (b) ? (a) : (b))) < (c) ? (((a) < (b) ? (a) : (b))): (c))
Macro parameters appearing inside string literals are not replaced by their corresponding actual arguments.
#define foo(x) x, "x"
foo(bar) → bar, "x"
When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument:
#define WARN_IF(EXP) \
do { if (EXP) \
fprintf (stderr, "Warning: " #EXP "\n"); } \
while (0)
WARN_IF (x == 0);
→ do { if (x == 0)
fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0);
The preprocessor backslash-escapes the quotes surrounding embedded string constants, but backslash won't duplicate itself:
p = "foo\n";
results in "p = \"foo\\n\";"
, but ‘\n’ by itself stringizes to "\n"
.
#define COMMAND(NAME) { #NAME, NAME ## _command }
struct command commands[] =
{
COMMAND (quit),
COMMAND (help),
…
};
will result in:
struct command commands[] =
{
{ "quit", quit_command },
{ "help", help_command },
…
};
A macro can be declared to accept a variable number of arguments much as a function can.
\#define eprintf(...) fprintf (stderr, __VA_ARGS__)
// example
eprintf ("%s:%d: ", input_file, lineno)
→ fprintf (stderr, "%s:%d: ", input_file, lineno)
Variadic Macros are a bit complicated and not so important, you can see more in:
Several object-like macros are predefined; you use them without supplying their definitions.
Macros can be redefined and undefined, if an identifier which is currently a macro is redefined, then the new definition must be effectively the same as the old one.
Directives Within Macro Arguments seems not so important, you can see more in:
Directives Within Macro Arguments
Macro Pitfalls
In addition, another pair of parentheses usually surround the entire macro definition.
Why do … while(0)
?, please see Swallowing the Semicolon.
总线、协议和接口
总线(有时会和接口混淆)
它的一个重要特征是由总线上的所有设备共享,可以将计算机系统内的多种设备连接到总线上。如果是某两个设备或设备之间专用的信号连线,就不能称之为总线。
可以看到有很多总线:内存总线、PCIE 总线、USB 总线等等。从使用场景来分,也可以分为:
- QPI 总线:QPI 最初能够发放异彩的是支持多个处理器的服务器平台,QPI 可以用于多处理器之间的互联。
- Memory 总线(内存总线):用来实现处理器和内存的之间的连接。
- 硬盘的总线:SATA, ATA 等。
SATA 总线上一般是 AHCI 协议,不是 NVMe 协议。
协议
协议不像接口,是看不见摸不着的。
NVMe > AHCI
接口
M.2 接口同时支持 SATA 总线以及 PCIE 总线,所以买固态硬盘时一定要看准。
SATA is not just a bus, it is also an interface.1
PCIE is not just a bus, it is also an interface.2
File system and partition
分区之后的每一个分区对于文件系统来说都是独立的存储设备。
文件系统和分区 一般来说 是一对一的关系,但是理论上是可以多对一的。Can we create two filesystems on a single partition? - Server Fault
创建文件系统(windows 下好像俗称格式化)。
挂载就是将存储设备上的文件和目录和已有目录树关联,让你可以通过文件系统的逻辑来访问存储设备。
The disk stores the information about the partitions' locations and sizes in an area known as the partition table that the operating system reads before any other part of the disk. Disk partitioning - Wikipedia
Partition table format
- Master Boot Record (MBR): The available size for block addresses and related information is limited to 32 bits. For hard disks with 512‑byte sectors, the MBR partition table entries allow a maximum size of 2 TiB (2³² × 512‑bytes) or 2.20 TB (2.20 × 10¹² bytes).[1]
- GUID Partition Table (GPT, for UEFI)
- Apple partition map (APM)
- BSD disklabel
The master boot record (MBR) partitioning scheme is used historically in DOS, Microsoft Windows and Linux (among others) on PC-compatible computer systems. As of the mid-2010s, most new computers use the GUID Partition Table (GPT) partitioning scheme instead.
File system driver
File system driver is a concept relative to VFS. From VFS's point of view, it should know how to "drive" a concrete file system, so the file system's implementation in the kernel can be seen as a "driver".
There is no doubt that the device driver is still needed to perform the real action on the physical device.
Here we can learn that the name "driver" is not so specific, it is a general concept.
File system type
Some of them are orthogonal and not mutual exclusive concepts. For example, initrd is a rootfs, a ramdisk fs and a ext2 filesystem.
Tmpfs
Tmpfs is a file system which keeps all of its files in virtual memory.
Everything in tmpfs is temporary in the sense that no files will be created on your hard drive.
By default, a tmpfs ramdisk will be set up to use a maximum of half of the available RAM in the system.
Tmpfs don't allocate any disk space but dynamically use RAM to store their files. As their content is lost when you shutdown the system they are used for files which should not survive a reboot.
An initramfs is an initial ram file system based on tmpfs.
tmpfs 的源码主要在 shmem.c 文件中。
static const struct file_operations shmem_file_operations = {
.mmap = shmem_mmap,
.open = generic_file_open,
.get_unmapped_area = shmem_get_unmapped_area,
#ifdef CONFIG_TMPFS
.llseek = shmem_file_llseek,
.read_iter = shmem_file_read_iter,
.write_iter = generic_file_write_iter,
.fsync = noop_fsync,
.splice_read = generic_file_splice_read,
.splice_write = iter_file_splice_write,
.fallocate = shmem_fallocate,
#endif
};
shmem_file_setup()
Kernel
Get an unlinked file living in tmpfs.
Tmpfs use example
mkdir /tmpfs
mount tmpfs -t tmpfs /tmpfs
touch test
// 给 test 写入 10G 随机数据
fallocate -l 10G test
// punch hole 这个文件的 3G 到 8G 的区域
// punch hole 不会影响文件的大小,但是会影响 free -h 显示出来的 shared 空间的大小
fallocate -n -p -o 3G -l 5G test
-
https://qr.ae/pvP7IT ↩