Skip to content

Instantly share code, notes, and snippets.

@itbakery
Created May 15, 2015 20:18
Show Gist options
  • Save itbakery/e4adae81a021cca9f53c to your computer and use it in GitHub Desktop.
Save itbakery/e4adae81a021cca9f53c to your computer and use it in GitHub Desktop.
lpi1-topic104

###Topic 104: Devices, Linux Filesystems, Filesystem Hierarchy Standard Harddisk ถูกแบ่งออกได้เป็นPartition ก่อนการใช้งาน โดยแต่ละ partition สามารถแบ่งออกได้หลายชนิด และสอดคล้องกับFile System โดย default สามารถแบ่ง patitionได้ไม่เกิน4 primary partition เช่น device (disk) /dev/sda สามารถแบ่งได้เป็น /dev/sda1 /dev/sdb2 /dev/sdb3 /dev/sdb4 หลังจากนั้นสามารถสร้าง filesystems บน partition ได้โดยตรงหรือนำpartition ไปสร้างเป็น physical volumes เพื่อนำไปสร้างเป็นlogical volume ก่อนที่จะสร้าง filesystems บน logical volumeพอสรุปได้ดังนี้

  1. Harddisk (Device) -> Partition -> FileSystems (format)
  2. Harddisk (Device) -> Partition -> Physical Volumes -> Logical Volume -> FileSystems (format) -> Mount
  3. Harddisk (Device) -> Partition -> Software RAID (mdadm) -> RAID[0,1,5] -> FileSystems (format) -> Mount

partition

ตัวอย่างการใช้งาน partition ร่วมกับ physical volumes ในการใช้งานจริง ผู้ใช้มีความต้องการในการใช้งานมากกว่า 4 partitionแต่เนื่องจากระบบไม่อนุญาตให้ primary partitionเกิน 4 ดังนั้น หากต้องการใช้งานมากกว่า 4 partition จำเป็นต้องทำให้ 1 partition เป็น Extended ซึ่งทำหน้าที่เป็นcontainer บรรจุ partition อื่น และจะสังเกตว่า partition ที่สร้างใน Extended จะเริ่มนับลำดับที่ 5 เสมอ โดย partition ที่อยู่ภายในนี้เรียกว่า logical partition

volume

รูปแสดงเปรียบเทียบระหว่างการแบ่ง partition ธรรมดา และ extended partition

###104.1 การสร้าง Partition และ File Systems การใช้คำสั่ง sudo fdisk -lเพื่อแสดง Partition ทั้งหมดที่มีอยู่ เมื่อเลือกdevice ได้แล้วให้ทำการแบ่ง partition

fdisk device /dev/vdb ยังไม่มี partition tables ยังเป็น raw disk สามารถนำ disk มาแบ่ง partition ด้วยคำสั่ง

fdisk /dev/vda  หรือ fdisk /dev/vdb

เมื่อโปรแกรมรับคำสั่งให้พิมพ์ m เพื่อแสดง helpfdisk

สามารถ กด "p" เพื่อแสดงข้อมูล partition layout fdisk

ให้สังเกตค่าใน column id (83,8e) เป็นเลขฐาน 16 ส่วนนี้มีความสำคัญมากเนื่องจากจะเป็นการระบุชนิดของ file system สามารถใช้คำสั่ง t (change a partition's system id) และ column Boot ใน /dev/vdb1 ถูก กำหนดให้เป็น boot partition (*)

กด n เพื่อสร้าง partition ใหม่ (/dev/vdb ที่มีขนาด 8 GB) ให้มีขนาด 4G (+4G) และ ลอง กด "p" เพื่อแสดง และ กด w เพื่อ commit ไปที่ kernel (save)

fdisk /dev/vdb

fdisk

โดยปรกติเมื่อสร้าง partitionด้วย fdisk จะกำหนดให้ filesystem ที่ใช้เป็นชนิด 83 Linux ลองพิมพ์ t เพื่อ เลือก ชนิดของ file system (partition system id) อื่นๆ (8e – Linux LVM, fd – Linux raid ..)

Command (m for help): t 
Hex code (type L to list codes): L

fdisk ต่อมาลองสร้าง partition ชนิด extended บน /dev/vdb โดยเรียกดูข้อมูลของ partitionก่อนโดยใช้ fdisk -l

# fdisk -l /dev/vdb 

Disk /dev/vdb: 8589 MB, 8589934592 bytes 
16 heads, 63 sectors/track, 16644 cylinders 
Units = cylinders of 1008 * 512 = 516096 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0xc189ad67 
   Device Boot      Start         End      Blocks   Id  System 
/dev/vdb1               1        8323     4194760+  83  Linux 

จะเห็นว่า /dev/vdb มีขนาดของ cylinders จำนวน 16644 แต่ถูกใช้ไป 8323 ให้สร้าง extended partition โดยใช้คำสั่ง fdisk /dev/vdb

# fdisk /dev/vdb 

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to 
         switch off the mode (command 'c') and change display units to 
         sectors (command 'u'). 

Command (m for help): n 
Command action 
   e   extended 
   p   primary partition (1-4) 
e 
Partition number (1-4): 2 
First cylinder (8324-16644, default 8324): (enter)
Using default value 8324 
Last cylinder, +cylinders or +size{K,M,G} (8324-16644, default 16644): (enter)
Using default value 16644 
Command (m for help): w 
The partition table has been altered! 

Calling ioctl() to re-read partition table. 
Syncing disks. 

ลองใช้ command p เพื่อให้ print layout

Command (m for help): p 
Disk /dev/vdb: 8589 MB, 8589934592 bytes 
16 heads, 63 sectors/track, 16644 cylinders 
Units = cylinders of 1008 * 512 = 516096 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0xc189ad67 

   Device Boot      Start         End      Blocks   Id  System 
/dev/vdb1               1        8323     4194760+  83  Linux 
/dev/vdb2            8324       16644     4193784    5  Extended 

จากนั้นลองสร้าง partition ด้วยการใช้ command n ครั้งนี้ fdisk จะแสดง option เพื่อสร้าง logical partition ให้ด้วยการการเลือก " l " และให้สังเกตการเริ่มต้นจะเป็นจุดเดียวกับ /dev/vdb2 คือ 8324 เพราะ extended ทำหน้าที่เป็นเพียง container เท่านั้น และ logical partition ที่สร้างใหม่นั้น จะเริ่มต้นที่ 5

Command (m for help): n 
Command action 
   l   logical (5 or over) 
   p   primary partition (1-4) 
l 
First cylinder (8324-16644, default 8324): 
Using default value 8324 
Last cylinder, +cylinders or +size{K,M,G} (8324-16644, default 16644): +1G 

Command (m for help): p 

Disk /dev/vdb: 8589 MB, 8589934592 bytes 
16 heads, 63 sectors/track, 16644 cylinders 
Units = cylinders of 1008 * 512 = 516096 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0xc189ad67 

   Device Boot      Start         End      Blocks   Id  System 
/dev/vdb1               1        8323     4194760+  83  Linux 
/dev/vdb2            8324       16644     4193784    5  Extended 
/dev/vdb5            8324       10405     1049296+  83  Linux 

###สร้าง Raid (Redundant Array of Independent Disks) การสร้าง raid device ด้วย linux software raid โดยการสร้าง logical อีกpartition มีขนาด 1 GB เหมือนกับด้านบน และนำเอา /dev/vdb5 และ /dev/vdb6 มาสร้างเป็นraid device ในตัวอย่างต่อไปจะเป็นการสร้าง RAID-1 partition ซึ่งข้อมูลจะถูกแบ่งเป็นสองชุดเท่ากัน ต้องการ disk อย่างน้อยสองลูกและเปลี่ยนชนิดของ ID จาก 83 เป็น fd

สิ่งที่ควรทราบเกี่ยวกับ RAID-1 ข้อดี ป้องกันการสูญหายของข้อมูล เนื่องจากจะทำหน้าที่ replicate ข้อมูล ข้อเสีย ต้องสูญเสียdisk ไปครึ่งหนึ่ง เนื่องจากต้องใช้ disk 2 ลูกทำ mirror กัน

Command (m for help): p 

Disk /dev/vdb: 8589 MB, 8589934592 bytes 
16 heads, 63 sectors/track, 16644 cylinders 
Units = cylinders of 1008 * 512 = 516096 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0xc189ad67 

   Device Boot      Start         End      Blocks   Id  System 
/dev/vdb1               1        8323     4194760+  83  Linux 
/dev/vdb2            8324       16644     4193784    5  Extended 
/dev/vdb5            8324       10405     1049296+  83  Linux 
/dev/vdb6           10406       12487     1049296+  83  Linux 

เปลี่ยน Id 83 เป็น fd

Command (m for help): t 
Partition number (1-6): 5 
Hex code (type L to list codes): fd 
Changed system type of partition 5 to fd (Linux raid autodetect) 

Command (m for help): t 
Partition number (1-6): 6 
Hex code (type L to list codes): fd 
Changed system type of partition 6 to fd (Linux raid autodetect) 

Command (m for help): w 
The partition table has been altered! 

Calling ioctl() to re-read partition table. 
Syncing disks. 

ตรวจสอบ command p

Command (m for help): p 

Disk /dev/vdb: 8589 MB, 8589934592 bytes 
16 heads, 63 sectors/track, 16644 cylinders 
Units = cylinders of 1008 * 512 = 516096 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0xc189ad67 

   Device Boot      Start         End      Blocks   Id  System 
/dev/vdb1               1        8323     4194760+  83  Linux 
/dev/vdb2            8324       16644     4193784    5  Extended 
/dev/vdb5            8324       10405     1049296+  fd  Linux raid autodetect 
/dev/vdb6           10406       12487     1049296+  fd  Linux raid autodetect 

raid สร้าง raid ด้วยคำสั่ง

# yum install mdadm
# mdadm --create /dev/md0  --level=1 --raid-devices=2 /dev/vdb5 /dev/vdb6 
Continue creating array? Y

ตรวจสอบ raid devices ในระบบ

# cat /proc/mdstat 
Personalities : [raid1] 
md0 : active raid1 vdb6[1] vdb5[0] 
      1048256 blocks super 1.2 [2/2] [UU] 
      [==================>..]  resync = 93.7% (983040/1048256) finish=0.0min speed=27534K/sec 
      
unused devices: <none> 

# cat /proc/mdstat 
Personalities : [raid1] 
md0 : active raid1 vdb6[1] vdb5[0] 
      1048256 blocks super 1.2 [2/2] [UU] 

สร้าง file system บน raids

# mkfs.ext4  /dev/md0

เรียกดูข้อมูล ของraid ที่ run อยู่

# mdadm --detail /dev/md0 
/dev/md0: 
        Version : 1.2 
  Creation Time : Mon Nov 17 14:51:33 2014 
     Raid Level : raid1 
     Array Size : 1048256 (1023.86 MiB 1073.41 MB) 
  Used Dev Size : 1048256 (1023.86 MiB 1073.41 MB) 
   Raid Devices : 2 
  Total Devices : 2 
    Persistence : Superblock is persistent 

    Update Time : Mon Nov 17 14:58:04 2014 
          State : clean 
 Active Devices : 2 
Working Devices : 2 
 Failed Devices : 0 
  Spare Devices : 0 

           Name : centos65.localdomain:0  (local to host centos65.localdomain) 
           UUID : a23a7ab6:c9380191:26cde877:74e63abc 
         Events : 17 

    Number   Major   Minor   RaidDevice State 
       0     252       21        0      active sync   /dev/vdb5 
       1     252       22        1      active sync   /dev/vdb6 

ขั้นตอนการ mount Raid Device

# mkdir /mnt/raid1
# mount /dev/md0 /mnt/raid1

เพิ่มเติมบรรทัดด้านล่างลงไปใน /etc/fstab

/dev/md0      /mnt/raid1     ext4    defaults    1 2

สร้าง Raid configuration file ด้วยคำสั่งด้านล่าง

# mdadm --detail --scan > /etc/mdadm.conf
# cat /etc/mdadm.conf 
ARRAY /dev/md0 metadata=1.2 name=centos65.localdomain:0 UUID=a23a7ab6:c9380191:26cde877:74e63abc 

ขั้นตอนการสร้าง swap บน partition ด้วยคำสั่ง mkswap ให้สร้าง partition เหมือนขั้นตอนที่ผ่านมา แต่เลือก System ID แบบ 82

# fdisk /dev/vdb
Command (m for help): n 
Command action 
   l   logical (5 or over) 
   p   primary partition (1-4) 
l 
First cylinder (12488-16644, default 12488): 
Using default value 12488 
Last cylinder, +cylinders or +size{K,M,G} (12488-16644, default 16644): +1G 

Command (m for help): t 
Partition number (1-7): 7 
Hex code (type L to list codes): 82 
Changed system type of partition 7 to 82 (Linux swap / Solaris) 

Command (m for help): w 

ตรวจสอบ Partition Layout

# fdisk /dev/vdb 
Command (m for help): p 

Disk /dev/vdb: 8589 MB, 8589934592 bytes 
16 heads, 63 sectors/track, 16644 cylinders 
Units = cylinders of 1008 * 512 = 516096 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0xc189ad67 

   Device Boot      Start         End      Blocks   Id  System 
/dev/vdb1               1        8323     4194760+  83  Linux 
/dev/vdb2            8324       16644     4193784    5  Extended 
/dev/vdb5            8324       10405     1049296+  fd  Linux raid autodetect 
/dev/vdb6           10406       12487     1049296+  fd  Linux raid autodetect 
/dev/vdb7           12488       14569     1049296+  82  Linux swap / Solaris 

reboot เครื่อง

# mkswap  /dev/vdb7 
Setting up swapspace version 1, size = 1049292 KiB 
no label, UUID=bb87f5a4-3a43-4050-915e-f41623ddd7b3

# swapon  /dev/vdb7        //enable

ตรวจสอบว่า swap ที่สร้างขึ้น active หรือไม่

# swapon  -s 
Filename		Type	  Size	     Used	Priority 
/dev/dm-1      partition	4095996	0	-1 
/dev/vdb7      partition	1049292	0	-2 

การformat partition (สร้าง filesystem) การใช้คำสั่ง mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev แล้วตามด้วย partition

# mkfs.ext3 /dev/vdb8 
mke2fs 1.41.12 (17-May-2010) 
Filesystem label= 
OS type: Linux 
Block size=4096 (log=2) 
Fragment size=4096 (log=2) 
Stride=0 blocks, Stripe width=0 blocks 
65408 inodes, 261442 blocks 
13072 blocks (5.00%) reserved for the super user 
First data block=0 
Maximum filesystem blocks=268435456 
8 block groups 
32768 blocks per group, 32768 fragments per group 
8176 inodes per group 
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376 

Writing inode tables: done                            
Creating journal (4096 blocks): done 
Writing superblocks and filesystem accounting information: 
done 

This filesystem will be automatically checked every 36 mounts or 
180 days, whichever comes first.  Use tune2fs -c or -i to override. 

###104.2 การดูแลรักษาความสมบูรณ์และถูกต้องของ file systems จากหัวข้อที่ผ่านมา file system ได้ถูกสร้างขึ้นเรียบร้อยแล้ว ต่อจากนั้นจะต้องสามารถตรวจสอบความถูกต้อง (integrity) ติดตามและตรวจสอบ free space และ inodes อีกทั้งยังต้องสามารถซ่อมแซม file ที่มีความเสียหาย (ด้วยการใช้คำสั่ง fsck)

ตรวจสอบพื้นที่ว่าง (free space) ด้วยคำสั่ง df คำสั่ง df (df -h)จะทำการ scan file ที่ทำการ mount ใน file system โดยหน่วยวัดจะเป็น 1k-blocks หากต้องการให้อ่านง่าย ให้เรา เพิ่ม option -h

# df 
Filesystem           1K-blocks   Used Available Use% Mounted on 
/dev/mapper/vg_centos65-lv_root 
                      35646184 810132  33018660   3% / 
tmpfs                   961160      0    961160   0% /dev/shm 
/dev/vda1               487652  49754    412298  11% /boot 
/dev/md0               1015384   1284    961688   1% /mnt/raid1 

# df -h 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/mapper/vg_centos65-lv_root 
                       34G  792M   32G   3% / 
tmpfs                 939M     0  939M   0% /dev/shm 
/dev/vda1             477M   49M  403M  11% /boot 
/dev/md0              992M  1.3M  940M   1% /mnt/raid1 

# df -i 
Filesystem            Inodes IUsed   IFree IUse% Mounted on 
/dev/mapper/vg_centos65-lv_root 
                     2272928 21116 2251812    1% / 
tmpfs                 240290     1  240289    1% /dev/shm 
/dev/vda1             128016    44  127972    1% /boot 
/dev/md0               65536    11   65525    1% /mnt/raid1 

inode ทำหน้าที่เหมือน database ที่หน้าที่บันทึกข้อมูลของ file ทุกๆ โดยจะบันทึก เป็นหมายเลข inodeโดยสามารถใช้ คำสั่ง ls -i และ state

# ls -i /etc/passwd 
654970 /etc/passwd 

# stat /etc/passwd 
  File: `/etc/passwd' 
  Size: 856       	Blocks: 8          IO Block: 4096   regular file 
Device: fd00h/64768d	Inode: 654970      Links: 1 
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root) 
Access: 2014-11-17 01:06:25.255000027 +0700 
Modify: 2014-11-17 01:06:25.105999959 +0700 
Change: 2014-11-17 01:06:25.105999959 +0700

จากตัวอย่างข้างต้น inode มีค่า 2272928 หมายถึงเราไม่สามารถที่สร้าง file ได้เกินจำนวนที่ inode ระบุ หลังจากสร้าง file จำนวนเท่ากับ ค่าดังกล่าวก็จะไม่สามารถ สร้าง file ได้อีก

ตรวจสอบการใช้งานของแต่ละ folder ด้วยคำสั่ง du และdu -h แสดงผลเป็นขนาดพื้นที่ ที่ถูกใช้ไป ไม่ได้บอก จำนวน file

# du 
...
20	./etc/audit 
4	./etc/ssl 
4	./etc/ppp/peers 
36	./etc/ppp 
4	./etc/gnupg 
20	./etc/bash_completion.d 
4	./etc/pm/power.d 
4	./etc/pm/config.d 
4	./etc/pm/sleep.d 
16	./etc/pm 
27184	./etc 
16	./mnt/raid1/lost+found 
20	./mnt/raid1 
24	./mnt 
4	./media 
10952	./sbin 
808839	. 

# du -h
...
20K	./etc/audit 
4.0K	./etc/ssl 
4.0K	./etc/ppp/peers 
36K	./etc/ppp 
4.0K	./etc/gnupg 
20K	./etc/bash_completion.d 
4.0K	./etc/pm/power.d 
4.0K	./etc/pm/config.d 
4.0K	./etc/pm/sleep.d 
16K	./etc/pm 
27M	./etc 
16K	./mnt/raid1/lost+found 
20K	./mnt/raid1 
24K	./mnt 
4.0K	./media 
11M	./sbin 
790M	. 

# du -h /home/ 
16K	/home/admin 
20K	/home/ 

หากต้องการ แสดงการใช้ พื้นที่ทุก user โดยไม่แสดงผล Folder อื่น ในที่นี้เรามี user 2 คนคือ admin และ tom

du -h --summarize /home/*

# du -h --summarize /home/* 
16K	/home/admin 
16K	/home/tom 

ตรวจสอบ ค่า UUID

# blkid 
/dev/vda1: UUID="dcc95422-ab3b-44b7-9aa8-817dba5c1d3f" TYPE="ext4" 
/dev/vda2: UUID="rpW2mJ-zeTc-csBd-q36r-Bpgp-eZYq-EEUOjM" TYPE="LVM2_member" 
/dev/vdb5: UUID="a23a7ab6-c938-0191-26cd-e87774e63abc" UUID_SUB="74af6964-0bd1-6932-5fdf-1150110b7806" LABEL="centos65.localdomain:0" TYPE="linux_raid_member" 
/dev/vdb6: UUID="a23a7ab6-c938-0191-26cd-e87774e63abc" UUID_SUB="75ed563b-f476-708f-93a1-6e8da99ccc67" LABEL="centos65.localdomain:0" TYPE="linux_raid_member" 
/dev/vdb7: UUID="bb87f5a4-3a43-4050-915e-f41623ddd7b3" TYPE="swap" 
/dev/vdb8: UUID="fa0f802b-aa28-49ae-be18-2a66811f5f82" SEC_TYPE="ext2" TYPE="ext3" 
/dev/mapper/vg_centos65-lv_root: UUID="53ab727f-1f70-4f09-a47e-6e0089bfb053" TYPE="ext4" 
/dev/mapper/vg_centos65-lv_swap: UUID="238718cf-3634-48ba-9363-0d59819cc8e1" TYPE="swap" 
/dev/md0: UUID="2e6fbb54-be3b-4e06-92d1-b4b488f34832" TYPE="ext4" 

การใช้คำสั่ง fsck (umount ก่อน) เมื่อสั่ง fsck บน partition จะทำการ load e2fsck ขึ้นมาทำงาน

format 1
# fsck UUID=fa0f802b-aa28-49ae-be18-2a66811f5f82 
fsck from util-linux-ng 2.17.2 
e2fsck 1.41.12 (17-May-2010) 
/dev/vdb8: clean, 11/65408 files, 8536/261442 blocks

format 2
# fsck /dev/vdb8 
fsck from util-linux-ng 2.17.2 
e2fsck 1.41.12 (17-May-2010) 
/dev/vdb8: clean, 11/65408 files, 8536/261442 blocks 

ในกรณีที่ device นั้น mount อยู่ก่อนแล้ว ให้ทำการ umount file ออกก่อน

[root@centos65 /]# umount  /mnt/raid1 
[root@centos65 /]# fsck -t ext3 /dev/md0 
fsck from util-linux-ng 2.17.2 
e2fsck 1.41.12 (17-May-2010) 
/dev/md0: clean, 11/65536 files, 8539/262064 blocks 

หากformat ด้วย file systemแบบใด จะต้องใช้ fsck -t ให้ตรง

# fsck -t ext3 /dev/vdb1 
fsck from util-linux-ng 2.17.2 
e2fsck 1.41.12 (17-May-2010) 
/dev/vdb1: clean, 11/262656 files, 51343/1048576 blocks

ทำการทดลอง format ใหม่ ด้วย file system ext4

# mkfs.ext4 /dev/vdb1 
mke2fs 1.41.12 (17-May-2010) 
warning: 114 blocks unused. 

Filesystem label= 
OS type: Linux 
Block size=4096 (log=2) 
Fragment size=4096 (log=2) 
Stride=0 blocks, Stripe width=0 blocks 
262656 inodes, 1048576 blocks 
52434 blocks (5.00%) reserved for the super user 
First data block=0 
Maximum filesystem blocks=1073741824 
32 block groups 
32768 blocks per group, 32768 fragments per group 
8208 inodes per group 
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736 

Writing inode tables: done                            
Creating journal (32768 blocks): done 
Writing superblocks and filesystem accounting information: 
done 

เมื่อใช้ คำสั่ง fsck จะต้องใช้ fsck -t ext4 หรือ fsck

# fsck.ext4 /dev/vdb1 
e2fsck 1.41.12 (17-May-2010) 
/dev/vdb1: clean, 11/262656 files, 51310/1048576 blocks 
# fsck -t ext4  /dev/vdb1 
fsck from util-linux-ng 2.17.2 
e2fsck 1.41.12 (17-May-2010) 
/dev/vdb1: clean, 11/262656 files, 51310/1048576 blocks 

ทดสอบ Centos ร่วมกับ reiserfs ให้ทำการ enable repo ชื่อ centosplus ที่อยู่ใน file <CentOS-Base.repo>

[centosplus] 
name=CentOS-$releasever - Plus 
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra 
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/ 
gpgcheck=1 
enabled=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

เพิ่มเติม elrepo

# rpm -Uvh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
# yum search reiser
kmod-reiserfs.x86_64 : reiserfs kernel module(s) 
reiserfs-utils.x86_64 : Tools for creating, repairing and debugging ReiserFS filesystems. 

# yum install kmod-reiserfs reiserfs-utils
# reboot

หลังจากการ reboot จะได้ คำสั่ง mkfs.reiserfs และ fsck.reiserfs ทดสอบ สร้างreiserfs และ fsck บน reiserfs file system

# mkfs.reiserfs /dev/vdb1 
mkfs.reiserfs 3.6.21 (2009 www.namesys.com) 

A pair of credits: 
Oleg Drokin was the debugger for  V3 during most of the time that  V4 was under 
development,  and was quite  skilled and fast at it.  He wrote  the large write 
optimization of V3. 

Vladimir Demidov wrote the parser for sys_reiser4(), the V3 alpha port, part of 
the V3  journal  relocation code,  and helped  Hans keep  the business  side of 
things running. 


Guessing about desired format.. Kernel 2.6.32-504.1.3.el6.x86_64 is running. 
Format 3.6 with standard journal 
Count of blocks on the device: 1048688 
Number of blocks consumed by mkreiserfs formatting process: 8244 
Blocksize: 4096 
Hash function used to sort names: "r5" 
Journal Size 8193 blocks (first block 18) 
Journal Max transaction length 1024 
inode generation number: 0 
UUID: <no libuuid installed> 
ATTENTION: YOU SHOULD REBOOT AFTER FDISK! 
	ALL DATA WILL BE LOST ON '/dev/vdb1'! 
Continue (y/n):y 
Initializing journal - 0%....20%....40%....60%....80%....100% 
Syncing..ok 
ReiserFS is successfully created on /dev/vdb1.
# fsck.reiserfs /dev/vdb1 
reiserfsck 3.6.21 (2009 www.namesys.com) 

************************************************************* 
** If you are using the latest reiserfsprogs and  it fails ** 
** please  email bug reports to reiserfs-list@namesys.com, ** 
** providing  as  much  information  as  possible --  your ** 
** hardware,  kernel,  patches,  settings,  all reiserfsck ** 
** messages  (including version),  the reiserfsck logfile, ** 
** check  the  syslog file  for  any  related information. ** 
** If you would like advice on using this program, support ** 
** is available  for $25 at  www.namesys.com/support.html. ** 
************************************************************* 

Will read-only check consistency of the filesystem on /dev/vdb1 
Will put log info to 'stdout' 
Do you want to run this program?[N/Yes] (note need to type Yes if you do):Yes 
###########
reiserfsck --check started at Mon Nov 17 20:08:01 2014 
########### 
Replaying journal: No transactions found 
Checking internal tree.. finished 
Comparing bitmaps..finished 
Checking Semantic tree: 
finished 
No corruptions found 
There are on the filesystem: 
	Leaves 1 
	Internal nodes 0 
	Directories 1 
	Other files 0 
	Data block pointers 0 (0 of them are zero) 
	Safe links 0 
########### 
reiserfsck finished at Mon Nov 17 20:08:02 2014 
###########

หากเลือกใช้ format filesystem ไม่ตรงกันจะทำให้เกิด error ยกตัวอย่าง file system เป็นชนิด reiserfsck แต่ใช้งาน fsck.ext4 ทำให้เกิด error The superblock could not be read or does....

# fsck.ext4 /dev/vdb1 
e2fsck 1.41.12 (17-May-2010) 
fsck.ext4: Superblock invalid, trying backup blocks... 
fsck.ext4: Bad magic number in super-block while trying to open /dev/vdb1 

The superblock could not be read or does not describe a correct ext2 
filesystem.  If the device is valid and it really contains an ext2 
filesystem (and not swap or ufs or something else), then the superblock 
is corrupt, and you might try running e2fsck with an alternate superblock: 
    e2fsck -b 8193 <device> 

ใน LPI จะต้องสามารถที่จะใช้เครื่องมือ เพื่อทำการซ่อม filesystem ได้แค่ tools ที่ชื่อว่า debugfs -w /dev/vdb1 (เน้นโดยเฉพาะ file system ที่เป็น ext base) เมื่อสั่งคำสั่ง debugfs จะได้ debugfs prompt รอรับคำสั่ง

# debugfs -w /dev/vdb1 
debugfs 1.41.12 (17-May-2010) 
debugfs:  

คำสั่งที่มีประโยชน์ กรณีต้องการกู้ file จากการลบ

debugs:  ls
debugs:  rm  <filesystem>
debugs:  lsdel     		// show file that deleted
debugs:  undel   <inode no>  <filename>

ทดสอบการกู้ไฟล์ ด้วยการสร้าง mount point ให้กับ /dev/vdb1 สร้าง content ด้วยการ cp file จาก /root/anaconda-ks.cfg ตามตัวอย่างคำสั่ง

# mkfs.ext4 /dev/vdb1
# mkdir /mnt/vdb1
# mount /dev/vdb1 /mnt/vdb1
# cp /root/anaconda-ks.cfg   /mnt/vdb1
# debugfs -w /dev/vdb1 
debugfs 1.41.12 (17-May-2010) 
debugfs:  ls
 2  (12) .    2  (12) ..    11  (20) lost+found    12  (4052) anaconda-ks.cfg
debugfs:  rm anaconda-ks.cfg
debugfs:  lsdel 
 Inode  Owner  Mode    Size      Blocks   Time deleted 
    12      0 100600   1131      1/     1 Mon Nov 17 20:35:03 2014 
1 deleted inodes found. 
debugfs:  ls 
 2  (12) .    2  (12) ..    11  (4072) lost+found 
debugfs:  undel <12> anaconda-ks.cfg.recover 
debugfs:  ls 
 2  (12) .    2  (12) ..    11  (20) lost+found   
 12  (4052) anaconda-ks.cfg.recover 
debugfs:  q

ตรวจ file

# ls /mnt/vdb1/ 
anaconda-ks.cfg.recover  lost+found

คำสั่งต่อมาในการทดสอบ คือ dumpe2fs ทดสอบคำสั่ง

# mkfs.ext3 /dev/vdb8 
mke2fs 1.41.12 (17-May-2010) 
Filesystem label= 
OS type: Linux 
Block size=4096 (log=2) 
Fragment size=4096 (log=2) 
Stride=0 blocks, Stripe width=0 blocks 
65408 inodes, 261442 blocks 
13072 blocks (5.00%) reserved for the super user 
First data block=0 
Maximum filesystem blocks=268435456 
8 block groups 
32768 blocks per group, 32768 fragments per group 
8176 inodes per group 
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376 

Writing inode tables: done                            
Creating journal (4096 blocks): done 
Writing superblocks and filesystem accounting information: 
done 

This filesystem will be automatically checked every 36 mounts or 
180 days, whichever comes first.  Use tune2fs -c or -i to override. 

คำสั่งต่อมาคือ tune2fs เพื่อใช้สำหรับการปรับแต่ง file system

# tune2fs -l /dev/vdb1
# tune2fs -l /dev/vdb1  | grep Block 
Block count:              1048576 
Block size:               4096 
Blocks per group:         32768 

คำสั่งต่อมาคือ xfs_info ซึ่งอยู่ ในpackage ที่ชื่อว่า xfsprogs-3.1.1-16.el6.i686 : Utilities for managing the XFS filesystem

# yum install  xfsprogs
# mkfs.xfs -f  /dev/vdb1
# mount /dev/vdb1 /mnt/vdb1
# xfs_info /dev/vdb1
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=262173 blks 
         =                       sectsz=512   attr=2, projid32bit=0 
data     =                       bsize=4096   blocks=1048690, imaxpct=25 
         =                       sunit=0      swidth=0 blks 
naming   =version 2              bsize=4096   ascii-ci=0 
log      =internal               bsize=4096   blocks=2560, version=2 
         =                       sectsz=512   sunit=0 blks, lazy-count=1 
realtime =none                   extsz=4096   blocks=0, rtextents=0 

ทดสอบคำสั่ง xfs_metadump ให้ทำการumount /mnt/vdb1

# umount  /mnt/vdb1
# xfs_info /dev/vdb1 
xfs_info: /dev/vdb1 is not a mounted XFS filesystem 
# xfs_metadump /dev/vdb1 dump.db 
# file dump.db 
dump.db: XFS filesystem metadump image 

ทดสอบคำสั่ง mke2fs -j option สำหรับ ต้องการ journal

# mke2fs /dev/vdb1 -j 
mke2fs 1.41.12 (17-May-2010) 
warning: 114 blocks unused. 

Filesystem label= 
OS type: Linux 
Block size=4096 (log=2) 
Fragment size=4096 (log=2) 
Stride=0 blocks, Stripe width=0 blocks 
262656 inodes, 1048576 blocks 
52434 blocks (5.00%) reserved for the super user 
First data block=0 
Maximum filesystem blocks=1073741824 
32 block groups 
32768 blocks per group, 32768 fragments per group 
8208 inodes per group 
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736 

Writing inode tables: done                            
Creating journal (32768 blocks): done 
Writing superblocks and filesystem accounting information: done 

This filesystem will be automatically checked every 21 mounts or 
180 days, whichever comes first.  Use tune2fs -c or -i to override. 

###104.3 การ mounting และ unmounting ของ filesystems กระบวนการนำ file system มาเชื่อมต่อ (Attach) กับ directory เรียกว่าการ mount และการยุติการเชื่อมต่อ (detach) เรียกว่า umount การเรียนรู้การ mount / umount แบบ manual และ การให้ file system mount ในขณะที่ boot โดยทำงานผ่าน file /etc/fstab หลังจากที่เรียนรู้การสร้าง file system

ในตัวอย่าง ต้องการ mount /dev/vdb1 กับ directory /mnt/drive จะต้องสร้าง folder ก่อนเสมอ ถ้า mount โดยไม่มีการสร้าง folderจะ error รูปแบบmount -t type device directory

# mount /dev/vdb1 /mnt/drive 
mount: mount point /mnt/drive does not exist
# mkdir /mnt/drive
# mount /dev/vdb1 /mnt/drive
# mount | grep drive 
/dev/vdb1 on /mnt/drive type ext3 (rw) 
# touch test.txt
# mkdir folder1
# umount /mnt/drive/
# mount /dev/vdb1 /mnt/drive 
# ls /mnt/drive/ 
folder1 lost+found   test.txt  
# mount 
/dev/mapper/vg_centos65-lv_root on / type ext4 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") 
/dev/vda1 on /boot type ext4 (rw) 
/dev/md0 on /mnt/raid1 type ext4 (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 

/dev/vdb1 on /mnt/drive type ext3 (rw)  

เมื่อทำการ umount แล้วfile และ folder ที่สร้างขึ้นจะยังคงอยู่ แต่จะไม่สามารถมองเห็นได้ ต้องรอจนมีการ mount อีกครั้ง

ตั้ง LABEL ให้ partition ด้วยคำสั่ง e2label

# e2label /dev/vdb1  VDB1_DRIVE
# e2label /dev/vdb1 
VDB1_DRIVE 
# blkid /dev/vdb1 
/dev/vdb1: UUID="3cb75704-d8a2-4d04-8b16-ee6da43f916c" TYPE="ext3" LABEL="VDB1_DRIVE" 

mount ด้วย LABEL

# umount /mnt/drive
# mount LABEL="VDB1_DRIVE" /mnt/drive

mount ด้วย UUID

# umount /mnt/drive
# mount UUID="3cb75704-d8a2-4d04-8b16-ee6da43f916c" /mnt/drive

On Boot mount vi /etc/fstab โดยมี format 6 column ดังนี้

File system Mount point Type Options Dump Pass
/dev/vdb1 /mnt/drive ext3 defaults 1 0
auto, ,rw,user,auto,exec,utfs8

Dump 1 : บอกว่าให้ save file อัตโนมัติ มีการshutdown Pass 0 : ไม่ต้องการให้มี การ check file system (ใช้กับ removable disk) Pass 1 : Check files system ตอนที่มีการ boot เป็นลำดับ ที่ 1 (ใช้กับ /) Pass 2 : Check files system ตอนที่มีการ boot เป็นลำดับ ที่ 2 (ใช้ กับ /boot)

/dev/mapper/vg_centos65-lv_root /                       ext4    defaults        1 1 
UUID=dcc95422-ab3b-44b7-9aa8-817dba5c1d3f /boot                   ext4    defaults        1 2 
/dev/mapper/vg_centos65-lv_swap swap                    swap    defaults        0 0 
tmpfs                   /dev/shm                tmpfs   defaults        0 0 
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0 
sysfs                   /sys                    sysfs   defaults        0 0 
proc                    /proc                   proc    defaults        0 0 
/dev/md0      /mnt/raid1     ext4    defaults    1 2
/dev/vdb1     /mnt/drive     ext3    defaults    1 2

สามารถใช้ df ตามด้วย option -T เพื่อดูชนิด file system ที่ mount อยู่

# df -Th 
Filesystem           Type   Size  Used Avail Use% Mounted on 
/dev/mapper/vg_centos65-lv_root 
                     ext4    34G  856M   32G   3% / 
tmpfs                tmpfs  939M     0  939M   0% /dev/shm 
/dev/vda1            ext4   477M   50M  403M  11% /boot 
/dev/md0             ext4   992M  1.3M  940M   1% /mnt/raid1 
/dev/vdb1            ext3   4.0G  137M  3.7G   4% /mnt/drive 

โดยปรกติการ mount จะต้องใช้สิทธิ root เท่านั้น หากต้องการให้ user สามารถที่จะ mount ได้ จะต้องใส่ option user ไปใน /etc/fstab

###104.4 การบริหารจัดการdisk quotas การบริหารจัดการการใช้งานพื้นที่บน disk ด้วยการกำหนดQuota สามารถกำหนดเป็นรายผู้ใช้งาน (user) หรือเป็น กลุ่ม (group) การจัดสรร quota ทำให้ user ไม่สามารถใช้งานเกินกว่าที่กำหนดไว้ได้

วิธีการกำหนด quotos บน disk

  1. ติดตั้ง package การบริหารจัดการการใช้งานพื้นที่บน disk ด้วยการกำหนดQuota สามารถกำหนดเป็นรายผู้ใช้งาน (user) หรือเป็น กลุ่ม (group) การจัดสรร quota ทำให้ user ไม่สามารถใช้งานเกินกว่าที่กำหนดไว้ได้

วิธีการกำหนด quotos บน disk

  1. ติดตั้ง package
# yum install quota            //install tools
  1. เพิ่มoption ใน fstab การเพิ่ม option ในการ mount disk ของ file /etc/fstab เป็น defaults, userquora, grpquora โดยเพิ่ม disk ใหม่ อีก 1 ลูก ในตัวอย่าง device ที่เพิ่ม คือ /dev/vdc
# fdisk /dev/vdc                                // create new partition
# mkfs.ext3 /dev/vdc1
# fdisk -l /dev/vdc
   Device Boot      Start         End      Blocks   Id  System 
/dev/vdc1               1       20805    10485688+  83  Linux 

# mkdir  /quotatest/
# mount /dev/vdc1  /quotatest/
# vi /etc/fstab
/dev/vdc1     /quotatest/   ext3    defaults,usrquota,grpquota    1 2

ทำการ remount file system (mount -o remout )

# mount -o remount /quotatest

folder ที่สร้าง สร้างโดย root ให้เปลี่ยน permission ของ folderเพื่อให้ user อื่นสามารถใช้ได้ ในที่นี้ให้สร้าง user ใหม่คือ developer และเปลี่ยน /quotatest ให้เป็นของ user developer

# ls -ld /quotatest 
drwxr-xr-x. 2 root root 4096 Nov 23 17:50 /quotatest

# chown developer:developer /quotatest
# chmod g+w /quotatest

# ls -ld /quotatest 
drwxrwxr-x. 2 developer developer 4096 Nov 23 17:50 /quotatest 

ให้เพิ่ม stickybit แก่ folder ที่สร้างนี้ เนื่องจากที่ผ่านมามีการเปิดให้ user ที่อยู่ในกลุ่ม developer สามารถเขียนใน folder นี้ได้ เมื่อมีการenable sticky bit บน folder ส่งผลให้เจ้าของ folder และ root เท่านั้นที่สามารถลบได้ ทั้งนี้เพื่อรักษาความปลอดภัยของข้อมูล

# chmod g+s  /quotatest
drwxrwsr-x. 2 developer developer 4096 Nov 23 17:50 /quotatest

สร้าง Quota Database file

# quotacheck -cug /quotatest 
quotacheck: Cannot create new quotafile /quotatest/aquota.user.new: Permission denied 
quotacheck: Cannot initialize IO on new quotafile: Permission denied 
quotacheck: Cannot create new quotafile /quotatest/aquota.group.new: Permission denied 
quotacheck: Cannot initialize IO on new quotafile: Permission denied

เนื่องจากติด SELinux fcontext ไม่ถูกต้องสำหรับการใช้งาน quotaต้องแก้ไขดังนี้

# ls -lZ /quotatest 
drwx------. root root system_u:object_r:file_t:s0      lost+found

# yum install policycoreutils-python
# semanage fcontext -a -t var_t "/quotatest(/.*)?"
# restorecon -R -v /quotatest 
# ls -lZd /quotatest 
drwxr-xr-x. root root system_u:object_r:var_t:s0       /quotatest 

สั่งคำสั่ง quotacheck อีกครั้ง

# quotacheck -cug /quotatest 
# ls /quotatest/ 
aquota.group  aquota.user  lost+found

# ls /quotatest
# ls /quotatest 
aquota.group  aquota.user  lost+found

หลังจากสร้าง file aquota.group และ aquota.user สร้างตารางการใช้งาน disk ปัจจุบัน

# quotacheck -avug  // all file system

คำสั่ง quotacheck -cug จะสร้าง file aquota.group และ aquota.user เพื่อคุมquota ในระดับgroup และ user

  1. การเปิดและปิดการใช้งานquota
# quotaon -avug     // all file system
/dev/vdc1 [/quotatest]: group quotas turned on 
/dev/vdc1 [/quotatest]: user quotas turned on

# quotaoff -avug 
/dev/vdc1 [/quotatest]: group quotas turned off 
/dev/vdc1 [/quotatest]: user quotas turned off

# quotaon -p /quotatest/ 
group quota on /quotatest (/dev/vdc1) is off 
user quota on /quotatest (/dev/vdc1) is off

//แบบระบุ file system ไม่ต้องมี -a
# quotaon -vug /quotatest
# quotaoff -vug /quotatest
  1. รายงานการใช้ quota user (ไม่มี options หมายถึงต้องการดู user)
# repquota /quotatest 
*** Report for user quotas on device /dev/vdc1 
Block grace time: 7days; Inode grace time: 7days 
                        Block limits                File limits 
User            used    soft    hard  grace    used  soft  hard  grace 
---------------------------------------------------------------------- 
root      --   73764       0       0              2     0     0       
developer --       8       0       0              1     0     0 
  1. รายงานการใช้ quota group
# repquota -g  /quotatest 
*** Report for group quotas on device /dev/vdc1 
Block grace time: 7days; Inode grace time: 7days 
                        Block limits                File limits 
Group           used    soft    hard  grace    used  soft  hard  grace 
---------------------------------------------------------------------- 
root      --   73768       0       0              2     0     0       
developer --       8       0       0              1     0     0 
  1. แก้ไข Grace period
 # edquota -t
Grace period before enforcing soft limits for users: 
Time units may be: days, hours, minutes, or seconds 
  Filesystem             Block grace period     Inode grace period 
  /dev/vdc1                     7days   
  1. กำหนดquota แก่ user กำหนดให้ developer เป็น prototype ให้แก่ user อื่นๆ
# edquota developer
Disk quotas for user developer (uid 501): 
  Filesystem                   blocks       soft       hard     inodes     soft     hard 
  /dev/vdc1                         8      10240      20480       0          1        0        0 

ดูผลลัพท์

# repquota /quotatest/ 
*** Report for user quotas on device /dev/vdc1 
Block grace time: 7days; Inode grace time: 7days 
                        Block limits                File limits 
User            used    soft    hard  grace    used  soft  hard  grace 
---------------------------------------------------------------------- 
root      --   73764       0       0              2     0     0       
developer --       8   10240   20480              1     1     0  6days 

สร้าง user ใหม่ และ กำหนด quota แก่ user ใหม่

# useradd tom 
# passwd tom
# useradd ian
# passwd ian
# usermod -a -G developer ian
# usermod -a -G developer tom 

# edquota -p developer ian tom

ทดสอบ

# edquota ian
Disk quotas for user ian (uid 502): 
  Filesystem                   blocks       soft       hard     inodes     soft     hard 
  /dev/vdc1                         0      10240      20480          0        1        0 

ความหมาย 0 คือไม่มี quota ใช้ได้ไม่จำกัด soft limitขนาดข้อมูล (หน่วยเป็น กิโลไบต์ - KB) ที่จำกัดไว้ เมื่อ Userมีการใช้งานเกินกว่าค่าที่ต้องไว้ผลคือเข้าสู่ grace period มีระยะเวลา 7 วัน และเตือนผู้ใช้เมื่อมีการใช้เกินจำนวน hard limitขนาดข้อมูลสูงสุดเช่นกันแต่ไม่สามารถใช้งานเกินกว่าที่กำหนด สร้างข้อมูลทดสอบ

 # su ian
[ian@centos65 ] $ dd if=/dev/zero of=/quotatest/ianfile bs=1024 count=1024
[ian@centos65 ]$ quota 
Disk quotas for user ian (uid 502): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace 
      /dev/vdc1    1032   10240   20480               1       1       0        
[ian@centos65 ] $ dd if=/dev/zero of=/quotatest/ianfile2 bs=1024 count=1024
[ian@centos65 root]$ quota 
Disk quotas for user ian (uid 502): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace 
      /dev/vdc1    2060   10240   20480               2*      1       0   7days 

ทดสอบการสร้างเกิน quota แต่ไม่ถึง limit ยังคงเขียนข้อมูลได้แต่ มี warning

[ian@centos65 ]$ dd if=/dev/zero of=/quotatest/ianfile3 bs=1024 count=10240 
vdc1: warning, user block quota exceeded. 
10240+0 records in 
10240+0 records out 
10485760 bytes (10 MB) copied, 0.0232901 s, 450 MB/s 

[ian@centos65 ]$ ls -l /quotatest/ 
total 12332 
-rw-------. 1 root root     7168 Nov 23 18:23 aquota.group 
-rw-------. 1 root root     7168 Nov 23 18:16 aquota.user 
-rw-rw-r--. 1 ian  ian   1048576 Nov 23 18:27 ianfile 
-rw-rw-r--. 1 ian  ian   1048576 Nov 23 18:28 ianfile2 
-rw-rw-r--. 1 ian  ian  10485760 Nov 23 18:32 ianfile3 

[ian@centos65 ]$ quota 
Disk quotas for user ian (uid 502): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace 
      /dev/vdc1   12316*  10240   20480   6days       3*      1       0   6days 

หากสร้างfile เกิน limit ที่กำหนดไว้ จะไม่สามารถเขียน file ได้

[ian@centos65 ]$ dd if=/dev/zero of=/quotatest/ianfile4 bs=1024 count=10240 
vdc1: write failed, user block limit reached. 
dd: writing `/quotatest/ianfile4': Disk quota exceeded 
8153+0 records in 
8152+0 records out 
8347648 bytes (8.3 MB) copied, 0.0187598 s, 445 MB/s 

แก้ไขเวลา grace peroid

# edquota -u tom 
Disk quotas for user tom (uid 500): 
  Filesystem                   blocks       soft       hard     inodes     soft     hard 
  /dev/vdc1                       0         10         20          0        0        0 


# quotaoff -avug
# quotaon -avug 

การสร้าง report ด้วย (root)

[root@centos65 ~]# repquota /quotatest 
*** Report for user quotas on device /dev/vdc1 
Block grace time: 7days; Inode grace time: 7days 
                        Block limits                File limits 
User            used    soft    hard  grace    used  soft  hard  grace 
---------------------------------------------------------------------- 
root      --   73764       0       0              2     0     0       
developer --       8   10240   20480              1     1     0  6days 
ian       ++   20480   10240   20480  6days       4     1     0  6days 

###104.5 การบริหารจัดการ file permissions และ ownership

มาตรการรักษาความปลอดภัย ของ file และ directory เป็นสิ่งที่สำคัญมากที่สุด ใน linux ผู้ใช้ต้องมีความรู้ ในการบริการจัดการดังนี้

การเปลี่ยนสิทธิ file owner และ group permission

การเปลี่ยนสามารถทำได้โดยใช้คำสั่ง chown และ chgrpด้วยสิทธิ root (sudo)

[ian@centos65 ~]$ cd ~ 
[ian@centos65 ~]$ touch  iantest.txt 
[ian@centos65 ~]$ chown root iantest.txt 
chown: changing ownership of `iantest.txt': Operation not permitted 
[ian@centos65 ~]$ sudo chown root iantest.txt 
[ian@centos65 ~]$ ls -l 
total 0 
-rw-rw-r--. 1 root ian 0 Nov 23 19:29 iantest.txt

[ian@centos65 ~]$ sudo chgrp root iantest.txt 
[ian@centos65 ~]$ ls -l 
total 0 
-rw-rw-r--. 1 root root 0 Nov 23 19:29 iantest.txt 
[ian@centos65 ~]$ mkdir ianfolder
[ian@centos65 ~]$ touch ianfolder/iantest.txt 
[ian@centos65 ~]$ touch ianfolder/iantest2.txt 
[ian@centos65 ~]$ touch ianfolder/iantest3.txt 
[ian@centos65 ~]$ ls -l  ianfolder/ 
total 0 
-rw-rw-r--. 1 ian ian 0 Nov 23 19:41 iantest2.txt 
-rw-rw-r--. 1 ian ian 0 Nov 23 19:41 iantest3.txt 
-rw-rw-r--. 1 ian ian 0 Nov 23 19:39 iantest.txt 

//-R , --recursive
$ sudo chown -R root ianfolder 
[sudo] password for ian: 
[ian@centos65 ~]$ ls -l  ianfolder/ 
total 0 
-rw-rw-r--. 1 root ian 0 Nov 23 19:41 iantest2.txt 
-rw-rw-r--. 1 root ian 0 Nov 23 19:41 iantest3.txt 
-rw-rw-r--. 1 root ian 0 Nov 23 19:39 iantest.txt 
//add admin

เพิ่มกลุ่ม admin พร้อมกับ เพิ่ม ianในกลุ่มนี้

[ian@centos65 ~]$ sudo groupadd admin 

ตรวจสอบ userid

[ian@centos65 ~]$ whoami 
ian 
[ian@centos65 ~]$ sudo usermod -a -G admin ian 
[ian@centos65 ~]$ groups 
ian developer 
[ian@centos65 ~]$ exec  su -l ian 
Password:
[ian@centos65 ~]$ groups 
ian developer admin
[ian@centos65 ~]$ mkdir admingroup 
[ian@centos65 ~]$ ls -l 
total 8 
drwxrwxr-x. 2 ian  ian  4096 Nov 23 19:52 admingroup 
drwxrwxr-x. 2 root ian  4096 Nov 23 19:41 ianfolder 
-rw-rw-r--. 1 root root    0 Nov 23 19:29 iantest.txt 
[ian@centos65 ~]$ sudo chgrp -R admin  admingroup 
[ian@centos65 ~]$ ls -l 
total 8 
drwxrwxr-x. 2 ian  admin 4096 Nov 23 19:52 admingroup 
drwxrwxr-x. 2 root ian   4096 Nov 23 19:41 ianfolder 
-rw-rw-r--. 1 root root     0 Nov 23 19:29 iantest.txt 

การเปลี่ยนแปลง permission ของ file และ folder ความหมายของ permission ที่ระบุใน linux มีความหมายดังนี้ permission ชุดที่ 1 ระบุว่าเป็น file หรือ folder ชุดที่ 2 คือ permission ของ owner ชุดที่ 3 คือ permission ของ group ชุดที่ 4 คือ permission ของ other users

ตาราง เปรียบเทียบ permission ที่แทนด้วยอักษร rwx และ ตัวเลข

Type User Group Other
d rwx r-x R-w
d 7 5 5

ค่า r=4,w=2,x=1 (6= r w -, 4= r- -)

การเปลี่ยน permission ทำได้ด้วยการใช้คำสั่ง chmod

[an@centos65 ~]$ echo "date" > chkdate.sh 
[ian@centos65 ~]$ ./chkdate.sh 
-bash: ./chkdate.sh: Permission denied
[ian@centos65 ~]$ chmod +x chkdate.sh 
[ian@centos65 ~]$ ./chkdate.sh 
Sun Nov 23 20:20:35 ICT 2014 
[ian@centos65 ~]$ ls -l chkdate.sh 
-rwxrwxr-x. 1 ian ian 5 Nov 23 20:03 chkdate.sh

ลบ permission ของ other ออก

[ian@centos65 ~]$ chmod o-rwx chkdate.sh
[ian@centos65 ~]$ ls -l chkdate.sh 
-rwxrwx---. 1 ian ian 5 Nov 23 20:03 chkdate.sh 

กลุ่มpermission พิเศษ suid , sgid และ sticky bit

special value เป้าหมาย
suid 4 4 755 Executable file
sgid 2 2 755 Executable file , folder
stickybit 1 1 755 Folder

Linux permission model มีโหมดพิเศษ คือ suid (set user id) และ sgid (set group id) โดย suid จะกำหนดที่ execute file (+x) เมื่อ มีการสั่งให้โปรแกรมทำงาน เช่น คำสั่ง passwd กำหนดให้ เป็น suid (ค่าrws) ดังนั้น ไม่ว่า user ใดก็ตามที่สั่ง คำสั่งนี้ก็เหมือนกับว่า root เป็นคนสั่งให้ทำ (สิทธิ์ root permission) ผลทำให้ user ทุกคนสามารถสั่งทำงานเพื่อกำหนด password ของตนเองได้

[ian@centos65 ~]$ ls -l /usr/bin/passwd 
-rwsr-xr-x. 1 root root 30768 Feb 22  2012 /usr/bin/passwd 

[ian@centos65 root]$ ls -l /bin/ping 
-rwsr-xr-x. 1 root root 40760 Sep 26  2013 /bin/ping 

[ian@centos65 ~]$ ping -c 2 google.com 
PING google.com (61.91.16.152) 56(84) bytes of data.
64 bytes from 61-91-16-152.static.asianet.co.th (61.91.16.152): icmp_seq=1 ttl=58 time=12.4 ms

-rws s คือ set use id r-x group สามารถสั่งให้ run ได r-x other สามารถสั่งให้ run ได้

[ian@centos65 ~]$ echo "date" > chkdate.sh
[ian@centos65 ~]$ chmod u+s chkdate.sh 
[ian@centos65 ~]$ ls -l chkdate.sh 
-rwsrwx--x. 1 ian ian 5 Nov 23 23:22 chkdate.sh 
[ian@centos65 ~]$ chmod g+s chkdate.sh
[ian@centos65 ~]$ ls -l chkdate.sh 
-rwsrws--x. 1 ian ian 5 Nov 23 23:22 chkdate.sh 
[ian@centos65 ~]$ chmod u-s chkdate.sh 
[ian@centos65 ~]$ ls -l chkdate.sh 
-rwxrws--x. 1 ian ian 5 Nov 23 23:22 chkdate.sh 

sgid มีความหมายเหมือนกันแต่จะใช้สิทธิ์ทำงานของ fileนั้นในลักษณะของgroup permission เช่น มี file ที่คนในกลุ่มเดียวกัน สามารถใช้งานได้ร่วมกัน share execute file หากมีการกำหนด sgid ให้ folder จะมีผลที่ให้ file ที่สร้างขึ้นมาไม่ว่าจะสร้างโดยใครก็ตาม จะมีสิทธิ์ของ group เหมือนกับfolder จะไม่เป็นสิทธิ์ของ user ที่สร้าง หรืออธิบายได้ว่า file ที่สร้างขึ้นมานั้น มี group เป็นเจ้าของ เช่น สร้าง group ที่ชื่อว่า accounting เพื่อสำหรับฝ่ายบัญชี file ที่สร้างทั้งหมด จะมี group เป็น accounting

[ian@centos65 ~]$ mkdir accouting 
[ian@centos65 ~]$ sudo groupadd accounting
[ian@centos65 ~]$ sudo chgrp accounting accouting
[ian@centos65 ~]$ ls -ld accounting 
drwxrwxr-x. 2 ian accounting 4096 Nov 23 23:28 accounting 

[ian@centos65 ~]$ sudo chmod g+s accounting 
[ian@centos65 ~]$ ls -ld accounting 
drwxrwsr-x. 2 ian accounting 4096 Nov 23 23:31 accounting 

//change to root
[ian@centos65 ~]$ su 
Password: 
[root@centos65 ian]# touch /home/ian/accounting/test-sgid.txt 
[root@centos65 ian]# ls -l  /home/ian/accounting/test-sgid.txt 
-rw-r--r--. 1 root accounting 0 Nov 23 23:32 /home/ian/accounting/test-sgid.txt 

ไฟล์ที่ root สร้าง root ยังคงเป็นเจ้าของ แต่สิทธิของ group จะเป็นค่าที่กำหนดไว้ด้วย sgid (accounting) ส่งผลให้คนอื่นที่อยู่ในกลุ่มเดียวกันก็สามารถที่ ใช้งาน file ที่ root สร้างได้

การเพิ่มและยกเลิก suid และ guid

chmod  u+s  <file or folder>     ||   chmod  u-s  <file or folder>
chmod  g+s  <fild or folder>     ||   chmod  g-s  <file or folder>

**การป้องกันการแก้ไข อนุญาตให้อ่านได้อย่างเดียว ** ให้ ian tom อยู่ในกลุ่ม admin เดียวกัน และให้ ian สร้าง file ใน folder admingroup และยกเลิกสิทธิ์การ write โดยใช้ g-w

[ian@centos65 ~]$ sudo usermod -a -G admin tom
[root@centos65 ~]# groups tom 
tom : tom developer admin 
[root@centos65 ~]# groups ian 
ian : ian developer admin 

[root@centos65 ~]# mkdir /share 
[root@centos65 ~]# chgrp admin /share
[root@centos65 ~]# chmod g+w /share

[root@centos65 ~]# su ian 
[ian@centos65 root]$ touch /share/ianprivate 
[ian@centos65 root]$ ls -l /share/ianprivate 
-rw-rw-r--. 1 ian ian 0 Nov 23 21:40 /share/ianprivate 
[ian@centos65 root]$ chmod g-w  /share/ianprivate 
[ian@centos65 root]$ ls -l /share/ianprivate 
-rw-r--r--. 1 ian ian 0 Nov 23 21:40 /share/ianprivate 

[tom@centos65 root]$ echo "something" >  /share/ianprivate 
bash: /share/ianprivate: Permission denied 

tom ไม่สามารถเขียน แต่สามารถลบได้

[tom@centos65 root]$ rm -rf /share/ianprivate

ให้ root เปลี่ยน folder ให้เป็น sticky folder ด้วยการ ใช้ sticky bit

[root@centos65 ~]# ls -ld /share 
drwxrwxr-t. 2 root admin 4096 Nov 23 21:49 /share 
[root@centos65 ~]# su ian 
[ian@centos65 ~]$ groups ian 
ian : ian developer admin 

[ian@centos65 root]$ touch /share/ianprivate 
[ian@centos65 root]$ chmod g-w  /share/ianprivate 

[tom@centos65 root]$  echo "something" >  /share/ianprivate 
bash: /share/ianprivate: Permission denied 
[tom@centos65 root]$  rm -rf /share/ianprivate 
rm: cannot remove `/share/ianprivate': Operation not permitted 

อีกตัวอย่างหนึ่ง

chmod o+t  /share
chmod g-t /share

Other ลบไม่ได้ แต่กลุ่มเดียวกัน สามารถลบได้ การใช้งาน octal mode (suid=4, sgid=2, stickybit=1)

[ian@centos65 ~]$ mkdir test 
[ian@centos65 ~]$ cd test 
[ian@centos65 test]$ touch file.txt 
[ian@centos65 ~]$ mkdir test 
[ian@centos65 ~]$ cd test 
[ian@centos65 test]$ touch file.txt 
[ian@centos65 test]$ mkdir folder 
[ian@centos65 test]$ ls -l 
total 4 
-rw-rw-r--. 1 ian ian    0 Nov 23 23:47 file.txt 
drwxrwxr-x. 2 ian ian 4096 Nov 23 23:47 folder 

[ian@centos65 test]$ chmod 2666 folder 
[ian@centos65 test]$ chmod 2666 file.txt 
[ian@centos65 test]$ ls -l 
total 4 
-rw-rwSrw-. 1 ian ian    0 Nov 23 23:47 file.txt 
drw-rwSrw-. 2 ian ian 4096 Nov 23 23:47 folder 

[ian@centos65 test]$ chmod 2733 file.txt 
[ian@centos65 test]$ ls -l file.txt 
-rwx-ws-wx. 1 ian ian 0 Nov 23 23:47 file.txt 

[ian@centos65 test]$ chmod 4733 file.txt 
[ian@centos65 test]$ ls -l file.txt 
-rws-wx-wx. 1 ian ian 0 Nov 23 23:47 file.txt 

// delete 
[ian@centos65 test]$ chmod 0733 file.txt 
[ian@centos65 test]$ ls -l file.txt 
-rwx-wx-wx. 1 ian ian 0 Nov 23 23:47 file.txt 

Immutable files กำหนดให้ file มีค่า attribute ห้ามไม่ให้มีการเปลี่ยน ค่าใดๆ ด้วย option (+i)

[root@centos65 ~]# touch keep.me 
[root@centos65 ~]# chattr +i keep.me 
[root@centos65 ~]# lsattr keep.me 
----i--------e- keep.me 
[root@centos65 ~]# rm -f keep.me 
rm: cannot remove `keep.me': Operation not permitted 
[root@centos65 ~]# chattr -i keep.me 
[root@centos65 ~]# lsattr keep.me 
-------------e- keep.me 
[root@centos65 ~]# rm -f keep.me 

ควบคุม permission ด้วย umask file และ folder ที่สร้าง จาก user จะมี permission ที่ระบุเป็น defaults (-rw-rw-r-- , drwxrwxr-x)

[ian@centos65 ~]$ touch test.txt 
[ian@centos65 ~]$ ls -l test.txt 
-rw-rw-r--. 1 ian ian 0 Nov 23 22:00 test.txt 

[ian@centos65 ~]$ ls -ld myfolder 
drwxrwxr-x. 2 ian ian 4096 Nov 23 22:03 myfolder

ดู file /etc/profile

[ian@centos65 ~]$ vi /etc/profile 
...
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 
    umask 002 
else 
    umask 022 
fi 
...

ระบุว่าหาก user มีค่า userid > 199 เป็นคนสร้าง folder จะมี umask (022) วิธีหา ค่าของ folder หลังจากการ mask ทำได้โดย (777 – 002 = 775 หรือ rwxrwxr-w) แต่ถ้า root สร้าง จะถูก mask ด้วย 022 ( 777 – 022 = 755 หรือ rwxr-xr-x)

[root@centos65 ~]# mkdir rootfolder 
[root@centos65 ~]# ls -ld rootfolder 
drwxr-xr-x. 2 root root 4096 Nov 23 22:14 rootfolder 

ส่วนการคำนวน ค่าของ file จะใช้ 666 แทน 777 ดังนั้น ผลที่ได้คือ 666 – 002 = 664 หรือ rw-rw-r และ 666 – 022 = 644 หรือrw -r- - r- -

[root@centos65 ~]# touch rootfile.txt 
[root@centos65 ~]# ls -l rootfile.txt 
-rw-r--r--. 1 root root 0 Nov 23 22:24 rootfile.txt

ค่าของ umask แก้ไขด้วย สามารถกำหนด ใน ~/.bash_profile

###104.6 การสร้างและปรับเปลี่ยน links (hard links และ symbolic links) เรียนรู้วิธีการสร้างและบริหารจัดการ hard links และ soft links สามารถที่จะระบุชนิดของ link ได้อย่างถูกต้อง และต้องบอกความแตกต่าง ระหว่าง การ copy และ linking

แนะนำเกี่ยวกับ Link storage device ,file และ directory ถูกจัดเก็บในรูปแบบของ block ข้อมูลของ file โดยจะจัดเก็บที่ inode ได้แก่ owner, file last accessed, ขนาดของ file, เป็นชนิดที่เป็น directory หรือไม่, ใครบ้างที่มีสิทธิเขียนหรืออ่าน inode เปรียบเสมือน file serial number ที่ไม่ซ้ำกันใน file system ส่วนของDirectory Entry จะเก็บชื่อของ file หรือ directory ที่บรรจุอยู่ และ pointer ชี้ไปยัง inode ซึ่งเก็บข้อมูล file และ directory

link เปรียบเสมือน Directory Entry สำหรับ file หรือ directory ที่อนุญาตให้มีสองหรือมากกว่าชี้ไปที่สิ่งเดียวกัน สามารถสรุปได้คือ hard link คือ directory entry ชี้ไปที่ inode โดยตรง และ softlink จะชี้ไปที่ directory entry ที่ชี้ไปที่ inode อีกทีหนึ่ง inode

[root@centos65 ~]# ls -li 
total 36 
1438985 -rw-------. 1 root root 1131 Nov 23 13:47 anaconda-ks.cfg 
1438978 -rw-r--r--. 2 root root 9458 Nov 23 13:47 hardlinkto_install.log 
1438978 -rw-r--r--. 2 root root 9458 Nov 23 13:47 install.log 
1438979 -rw-r--r--. 1 root root 3161 Nov 23 13:45 install.log.syslog 
1438988 -rw-r--r--. 1 root root    0 Nov 23 22:24 rootfile.txt 
1438987 drwxr-xr-x. 2 root root 4096 Nov 23 22:14 rootfolder 

ค่าของ inode ของ install.log hardlinkto_install.log เป็นค่าเดียวกัน

[root@centos65 ~]# mkdir testlink 
[root@centos65 ~]# cd testlink/ 
[root@centos65 testlink]# touch file1.txt 
[root@centos65 testlink]# ln file1.txt file1hardlink.txt 
[root@centos65 testlink]# echo "Hello" > file1hardlink.txt 
[root@centos65 testlink]# cat file1.txt 
Hello 

[root@centos65 testlink]# ls -li 
total 8 
1438990 -rw-r--r--. 2 root root 6 Nov 24 01:19 file1hardlink.txt 
1438990 -rw-r--r--. 2 root root 6 Nov 24 01:19 file1.txt 

เมื่อ file ใด file หนึ่งมีการเปลี่ยนแปลง file ที่เป็น hard link ก็จะเปลี่ยนแปลงด้วย

inode

soft link สร้างได้ด้วย คำสั่ง ln -s {source} {link} และ softlink จะชี้ไปที่ file ที่ชี้ไปที่ inode

[root@centos65 testlink]# ln -s file1.txt file1softlink.txt 
[root@centos65 testlink]# ls -li 
total 8 
1438990 -rw-r--r--. 2 root root 6 Nov 24 01:19 file1hardlink.txt 
1438991 lrwxrwxrwx. 1 root root 9 Nov 24 01:33 file1softlink.txt -> file1.txt 
1438990 -rw-r--r--. 2 root root 6 Nov 24 01:19 file1.txt 
[root@centos65 testlink]# cat file1softlink.txt 
Hello 

source ต้องกำหนดด้วย full path และหากสร้าง softlink ไปยัง folder จะได้ link ที่มีชื่อเดียวกับ source ใน folder ที่ถูก link

[root@centos65 testlink]# mkdir folder 
[root@centos65 testlink]# ln -s /root/testlink/  folder/ 
[root@centos65 testlink]# ls -l folder/ 
total 0 
lrwxrwxrwx. 1 root root 15 Nov 24 01:59 testlink -> /root/testlink/ 

หากมีการเปลี่ยนชื่อของ source ไป จะทำให้ softlink จะกลายเป็น broken like

[root@centos65 testlink]# mv file1.txt newfile.txt 
[root@centos65 testlink]# ls -li 
total 8 
1438990 -rw-r--r--. 2 root root 6 Nov 24 01:19 file1hardlink.txt 
1438991 lrwxrwxrwx. 1 root root 9 Nov 24 01:33 file1softlink.txt -> file1.txt 
1438990 -rw-r--r--. 2 root root 6 Nov 24 01:19 newfile.txt 

[root@centos65 testlink]# cat file1softlink.txt 
cat: file1softlink.txt: No such file or directory 

รูปแบบการใช้งานแตกต่างกัน hardlink ไม่สามารถ link ชนิดที่ข้าม partition/device ได้

[root@centos65 testlink]# touch /quotatest/testcross-device.txt 
[root@centos65 testlink]# ln /quotatest/testcross-device.txt  /root/testlink/test.txt 
ln: creating hard link `/root/testlink/test.txt' => `/quotatest/testcross-device.txt': Invalid cross-device link

แต่ softlink หรือ symbolic link สามารถ link ข้าม partition/deviceได้

[root@centos65 testlink]# ln -s /quotatest/testcross-device.txt /root/testlink/testsoftlink.txt 
[root@centos65 testlink]# ls -l /root/testlink/testsoftlink.txt 
lrwxrwxrwx. 1 root root 31 Nov 24 01:46 /root/testlink/testsoftlink.txt -> /quotatest/testcross-device.txt 

[root@centos65 testlink]# echo "test test test" > /quotatest/testcross-device.txt 
[root@centos65 testlink]# cat /quotatest/testcross-device.txt 
test test test 

ใช้ softlink กับ grub.conf

[root@centos65 ~]# ls -l /boot/grub/menu.lst 
lrwxrwxrwx. 1 root root 11 Nov 23 13:47 /boot/grub/menu.lst -> ./grub.conf 

ความแตกต่างระหว่างการ copy และ การ link

[root@centos65 testlink]# cp testfile.txt copytestfile.txt 
[root@centos65 testlink]# ls -l 
total 12 
-rw-r--r--. 1 root root   12 Nov 24 02:09 copytestfile.txt 
drwxr-xr-x. 2 root root 4096 Nov 24 01:59 folder 
-rw-r--r--. 1 root root   12 Nov 24 01:59 testfile.txt 
[root@centos65 testlink]# ln testfile.txt testfilehardlink.txt 
[root@centos65 testlink]# ls -li 
total 16 
1438993 -rw-r--r--. 1 root root   12 Nov 24 02:09 copytestfile.txt 
1438990 drwxr-xr-x. 2 root root 4096 Nov 24 01:59 folder 
1438991 -rw-r--r--. 2 root root   12 Nov 24 01:59 testfilehardlink.txt 
1438991 -rw-r--r--. 2 root root   12 Nov 24 01:59 testfile.txt 

สามารถจำแนกด้วย การใช้ ln -li ตัวสอบการไม่ใช่ inode เดียวกันแสดงว่าเป็น copy

###104.7 การค้นหา system files และวาง files ในตำแหน่งที่ถูกต้อง มาตรฐานการจัดวาง layout ระบบของ file system ใน linux เรียกว่า Filesystem Hierarchy Standard แบ่งกลุ่มไฟล์ออกเป็นสองกลุ่ม

1. Shareable vs Unshareable shareable files สามารถจัดเก็บที่ระบบหนึ่ง และใช้งานจากอีกระบบหนึ่ง แต่ตรงกันข้าม unshareable จะต้องเก็บไว้ที่ระบบเดียวกันกับระบบที่เรียกการใช้งาน

2 Static vs Variable Static files เปลี่ยนแปลงเมื่อเกิดการเปลี่ยนแปลงจาก administration ได้แก่ การติดตั้ง การupgrade รวมไปถึง file ที่เป็นชนิด document , libraries และ binary ส่วน variable file ได้แก่ logs, spool files, databases, user data จะเปลี่ยนแปลงเนื่องการเปลี่ยนแปลงในฝั่ง user และ system process

ตาราง FHS example

shareable unshareble
static /usr/ /etc
/opt/ /boot
variable /var/mail/ /var/run
/var/spool/news /var/lock

ประเภทไฟล์ที่เก็บ ในแต่ละ folder ของ FHS

/ หน้าที่
/bin คำสั่งชนิด binary
/boot เก็บ kernel, grub file
/dev อุปกรณ์
/etc เก็บค่า config ต่างๆ
/home User directory
/lib Libraried file สำหรับ คำสั่ง 32 bit
/lib64 Libraried file สำหรับ คำสั่ง 64 bit
/proc Virtual file system
/root Home directory ของ root
/sbin Binary สำหรับ ระบบsystem
/tmp ที่เก็บชั่วคราว
/usr Binary สำหรับ user เมื่อเราติดตั้งโปรแกรม
/var Variable data files, logs, mail /var/mail, /var/cache, /var/cache/fonts/, /var/spool/news
/media จุด mount point ของ removeable media (usb drive)
/mnt (legacy) จุด mount point ของ removeable media

คำสั่งค้นหา find และ locate เครื่องมือ ค้นหา file ใน ระบบ linux ค้นหาแบบ real time มีรูปแบบ

ค้นหา file ตามชื่อ find {location} -name {filename}

# find / -name mail
/var/spool/mail 
/var/mail 

ค้นหา file ของ user find {location} -user {username}

[root@centos65 ~]# find /tmp -user ian 
/tmp/EdP.aAcd54z 
[root@centos65 ~]# find / -user ian 

ค้นหา file ของ group find {location} -group {groupname}

[root@centos65 ~]# find /quotatest/ -group admin 
/quotatest/ianteam 

สามารถใช้ -uid , -gid ได้

[root@centos65 ~]# find /home  -uid 500 
/home/tom 
/home/tom/.bash_logout 
/home/tom/.bash_profile 
/home/tom/.bashrc 
/home/tom/.bash_history 

คำสั่ง locate จะค้นหาในฐานข้อมูลของ file และ path ที่ได้เก็บไว้ก่อนเหมือนกับการทำสารบัญไว้แล้ว (สร้างฐานข้อมูลด้วยคำสั่ง updatadb) ในขณะที่ find ค้นหาจาก file system ทันทีที่สั่งให้ค้นหา

# yum install mlocate -y
# updatedb
# locate mail

สังเกตว่าผลลัพท์ที่ได้จะมีมากกว่าคำสั่ง find เพราะ locate จะค้นหาแบบค้นคำในฐานข้อมูล จะแสดงผลทุกไฟล์ที่มีคำว่า mail อยู่

/usr/share/selinux/devel/include/services/fetchmail.if 
/usr/share/selinux/devel/include/services/mailman.if 
/usr/share/selinux/devel/include/services/procmail.if 
/usr/share/selinux/devel/include/services/qmail.if 
/usr/share/selinux/devel/include/services/sendmail.if 
/usr/share/selinux/targeted/fetchmail.pp.bz2 
/usr/share/selinux/targeted/mailman.pp.bz2 
/usr/share/selinux/targeted/procmail.pp.bz2 
/usr/share/selinux/targeted/qmail.pp.bz2 
/var/mail 
/var/log/maillog 
/var/spool/mail 

คำสั่ง updatedb สร้างฐานข้อมูลของ file system และนำไปเก็บไว้ที่ /var/lib/mlocate/mlocate.db
สามารถใช้ option -S เพื่อดูสถิติของฐานข้อมูล

[root@centos65 lib]# locate -S 
Database /var/lib/mlocate/mlocate.db: 
	3,542 directories 
	25,477 files 
	1,239,131 bytes in file names 
	543,413 bytes used to store database 

fileconfig ของ updatedb /etc/updatedb.conf

PRUNE_BIND_MOUNTS = "yes" 
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs" 
PRUNENAMES = ".git .hg .svn" 
PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp" 

ให้ updatedb ก่อนการ locate เสมอ

[root@centos65 ~]# touch norealtime.txt 
[root@centos65 ~]# locate norealtime.txt 
[root@centos65 ~]# 
update database 
[root@centos65 ~]# updatedb 
[root@centos65 ~]# locate norealtime.txt 
/root/norealtime.txt

ตัวแปร PATH environment varaiable เมื่อสั่งคำสั่งผ่าน command line bash shell จะรับคำสั่งจาก keyboard และจะทำการค้นหาจาก folder ที่ระบุไว้ในตัวแปร path

[admin@newton ~]$ echo $PATH 
/opt/Komodo/bin:/home/admin/.rbenv/shims:/home/admin/.rbenv/bin:/usr/lib64/qt-3.3/bin:/usr/libexec/lightdm:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/admin/.local/bin:/home/admin/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment