inode
在linux下,一個檔案產生後,會有一個inode,可以用來當作這個檔案的編號。
假設有一個檔案叫做cc.txt,它的inode編號為135,
當我用mv
移動到不管哪個資料夾下,它的inode編號依然會是135,
但當使用cp
複製它時,它的inode編號就會重新指派了。
查看inode
ls -i
7042739 README.md 6954370 bashrc 2287673 bin
用 inode 找 file / folder
find ~/conf -inum 2287673
/Users/test/conf/bin
FAT32
- 支援 windows, mac
- 單檔最大 4GB
- 最大容量 2TB
- 較容易 disk errosr
- 效率稍差
exFAT
- 支援 windows, mac, linux
- 單檔最大 4GB
- 最大容量 2TB
NTFS (windows only)
- windows 原生支援
- 在 mac 上只能讀
- 最大單檔 16TB
- 最大容量 256TB
HFS+
- mac 原生支援
- Required for Time Machine
- 最大單檔 8EiB
- 最大容量 8EiB
XFS
- xfs 是一種 file system 的 format, 像 ext4 一樣, 不過它特別的地方在於它可以方便的對目錄做 quota 限制
Ubuntu 安裝 :
sudo apt-get install xfsprogs xfsdump
Set quota :
Make the filesystem and mount it:
mount /dev/sda5 /mnt/xfs -o pquota
Create a project named “project1”, which is the “/mnt/xfs/test1” tree:
mkdir test1 #on /mnt/xfs
echo "11:/mnt/xfs/test1" >> /etc/projects
echo "project1:11" >> /etc/projid
xfs_quota -x -c "project -s project1" /dev/sda5
Set the tree quota to 2 MB:
xfs_quota -x -c "limit -p bsoft=1m bhard=2m project1" /dev/sda5
或直接指定project id
xfs_quota -x -c "limit -p bsoft=1m bhard=2m 1" /dev/sda5
也可以使用projects id指定 ex : “limit -p bsoft=1m bhard=2m 11”
That’s it.. Now let’s make some tests:
dd if=/dev/zero of=/mnt/xfs/test1/aaa count=10 bs=1024k
And there’s also a nice report! (looks nicer with a fixed-width console font)
xfs_quota -x -c "report /dev/sda5"
將一個quota綁定多個資料夾:
在/etc/projects設定:
7:/test_ugc/teststorage/default/test_user
7:/test_ugc/teststorage/default/hun
xfs report 中 id=7 的 used欄位會將test_user及hun資料夾使用空間加總,超過quota限制的話就會將兩個資料夾鎖住
Set Unlimited Quota
sudo xfs_quota -x -c "limit -p bsoft=0m bhard=0m project1" /dev/mapper/test--vg-test--lv
Remove quota :
執行清除project quota指令
$ sudo xfs_quota -xc "project -C 1" /dev/mapper/test--vg-test--lv
Clearing project 1 (path /test_ugc/teststorage/test)...
Processed 1 (/etc/projects and cmdline) paths for project 1 with recursion depth infinite (-1).
刪除資料夾與 /etc/projects 及 /etc/projid 的對應資料
如果要將/etc/projects其中一個project id的資料夾做更改一定要先執行clear quota再改,不然如果舊的資料夾還存在的話會變成限制成兩個資料夾
要先執行clear quota再刪除資料夾否則會發生找不到資料夾的錯誤
test_acount@test:/test_ugc/teststorage$ sudo xfs_quota -xc "project -C 1" /dev/mapper/test--vg-test--lv
xfs_quota: cannot find mount point for path `/test_ugc/teststorage/test': No such file or directory
Processed 0 (/etc/projects and cmdline) paths for project 1 with recursion depth infinite (-1).
其他功能:
To suspend (i.e. freeze) an XFS file system, use:
xfs_freeze -f /mount/point
To unfreeze an XFS file system, use:
xfs_freeze -u /mount/point
command :
- -c cmd xfs_quota commands may be run interactively (the default) or
as arguments on the command line. Multiple -c arguments may
be given. The commands are run in the sequence given, then
the program exits.
- -p prog Set the program name for prompts and some error messages, the
default value is xfs_quota.
- -x Enable expert mode. All of the administrative commands (see
the ADMINISTRATOR COMMANDS section below) which allow modifi -
cations to the quota system are available only in expert
mode.
- -d project
Project names or numeric identifiers may be specified with
this option, which restricts the output of the individual
xfs_quota commands to the set of projects specified. Multiple
- -d arguments may be given.
Command details :
xfs_quota> test_acount@test:/test_ugc/teststorage/test$ sudo xfs_quota -x
xfs_quota> ?
df [-bir] [-hn] [-f file] -- show free and used counts for blocks and inodes
disable [-gpu] [-v] -- disable quota enforcement
dump [-gpu] [-f file] -- dump quota information for backup utilities
enable [-gpu] [-v] -- enable quota enforcement
help [command] -- help for one or all commands
limit [-gpu] bsoft|bhard|isoft|ihard|rtbsoft|rtbhard=N -d|id|name -- modify quota limits
off [-gpu] [-v] -- permanently switch quota off for a path
path [N] -- set current path, or show the list of paths
print -- list known mount points and projects
project [-c|-s|-C|-d <depth>|-p <path>] project ... -- check, setup or clear project quota trees
quit -- exit the program
quot [-bir] [-gpu] [-acv] [-f file] -- summarize filesystem ownership
quota [-bir] [-gpu] [-hnNv] [-f file] [id|name]... -- show usage and limits
remove [-gpu] [-v] -- remove quota extents from a filesystem
report [-bir] [-gpu] [-ahnt] [-f file] -- report filesystem quota information
restore [-gpu] [-f file] -- restore quota limits from a backup file
state [-gpu] [-a] [-v] [-f file] -- get overall quota state information
timer [-bir] [-gpu] value -d|id|name -- get/set quota enforcement timeouts
warn [-bir] [-gpu] value -d|id|name -- get/set enforcement warning counter
Use 'help commandname' for extended help.
測試紀錄
測試刪除資料夾再新增回來同樣的名稱,quota限制是否還在?
將test資料夾刪除,這時看report,會有之前設定的記錄(只要projects及projid沒刪除,都會顯示project{id}的狀態)
Project ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
project1 0 1024 2048 00 [--------]
新增test資料夾
複製10m的檔案,不受quota 2m的影響
test_acount@test:/test_ugc/teststorage$ dd if=/dev/zero of=/test_ugc/teststorage/test/aaa count=10 bs=1024k
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00780022 s, 1.3 GB/s
report顯示的Used也不正常 (其實是10m但report那還是顯示0)
test_acount@test:/test_ugc/teststorage$ du
10240 ./test
10240 .
test_acount@test:/test_ugc/teststorage$ sudo xfs_quota -xc "report /dev/mapper/test--vg-test--lv"
Project quota on /test_ugc/teststorage (/dev/mapper/test--vg-test--lv)
Blocks
Project ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
project1 0 1024 2048 00 [--------]
解決方法: 重新將test與project{id}綁定,再觀察report結果,Used那正常顯示
test_acount@test:/test_ugc/teststorage$ sudo xfs_quota -xc "project -s project1" /dev/mapper/test--vg-test--lv
Setting up project project1 (path /test_ugc/teststorage/test)...
Processed 1 (/etc/projects and cmdline) paths for project project1 with recursion depth infinite (-1).
test_acount@test:/test_ugc/teststorage$ sudo xfs_quota -xc "report /dev/mapper/test--vg-test--lv"
Project quota on /test_ugc/teststorage (/dev/mapper/test--vg-test--lv)
Blocks
Project ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
project1 10240 1024 2048 00 [--none--]
再copy 10m進去,quota就把它擋掉了,得到預期結果
test_acount@test:/test_ugc/teststorage$ dd if=/dev/zero of=/test_ugc/teststorage/test/bbb count=10 bs=1024k
dd: opening /test_ugc/teststorage/test/bbb: Disk quota exceeded
就一個已存在的project id指向到一個新建立的資料夾(abc),會發生什麼情形?
將
2:/test_ugc/teststorage/default/test
改成:
2:/test_ugc/teststorage/default/abc
- 綁定後再設quota, 在report顯示出來的used其實是舊的資料夾加上新的資料夾的使用容量,如果超過使用容量就會變成兩個都鎖
解決辦法:
XFS在將路徑與project id做綁定時,極大的可能是以inode做綁定,所以只要有綁定都會記錄在xfs資料庫中,所以即使將/etc/projects刪除,但只要曾經綁過就會被紀錄下來,而將quota限制在同一個project id曾綁定過所有資料夾使用的總合
- 最好的做法是在將/etc/projects變更前先將舊資料夾做Clear Quota,讓舊資料夾跟project id沒有關係後,再變更/etc/projects及將新資料夾與project id綁定
- 或者是直接刪除該舊資料夾讓xfs找不到它的inode,但可能潛在如果後來生成的資料夾被指派到一樣的indoe,造成xfs也對他做quota 限制
在一個有設定quota的資料夾下將檔案搬移出的問題
假設資料夾 a 有設定quota,project id 為 8 (以下簡稱 p8),裡面有一個檔案test.txt 它的 inode 為 1130
[情況1]
將 a 的 test.txt 檔案搬移到 未設定 quota 的資料夾 b,會產生問題是 p8 仍然還是指向 inode 1130,如果將 a 的 quota 給註銷掉換成資料夾 c,則 p8 的新主人雖然是資料夾 c ,但查看 report 會發現 used 那邊會出錯,因為 inode 1130 這個檔案的使用量會被算到資料夾 c (實際上檔案是在 b 那邊) ,解決方法只要把 inode 1130 的檔案刪除
[情況2]
將 a 的 test.txt 檔案搬移到 已設定 quota 的資料夾 b,就不會發生 p8 指向 inode 錯亂的問題了,因為搬過去時 inode也變重新指派
用 inode 反查
sudo xfs_db -xr -c 'inode 16777350' -c p /dev/sda5
http://xfs.org/docs/xfsdocs-xml-dev/XFS_User_Guide/tmp/en-US/html/ch08s16.html
備註 :
- 現代的文件系統如JFS和XFS能夠動態地增加inode,因此不會用盡inode。(From: inode wiki)
- 動態變更上限值 : 直接執行步驟3重新指定上限值即可
- 步驟2 “11:/mnt/xfs/test1"中的編號11只可以使用數字,並且 不能超過65535 ,超始值 : 1
- 當資料夾rename時,/etc/projects更改路徑,原有的quota限制也都會對應到
- About soft and hard : When a user exceeds the soft limit, the timer is enabled. Any time the quota drops below the soft limits, the timer is disabled. If the timer pops, the particular limit that has been exceeded is treated as if the hard limit has been reached, and no more resources are allocated to the user. The only way to reset this condition, short of turning off limit enforcement or increasing the limit, is to reduce usage below quota. Only the superuser (i.e. a sufficiently capable process) can set the time limits and this is done on a per filesystem basis.
- 中文檔名:
- Max. filename length : 255bytes
- 85個中文 => OK!
- 80個中文+15英文 => OK!
- 80個中文+15數字 => OK!
- 80個中文+16個英文或數字 => error!
- 符號:
- 允許符號 : ~!@#$%^&*()_+ ' "
- 除了 / 不允許其他都OK
其他限制方式:
User Limits
limits xfs_quota -x -c 'limit bsoft=1000m bhard=1200m username'
limits xfs_quota -x -c 'limit isoft=500 ihard=700 username'
Group Limits.
xfs_quota -x -c 'limit -g bsoft=1000m bhard=1200m groupname'
Project Limits
參考:
http://xfs.org/docs/xfsdocs-xml-dev/XFS_User_Guide/tmp/en-US/html/index.html
http://www.held.org.il/blog/2008/07/per-directory-quota-not-a-dream/
http://dell9.ma.utexas.edu/cgi-bin/man-cgi?xfs_quota+8
http://docs.fedoraproject.org/en-US/Fedora/14/html/Storage_Administration_Guide/xfsfreeze.html
磁碟分割
當安裝linux,要分割磁碟時,類型有分為兩種:
- primary : 放置主要系統檔
- logical : 如果要自行利用磁區可以選擇它
假設磁碟共有80G,但不想全部都給主要系統檔,可以選擇:
- 分 20G 給 primary
- 分 25G 給 logical
- 分 25G 給 logical
- 建議將**“記憶體”**的2倍分給swap
這樣就有兩個25G磁碟可以讓你任意掛載運用, 但要注意分割磁碟時預設會mount, 要記得設定不要mount
fdisk
參數:
-l
: 查閱目前系統所有的partition有哪些
-l /dev/sda1
: 確認一下它是否可以分割
/dev/sda1
: 輸入h可以查閱可使用的指令
掛/卸載磁碟
-
在 /home/user_me
建立 test1 及 test2, 資料夾
-
查詢磁碟狀態
列出有磁碟
fdisk -l
顯示己掛載之磁碟
df -l
顯示filesystem type
df -T
-
掛載
mount /dev/sda5 /home/user_me/test1
mount /dev/sda6 /home/user_me/test2
如果test1資料夾裡有資料, 在掛載後, 只會顯示/dev/sda5
磁碟的資料, 但只要卸載/dev/sda5
,原本test1資料就會回來了
如果partition出現錯誤不能掛載: Partition 2 does not start on physical sector boundary.
解法方法 : 刪除 partition -> 再建立 partition -> format 要的檔案格式
-
卸載:
umount /home/user_me/test1 #卸載/dev/sda5
umount /home/user_me/test2 #卸載/dev/sda6
mount 唯讀
mount -r /dev/sda1 /test
掛載 usb
sudo fdisk -l
cd /mnt
sudo mkdir usb
sudo mount -v -t ntfs /dev/sdb5 /mnt/usb
-v, –verbose : Verbose mode.
-t, –types vfstype : The argument following the -t is used to indicate the filesystem type.
-t
不一定要明確指定格式, 也可以讓系統自動判別, ex: -t auto
卸載 usb
sudo umount /dev/sdb5
重新開機後自動 mount
/etc/fstab
:
以空格為區塊各代表:
[磁碟裝置檔名或該裝置的 Label] [掛載點] [磁碟分割槽的檔案系統] [檔案系統參數] [能否被 dump 備份指令作用] [是否以 fsck 檢驗磁區 (0:不檢驗, 1:最早檢驗, 2: 要檢驗)]
/dev/sda5 /home/user_me/test1 xfs pquota 0 2
/dev/sda5 /home/user_me/test1 ext4
刪除partition
sudo fdisk /dev/sda
Command (m for help): d
Partition number (1-6): 2
Command (m for help): w (存檔後重開機)
建立partition
extend:
sudo fdisk /dev/sda
Command (m for help): n
第一次切要先選extend(按enter按到底), 第二次之後再切才能選logic
logical
Command (m for help): n
Command (m for help): w (存檔後重開機)
sudo cfdisk /dev/sda5
> 選logical, 指定完大小後選write
format to xfs
sudo mkfs.xfs /dev/sda5 -f
切割LVM硬碟
-
檢查有無 PV 在系統上,然後將 /dev/hda6~/dev/hda9 建立成為 PV 格式
sudo pvscan
(會顯示lvm那個partition資料)
-
建立vg
sudo vgcreate test /dev/sda5
-
檢查有無vg
sudo vgscan
-
顯示vg內容(主要是取得Free PE(example:214564))
sudo vgdisplay
-
建立lv
sudo lvcreate -l 214564 -n test-lv test-vg
Logical volume “test-lv” created
-
顯示lv內容(主要是取得LV Path(lv的全名, mkfs要用的))
sudo lvdisplay
-
檔案系統格式化
sudo mkfs.xfs /dev/test-vg/test-lv
-
mount
sudo mount -t xfs /dev/test-vg/test-lv /test -o pquota
Mac 系統格式化
mac 系統原生就有 disk utility 可以做格式化, 在 spotlight 搜尋就可以開啟了
在左側選取要格式化的 usb 或硬碟,在右側再選取想要格式化的檔案格式
如果檔案系統需要同時支援 window, mac 建議格式化為 FAT32
如果要格式化成 FAT32 請選擇 MS-DOS (FAT)
dd
用來複製或轉換為一個檔案
假設目前系統環境只有一顆硬碟,當初安裝時沒有多切,但不想要重新再切,所以以dd
產生一個檔案,並且利用這個檔案所佔的空間去模擬一個磁碟
[1] 產生一個50M的檔案
sudo dd if=/dev/sda1 of=~/virtual2.img bs=1M count=50
[2] 將這個檔案切成磁碟
cfdisk virtual2.img
選logical
[3] 格式化成XFS
sudo mkfs.xfs virtual2.img -f
[4] mount它,-o pquota
是xfs特有的參數不需理會,可以將xfs改成你熟悉的ext4
sudo mount -t xfs ~/virtual2.img ./test -o pquota
xfs的 folder quota 正常:)
du
目錄下每一個檔案/資料夾的所佔空間
du -smh *
計算資料夾容量(total)
cd /test
du -s
441800 .
-s, –summarize : display only a total for each argument
資料夾下每個資料夾的大小
cd /test
du -h
-h : 單位為 mb
列出佔用空間前 5 名
du -s /var/* | sort -rn | head -5
xargs du -s
:列出資料夾大小 (單位是 K)
df
/
: 找出你系統中的根目錄所在磁碟,並顯示相關資訊
-a
: 全部檔案系統和各分割區的磁碟用情形
-h
: 容量 (G / M / K)
-T
: 列出檔案型態
只列出sd磁碟
ls /dev/sd*
列出所有磁碟(hd及sd)
ls /dev/[sh]d*
顯示磁碟資訊
$ df -h | grep /dev/sda1 | awk '{print $0"=>"$1" "$2" "$3}'
/dev/sda1 226G 2.1G 212G 1% /=>/dev/sda1 226G 2.1G