# Linux Disk & File Systems Management
[TOC]
## ls (list)
### inode (Index Node)

類Linux Data Structure
Describing the file system object
包含文件或目錄的「元信息」
- file
- socket
- pipe
- directory
- device
```
ls -i "filename"
```
可以查看 directory info
# Simple File Systems Operation
## df
https://blog.gtwang.org/linux/linux-df-command-check-disk-space-usage-tutorial-script-example/
Superblock 會記錄 磁碟的整體資料
inode 記錄 各別檔案的容量
df
-> Display the amount of disk space available on the file system each of the file name
-> 列出檔案系統的整體磁碟使用量
Mounted on -> 掛載點
```
user@ubuntu:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 394M 6.2M 388M 2% /run
/dev/sda1 97G 37G 55G 40% /
tmpfs 2.0G 164K 2.0G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 394M 28K 394M 1% /run/user/1000
```
查看本地 Local Disk space
```
[root@localhost dev]# df -l
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1914852 0 1914852 0% /dev
tmpfs 1930632 0 1930632 0% /dev/shm
tmpfs 1930632 12812 1917820 1% /run
tmpfs 1930632 0 1930632 0% /sys/fs/cgroup
/dev/sda3 79474776 6157424 73317352 8% /
/dev/sda1 303780 166916 136864 55% /boot
tmpfs 386128 20 386108 1% /run/user/1000
tmpfs 386128 0 386128 0% /run/user/0
/dev/sdb1 20960256 32992 20927264 1% /home/centos/xfs
```
Centos 7 file systems(xfs)
```
[centos@localhost scirpt]$ df -t xfs
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 79474776 5811968 73662808 8% /
/dev/sda1 303780 166916 136864 55% /boot
```
-t: type: File Systems
### 取出 Disk Name 跟 Disk space
```
[centos@localhost scirpt]$ df -t xfs | awk '{print $1 " " $5 }'
Filesystem Use%
/dev/sda3 8%
/dev/sda1 55%
```
### 取出Disk Name
```
df -t xfs | awk '{print $1 " " $5 }' | awk '{ print $1}'
Filesystem
/dev/sda3
/dev/sda1
```
### 取出使用量
```
[root@localhost scirpt]# df -t xfs | awk '{print $1 " " $5 }' | awk '{ print $2}' | cut -d % -f 1
Use
8
55
```
-d, --delimiter 界定符
-f, --fields 第幾個欄位
### Script
```
#!/bin/sh
df -t xfs | tail -n +2 | awk '{ print $5 " " $1 }' | while read output;
do
# 取出使用量(百分比)
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
# 檔案系統
partition=$(echo $output | awk '{ print $2 }' )
# 若用量大於 90% 則用 Email 發出警告訊息
if [ $usep -ge 90 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" meowhecker@gmail.com
fi
done
```
Script for Centos7
定期檢Disk用量
## du
du 到檔案系統內去搜尋所有的檔案資料
列出目前目錄下的所有檔案容量
```
du
```
```
[root@localhost etc]# du -h --max-depth 1 /home/centos/
0 /home/centos/.mozilla
3.8M /home/centos/.cache
4.0K /home/centos/.dbus
108K /home/centos/.config
```
第一層 directory space 跟 File
```
[root@localhost etc]# du -h --max-depth 2 /home/centos/
0 /home/centos/.mozilla/extensions
0 /home/centos/.mozilla/plugins
```
第二層 directory
### 找佔用最大容量的次目錄
```
[root@localhost dev]# du -smh /*
0 /bin
148M /boot
0 /dev
263M /etc
4.3M /home
0 /lib
0 /lib64
0 /media
0 /mnt
0 /opt
du: cannot access ‘/proc/4336/task/4336/fd/3’: No such file or directory
du: cannot access ‘/proc/4336/task/4336/fdinfo/3’: No such file or directory
du: cannot access ‘/proc/4336/fd/3’: No such file or directory
du: cannot access ‘/proc/4336/fdinfo/3’: No such file or directory
0 /proc
120K /root
du: cannot access ‘/run/user/1000/gvfs’: Permission denied
13M /run
0 /sbin
0 /srv
0 /sys
1.1M /tmp
3.9G /usr
1.7G /var
```
## Hard Link & Symbol Link: ln
### Hard Link
#### SHow index number of the file
ll = ls -l
ll -i -> inode(print index number of the each file)
```
[root@localhost ~]# ll -i /etc/crontab
67675075 -rw-r--r--. 1 root root 451 Jun 9 2014 /etc/crontab
or
[root@localhost ~]# ls -l -i /etc/crontab
67675075 -rw-r--r--. 1 root root 451 Jun 9 2014 /etc/crontab
```
#### Create Hard LINk
我們能發現 兩個不同file 都指向 67675075 這個Index
```
[root@localhost ~]# ln /etc/crontab .
[root@localhost ~]# ll -i /etc/crontab crontab
67675075 -rw-r--r--. 2 root root 451 Jun 9 2014 crontab
67675075 -rw-r--r--. 2 root root 451 Jun 9 2014 /etc/crontab
```

表示有2兩個file 指向他

1 或 2 的目錄之 inode 指定的 block 找到兩個不同的檔名
不管使用哪個檔名均可以指到 real 那個 inode 去讀取到最終資料
如果你將任何一個『檔名』刪除,其實 inode 與 block 都還是存在
使用 hard link 設定連結檔時,磁碟的空間與 inode 的數目都不會改變 -> 只是block 關聯增加而已
### hard Link limited
- 不能跨 Filesystem;
- 不能 link 目錄。 (會有死結問題)
### Symbolic Link
類似Windows 捷徑, 類似 pointer
檔案會讓資料的讀取指向他 link 的那個檔案的檔名
當來源檔被刪除之後,symbolic link 的檔案會 開不了

```
[root@localhost ~]# ln -s /etc/crontab crontab2
[root@localhost ~]# ll -i /etc/crontab crontab2
137310340 lrwxrwxrwx 1 root root 12 Jun 5 07:48 crontab2 -> /etc/crontab
67675075 -rw-r--r--. 2 root root 451 Jun 9 2014 /etc/crontab
```
Symbolic link 所建立的檔案為一個獨立的新的檔案,所以會佔用掉 inode 與 block
# 磁碟的分割、格式化、檢驗與掛載
## 在VMware New 一個 Virtual Disk






### dmesg
dmesg 會將開機時的資訊顯示出來
Check Net virtual disk 在不在
sdb -> 為我們新增的SCSI 硬碟
```
[centos@localhost ~]$ dmesg | grep sdb
[ 9.013114] sd 0:0:1:0: [sdb] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
[ 9.013256] sd 0:0:1:0: [sdb] Write Protect is off
[ 9.013260] sd 0:0:1:0: [sdb] Mode Sense: 61 00 00 00
[ 9.013464] sd 0:0:1:0: [sdb] Cache data unavailable
[ 9.013466] sd 0:0:1:0: [sdb] Assuming drive cache: write through
[ 9.071108] sd 0:0:1:0: [sdb] Attached SCSI disk
```
## Disk Partition
### fdisk
```
[root@localhost dev]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9f7432da.
```
### Command Action

p -> print partition table
### New a partition
```
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
```
### Write (save)
Action
```
w
```
### quit
```
q
```
quit
---
## Disk 格式化(建置檔案系統)
對割好的 Partition 進行File Systems 的格式化
### mkfs
make file systems
mkfs -t xfs == mkfs.xfs
格式化 Partition
e.g. /dev/sdb1
```
mdfs.xfs /dev/sdb1
or
mdfs -t xfs /dve/sdb1
```
## 檔案系統掛載與卸載
### Notice:
- Mount point <----> file systems 通常是一對一
- 要作為掛載點的目錄,理論上會是空目錄
如果 掛載點的目錄 有東西 會暫時處於 Hided status
在VM 新增一個虛擬DISK
掛載方式
### 檔名mount
```
[root@study ~]# mount UUID='' 掛載點 喔!
[root@study ~]# mount 裝置檔名 掛載點
```
```
[root@localhost dev]# mount ./sdb1 /home/centos/xfs/
```
### UUID mount (blkid )
blkid
可以用來找file systems UUID
```
[root@localhost dev]# blkid sdb1
sdb1: UUID="4bd1fc19-3dbc-4831-9eb9-749c7bc43726" TYPE="xfs"
[root@localhost dev]# mount UUID='4bd1fc19-3dbc-4831-9eb9-749c7bc43726' /home/centos/xfs/
```
```
[root@localhost dev]# df -h /home/centos/xfs/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 20G 33M 20G 1% /home/centos/xfs
```
### 產生測試檔案
生出 666M 的 File
```
[root@meowhecker mountdir]# dd if=/dev/zero of=666M bs=1M count=666
666+0 records in
666+0 records out
```
## 重新掛載根目錄
在單人維護時 / direcotry -> only Read 狀態
可以透過重新 mount 來改變root 權限
```
mount -o remount,rw,auto /
```
## 目錄與目錄的 mount
```
[root@study ~]# mkdir /data/var
[root@study ~]# mount --bind /var /data/var
```
## umount
要退出 USB 與 Disk 這種 Storage Device 要先 umount
```
umoumt "跨載點"
```