btrfs文件系统、压缩工具及for语句、程序包管理

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 重建
无论当前存在与否,直接重新创建数据库

猜你喜欢

转载自blog.51cto.com/3832598/2132819
今日推荐