Skip to content

Instantly share code, notes, and snippets.

@itbakery
Created May 15, 2015 14:00
Show Gist options
  • Save itbakery/3ce4d6fa49ac97e5f4f7 to your computer and use it in GitHub Desktop.
Save itbakery/3ce4d6fa49ac97e5f4f7 to your computer and use it in GitHub Desktop.
LPI1-Topic 101 สถาปัตยกรรมระบบ System Architecture

##Linux Server Professional Certification LPIC-1 : Exam 101 ###Topic 101: สถาปัตยกรรมระบบ System Architecture ####101.1 การระบุและปรับแต่งฮาร์ดแวร์ (Determine and configure hardware setting) มีวัตถุประสงค์เพื่อการเรียนรู้เกี่ยวกับการกำหนดค่าของ hardware ซึ่งเป็นพื้นฐานสำคัญของการศึกษาระบบปฎิบัติการ Linux

  1. HAL, dbus, udev
  2. /sys, /proc , /dev
  3. lsmod, lsusb, lspci และเรียนรู้เกี่ยวกับการกำหนดค่าต่างๆ ที่เกี่ยวข้องกับ Linux
  4. set device ID
  5. kernel module
  6. เพิ่มและลบอุปกรณ์

HAL, d-bus, udev คืออะไร HAL ย่อมาจาก Hardware Abstraction Layer Linux มีความสามารถในการเข้าใจความแตกต่างของอุปกรณ์ เช่น card lan ที่มีความแตกต่างกัน โดยจะมีฐานข้อมูลทุกอุปกรณ์ที่เชื่อมต่อกับระบบโดยฐานข้อมูลจะถูกเก็บอยู่ที่ /sys

# ls /sys
block  bus  class  dev  devices  firmware  fs  hypervisor  kernel  module  power          
# ls /sys/bus/pci/devices/
0000:00:00.0  0000:00:15.1  0000:00:16.3  0000:00:17.5  0000:00:18.7
0000:00:01.0  0000:00:15.2  0000:00:16.4  0000:00:17.6  0000:02:00.0
0000:00:07.0  0000:00:15.3  0000:00:16.5  0000:00:17.7  0000:02:01.0
0000:00:07.1  0000:00:15.4  0000:00:16.6  0000:00:18.0  0000:02:02.0
0000:00:07.3  0000:00:15.5  0000:00:16.7  0000:00:18.1  0000:02:03.0
0000:00:07.7  0000:00:15.6  0000:00:17.0  0000:00:18.2  0000:02:05.0
0000:00:0f.0  0000:00:15.7  0000:00:17.1  0000:00:18.3
0000:00:10.0  0000:00:16.0  0000:00:17.2  0000:00:18.4
0000:00:11.0  0000:00:16.1  0000:00:17.3  0000:00:18.5
0000:00:15.0  0000:00:16.2  0000:00:17.4  0000:00:18.6
# ls /sys/block/
dm-0  dm-1  fd0  sda  sr0
# ls /sys/bus/
ac97         cpu           machinecheck  pci          scsi        workqueue
acpi         event_source  mdio_bus      pci_express  serio       xen
clocksource  hid           memory        platform     usb
container    i2c           node          pnp          usb-serial

dbus คือ วิธีการสำหรับการสื่อสารระหว่างอุปกรณ์และโปรแกรม udev คือ การนำฐานข้อมูลที่ได้จากตรวจสอบอุปกรณ์ที่เชื่อมต่อกับคอมพิวเตอร์ของ HAL แล้ว นำไป mount ที่ /dev/ ของ filesystem เพื่อให้ง่ายและสะดวกต่อการจดจำของผู้ใช้

# ls /dev/
agpgart          initctl             rtc       tty21  tty46  uhid
autofs           input               rtc0      tty22  tty47  uinput
block            kmsg                sda       tty23  tty48  urandom
bsg              log                 sda1      tty24  tty49  usbmon0
btrfs-control    loop-control        sda2      tty25  tty5   usbmon1
bus              lp0                 shm       tty26  tty50  usbmon2
cdrom            lp1                 snapshot  tty27  tty51  vcs
centos           lp2                 snd       tty28  tty52  vcs1
char             lp3                 sr0       tty29  tty53  vcs2
console          mapper              stderr    tty3   tty54  vcs3
core             mcelog              stdin     tty30  tty55  vcs4
cpu              mem                 stdout    tty31  tty56  vcs5
cpu_dma_latency  midi                tty       tty32  tty57  vcs6
crash            mqueue              tty0      tty33  tty58  vcsa
disk             net                 tty1      tty34  tty59  vcsa1
dm-0             network_latency     tty10     tty35  tty6   vcsa2
dm-1             network_throughput  tty11     tty36  tty60  vcsa3
dmmidi           null                tty12     tty37  tty61  vcsa4
dri              nvram               tty13     tty38  tty62  vcsa5
fb0              oldmem              tty14     tty39  tty63  vcsa6
fd               port                tty15     tty4   tty7   vfio
fd0              ppp                 tty16     tty40  tty8   vga_arbiter
full             ptmx                tty17     tty41  tty9   vhost-net
fuse             pts                 tty18     tty42  ttyS0  vmci
hidraw0          random              tty19     tty43  ttyS1  zero
hpet             raw                 tty2      tty44  ttyS2
hugepages        rfkill              tty20     tty45  ttyS3

เมื่อเจาะลึกลงไปใน /dev/ ที่สร้างโดย udev จะพบ mount point ของ Hard disk, cdrom, net

# ls -l /dev/sd*
brw-rw----. 1 root disk 8, 0 May  1 10:11 /dev/sda
brw-rw----. 1 root disk 8, 1 May  1 10:11 /dev/sda1
brw-rw----. 1 root disk 8, 2 May  1 10:11 /dev/sda2

/dev/sda คือ SCSI disk
/dev/sda1 คือpartition 1 ของ /dev/sda ส่วน /dev/sda2 partition2 ในส่วนของ cdrom จะเป็นการสร้าง link ไปที่ file -> sr0

# ls -l /dev/
total 0
crw-rw----. 1 root video    10, 175 May  1 10:11 agpgart
crw-------. 1 root root     10, 235 May  1 10:11 autofs
drwxr-xr-x. 2 root root         180 May  1 10:10 block
drwxr-xr-x. 2 root root          80 May  1 10:10 bsg
crw-------. 1 root root     10, 234 May  1 10:11 btrfs-control
drwxr-xr-x. 3 root root          60 May  1 10:10 bus
lrwxrwxrwx. 1 root root           3 May  1 10:11 cdrom -> sr0
drwxr-xr-x. 2 root root          80 May  1 10:10 centos
drwxr-xr-x. 2 root root        3000 May  1 10:13 char
crw-------. 1 root root      5,   1 May  1 10:11 console
lrwxrwxrwx. 1 root root          11 May  1 10:10 core -> /proc/kcore
drwxr-xr-x. 4 root root         100 May  1 10:10 cpu
crw-------. 1 root root     10,  61 May  1 10:11 cpu_dma_latency
...

ต่อไปคือส่วนของ /proc (proc filesystem) ที่สร้างไม่ได้เป็น file จริง แต่เป็น file ที่สร้างแล้วเก็บไว้ใน ram (virtual file) เมื่อ computer boot ขึ้นมา จะสามารถเขียนหรือแก้ไขค่าต่างๆ ของ Kernel ในขณะที่กำลังทำงาน แต่ค่าตัวแปรดังกล่าวจะเป็นตัวแปรชั่วคราวเท่านั้น หากมีการ reboot เครื่อง ค่าดังกล่าวจะหายไปและต้องใช้สิทธิ Root เท่านั้นสำหรับการใช้งานแก้ไขค่าดังกล่าว

# ls /proc/
1      14     18    228   273   3867  556  624   882        driver
10     140    180   229   274   3884  557  625   884        execdomains
100    141    181   23    275   3890  558  626   8854       fb
101    142    182   230   276   3893  559  627   8861       filesystems
10128  143    183   231   277   39    56   628   89         fs
1019   14394  184   232   278   3906  560  63    890        interrupts
102    144    185   233   279   3908  561  64    891        iomem
1027   145    186   234   28    3916  562  65    892        ioports
103    146    187   235   280   3918  563  653   893        irq
104    14698  188   236   284   3964  564  66    894        kallsyms
105    147    189   237   285   3989  565  67    9          kcore
106    148    19    238   286   40    566  68    90         keys
107    149    190   239   287   4012  567  69    91         key-users
108    14970  191   24    288   4017  568  698   92         kmsg
109    15     192   240   289   4019  569  699   93         kpagecount
10920  150    193   241   29    4041  57   7     931        kpageflags
1099   15052  194   242   298   4047  570  70    94         loadavg
11     151    195   2425  3     4053  576  708   942        locks
110    152    196   2426  30    4055  577  709   95         mdstat
111    153    197   243   300   4063  58   71    951        meminfo
112    154    198   244   301   41    59   72    955        misc
113    155    199   2441  31    4180  590  725   956        modules
114    156    2     2442  3144  42    591  726   957        mounts
115    157    20    245   32    43    593  727   959        mpt
116    158    200   246   321   4314  594  728   96         mtrr
117    159    201   247   33    44    595  729   962        net
118    16     202   248   3394  45    596  73    963        pagetypeinfo
11852  160    203   249   34    46    597  730   964        partitions
119    161    204   25    343   47    598  731   965        sched_debug
12     162    205   250   35    48    60   732   97         scsi
120    163    206   251   3531  49    600  74    973        self
12005  164    207   252   3533  5     602  75    974        slabinfo
121    165    208   253   3541  50    603  7571  975        softirqs
122    166    209   254   3542  51    604  7579  977        stat
1221   167    21    255   36    52    605  7581  98         swaps
123    168    210   256   3607  526   606  76    983        sys
124    1683   211   257   3617  527   607  77    985        sysrq-trigger
125    1687   212   258   3685  528   608  78    989        sysvipc
126    169    213   259   37    529   609  785   99         timer_list
127    1695   214   26    3701  53    61   79    990        timer_stats
128    1696   215   260   3705  530   610  8     991        tty
12803  1698   216   261   3709  534   611  80    997        uptime
129    17     217   262   3720  535   612  81    999        version
13     170    218   263   3734  537   613  819   acpi       vmallocinfo
130    171    219   264   3768  538   614  82    asound     vmstat
131    172    22    265   3772  54    615  83    buddyinfo  zoneinfo
132    1723   220   266   38    547   616  833   bus
1320   173    221   267   3812  548   617  84    cgroups
133    174    222   268   3819  549   618  847   cmdline
134    175    223   2686  3824  55    619  85    consoles
135    176    2230  269   3827  550   62   86    cpuinfo
136    1761   224   27    3841  551   620  87    crypto
137    177    225   2708  3844  552   621  88    devices
138    178    226   271   3854  554   622  880   diskstats
139    179    227   272   3859  555   623  881   dma

ลองอ่านค่า ของ /proc/cpuinfo

# cat /proc/cpuinfo 
processor		: 0
vendor_id		: AuthenticAMD
cpu family		: 16
model			: 6
model name		: AMD Athlon(tm) II P320 Dual-Core Processor
stepping		: 3
microcode		: 0x10000c8
cpu MHz		: 2094.758
cache size		: 512 KB
physical id		: 0
siblings		: 1
core id		: 0
cpu cores		: 1
apicid			: 0
initial apicid	: 0
fpu			: yes
fpu_exception	: yes
cpuid level		: 5
wp			: yes
flags			: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl tsc_reliable nonstop_tsc pni cx16 x2apic popcnt hypervisor lahf_lm extapic cr8_legacy abm sse4a 3dnowprefetch osvw
bogomips		: 4189.51
TLB size		: 1024 4K pages
clflush size		: 64
cache_alignment	: 64
address sizes	: 42 bits physical, 48 bits virtual
power management	:

processor		: 1
vendor_id		: AuthenticAMD
cpu family		: 16
model			: 6
model name		: AMD Athlon(tm) II P320 Dual-Core Processor
stepping		: 3
microcode		: 0x10000c8
cpu MHz		: 2094.758
cache size		: 512 KB
physical id		: 2
siblings		: 1
core id		: 0
cpu cores		: 1
apicid			: 2
initial apicid	: 2
fpu			: yes
fpu_exception	: yes
cpuid level		: 5
wp			: yes
flags			: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl tsc_reliable nonstop_tsc pni cx16 x2apic popcnt hypervisor lahf_lm extapic cr8_legacy abm sse4a 3dnowprefetch osvw
bogomips		: 4189.51
TLB size		: 1024 4K pages
clflush size		: 64
cache_alignment	: 64
address sizes	: 42 bits physical, 48 bits virtual
power management:

ลองอ่านค่า /proc/mounts

# cat /proc/mounts 
rootfs / rootfs rw 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=1924648k,nr_inodes=481162,mode=755 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev 0 0
devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0
tmpfs /sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/mapper/centos-root / xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=31,pgrp=1,timeout=300,minproto=5,maxproto=5,direct 0 0
mqueue /dev/mqueue mqueue rw,seclabel,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0
gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0

ลองอ่านค่า /proc/sys/fs/file-max ซึ่งเป็นจำนวนไฟล์ที่สามารถเปิดได้จากระบบ ค่านี้สามารถเปลี่ยนได้ทันทีด้วยการแก้ไขค่าที่อยู่ใน file-max

# cat /proc/sys/fs/file-max 
382368
# echo 10000000  > /proc/sys/fs/file-max
# cat /proc/sys/fs/file-max 
10000000

ลองอ่านค่า /proc/sys/net/ipv4/ip_forward (เพื่อให้สามารถ forward trafficระหว่าง interface)

# cat /proc/sys/net/ipv4/ip_forward 
1

*** ค่าทั้งหมดที่มีการแก้ไขจะหายไปทันทีเมื่อมีการ reboot ดังนั้นหากต้องการรักษาค่าที่แก้ไขต้องเข้าไปแก้ไข file ที่มีชื่อว่า /etc/sysctl.conf โดยตรง

# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
#
# For more information, see sysctl.conf(5) and sysctl.d(5).

net.ipv4.ip_forward = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.all.rp_filter = 3

คำสั่ง lsmod, lspci, lsusb คืออะไรและมีความสำคัญอย่างไร

คำสั่ง รายละเอียด
lsmod เป็นคำสั่งสำหรับการแสดงผล kernel module
lspci เป็นคำสั่งสำหรับการแสดงผลอุปกรณ์ชนิด pci devices (pciutils package)สามารถติดตั้งโดยคำสั่งyum install pciutils
lsusb เป็นคำสั่งสำหรับการแสดงผลอุปกรณ์ชนิด usb

ทดสอบคำสั่ง lsmod

# lsmod 
Module                  Size  Used by
fuse                   87741  3 
lp                     17759  0 
ip6t_rpfilter          12546  1 
ip6t_REJECT            12939  2 
ipt_REJECT             12541  2 
xt_conntrack           12760  7 
ebtable_nat            12807  0 
ebtable_broute         12731  0 
bridge                115385  1 ebtable_broute
stp                    12976  1 bridge
llc                    14552  2 stp,bridge
ebtable_filter         12827  0 
ebtables               30913  3 ebtable_broute,ebtable_nat,ebtable_filter
ip6table_nat           12864  1 
nf_conntrack_ipv6      18738  5 
nf_defrag_ipv6         34651  1 nf_conntrack_ipv6
nf_nat_ipv6            14131  1 ip6table_nat
ip6table_mangle        12700  1 
ip6table_security      12710  1 
ip6table_raw           12683  1 
ip6table_filter        12815  1 
ip6_tables             27025  5 
…

dm_mirror              22135  0 
dm_region_hash         20862  1 dm_mirror
dm_log                 18411  2 dm_region_hash,dm_mirror
dm_mod                104038  8 dm_log,dm_mirror

ทดสอบคำสั่ง lspci

# lspci
00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)
00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)
00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10)
00:0f.0 VGA compatible controller: VMware SVGA II Adapter
00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)
00:11.0 PCI bridge: VMware PCI bridge (rev 02)
00:15.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.7 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.7 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.7 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01)
02:00.0 USB controller: VMware USB1.1 UHCI Controller
02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
02:02.0 Multimedia audio controller: Ensoniq ES1371 / Creative Labs CT2518 [AudioPCI-97] (rev 02)
02:03.0 USB controller: VMware USB2 EHCI Controller
02:05.0 SATA controller: VMware SATA AHCI controller

นอกจากนี้ยังมีคำสั่ง lspcmcia, lshal ทดสอบคำสั่ง

#yum install hal
#service messagebus restart
#service haldaemon restart
#lshal
คำสั่ง รายละเอียด
# lsmod แสดงรายการ kernel module ทั้งหมด
# rmmod ลบ kernel ออกจาก module โดยดูชื่อจาก /proc/modules
# insmod Insert module เข้าไปใน kernel
# modprobe เป็น program ที่ใช้สำหรับเพิ่มลบ kernel module แต่จะทำหน้าที่ load module ที่เป็น dependencies ด้วย โดยจะทำการอ่านค่าของ modules ที่ /etc/modules/$(uname -r)/modules.dep.bin
# modinfo เรียกดูรายละเอียดของ module นั้น

ทดสอบคำสั่งmodinfo ร่วมกับ lsmod

# lsmod | grep iptable_filter
iptable_filter         12810  1 
ip_tables              27239  5 iptable_security,iptable_filter,iptable_mangle,iptable_nat,iptable_raw
# modinfo iptable_filter 
filename:       /lib/modules/3.10.0-229.1.2.el7.x86_64/kernel/net/ipv4/netfilter/iptable_filter.ko
description:    iptables filter table
author:         Netfilter Core Team <coreteam@netfilter.org>
license:        GPL
rhelversion:    7.1
srcversion:     91D2BD9B036F1510ECEBFF9
depends:        ip_tables
intree:         Y
vermagic:       3.10.0-229.1.2.el7.x86_64 SMP mod_unload modversions 
signer:         CentOS Linux kernel signing key
sig_key:        34:B5:BC:A2:B7:06:D8:2E:72:A5:BE:3E:E4:09:BE:C7:19:5E:A5:08
sig_hashalgo:   sha256
parm:           forward:bool

การดูข้อมูลของ module ที่ชื่อว่า iptable_filterจะเห็นว่า module นี้เรียกใช้งาน kernel module ชื่อว่า iptable_filter.ko และมี depends กับ module ip_tables ทดลองเรียก modinfo ip_tables อีกครั้ง

# modinfo ip_tables
filename:       /lib/modules/3.10.0-229.1.2.el7.x86_64/kernel/net/ipv4/netfilter/ip_tables.ko
description:    IPv4 packet filter
author:         Netfilter Core Team <coreteam@netfilter.org>
license:        GPL
rhelversion:    7.1
srcversion:     44A16130862F8CA2ECA59D9
depends:        
intree:         Y
vermagic:       3.10.0-229.1.2.el7.x86_64 SMP mod_unload modversions 
signer:         CentOS Linux kernel signing key
sig_key:        34:B5:BC:A2:B7:06:D8:2E:72:A5:BE:3E:E4:09:BE:C7:19:5E:A5:08
sig_hashalgo:   sha256

หากต้องการ ให้ module load เป็นอัตโนมัติให้ทำการสร้าง file มีนามสกุล *.conf ไปไว้ที่ /etc/modprobe.d/

# ls -l /etc/modprobe.d/
total 28
-rw-r--r--. 1 root root   52 Dec  6  2013 anaconda.conf
-rw-r--r--. 1 root root  884 Nov 23  2013 blacklist.conf
-rw-r--r--. 1 root root  382 Jun 20  2013 dist-alsa.conf
-rw-r--r--. 1 root root 5596 Jun 20  2013 dist.conf
-rw-r--r--. 1 root root  473 Jun 20  2013 dist-oss.conf
-rw-r--r--. 1 root root   30 Oct  9  2009 openfwwf.conf

การอ้างอิง หรือการกำหนดDevice ID - UUID คืออะไรและใช้งานอย่างไร

UUID ย่อมาจากคำว่า Universal Unique Identifier ซึ่งเป็นการกำหนด ID ตามมาตรฐาน Open Software Foundation(OSF) ที่ไม่ซ้ำกัน เพื่อไม่ให้เกิด error เนื่องจาก partition name สามารถเปลี่ยนได้ตามลำดับ แต่ UUID จะไม่เปลี่ยน เช่น กรณีที่ต้องการ mount hard disk ไว้กับ folder ใน FileSystem สามารถใช้ UUIDได้ ในระบบ linux สามารถใช้คำสั่ง blkid เพื่อดูหมายเลข UUID แล้วนำไปใส่ไว้ใน /etc/fstab เพื่อสร้าง mount point ไปยัง folder ที่ต้องการ

# blkid 
/dev/sda1: UUID="3e7a2254-fa06-4596-ab1d-cf8bc14ee68b" TYPE="xfs" 
/dev/sda2: UUID="7GLdpq-USO3-WELX-5LHX-bofm-WudO-AtOArM" TYPE="LVM2_member" 
/dev/mapper/centos-root: UUID="cbf999a3-4e56-4675-b17c-743464897705" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="9f5d8316-7a24-42af-b623-7f9e7b24c322" TYPE="swap" 

จะสังเกตได้ว่า partition /dev/sda1 มี UUID หมายเลข UUID="3e7a2254-fa06-4596-ab1d-cf8bc14ee68b" เมื่อนำไปใช้งาน ใน /etc/fstab ก็จะได้เป็น

# vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Apr 26 00:06:30 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                        xfs     defaults        0 0
UUID=3e7a2254-fa06-4596-ab1d-cf8bc14ee68b /boot  xfs     defaults        0 0
/dev/mapper/centos-swap swap                     swap    defaults        0 0

และเมื่อตรวจสถานะปัจจุบันของ kernel ด้วย cat /proc/mounts จะเห็นสถานะดังนี้

# cat /proc/mounts
rootfs / rootfs rw 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=1924648k,nr_inodes=481162,mode=755 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev 0 0
devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0
tmpfs /sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/mapper/centos-root / xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=31,pgrp=1,timeout=300,minproto=5,maxproto=5,direct 0 0
mqueue /dev/mqueue mqueue rw,seclabel,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0
gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0

##101.2 ฺกลไกการเริ่มต้นBoot ของระบบ (Boot the system)

bootprocess

Bios (Basic Input/Output System) และBoot process BIOSเป็น firmware ที่อยู่ในเครื่องคอมพิวเตอร์ ทำหน้าที่เตรียมความพร้อมให้กับระบบ (System integrity Checks) สำหรับการเริ่มต้น system initialization ทดสอบหน่วยความจำ และทดสอบอุปกรณ์

bios

หลังจากการตรวจสอบระบบแล้ว BIOS จะทำการเลือก boot order เพื่อเริ่มต้นการ boot ระบบ โดยพื้นฐานสามารถเลือก boot จาก Boot Sequence ได้แก่ cdrom, harddisk, Usb (removable devices) เป็นต้น และจะทำการค้นหา Boot Loader program ถ้าเป็นระบบปฏิบัติการ Linux จะเป็น LILO หรือ GRUB (Grand Unified Bootloader) เมื่อพบแล้วจะทำการ Load และ Execute จากนั้นจะโอนงานต่อให้กับ Master Boot Record (MBR) ซึ่งติดตั้งอยู่ที่ sector แรกของ Disk (/dev/hda /dev/sda) มีขนาดน้อยกว่า 512 byte ประกอบด้วยองค์ประกอบ 3 ส่วนด้วยกัน คือ

1 Primary boot loader info 446 byte 2 Partition Table info 64 byte 3 MBR valiation check 2 byte

ต่อจากนั้น MBR จะทำการ load และ execute ให้ GRUB ทำงาน หากในเครื่องมี Kernelหลายตัว สามารถเลือก boot ได้จากGRUB นอกจากนั้น GRUB จะทำการแสดง Splash screen และพักหน้าจออยู่สักครู่ หลังจากนั้นก็จะ แสดง menu boot เพื่อจะไปที่ default kernel image

bootmenu

Bootloders (grub) grub จะอ่านค่า ( /boot/grub2/grub.cfg) เมื่อเลือกได้แล้วก็จะทำการ load kernel และ init rd (initial ram disk) init rd จะถูกเรียกใช้งานจาก kernel ทำหน้าที่เป็น root filesystem ชั่วคราวจนกระทั่ง kernel บูธ filesystem จริงจนถึงสิ้นสุดกระบวนการ mount root filesystem

# cat /boot/grub2/grub.cfg | grep linux16
	linux16 /vmlinuz-3.10.0-229.1.2.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet LANG=en_US.UTF-8 systemd.debug
	linux16 /vmlinuz-3.10.0-229.1.2.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet LANG=en_US.UTF-8 systemd.debug
	linux16 /vmlinuz-3.10.0-229.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet LANG=en_US.UTF-8
	linux16 /vmlinuz-0-rescue-83c9f47b42064fcbb33baee958e43d4f root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet
# cat /boot/grub2/grub.cfg | grep initrd16
	initrd16 /initramfs-3.10.0-229.1.2.el7.x86_64.img
	initrd16 /initramfs-3.10.0-229.1.2.el7.x86_64.img
	initrd16 /initramfs-3.10.0-229.el7.x86_64.img
	initrd16 /initramfs-0-rescue-83c9f47b42064fcbb33baee958e43d4f.img

ไฟล์ kernel (vmlinuz-3.10.0-229.1.2.el7.x86_64) และ initrd (/initramfs-3.10.0-229.1.2.el7.x86_64.img) จะเก็บไว้ที่ /boot

# ls /boot/
config-3.10.0-229.1.2.el7.x86_64
config-3.10.0-229.el7.x86_64
grub
grub2
initramfs-0-rescue-83c9f47b42064fcbb33baee958e43d4f.img
initramfs-3.10.0-229.1.2.el7.x86_64.img
initramfs-3.10.0-229.1.2.el7.x86_64kdump.img
initramfs-3.10.0-229.el7.x86_64.img
initramfs-3.10.0-229.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-229.1.2.el7.x86_64.gz
symvers-3.10.0-229.el7.x86_64.gz
System.map-3.10.0-229.1.2.el7.x86_64
System.map-3.10.0-229.el7.x86_64
vmlinuz-0-rescue-83c9f47b42064fcbb33baee958e43d4f
vmlinuz-3.10.0-229.1.2.el7.x86_64
vmlinuz-3.10.0-229.el7.x86_64

เมื่อ kernel ทำการ mount filesystem เรียบร้อยแล้วจะทำการ run คำสั่ง init (กรณี CentOS6) หรือsystemd (กรณี CentOS7) เป็น Process ที่ run สามารถตรวจสอบคำสั่งโดย

# ps -ef | grep init

หรือ

# systemctl get-default

init หรือ systemd จะเลือกว่าให้ระบบทำงาน ใน runlevel อะไร และเลือกว่ามีโปรแกรมอะไรบ้างที่ต้องทำงานเมื่อ start up รายละเอียดของ runlevel ต่างมีดังต่อไปนี้

0 – halt 1 – Single user mode 2 – Multiuser, without NFS 3 – Full multiuser mode 4 – unused 5 – X11 6 – Reboot ในแต่ละ runlevel มีโปรแกรมเริ่มต้นการทำงานที่แตกต่างกัน โดยปรกติ server จะใช้ runlevel 3

ตำแหน่งของที่เก็บ script ของแต่ละ level run level 0 --> /etc/rc.d/rc0.d/ run level 1 --> /etc/rc.d/rc1.d/ - Single User mode run level 2 --> /etc/rc.d/rc2.d/ - Debian / Ubuntu - default run level 3 --> /etc/rc.d/rc3.d/ - Rehat/ Suse - Text mode run level 4 --> /etc/rc.d/rc4.d/ run level 5 --> /etc/rc.d/rc5.d/ - Rehat/Suse GUI mode run level 6 --> /etc/rc.d/rc6.d/ - Reboot

# ls -l /etc/rc*
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root   10 Apr 26 00:07 /etc/rc6.d -> rc.d/rc6.d
lrwxrwxrwx.  1 root root   13 Apr 25 17:59 /etc/rc.local -> rc.d/rc.local

/etc/rc.d:
total 8
drwxr-xr-x. 2 root root 4096 Apr 25 18:29 init.d
drwxr-xr-x. 2 root root   97 Apr 25 18:29 rc0.d
drwxr-xr-x. 2 root root   97 Apr 25 18:29 rc1.d
drwxr-xr-x. 2 root root   97 Apr 25 23:44 rc2.d
drwxr-xr-x. 2 root root   97 Apr 25 23:44 rc3.d
drwxr-xr-x. 2 root root   97 Apr 25 23:44 rc4.d
drwxr-xr-x. 2 root root   97 Apr 25 23:44 rc5.d
drwxr-xr-x. 2 root root   97 Apr 25 18:29 rc6.d
-rw-r--r--. 1 root root  473 Mar 26 20:03 rc.local

ให้สังเกตดูว่า symbolic Link (ขึ้นต้นด้วย “l (แอล) ” เช่น lrwxrwxrwx) มีอักษรแรกเป็นอักษรอะไร (S = Start, K=Shutdown,kill) ตัวเลขถัดมาก็เป็นลำดับการเริ่มต้นโปรแกรม (S12, S20, S50) ตัวอย่างตามด้านล่าง

# ls -l /etc/rc.d/rc3.d/
total 0
lrwxrwxrwx. 1 root root 20 Apr 26 00:09 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 22 Apr 25 23:44 S03vmware-tools -> ../init.d/vmware-tools
lrwxrwxrwx. 1 root root 17 Apr 26 00:09 S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 32 Apr 25 18:29 S57vmware-tools-thinprint -> ../init.d/vmware-tools-thinprint

ตรวจสอบผลการ boot ด้วยคำสั่ง dmesg และดูเนื้อหาใน file /var/log/dmesg ทดสอบคำสั่ง

# dmesg |  less
# cat /var/log/dmesg   | less
# cat /var/log/message

ตรวจสอบการทำงาน ของ INIT PROCESS (CentOS 6) init processเป็น process ตั้งต้นหรือ Process แม่ (parent process) ของทุกprocess ในระบบ Linux โดยการ run คำสั่งที่ระบุไว้ใน runlevel และสามารถตรวจสอบได้โดยคำสั่ง pstree

init─┬─abrtd
     ├─acpid
     ├─agetty
     ├─atd
     ├─auditd───{auditd}
     ├─crond
     ├─dbus-daemon───{dbus-daemon}
     ├─dhclient
     ├─hald─┬─hald-runner─┬─hald-addon-acpi
     │      │             └─hald-addon-inpu
     │      └─{hald}
     ├─master─┬─pickup
     │        └─qmgr
     ├─6*[mingetty]
     ├─rpc.statd
     ├─rpcbind
     ├─rsyslogd───3*[{rsyslogd}]
     ├─sshd───sshd───sshd───bash───sudo───bash───pstree
     └─udevd───2*[udevd]

สำหรับ CentOS 7 จะใช้ systemd แทน

# pstree
systemd─┬─NetworkManager─┬─dhclient
        │                └─3*[{NetworkManager}]
        ├─2*[agetty]
        ├─anacron
        ├─auditd───{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─docker───6*[{docker}]
        ├─iprdump
        ├─iprinit
        ├─iprupdate
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rpc.statd
        ├─rpcbind
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───sshd───bash───sudo───su───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]

####101.3 วิธีการเปลี่ยน runlevels การปิดระบบ (shutdown) และการ Reboot เครื่อง runlevels ระบุไว้ที่ /etc/inittab และ โปรแกรมอยู่ที่ /etc/init.d/
หากต้องการเปลี่ยนแปลงค่าของ runlevels แก้ไขได้ที่ /etc/inittab

# vi  /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:

สำรวจ /etc/init/

# ls -l /etc/init/*
-rw-r--r--. 1 root root  412 Oct 10  2013 /etc/init/control-alt-delete.conf
-rw-r--r--. 1 root root  130 Jun 25  2013 /etc/init/init-system-dbus.conf
-rw-r--r--. 1 root root  463 Oct 10  2013 /etc/init/kexec-disable.conf
-rw-r--r--. 1 root root  560 Oct 10  2013 /etc/init/plymouth-shutdown.conf
-rw-r--r--. 1 root root  357 Oct 10  2013 /etc/init/prefdm.conf
-rw-r--r--. 1 root root  505 Oct 10  2013 /etc/init/quit-plymouth.conf
-rw-r--r--. 1 root root  417 Oct 10  2013 /etc/init/rc.conf
-rw-r--r--. 1 root root 1046 Oct 10  2013 /etc/init/rcS.conf
-rw-r--r--. 1 root root  430 Oct 10  2013 /etc/init/rcS-emergency.conf
-rw-r--r--. 1 root root  725 Oct 10  2013 /etc/init/rcS-sulogin.conf
-rw-r--r--. 1 root root 2915 Nov 23  2013 /etc/init/readahead-collector.conf
-rw-r--r--. 1 root root 1559 Nov 23  2013 /etc/init/readahead.conf
-rw-r--r--. 1 root root  726 Nov 23  2013 /etc/init/readahead-disable-services.conf
-rw-r--r--. 1 root root 1302 Oct 10  2013 /etc/init/serial.conf
-rw-r--r--. 1 root root  791 Oct 10  2013 /etc/init/splash-manager.conf
-rw-r--r--. 1 root root  473 Oct 10  2013 /etc/init/start-ttys.conf
-rw-r--r--. 1 root root  335 Oct 10  2013 /etc/init/tty.conf

ทดสอบคำสั่งต่อไปนี้

# cat /etc/init/control-alt-delect.conf
# cat /etc/init/rc.conf

สำรวจ /etc/init.d/

# ls -l /etc/init.d/*
-rwxr-xr-x. 1 root root  1288 Nov 22  2013 /etc/init.d/abrt-ccpp
-rwxr-xr-x. 1 root root  1628 Nov 22  2013 /etc/init.d/abrtd
-rwxr-xr-x. 1 root root  1642 Nov 22  2013 /etc/init.d/abrt-oops
-rwxr-xr-x. 1 root root  1725 Aug 18  2010 /etc/init.d/acpid
-rwxr-xr-x. 1 root root  2062 Jan 30  2012 /etc/init.d/atd
-rwxr-xr-x. 1 root root  3378 Jun 22  2012 /etc/init.d/auditd
-r-xr-xr-x. 1 root root  1340 Nov 23  2013 /etc/init.d/blk-availability
-rwxr-xr-x. 1 root root 11355 Aug 13  2013 /etc/init.d/cpuspeed
-rwxr-xr-x. 1 root root  2826 Nov 23  2013 /etc/init.d/crond
-rw-r--r--. 1 root root 18586 Oct 10  2013 /etc/init.d/functions
-rwxr-xr-x. 1 root root  1801 Jul 19  2011 /etc/init.d/haldaemon
-rwxr-xr-x. 1 root root  5866 Oct 10  2013 /etc/init.d/halt
-rwxr-xr-x. 1 root root 10804 Nov 22  2013 /etc/init.d/ip6tables
-rwxr-xr-x. 1 root root 10688 Nov 22  2013 /etc/init.d/iptables
-rwxr-xr-x. 1 root root  1938 Aug 22  2013 /etc/init.d/irqbalance
-rwxr-xr-x. 1 root root 18133 Nov 22  2013 /etc/init.d/kdump
-rwxr-xr-x. 1 root root   652 Oct 10  2013 /etc/init.d/killall
-r-xr-xr-x. 1 root root  2134 Nov 23  2013 /etc/init.d/lvm2-lvmetad
-r-xr-xr-x. 1 root root  2665 Nov 23  2013 /etc/init.d/lvm2-monitor
-rwxr-xr-x. 1 root root  2571 Oct 11  2013 /etc/init.d/mdmonitor
-rwxr-xr-x. 1 root root  2200 Sep 13  2012 /etc/init.d/messagebus
-rwxr-xr-x. 1 root root  2989 Oct 10  2013 /etc/init.d/netconsole
-rwxr-xr-x. 1 root root  5428 Oct 10  2013 /etc/init.d/netfs

ทดลองเปิดและปิด service

# cd /etc/init.d/
# ./postfix
# ./postfix  stop
# ./postfix  stop

####เรียนรู้ upstart และ systemd ที่ผ่านมาได้เรียนรู้เกี่ยวกับขั้นตอนการ boot ของ Linux ในรูปแบบของ System V init processและ LSB init Script ที่ต้องทำการโหลด initrd (Initial RAM disk) และส่งต่อการทำงานให้กับโปรแกรมชื่อ init ทำงานต่อ โปรแกรม init นี้เป็นส่วนหนึ่งของแพคแกจที่ชื่อว่า sysvinit โปรแกรม init เป็นโปรแกรมแรกที่เริ่มต้นการทำงานจึงมีหมายเลข process ID เท่ากับ 1 โปรแกรมนี้จะทำหน้าที่ในการ run scripts ที่ได้กำหนดไว้สำหรับการเริ่มต้นการทำงานของระบบ runlevelที่ระบุใน /etc/inittab และ script ของแต่ละ runlevel ที่กำหนดใน /etc/init.d/rc

การกำหนดค่า script ของแต่ละ runlevel ประกอบด้วย S-script ซึ่งจะทำงานเมื่อมีการเริ่มต้นในแต่ละ runlevel และ K-script ซึ่งจะทำงานเมื่อมีการออกจาก runlevel ลักษณะของการทำงานจะทำงานเป็นลำดับ (order) ตามหมายเลขที่กำหนด และจะทำงานครั้งละ process เท่านั้น

ต่อจากนี้ไป จะเรียนนรู้ศึกษาการทำงานของ boot อีก 2 ระบบที่ทำหน้าที่ แทน System V init นั้นคือUpstart และ Systemd

upstart ####การทำงานของ Upstart ใช้หลักการทำงานแบบ event-based programming ทำหน้ารองรับกระบวนการการทำงานระหว่างการเริ่มต้น ระบบ และระหว่างการปิดระบบ เริ่มต้นการพัฒนาจาก Ubuntu distribution (http://upstart.ubuntu.com/) และได้ถูกนำไปใช้งานต่อในหลาย distribution

**คุณสมบัติหลักของการทำงานของ Upstart (Feature Highlights) **

  • การเริ่มต้นการทำงานและหยุดการทำงานของ Tasks และ Services จะถูกควบคุมด้วย Events
  • เหตุการณ์ (Events) ที่ถูกสร้างขึ้นนั้นสามารถสร้างเป็น Tasks หรือ Services ยกตัวอย่างเช่น เมื่อมีการนำUSB drive มาเสียบเข้ากับเครื่องคอมพิวเตอร์ ทำให้ Udev Services สร้าง event ที่ชื่อว่า ฺBlock-Device-added event ในEvent นี้กำหนดหน้าที่หรือ Task ไว้ให้ทำการตรวจสอบค่าที่กำหนดไว้ใน /etc/inittab และทำการเชื่อมต่อหรือ mount อุปกรณ์ อย่างเหมาะสม
  • Events ที่สร้างขึ้นสามารถส่งต่อให้กับ Process อื่นๆ ในระบบได้
  • Services สามารถเริ่มต้นการทำงานใหม่ (respawning) ได้
  • กระบวนการ supervision และ respawning ของ deamons จะแยกออกจาก parent process
  • การสื่อสารจะทำการสื่อสารผ่าน D-bus
  • Services ของ User ต่างๆ สามารถควบคุมผ่านผู้ใช้งานในระบบได้
  • รองรับการทำงานสำหรับอุปกรณ์ hotplug devices
  • มีการทำงานแบบ Asynchronous สามารถทำงานหลาย script พร้อมกันได้ ทำให้มีประสิทธิภาพการทำงานสูงขึ้นและเร็วมากขึ้น

ค่า Configuration ของ Upstart จะอยู่ที่ /etc/init/ และ Application ที่หน้าที่สำหรับการควบคุมการทำงานของ upstart ชื่อว่า initctl

#cd /etc/init
# ls
control-alt-delete.conf  rcS-sulogin.conf
init-system-dbus.conf    readahead-collector.conf
kexec-disable.conf       readahead.conf
plymouth-shutdown.conf   readahead-disable-services.conf
prefdm.conf              serial.conf
quit-plymouth.conf       splash-manager.conf
rc.conf                  start-ttys.conf
rcS.conf                 tty.conf
rcS-emergency.conf

# initctl list
rc stop/waiting
tty (/dev/tty3) start/running, process 1410
tty (/dev/tty2) start/running, process 1406
tty (/dev/tty1) start/running, process 1403
tty (/dev/tty6) start/running, process 1426
tty (/dev/tty5) start/running, process 1422
tty (/dev/tty4) start/running, process 1416
plymouth-shutdown stop/waiting
control-alt-delete stop/waiting
rcS-emergency stop/waiting
readahead-collector stop/waiting
kexec-disable stop/waiting
quit-plymouth stop/waiting
rcS stop/waiting
prefdm stop/waiting
init-system-dbus stop/waiting
readahead stop/waiting
splash-manager stop/waiting
start-ttys stop/waiting
readahead-disable-services stop/waiting
rcS-sulogin stop/waiting
serial (ttyS0) start/running, process 1399

####การทำงานของ Systemd (CentOS 7) Systemd ถูกพัฒนาขั้นโดย Lennard Poettering ซึ่งเป็น Redhat System Engineerตั้งแต่ปี 2010 เป็น Service manager และ Session Managerอีกตัวหนึ่งที่นำมาแทน SysV และ LSB init Scripts มีความสามารถในการรองรับการทำงานแบบขนาน ใช้ Socket – D-Bus Activation เพื่อเริ่มต้น Service มีการใช้งานครั้งแรกใน Fedora 15, OpenSUSE 12 และ Mandriva 2011 หลักการทำงานของ Systemd สามารถที่ดูรายละเอียดของการทำงานได้ที่ http://www.freedesktop.org/wiki/Software/systemd

Systemd ได้รับการออกแบบให้ทำงานได้มากกว่า SysV เน้นประสิทธิภาพการทำงานโดยการพัฒนาการบริหารจัดการการทำงานของ Dependencies ของระบบหน่วย (units) ต่างๆ รวม 11 unit ได้แก่ Services, Sockets, Targets, Devices, Mounts, Automounts, Snapshots, Timeers, Swaps, Paths และ Slices (Resource management) รวมถึงควบคุมระบบ Dependencies System ระหว่าง units และ SystemD มีความสามารถในการ track of processes ที่มาใช้งาน Linux Cgroups ซึ่งเป็นการควบคุมการใช้งานทรัพยากรของระบบ การใช้งาน SystemD ระบบปฏิบัติการจะมีความสามารถในการ Snapshots และ Restoring สถานะของระบบ ทำให้สามารถย้อนกลับไปสู่สถานะของระบบปฎิบัติการที่ได้ทำการ Snapshots ไว้ก่อนได้

systemd

จากภาพจะเห็นได้ว่า Systemd สามารถทำงานได้หลาย process ในเวลาเดียวกัน โดยการสร้าง socket ไว้รองรับ application และได้ปรับปรุงให้แต่ละกระบวนการของระบบสื่อสารผ่านทาง socket เพื่อให้รองรับการเพิ่มความเร็วในการสื่อสารและรองรับการสื่อสารแบบขนาน (parallelism) ####โครงสร้างองค์ประกอบของ Systemd systemd

ภายใน Linux kernel นอกจาก systemd ที่ทำงานผ่าน SystemCall Interface แล้ว ยังมี daemond อื่นที่ทำงานร่วมกัน ได้แก่ journald, logind, networkd, consoled, timedated และ udevd ดังนั้นจะเห็นได้ว่า systemd ไม่ได้เป็นเพียงโปรแกรมเดียวที่เป็นชุดของ software ขนาดใหญ่แต่เป็นโปรแกรมแรกที่เริ่มต้นในการทำงาน

# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─3*[{NetworkManager}]
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───{at-spi2-registr}
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd───dbus
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dconf-service───2*[{dconf-service}]
        ├─evolution-addre───4*[{evolution-addre}]
        ├─evolution-calen───5*[{evolution-calen}]
        ├─evolution-sourc───2*[{evolution-sourc}]
        ├─firewalld───{firewalld}
        ├─fprintd
        ├─gconfd-2
        ├─gdm─┬─gdm-simple-slav─┬─Xorg───2*[{Xorg}]
        │     │                 ├─gdm-session-wor─┬─gnome-session─┬─abrt-applet───{abrt-applet}
        │     │                 │                 │               ├─gnome-settings-───4*[{gnome-settings-}]
        │     │                 │                 │               ├─gnome-shell───8*[{gnome-shell}]
        │     │                 │                 │               ├─nautilus───3*[{nautilus}]
        │     │                 │                 │               ├─seapplet
        │     │                 │                 │               ├─ssh-agent
        │     │                 │                 │               ├─tracker-miner-f───3*[{tracker-miner-f}]
        │     │                 │                 │               ├─tracker-store───7*[{tracker-store}]
        │     │                 │                 │               └─3*[{gnome-session}]
        │     │                 │                 └─3*[{gdm-session-wor}]
        │     │                 └─2*[{gdm-simple-slav}]
        │     └─2*[{gdm}]
        ├─gnome-keyring-d───4*[{gnome-keyring-d}]
        ├─gnome-shell-cal───4*[{gnome-shell-cal}]
        ├─gnome-terminal-─┬─bash───su───bash───pstree
        │                 ├─gnome-pty-helpe
        │                 └─3*[{gnome-terminal-}]
        ├─goa-daemon───3*[{goa-daemon}]
        ├─goa-identity-se───2*[{goa-identity-se}]
        ├─gsd-printer───2*[{gsd-printer}]
        ├─gvfs-afc-volume───2*[{gvfs-afc-volume}]
        ├─gvfs-goa-volume───{gvfs-goa-volume}
        ├─gvfs-gphoto2-vo───{gvfs-gphoto2-vo}
        ├─gvfs-mtp-volume───{gvfs-mtp-volume}
        ├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
        ├─gvfsd───{gvfsd}
        ├─gvfsd-fuse───4*[{gvfsd-fuse}]
        ├─gvfsd-metadata───{gvfsd-metadata}
        ├─gvfsd-trash───2*[{gvfsd-trash}]
        ├─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │             └─2*[{ibus-daemon}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─irqbalance
        ├─ksmtuned───sleep
        ├─libvirtd───10*[{libvirtd}]
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mission-control───2*[{mission-control}]
        ├─polkitd───5*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd
        ├─systemd-hostnam
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tpvmlp
        ├─tuned───4*[{tuned}]
        ├─udisksd───4*[{udisksd}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        └─vmtoolsd

นอกจากความสามารถที่ผ่านมา systemd มีservice ชื่อ systemd-nspawn ทำให้ container อย่าง Docker สามารถใช้งาน cgroups ในkernel ผ่าน SystemCall Interface ได้

systemcall

สามารถตรวจสอบ process id ด้วยคำสั่ง ps -ef

#  ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 06:09 ?        00:00:01 /usr/lib/systemd/systemd --switc
root         2     0  0 06:09 ?        00:00:00 [kthreadd]
root         3     2  0 06:09 ?        00:00:00 [ksoftirqd/0]
root         5     2  0 06:09 ?        00:00:00 [kworker/0:0H]
root         7     2  0 06:09 ?        00:00:00 [migration/0]
root         8     2  0 06:09 ?        00:00:00 [rcu_bh]
root         9     2  0 06:09 ?        00:00:00 [rcuob/0]
root        10     2  0 06:09 ?        00:00:00 [rcu_sched]
root        11     2  0 06:09 ?        00:00:00 [rcuos/0]
root        12     2  0 06:09 ?        00:00:00 [watchdog/0]
root        13     2  0 06:09 ?        00:00:00 [khelper]
root        14     2  0 06:09 ?        00:00:00 [kdevtmpfs]
root        15     2  0 06:09 ?        00:00:00 [netns]
root        16     2  0 06:09 ?        00:00:00 [writeback]
root        17     2  0 06:09 ?        00:00:00 [kintegrityd]
root        18     2  0 06:09 ?        00:00:00 [bioset]

ค่า Configuration ของ systemd จะอยู่ที่ /etc/systemd และ Application ที่มีหน้าที่สำหรับควบคุมการทำงาน มีชื่อว่า systemctl

การใช้งาน Systemd ควบคุมการทำงานของ runlevel สามารถตรวจสอบการค่าของ runlevel ด้วยคำสั่ง

# runlevel 
N 3

การใช้งานคำสั่ง systemctl จะแสดงรายการของทุก unit ที่ systemd ควบคุมอยู่

# systemctl 
UNIT                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys...t_misc.automount loaded active waiting   Arbitrary Executable File Fo
sys-devi...-sda-sda1.device loaded active plugged   VMware_Virtual_S
sys-devi...-sda-sda2.device loaded active plugged   LVM PV 7GLdpq-USO3-WELX-5LHX
sys-devi...block-sda.device loaded active plugged   VMware_Virtual_S
sys-devi...ooth-hci0.device loaded active plugged   /sys/devices/pci0000:00/0000
sys-devi...o16777736.device loaded active plugged   PRO/1000 MT Single Port Adap
sys-devi...und-card0.device loaded active plugged   AudioPCI 64V/128 / Creative 
sys-devi...block-sr0.device loaded active plugged   VMware_Virtual_SATA_CDRW_Dri
sys-devi...block-fd0.device loaded active plugged   /sys/devices/platform/floppy
sys-devi...tty-ttyS1.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS0.device loaded active plugged   /sys/devices/pnp0/00:08/tty/
sys-devi...k-dm\x2d0.device loaded active plugged   /sys/devices/virtual/block/d
sys-devi...k-dm\x2d1.device loaded active plugged   /sys/devices/virtual/block/d
sys-module-configfs.device  loaded active plugged   /sys/module/configfs
sys-module-fuse.device      loaded active plugged   /sys/module/fuse
sys-subs...ices-hci0.device loaded active plugged   /sys/subsystem/bluetooth/dev
sys-subs...o16777736.device loaded active plugged   PRO/1000 MT Single Port Adap
-.mount                     loaded active mounted   /
boot.mount                  loaded active mounted   /boot
dev-hugepages.mount         loaded active mounted   Huge Pages File System
dev-mqueue.mount            loaded active mounted   POSIX Message Queue File Sys
….

ตรวจสอบ ด้วยlist-units ร่วมกับ –type เพื่อเลือกดูเฉพาะ target ที่ต้องการ

# systemctl list-units --type target
UNIT                LOAD   ACTIVE SUB    DESCRIPTION
basic.target        loaded active active Basic System
cryptsetup.target   loaded active active Encrypted Volumes
getty.target        loaded active active Login Prompts
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target     loaded active active Local File Systems
multi-user.target   loaded active active Multi-User System
network.target      loaded active active Network
nfs.target          loaded active active Network File System Server
paths.target        loaded active active Paths
remote-fs.target    loaded active active Remote File Systems
slices.target       loaded active active Slices
sockets.target      loaded active active Sockets
swap.target         loaded active active Swap
sysinit.target      loaded active active System Initialization
timers.target       loaded active active Timers

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

15 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files

การใช้งาน systemd เพื่อควบคุมการทำงานของ service http

ตัวอย่างการใช้งานควบคุมการใช้งาน http

# systemctl stop httpd

การใช้งานคำสั่ง systemd สามารถใช้ autocomplete ด้วยการใช้ double tab

# systemctl 
cancel                 is-active              reload-or-restart
condreload             is-enabled             reload-or-try-restart
condrestart            is-failed              rescue
condstop               isolate                reset-failed
daemon-reexec          kexec                  restart
daemon-reload          kill                   set-default
default                link                   set-environment
delete                 list-dependencies    st  show
disable                list-jobs              show-environment
emergency              list-sockets           snapshot
enable                 list-unit-files        start
exit                   list-units             status
force-reload           mask                   stop
get-default            poweroff               suspend
halt                   preset                 try-restart
help                   reboot                 unmask
hibernate              reenable               unset-environment
hybrid-sleep           reload   

จะแสดง subcommand ทั้งหมด ในที่นี้ subcommand ที่ใช้บ่อยได้แก่ status, start, stop, restart, reload, enable, disable, is-enabled

# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: inactive (dead)
Service Unit ของ applicationจะอยู่ใน /usr/lib/systemd/system/httpd.service

เริ่ม service ของ httpd และดูสถานะของ service

# systemctl start httpd
# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: active (running) since Thu 2015-02-26 11:10:11 EST; 1min 3s ago
 Main PID: 23716 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─23716 /usr/sbin/httpd -DFOREGROUND
           ├─23717 /usr/sbin/httpd -DFOREGROUND
           ├─23718 /usr/sbin/httpd -DFOREGROUND
           ├─23719 /usr/sbin/httpd -DFOREGROUND
           ├─23720 /usr/sbin/httpd -DFOREGROUND
           └─23721 /usr/sbin/httpd -DFOREGROUND

Feb 26 11:10:11 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
Feb 26 11:10:11 localhost.localdomain httpd[23716]: AH00558: httpd: Could not...
Feb 26 11:10:11 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.

ถึงแม้ว่า httpd ได้ start แล้ว แต่ยังไม่อนุญาตให้ทำงาน หากจะต้องใช้งานต้องทำการอนุญาตเสียก่อน

# systemctl is-enabled  httpd
disabled

การอนุญาตทำได้ด้วยการใช้ subcommand ชื่อ enable

# systemctl enable  httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

ตรวจสอบอีกครั้ง

# systemctl is-enabled  httpd
enabled

สามารถใช้คำสั่งเพื่อดูว่ามี service ใดบ้างที่ enable อยู่

# systemctl list-unit-files | grep enabled
cups.path                                   enabled 
abrt-ccpp.service                           enabled 
abrt-oops.service                           enabled 
abrt-vmcore.service                         enabled 
abrt-xorg.service                           enabled 
abrtd.service                               enabled 
accounts-daemon.service                     enabled 
atd.service                                 enabled 
auditd.service                              enabled 
avahi-daemon.service                        enabled 
bluetooth.service                           enabled 
chronyd.service                             enabled 
crond.service                               enabled 
cups.service                                enabled 
dbus-org.bluez.service                      enabled 
dbus-org.fedoraproject.FirewallD1.service   enabled 

โครงสร้างของ services ของ httpd.service

# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

หากใช้งานแบบเดิมเหมือนกับที่ใช้กับSysV ใน Linux ที่ใช้งาน systemdจะทำการ redirect ไปยัง systemd

# service httpd start
Redirecting to /bin/systemctl start  httpd.service

# chkconfig httpd on
Note: Forwarding request to 'systemctl enable httpd.service'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment