跳到主要内容
版本:2.8.4

模拟文件故障

本文主要介绍如何使用 Chaosd 模拟文件故障场景,包括创建文件、向文件追加数据、删除文件、修改文件权限、重命名文件以及替换文件数据等。

使用命令行模式创建实验

本节介绍如何在命令行模式中创建文件故障实验。

在创建文件故障实验前,可运行以下命令行查看 Chaosd 支持的文件故障类型:

chaosd attack file -h

输出结果如下所示:

File attack related commands

Usage:
chaosd attack file [command]

Available Commands:
append append file
create create file
delete delete file
modify modify file privilege
rename rename file
replace replace data in file

Flags:
-h, --help help for file

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

Use "chaosd attack file [command] --help" for more information about a command.

使用命令行模式向文件追加数据

通过该功能以追加的方式将数据写到文件的末尾。

追加数据命令

具体命令如下所示:

chaosd attack file append -h

输出结果如下所示:

append file

Usage:
chaosd attack file append [flags]

Flags:
-c, --count int append count with default value is 1 (default 1)
-d, --data string append data
-f, --file-name string append data to the file
-h, --help help for append

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

追加数据相关配置说明

配置项配置缩写说明
countc写数据的次数int,默认值为 1
datad要写入文件的数据string,例如 "test",必须要设置
file-namef要写入数据的文件路径string,例如 "/tmp/test.txt",必须要设置

使用命令行模式向文件追加数据示例

chaosd attack file append --count 2 --data "test" --file-name /tmp/test.txt

使用命令行模式创建文件

通过该功能可以创建新的文件或者目录。

创建文件命令

具体命令如下所示:

chaosd attack file create -h

输出结果如下所示:

create file

Usage:
chaosd attack file create [flags]

Flags:
-d, --dir-name string the name of directory to be created
-f, --file-name string the name of file to be created
-h, --help help for create

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

创建文件相关配置说明

配置项配置缩写说明
dir-named创建的目录名称string,例如 "/tmp/test"dir-namefile-name 必须要设置其中一个
file-namef创建的文件名称string,例如 "/tmp/test.txt"dir-namefile-name 必须要设置其中一个

使用命令行模式创建文件示例

chaosd attack file create --file-name "/tmp/test.txt"

使用命令行模式删除文件

使用该功能删除文件或者目录。

删除文件命令

具体命令如下所示:

chaosd attack file delete -h

输出结果如下所示:

delete file

Usage:
chaosd attack file delete [flags]

Flags:
-d, --dir-name string the directory to be deleted
-f, --file-name string the file to be deleted
-h, --help help for delete

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

删除文件相关配置说明

配置项配置缩写说明
dir-named删除的目录名称string,例如 "/tmp/test"dir-namefile-name 必须要设置其中一个
file-namef删除的文件名称string,例如 "/tmp/test.txt"dir-namefile-name 必须要设置其中一个

使用命令行模式删除文件示例

chaosd attack file delete --file-name "/tmp/test.txt"

使用命令行模式修改文件权限

使用该功能修改文件的权限。

修改文件权限命令

具体命令如下所示:

chaosd attack file modify -h

输出结果如下所示:

modify file privilege

Usage:
chaosd attack file modify [flags]

Flags:
-f, --file-name string file to be change privilege
-h, --help help for modify
-p, --privilege uint32 privilege to be update

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

修改文件权限相关配置说明

配置项配置缩写说明
file-namef要修改权限的文件名称string,例如 "/tmp/test.txt",必须要设置
privilegep将文件权限修改为该值uint32,例如 777,必须要设置

使用命令行模式修改文件权限示例

chaosd attack file modify --file-name /tmp/test.txt --privilege 777

使用命令行模式重命名文件

使用该功能重命名文件。

重命名文件命令

具体命令如下所示:

chaosd attack file rename -h

输出结果如下所示:

rename file

Usage:
chaosd attack file rename [flags]

Flags:
-d, --dest-file string the destination file/dir of rename
-h, --help help for rename
-s, --source-file string the source file/dir of rename

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

重命名文件相关配置说明

配置项配置缩写说明
dest-filed目标文件名称string,例如 "/tmp/test2.txt",必须要设置
source-files源文件名称string,例如 "/tmp/test.txt",必须要设置

使用命令行模式重命名文件示例

chaosd attack file rename --source-file /tmp/test.txt --dest-file /tmp/test2.txt

使用命令行模式替换文件内容

使用该功能替换文件中的内容。

替换文件内容命令

具体命令如下所示:

chaosd attack file replace -h

输出结果如下所示:

replace data in file

Usage:
chaosd attack file replace [flags]

Flags:
-d, --dest-string string the destination string to replace the origin string
-f, --file-name string replace data in the file
-h, --help help for replace
-l, --line int the line number to replace, default is 0, means replace all lines
-o, --origin-string string the origin string to be replaced

Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID

替换文件内容相关配置说明

配置项配置缩写说明
dest-stringd将文件中的内容替换为该值string,例如 "text",必须要设置
file-namef要替换内容的文件名称string,例如 "/tmp/test.txt",必须要设置
linel替换文件中哪一行的数据int,默认为 0,表示替换所有能匹配到 origin-string 的行的数据
origin-stringo文件中要替换的数据string,例如 "test",必须要设置

使用命令行模式替换文件内容示例

chaosd attack file replace --origin-string test --dest-string text --file-name /tmp/test.txt --line 1