1. 程式人生 > >btrfs文件系統、壓縮工具及for語句、程序包管理

btrfs文件系統、壓縮工具及for語句、程序包管理

seq sys 寫一個腳本 ofo linux w 引擎 snapshot 狀態 之前

btrfs(b-tree、butter fs、better fs)oracle 公司研發的替代ext系列的cow(寫時復制)機制的文件系統,並遵循GPL協定

核心特性:
1、多物理卷支持;支持將多個底層物理設備組織成同一個文件系統,類似物理卷,btrfs可由多個地城物理卷組成,支持raid,以聯機實現物理卷的"添加"、"移除"、"修改"
2、寫時復制更新機制(CoW);復制、更新及替換指針,而非"就地"更新
不修改文件時,復制的文件無文件內容,指針仍指向原文件的數據
修改文件時,不立即修改原文件,會對目標文件進行備份,對備份文件進行修改,把文件名指向,有原文件改為備份文件,原數據保留,方便數據恢復

3、數據及元數據校驗碼機制;存儲每個文件時,把文件的元數據的校驗碼和數據的校驗碼,通過文件的屬性拓展保存下來,訪問文件時,通過掃描校驗碼,可快速判斷文件是否受損,一旦受損,中的嘗試修復,極大保證數據的可靠性
4、支持子卷 subvolume;在某卷上創建諸多子卷,每個子卷可以實現單獨使用和掛載
5、快照 支持快照的快照;基於寫時復制機制,增加了差異(增量)快照,可以對快照進行快照,完成對快照後的文件改變進行再次快照
6、透明壓縮機制;在這個分區上存儲文件,想節約空間,可以把任何數據流發往btrfs文件系統上,自動通過某些占據cpu的時鐘周期完成數據壓縮存放,用戶不知道,讀取時自動解壓縮,但消耗cpu的時鐘周期

創建btrfs文件系統

mkfs.btrfs命令
mkfs.btrfs [options]
-L ‘LABEL‘ 指明卷標
-d type 指明數據存儲類型(raid0,raid1,raid5,raid6,raid10,single默認)
-m profile 指明元數據存儲機制(底層物理存儲設備空間夠用)(raid0,raid1,raid5,raid6,raid10,single默認(單個文件),dup(冗余))
-O feature 在格式化是,指明其他特性
-O list-all 列出支持的所有feature

註意:在一塊磁盤上組合不同分區除了將多個分區組合成一個大分區並無什麽其余的意義,所以建議使用不同的磁盤做btrfs。

例如:mkfs.btrfs -L mybtr /dev/sd{b,c,d} 把3塊磁盤創建為一個btrfs系統

屬性查看 (filesystem);
btrfs filesystem show 查看btrfs分區信息
btrfs filesystem df /掛載點 顯示分區使用情況
btrfs filesystem resize <+|->#[MG] /掛載點 增加或減少掛載點的使用空間
btrfs filesystem resize max /掛載點 增加全部使用空間到掛載點

掛載文件系統
mount -t btrfs /dev/device mount_point /dev/device 可為btrfs中的任意一塊磁盤

擴展空間(向已存在的btrfs系統增加空間)(device);
btrfs device add /dev/device /掛載點
向掛載點增加一個磁盤空間
btrfs device delete /dev/device /掛載點
把掛載點某個磁盤卸載

擴展空間後必須使用命令來平衡btrfs系統中數據存放
btrfs balance start /掛載點

改變btrfs系統屬性(balance)
btrfs balance start mount_point
平衡btrfs系統中的數據

btrfs balance status mount_point
顯示btrfs平衡數據過程(磁盤數據過少,不容易看到)

btrfs balance start -dconvert=single mount_point
修改btrfs文件系統的數據存放格式

btrfs balance start -mconvert=raid1 mount_point
修改btrfs文件系統的元數據存放格式

創建子卷和快照(subvolume)
查看子卷id等信息
btrfs subvolume list mount_point
btrfs subvolume show mount_point

btrfs subvolume create mount_point/dir 創建名為dir的子卷

掛載子卷的方式(先卸載父卷)
mount -o subvol=dir /dev/sd# mount_point<新掛載點>
/dev/sd# 為btrfs文件系統中的任意一個磁盤都可以

btrfs subvolume delete mount_point/dir
刪除子卷

mount /dev/sdb /mydata 掛載父卷,子卷自動掛載

btrfs subvolume snapshot mount_point mount_point/snapshot_dir
創建父卷或子卷的快照

btrfs subvolume delete mount_point/snapshot_dir
刪除快照

其中:對某個單獨文件做快照:cp –reflink file file_snapshot

透明壓縮機制:
mount -o compress={lzo|zlib} device mount_point

如何升級為btrfs文件系統
btrfs-convert /dev/device
升級為btrfs文件系統
btrfs-convert -r /dev/device
降級為原來的文件系統

壓縮、解壓縮及歸檔工具
compress/uncompress .Z
gzip/gunzip .gz
bzip2/bunzip2 bz2
xz/unxz .xz
zip/unzip
tar,cpio

1、gzip/gunzip
gzip [option]... file... 默認壓縮後,刪除源文件
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages
[root@localhost tmp]# gzip messages
[root@localhost tmp]# ll -h messages.gz
-rw-------. 1 root root 68K Jun 14 09:26 messages.gz

-d 解壓縮 相當於gunzip
[root@localhost tmp]# gzip -d messages.gz

[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages

[root@localhost tmp]# gunzip messages.gz
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages

-c 將結果輸出至標準輸出
[root@localhost tmp]# gzip -c messages > messages.gz
[root@localhost tmp]# ll -h messages*

-rw-------. 1 root root 503K Jun 14 09:26 messages
-rw-r--r--. 1 root root 68K Jun 14 09:43 messages.gz

-# 1-9 指定壓縮比,壓縮比越大,壓縮後的文件越小,默認為6
[root@localhost tmp]# gzip -9 messages

[root@localhost tmp]# ll -h messages.gz
-rw-------. 1 root root 67K Jun 14 09:26 messages.gz

zcat 不顯式展開的前提下查看文件內容
[root@localhost tmp]# zcat messages.gz

2、bzip2/bunzip2/bzcat
bzip2 [option]... file...默認壓縮後,刪除源文件
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages
[root@localhost tmp]# bzip2 messages
[root@localhost tmp]# ll -h messages.bz2
-rw-------. 1 root root 28K Jun 14 09:26 messages.bz2

-k keep 保留源文件
[root@localhost tmp]# bzip2 -k messages
[root@localhost tmp]# ll -h messages*
-rw-------. 1 root root 503K Jun 14 09:26 messages
-rw-------. 1 root root 28K Jun 14 09:26 messages.bz2

-d 解壓縮 相當於bunzip2
[root@localhost tmp]# bzip2 -d messages.bz2
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages

[root@localhost tmp]# bunzip2 messages.bz2
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages

-# 1-9 壓縮比,壓縮比越大,壓縮後的文件越小,默認為6
[root@localhost tmp]# bzip2 -9 messages
[root@localhost tmp]# ll -h messages.bz2
-rw-------. 1 root root 28K Jun 14 09:26 messages.bz2

bzcat 不顯式展開的前提下查看文本文件內容
[root@localhost tmp]# bzcat messages.bz2

3、xz/unxz/xzcat
xz [option]... file... 默認壓縮後,刪除源文件
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages
[root@localhost tmp]# xz messages
[root@localhost tmp]# ll -h messages.xz
-rw-------. 1 root root 21K Jun 14 09:26 messages.xz

-k keep 保留原文件
root@localhost tmp]# xz -k messages
[root@localhost tmp]# ll -h messages*
-rw-------. 1 root root 503K Jun 14 09:26 messages
-rw-------. 1 root root 21K Jun 14 09:26 messages.xz

-d 解壓縮,相當於unxz
[root@localhost tmp]# xz -d messages.xz
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages

[root@localhost tmp]# unxz messages.xz
[root@localhost tmp]# ll -h messages
-rw-------. 1 root root 503K Jun 14 09:26 messages

-# 1-9 壓縮比,壓縮比越大,壓縮後的文件越小,默認為6
[root@localhost tmp]# xz -9 messages
[root@localhost tmp]# ll -h messages.xz
-rw-------. 1 root root 21K Jun 14 09:26 messages.xz

xzcat 不顯式展開的前提下查看文本文件內容
[root@localhost tmp]# xzcat messages.xz

4、tar
tar [option]...
創建歸檔
tar -c -f /path/to/somefile.tar file...
[root@localhost tmp]# tar -c -f messages.tar messages

tar cf /path/to/somefile.tar file...
[root@localhost tmp]# tar -cf messages.tar messages

[root@localhost tmp]# tar cf messages.tar messages

查看歸檔文件中的文件列表
tar -t -f /path/to/somefile.tar
[root@localhost tmp]# tar -t -f messages.tar

messages
[root@localhost tmp]# tar -tf messages.tar
messages
[root@localhost tmp]# tar tf messages.tar
messages

展開歸檔
tar -x -f /path/to/somefile.tar
[root@localhost tmp]# tar -x -f messages.tar
[root@localhost tmp]# tar -xf messages.tar

[root@localhost tmp]# tar xf messages.tar

tar xf /path/to/somefile.tar -C /path/to/dir
[root@localhost tmp]# tar xf messages.tar -C /root
[root@localhost tmp]# ll /root/messages
-rw-------. 1 root root 514730 Jun 14 09:26 /root/messages

結合壓縮工具實現,歸檔並壓縮
-z gzip
-j bzip2
-J xz

[root@localhost tmp]# tar -czf messages.gz.tar messages

[root@localhost tmp]# tar -cjf messages.bz2.tar messages
[root@localhost tmp]# tar -cJf messages.xz.tar messages
[root@localhost tmp]# ll -h messages*
-rw-------. 1 root root 503K Jun 14 09:26 messages
-rw-r--r--. 1 root root 28K Jun 14 11:04 messages.bz2.tar
-rw-r--r--. 1 root root 68K Jun 14 11:04 messages.gz.tar
-rw-r--r--. 1 root root 510K Jun 14 10:51 messages.tar
-rw-r--r--. 1 root root 21K Jun 14 11:04 messages.xz.tar

bash腳本編程
if語句 bash -n 檢查語法 bash -x 分布執行

condition
bash命令
用命令的執行狀態結果
成功 true
失敗 false

成功或失敗的意義,取決於用到的命令

單分支
    if condition;then
        if-true
    fi

雙分支
    if condition;then
        if-true
    else
        if-false
    fi

多分支
    if condition1;then
        if-true
    elif condition2;then
        if-true
    elif condition3;then
        if-true
    ...
    else
        if-false
    fi

    逐條件進行判斷,第一次遇到"真"條件時,執行其分支,而後結束

示例:用戶鍵入文件路徑,腳本來判斷文件類型
#!/bin/bash

read -p "enter a file path:" filename

if [ -z "$filename" ];then
echo "useage:enter a file path."
exit 2
fi

if [ ! -e $filename ];then
echo "no such file."
exit 3
fi

if [ -f $filename ];then
echo "a common file."
elif [ -d $filename ];then
echo "a directory."
elif [ -L $filename ];then
echo "a symbolic file."
else
echo "other type."
fi

註意:if語句可嵌套

循環: for while until

循環體 要執行的代碼 可能要執行n遍
進入條件
退出條件

for循環
for 變量名 in 列表;do
循環體
done

執行機制
依次將列表中的元素賦值給"變量名";每次賦值後即執行一次循環體;知道列表中的元素耗盡,循環結束;

示例:添加10個用戶,user1-user10密碼同用戶名
#!/bin/bash

if [ ! $UID -eq 0 ];then
echo "only root."
exit 1
fi

for i in {1..10};do
if id user$i &> /dev/null;then
echo "user$i exists."
else
useradd user$i
if [ $? -eq 0 ];then
echo "$user$i" | passwd --stdin user$i &> /dev/null
echo "add user$i finished."
fi
fi
done

列表生成方式:
1、直接給出列表
2、整數列表
a、{start..end}
b、$(seq [start [step]] end)
3、返回列表的命令 $(command)
4、glob 文件名通配
5、變量引用 $@ $*

示例:判斷某路徑下所有文件的類型
#!/bin/bash

for file in $(ls /var);do
if [ -f /var/$file ];then
echo "common file."
elif [ -L /var/$file ];then
echo "symbolic file."
elif [ -d /var/$file ];then
echo "directory."
else
echo "other type."
fi
done

示例:
#!/bin/bash

declare -i estab=0
declare -i listen=0
declare -i other=0

for state in $(netstat -tan | grep "^tcp\>" | awk ‘{print $NF}‘);do
if [ "$state" == ‘ESTABLISHED‘ ];then
let estab++
elif [ "$state" == ‘LISTEN’ ];then
let listen++
else
let other++
fi
done

echo "ESTABLISHEE:$estab"
echo "LISTEN:$listen"
echo "unkown:$other"

練習1:/etc/rc.d/rc3.d目錄下分別有多個以k開頭和以s開頭的文件;分別讀取每個1文件,以k開頭的文件輸出為文件名加stop,以s開頭的文件輸出為文件名加start
例如:"k34filename stop" "s66filename start"
#!/bin/bash

for file in ls /etc/rc.d/rc3.d;do
if [[ "$file" =~ "^K" ]];then
echo "$file stop."
elif [[ "$file" =~ "^S" ]];then
echo "$file start."
else
echo "unkown option."
fi
done

練習2:寫一個腳本,使用ping命令探測172.16.250.1-254之間的主機的在線狀態
#!/bin/bash

for i in {1..254};do
ping -c 1 172.16.250.$i &> /dev/null
state=echo $?
if [ $state -eq 0 ];then
echo "172.16.250.$i is online."
else
echo "172.16.250.$i is out."
fi
done

linux程序包管理

api application programing interface 應用編程接口
posix protable os 可移植操作系統

程序源代碼--預處理--編譯--匯編--鏈接
靜態編譯
共享編譯

abi application binary interface 應用二進制接口
windows與linux不兼容
庫級別的虛擬化
linux wine
windows cywin

系統級開發 C C++
應用級開發 java python php perl ruby

二進制應用程序的組成部分
二進制文件、庫文件、配置文件、幫助文件

程序包管理器
debian deb,dpt
redhat rpm,rpm
rpm redhat package manager
rpm is package manager

源代碼 name-version-tar.gz
version:major-minor.release

rmp包命令方式
name-version-arch.rpm
version:major-minor.release
release.arch
release:release.os

例如:zlib-1.2.7-13.el7.i386.rpm

常見的arch
x86 i386 i486 i586 i686
x86_64 x64 x86_64 amd64
powerpc ppc
跟平臺無關 noarch

testapp
testapp-version-arch.rpm 主包
testapp-devel-version-arch.rpm 支包
testapp-testing-version-arch.rpm

程序包之間,存在依賴關系
X Y Z
X依賴於Y 安裝X之前,先安裝Y
Y依賴於Z 安裝Y之前,先安裝Z

yum rpm包管理器的前端工具
apt-get deb包管理器的前端工具
zypper suse上的rpm前端管理工具
dnf fedora 22+ rmp包管理器的前端管理工具

查看二進制程序所依賴的庫文件
ldd /path/to/binary_file
[root@localhost ~]# ldd /bin/ls
linux-vdso.so.1 => (0x00007ffc7cc82000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f63049a4000)
librt.so.1 => /lib64/librt.so.1 (0x00007f630479c000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007f6304597000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f630438f000)
libc.so.6 => /lib64/libc.so.6 (0x00007f6303ffb000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f6303df6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6304bc8000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f6303bd9000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f63039d4000)

管理及查看本機裝載的庫文件
ldconfig
/sbin/ldconfig -p 顯示本機已經緩存的所有可用庫文件名及文件路徑映射關系
[root@localhost ~]# ldconfig -p
183 libs found in cache `/etc/ld.so.cache‘
libz.so.1 (libc6,x86-64) => /lib64/libz.so.1

配置文件為 /etc/ld.so.conf /etc/ld.so.conf.d/*.config
緩存文件為 /etc/ld.so.cache

程序包管理
功能:將編譯好的應用程序的各組成文件打包一個或多個程序包文件,從而方便快捷地實現程序包的安裝、卸載、查詢、升級和校驗等管理操作

1、程序的組成清單(每個包獨有)
文件清單
安裝或卸載時運行的腳本

2、數據庫(公共)
程序包名稱及版本
依賴關系
功能說明
安裝生成的各文件的文件路徑及校驗信息

管理程序包的方式
使用包管理器 rpm
使用前端工具 yum dnf

獲取程序報的途徑
1、系統發行版的光盤或官方的服務器
centos鏡像
http://mirrors.aliyun.com
http://mirrors.sohu.com
http://mirrors.163.com

2、項目官方站點

3、第三方組織
fedora-epel
搜索引擎
http://pkgs.org
http://rpmfind.net
http://rpm.pbone.net

4、自己制作

建議:檢查其合法性(來源合法性、程序包的完整性)

centos系統上rpm命令管理程序包
安裝 卸載 升級 查詢 校驗 數據庫維護

安裝
rpm [-i|--install] [install-options] package_file ...
-v verbose 顯示詳細信息
-vv
-h 以#顯示程序包安裝執行進度,每個#表示2%的進度

rpm -ivh package_file ...
[install-options]
--test 測試安裝 但不真正執行安裝過程,dry run模式
--nodeps 忽略依賴關系
--replacepkgs 重新安裝

--nosignature 不檢查來源合法性
--nodigest 不檢查包完整性

--noscripts 不執行程序包腳本片段
%pre 安裝前腳本 --nopre
%post 安裝後腳本 --nopost
%preun 卸載前腳本 --nopreun
%postun 卸載後腳本 --nopostun

升級
rpm {-U|--upgrade} [install-options] packages_file ...
rpm {-F|--freshen} [install-options] packages_file ...

upgrade 安裝有舊版本程序包,則升級,如果不存在舊版程序包,則安裝
freshen 安裝有舊版本程序包,則升級,如果不存在舊版程序包,則不執行升級操作

rpm -Uvh package_file ...
rpm -Fvh package_file ...

--oldpackage 降級安裝
--force 強制升級

註意
1、不要對內核做升級操作,linux支持多內核版本內存,因此,對直接安裝新版本內核
2、如果原程序包的配置文件安裝後曾被修改,升級時,新版本的提供的同一個配置文件並不會直接覆蓋老版本的配置文件,而把新版的文件重命名為(filename.rpmnew)後保留

查詢
rpm {-q|--query} []select-options] [query-options]

[select-options]
-a 所有包
-f 查看指定的文件有哪個程序包安裝生成

-p /path/to/package_file 針對尚未安裝的程序包文件做查詢操作

--whatprovides CAPABILITY 查詢指定的CAPABILITY由哪個包所提供
--whatrequires CAPABILITY 查詢指定的CAPABILITY被哪個包所依賴

[query-options]
--changelog 查詢rpm包的changelog
-c 查詢程序的配置文件
-d 查詢程序的文檔
-i information
-l 查看指定的程序包安裝後生成的所有文件
--scripts 程序包自帶的腳本片段
-R 查詢指定的程序包所以來的CAPABILITY
--provides 列出指定程序包所提供的CAPABILITY

用法
-qi package
-qf file
-qc package
-ql package
-qd package
-qpi package_file
-qpl package_file

卸載
rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--notriggers] [--test] package_name ...

校驗
rpm {-V|--verify} [select-options] [verify-options]

S file size differs
M mode differs (includes permissions and file type)
5 digest (formerly md5 sum)differs
D device major/minor number mismatch
L readlink(2) pathmimatch
U user ownership differs
G group ownership differs
T mtime differs
P capability differs

包來源合法性驗證及完整性驗證
完整性驗證 SHA256
來源合法性驗證 RSA

公鑰加密
對稱加密 加密解密使用同一密鑰
非對稱加密 密鑰是成對的
public key 公鑰 公開所有人
secret key 私鑰 不能公開

導入所需要公鑰
rpm --import /path/from/gpg-pubkey-file

centos 7 發行版光盤提供的密鑰文件 rpm-gpg-ke-centos-7

數據重建
rpm {--initdb|rebuliddb}
initdb 初始化
如果事先不存在數據庫,則新建之,否者,不執行任何操作

rebuliddb 重建
無論當前存在與否,直接重新創建數據庫

btrfs文件系統、壓縮工具及for語句、程序包管理