顯示具有 Linux System 標籤的文章。 顯示所有文章
顯示具有 Linux System 標籤的文章。 顯示所有文章

2018年2月23日 星期五

mount bind

The bind mounts.
Since Linux 2.4.0 it is possible to remount part of the file hierarchy somewhere else. The call is
   mount --bind olddir newdir

remount 只能將當前的掛載點重新掛載;
bind mounts 則可以掛載檔案系統下的檔案目錄,不受限於掛載點

# mkdir dir1 dir2
# touch dir1/dir1_foo
# touch dir2/dir2_foo
# ls -li -R
1177191 drwxr-xr-x 2 root root 4096 Feb 23 07:11 dir1
1177192 drwxr-xr-x 2 root root 4096 Feb 23 07:12 dir2
./dir1:
1085818 -rw-r--r-- 1 root root 0 Feb 23 07:11 dir1_foo
./dir2:
1085819 -rw-r--r-- 1 root root 0 Feb 23 07:12 dir2_foo

###################
# mount -o bind,ro dir1 dir2
mount: warning: dir2 seems to be mounted read-write.
# mount -o bind,ro,remount dir1 dir2
# mount | grep dir2
/home/bh0322/workspace/mount/dir1 on /home/bh0322/workspace/mount/dir2 type none (ro,bind)
在系統就永遠找不到dir2_foo這個檔案了,除非umount dir2

# ls -li -R
1177191 drwxr-xr-x 2 root root 4096 Feb 23 07:11 dir1
1177191 drwxr-xr-x 2 root root 4096 Feb 23 07:11 dir2
./dir1:
1085818 -rw-r--r-- 1 root root 0 Feb 23 07:11 dir1_foo
./dir2:
1085818 -rw-r--r-- 1 root root 0 Feb 23 07:11 dir1_foo

###################
Symbolic link
1. symbolic link 有自己的 inode、檔案和屬性等
2. 可以對不存在的檔案或目錄建立 symbolic link
3. 可以對不同檔案系統或掛載點下的檔案或目錄建立 symbolic link
4. 刪除 symbolic link 不會影響被連結的檔案或目錄;但如果連結的檔案或目錄被删除,symbolic link 會存取錯誤

Hard link
1. 跟被連結的檔案或目錄有相同的 inode、檔案 和屬性等
2. 只能對存在的文件檔案或目錄建立 hard link
3. 不能對不同檔案系統或掛載點下的檔案或目錄建立 hard link
4. 不能目錄建立 hard link
5. 删除 hard link 不會影響被連結的檔案或目錄;但如果連結的檔案或目錄被删除,hard link 仍然可以讀寫
# ln -s dir1 dir3
# ln dir1 dir4
ln: ‘dir1’: hard link not allowed for directory
# ls -li -R
1177191 drwxr-xr-x 2 root root 4096 Feb 23 07:11 dir1
1177191 drwxr-xr-x 2 root root 4096 Feb 23 07:11 dir2
393735 lrwxrwxrwx 1 root root    4 Feb 23 07:12 dir3 -> dir1
./dir1:
1085818 -rw-r--r-- 1 root root 0 Feb 23 07:11 dir1_foo
./dir2:
1085818 -rw-r--r-- 1 root root 0 Feb 23 07:11 dir1_foo

###################
# rm -rf dir2
rm: cannot remove ‘dir2’: Device or resource busy
裡面的檔案都被rm只剩dir2
rm: cannot remove ‘dir2/dir1_foo’: Read-only file system

在 Linux 是可以的。但 bind mount 檔案或 bind mount 到 的 徑上有 麼優點?前 說到 bind mounts 跟 hard link 很像,但 bind mounts 是掛載。除 沒有卸載無法刪除以外,也同樣可以使  mount 的 option, 如:ro、noexec、nouser、nosuid 等,在檔案系統的權限外多增加 增保護。

###################
# strace ln a.txt c.txt
symlink("a.txt", "b.txt")               = 0

# strace ln -s a.txt b.txt
lstat("a.txt", {st_mode=S_IFREG|0664, st_size=6, ...}) = 0
linkat(AT_FDCWD, "a.txt", AT_FDCWD, "c.txt", 0) = 0

參考資料:
https://lwn.net/Articles/281157/
https://wiki.archlinux.org/index.php/Fstab_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)

2017年10月30日 星期一

Ftrace - function tracer

ftrace: /sys/kernel/debug/tracing
Ring buffer: /sys/kernel/debug/tracing/trace
debugfs:
mount | grep debugfs
debugfs on /sys/kernel/debug type debugfs (rw,relatime)


/sys/kernel/debug/tracing/
├── available_events
├── available_filter_functions
├── available_tracers
├── buffer_size_kb
├── buffer_total_size_kb
├── current_tracer
├── dyn_ftrace_total_info
├── enabled_functions
├── events
├── free_buffer
├── function_profile_enabled
├── hwlat_detector
├── instances
├── kprobe_events
├── kprobe_profile
├── max_graph_depth
├── options
├── per_cpu
├── printk_formats
├── README
├── saved_cmdlines
├── saved_cmdlines_size
├── saved_tgids
├── set_event
├── set_event_pid
├── set_ftrace_filter
├── set_ftrace_notrace
├── set_ftrace_pid
├── set_graph_function
├── set_graph_notrace
├── snapshot
├── stack_max_size
├── stack_trace
├── stack_trace_filter
├── trace
├── trace_clock
├── trace_marker
├── trace_marker_raw
├── trace_options
├── trace_pipe
├── trace_stat
├── tracing_cpumask
├── tracing_max_latency
├── tracing_on
├── tracing_thresh
├── uprobe_events
└── uprobe_profile

root@instance-1:/sys/kernel/debug/tracing# cat current_tracer
nop
root@instance-1:/sys/kernel/debug/tracing# echo function > current_tracer

root@instance-1:/sys/kernel/debug/tracing# cat available_tracers
hwlat blk mmiotrace function_graph wakeup_dl wakeup_rt wakeup function nop
root@instance-1:/sys/kernel/debug/tracing# cat set_graph_function
#### all functions enabled ####
root@instance-1:/sys/kernel/debug/tracing# cat buffer_size_kb
7 (expanded: 1408)

1. event
available_events: A list of events that can be enabled in tracing.
set_event: By echoing in the event into this file, will enable that event.
# cat available_events
net:netif_rx_ni_entry
net:netif_rx_entry
net:netif_receive_skb_entry
net:napi_gro_receive_entry
net:napi_gro_frags_entry
net:netif_rx
net:netif_receive_skb
net:net_dev_queue
net:net_dev_xmit
net:net_dev_start_xmit
skb:skb_copy_datagram_iovec
skb:consume_skb
skb:kfree_skb
syscalls:sys_exit_socket
syscalls:sys_enter_socket

# echo 0 > tracing_on
# echo net:netif_receive_skb > set_event
# echo 1 > tracing_on
# cat trace_pipe
# cat trace
# tracer: nop
#
#                              _-----=> irqs-off
#                             / _----=> need-resched
#                            | / _---=> hardirq/softirq
#                            || / _--=> preempt-depth
#                            ||| /     delay
#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
#              | |       |   ||||       |         |
          <idle>-0     [000] ..s.  4175.515810: netif_receive_skb: dev=ens4 skbaddr=ffff91034b184900 len=52
          <idle>-0     [000] ..s.  4175.556855: netif_receive_skb: dev=ens4 skbaddr=ffff91034b184900 len=52

2. function
available_filter_functions / set_ftrace_filter
This lists the functions that ftrace has processed and can trace.
These are the function names that you can pass to
"set_ftrace_filter" or "set_ftrace_notrace".
(See the section "dynamic ftrace" below for more details.)

# cat available_filter_functions
netif_receive_skb
ip_rcv_finish
ip_rcv
ip_local_deliver_finish
ip_local_deliver
ip_forward_finish
ip_forward
ip_output
ip_finish_output2
ip_finish_output
dev_queue_xmit
dev_hard_start_xmit
[...]

3. kprobe: Kernel space;
kprobe_events: Enable dynamic trace points.
kprobe_profile: Dynamic trace points stats.

4. uprobe: User space; See uprobetrace.txt
uprobe_events: Add dynamic tracepoints in programs.
uprobe_profile: Uprobe statistics.

tracer: ftrace, perf, systemtap
debugger: gdb

參考資料:
Debugging the kernel using Ftrace - part 1

https://www.kernel.org/doc/Documentation/trace/ftrace.txt
kernel/linux-4.13/kernel/trace/ftrace.c
http://www.brendangregg.com/blog/2015-07-08/choosing-a-linux-tracer.html
https://www.ibm.com/developerworks/cn/linux/1609_houp_ftrace/index.html

2017年9月7日 星期四

Ubuntu 17.04編譯kernel 4.13

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.04
Release: 17.04
Codename: zesty

# uname -a
Linux instance-1 4.10.0-32-generic #36-Ubuntu SMP Tue Aug 8 12:10:06 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.13.tar.xz
# tar Jxvf linux-4.13.tar.xz
# apt-get install libncurses5-dev make gcc bc libssl-dev
# make menuconfig


# make -j 2
# make modules_install install

# ls /boot/*4.13*
/boot/config-4.13.0
/boot/initrd.img-4.13.0
/boot/System.map-4.13.0
/boot/vmlinuz-4.13.0

How to create Linux Kernel Headers from Linux Kernel Source?
➠ Ubuntu distribution
# apt-get install linux-headers-$(uname -r)
dpkg -l | grep linux-headers-$(uname -r)
ii  linux-headers-3.16.0-77-generic        3.16.0-77.99~14.04.1                       amd64        Linux kernel headers for version 3.16.0 on 64 bit x86 SMP

# ls -al /lib/modules/`uname -r`/
lrwxrwxrwx  1 root root     40 Jun 28  2016 build -> /usr/src/linux-headers-3.16.0-77-generic
ls /usr/src/linux-headers-3.16.0-77
arch  block  crypto  Documentation  drivers  firmware  fs  include  init  ipc  Kbuild  Kconfig  kernel  lib  Makefile  mm  net  samples  scripts  security  sound  tools  ubuntu  usr  virt

➠ Kernel.org
# make headers_install
http://smilejay.com/2013/03/update-linux-headers/

參考資料:
kernel source code

2017年8月31日 星期四

Linux system

一般Linux system分為user space(以下簡稱U), kernel space(以下簡稱K), hw
我們在寫應用程式就是在user space,每個程式都有獨立的virtual memory空間
裡面會有stack, heap, .bss, .data, .text等這些存放資訊與資料
透過檢查memory leak讓系統不會有OOM(out of memory)產生,
透過檢查pointer的部分讓程式不會有segmentation fault產生
程式與程式之間要透過IPC來溝通,有以下這幾種方式
(1) unix domain socket
(2) message queue
(3) shared memory
(4) semaphore
(5) signal
(6) pipe

U跟K中間會有一層system call介面,可用strace這個tool去查看
K大略可分為三大項
(1) Memory
(2) Process
(3) I/O子系統