0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ファイルシステムのコマンドまとめ

Last updated at Posted at 2024-07-24

以下に移行しました。
https://hana-shin.hatenablog.com/entry/2022/06/08/193401

#1 はじめに
ext4,xfsファイルシステムで使用するコマンドについてまとめてみました。
実際にディスクを購入することは難しいので、ループバックデバイスを使って、
コマンドの使い方を確認してみました。
なお、各コマンドの使い方については、深く調べていません(概要のみ)。

#2 環境
VMware Workstation 15 Player上の仮想マシンを使いました。
仮想マシンのOS版数は以下のとりです。

OS版数
[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

[root@server ~]# uname -r
3.10.0-957.el7.x86_64

#3 コマンド一覧
以下は、ext4,xfsファイルシステムを操作するコマンド一覧です。

ファイルシステムに対する操作 ext4 xfs
ファイルシステムの作成 mke2fs,mkfs -t ext4 mkfs.xfs
ファイルシステムのパラメータ表示 dumpe2fs xfs_info
ファイルシステムのパラメータ変更 tune2fs xfs_admin
ファイルシステムのラベル表示,変更 e2label xfs_admin
ファイルシステムのエラーチェック,修復 fsck.ext4,e2fsck xfs_repair,fsck.xfs
ファイルシステムのデバッグ debugfs xfs_db
ファイルシステムへのアクセス禁止,解除 fsfreeze xfs_freeze,fsfreeze
ファイルシステムのデフラグ e4defrag xfs_fsr
ファイル断片化の確認 filefrag xfs_bmap

#4 ext4ファイルシステム
##4.1 ファイルシステム作成(mke2fs)
ループバックデバイスで使用するファイル(100M)を作成します。
なお、fallocateコマンドの詳細は、ここ(ファイルの作り方)を参照してください。

ファイルの作成
[root@server ~]# fallocate -l 100M disk.img
[root@server ~]# ls -l disk.img
-rw-r--r--. 1 root root 104857600  2月  1 15:51 disk.img

作成したファイルをループバックデバイスとして登録します。
なお、losetupコマンドの詳細は、ここ(losetupコマンドの使い方)を参照してください。

ループバックデバイスの作成
[root@server ~]# losetup -f /root/disk.img
[root@server ~]# losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0         0      0         0  0 /root/disk.img

ループバックデバイス(/dev/loop0)にext4ファイルシステムを作成します。

ファイルシステムの作成
[root@server ~]# mke2fs -t ext4 /dev/loop0
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

ループバックデバイスに作成したファイルシステムを確認します。
ext4ファイルシステムが作成されたことがわかります。

ファイルシステムの確認
[root@server ~]# lsblk -fip /dev/loop0
NAME       FSTYPE LABEL UUID                                 MOUNTPOINT
/dev/loop0 ext4         7b7292bd-1481-41d5-95e6-567be9ec4aec

作成したループバックデバイスを/mntにマウントします。

マウント結果
[root@server ~]# mount -o loop /dev/loop0 /mnt

ファイルシステムを確認します。
ループバックデバイスが/mntにマウントされていることがわかります。

ファイルシステムの確認
[root@server ~]# df -hT -t ext4
ファイルシス   タイプ サイズ  使用  残り 使用% マウント位置
/dev/loop1     ext4      93M  1.6M   85M    2% /mnt

##4.2 パラメータ表示/変更(dumpe2fs/tune2fs)
ここでは、ファイルシステムのチェック回数を変更してみます。
マウント回数が、チェック回数を超えると、次回ブート時にファイルシステムにfsckを行います。

ファイルシステム作成直後は、チェック回数が-1になっています。

チェック回数の確認(変更前)
[root@server ~]# dumpe2fs -h /dev/loop0 |grep "Maximum mount count"
dumpe2fs 1.42.9 (28-Dec-2013)
Maximum mount count:      -1

チェック回数を5に設定してみます。

チェック回数の変更
[root@server ~]# tune2fs -c 5 /dev/loop0
tune2fs 1.42.9 (28-Dec-2013)
Setting maximal mount count to 5

チェック回数が5に設定されたことがわかります。

チェック回数の確認(変更後)
[root@server ~]# dumpe2fs -h /dev/loop0 |grep "Maximum mount count"
dumpe2fs 1.42.9 (28-Dec-2013)
Maximum mount count:      5

##4.3 ラベルの表示/設定(e2label)
ファイルシステム作成直後のラベルを確認してみます。
ファイルシステムには、ラベルが設定されていないことがわかります。

ラベルの確認
[root@server ~]# e2label /dev/loop0

[root@server ~]#

TESTという名前のラベルを設定してみます。

ラベルの設定
[root@server ~]# e2label /dev/loop0 TEST

TESTという名前のラベルが設定されたことがわかります。

ラベルの確認
[root@server ~]# e2label /dev/loop0
TEST

なお、他のコマンドでも、ラベルの確認ができます。

dumpe2fsコマンドによるラベル確認
[root@server ~]# dumpe2fs -h /dev/loop0 |grep "Filesystem volume name"
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   TEST
lsblkコマンドによるラベル確認
[root@server ~]# lsblk -fip /dev/loop0
NAME       FSTYPE LABEL UUID                                 MOUNTPOINT
/dev/loop0 ext4   TEST  a081a45a-81a6-4fb6-bbdc-604dc5123165
blkidコマンドによるラベル確認
[root@server ~]# blkid /dev/loop0
/dev/loop0: LABEL="TEST" UUID="a081a45a-81a6-4fb6-bbdc-604dc5123165" TYPE="ext4"

##4.4 エラーチェック(fsck.ext4)

実行結果
[root@server ~]# fsck.ext4 /dev/loop0
e2fsck 1.42.9 (28-Dec-2013)
TEST: clean, 11/25688 files, 8896/102400 blocks (check in 4 mounts)

##4.5 デバッグ(debugfs)
debugfsコマンドを実行します。

debugfsコマンドの実行
[root@server ~]# debugfs /dev/loop0
debugfs 1.42.9 (28-Dec-2013)
debugfs:

コマンドプロンプトに対してhelpと入力すると、オプションが表示されます。

オプションの表示
[root@server ~]# debugfs /dev/loop0
debugfs 1.42.9 (28-Dec-2013)
debugfs:  help
Available debugfs requests:

show_debugfs_params, params
                         Show debugfs parameters
open_filesys, open       Open a filesystem
close_filesys, close     Close the filesystem
-以下、略-

##4.6 アクセス禁止/解除(fsfreeze)
ここでは、/mntへのアクセス禁止/解除を試してみます。

ファイルシステムの確認
[root@server ~]# df -hT -t ext4
ファイルシス   タイプ サイズ  使用  残り 使用% マウント位置
/dev/loop1     ext4      93M  1.6M   85M    2% /mnt

/mntへのアクセスを禁止にします。

アクセス禁止(-f)
[root@server ~]# fsfreeze -f /mnt

アクセス禁止にしたファイルシステムにファイルを作成します。
アクセス禁止になっているため、touchコマンドが復帰しません。

ファイルの作成
[root@server ~]# touch /mnt/test.txt

もう1つターミナルを開きます。
新しく開いたターミナルで/mntへのアクセス禁止を解除します。

アクセス禁止を解除(-u)
[root@server ~]# fsfreeze -u /mnt

アクセス禁止を解除するとtouchコマンドが復帰します。
touchコマンドが復帰したら、作成したファイルを確認します。

作成したファイルの確認
[root@server ~]# touch /mnt/test.txt
[root@server ~]#
[root@server ~]# ls /mnt/test.txt
/mnt/test.txt

##4.7 断片化の状況確認/デフラグ(e4defrag)

テスト用ファイルの作成
[root@server ~]# fallocate -l 80M /mnt/test.dat

断片化の状況を確認します。
ファイルが、22個のextentに分割されていることがわかります。
また、This directory (/mnt) does not need defragmentation.と表示されており、
デフラグの必要がないことがわかります。

断片化の状況確認(-c)
[root@server ~]# e4defrag -c /mnt
<Fragmented files>                             now/best       size/ext
1. /mnt/test.dat                                22/1           3723 KB

 Total/best extents                             22/1
 Average size per extent                        3723 KB
 Fragmentation score                            0
 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
 This directory (/mnt) does not need defragmentation.
 Done.

ためしに、デフラグを実行してみます。
デフラグをするには、オプションなしでe4defragコマンドを実行します。

デフラグの実行結果
[root@server ~]# e4defrag /mnt
ext4 defragmentation for directory(/mnt)

        Success:                        [ 0/3 ]
        Failure:                        [ 3/3 ]

##4.8 ファイル断片化の確認(filefrag)
テスト用ファイルを作成するファイルシステムを確認します。
テスト用ファイルは、/mntに作成します。

[root@server ~]# df -hT -t ext4
ファイルシス   タイプ サイズ  使用  残り 使用% マウント位置
/dev/loop1     ext4      93M   82M  4.4M   95% /mnt

/mntにテスト用ファイル(80M)を作成してみます。

テスト用ファイルの作成
[root@server ~]# fallocate -l 80M /mnt/test.dat

ファイルの各部分がファイルシステム内のどのブロックにマッピングされているかがわかります

実行結果
[root@server ~]# filefrag -v /mnt/test.dat
Filesystem type is: ef53
File size of /mnt/test.dat is 83886080 (81920 blocks of 1024 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..    2047:      71681..     73728:   2048:             unwritten
   1:     2048..   26623:      75777..    100352:  24576:      73729: unwritten
   2:    26624..   28671:       6145..      8192:   2048:     100353: unwritten
   3:    28672..   30719:       3512..      5559:   2048:       8193: unwritten
   4:    30720..   32766:     100353..    102399:   2047:       5560: unwritten
   5:    32767..   32767:       5560..      5560:      1:     102400: unwritten
   6:    32768..   34557:      24835..     26624:   1790:       5561: unwritten
   7:    34558..   34815:      41219..     41476:    258:      26625: unwritten
   8:    34816..   36605:      57603..     59392:   1790:      41477: unwritten
   9:    36606..   36863:      73987..     74244:    258:      59393: unwritten
  10:    36864..   38651:       8453..     10240:   1788:      74245: unwritten
  11:    38652..   38911:      41477..     41736:    260:      10241: unwritten
  12:    38912..   40443:      74245..     75776:   1532:      41737: unwritten
  13:    40444..   40959:       5561..      6076:    516:      75777: unwritten
  14:    40960..   42231:      41737..     43008:   1272:       6077: unwritten
  15:    42232..   42299:       6077..      6144:     68:      43009: unwritten
  16:    42300..   47103:      10241..     15044:   4804:       6145: unwritten
  17:    47104..   55295:      16385..     24576:   8192:      15045: unwritten
  18:    55296..   69631:      26625..     40960:  14336:      24577: unwritten
  19:    69632..   75775:      43009..     49152:   6144:      40961: unwritten
  20:    75776..   79871:      53249..     57344:   4096:      49153: unwritten
  21:    79872..   81919:      59393..     61440:   2048:      57345: unwritten,eof
/mnt/test.dat: 22 extents found

##4.9 あと始末
テストに使用したファイルシステムのアンマウント、ループバックデバイスの削除をします。

アンマウント
[root@server ~]# umount /mnt
ループバックデバイス削除
[root@server ~]# losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0         0      0         0  0 /root/disk.img
[root@server ~]# losetup -d /dev/loop0
[root@server ~]# losetup -l
[root@server ~]#
ファイルの削除
[root@server ~]# rm disk.img
rm: 通常ファイル `disk.img' を削除しますか? y

#5 xfsファイルシステム

##5.1 ファイルシステム作成(mkfs.xfs)
ループバックデバイスで使用するファイル(100M)を作成します。

ファイルの作成
[root@server ~]# fallocate -l 100M disk.img
[root@server ~]# ls -l disk.img
-rw-r--r--. 1 root root 104857600  2月  1 18:22 disk.img

作成したファイルをループバックデバイスとして登録します。
なお、losetupコマンドの詳細は、ここ(losetupコマンドの使い方)を参照してください。

ループバックデバイスの作成
[root@server ~]# losetup -f /root/disk.img
[root@server ~]# losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0         0      0         0  0 /root/disk.img

ループバックデバイスにxfsファイルシステムを作成します。

ファイルシステムの作成
[root@server ~]# mkfs.xfs /dev/loop0
meta-data=/dev/loop0             isize=512    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

ループバックデバイス(/dev/loop0)にxfsファイルシステムが作成されたことを確認します。

ファイルシステムの確認
[root@server ~]# lsblk -fip /dev/loop0
NAME       FSTYPE LABEL UUID                                 MOUNTPOINT
/dev/loop0 xfs          80b2398e-9741-45fb-86ca-8a2675c0b231

作成したループバックデバイス(/dev/loop0)を/mntにマウントします。

マウント結果
[root@server ~]# mount -o loop /dev/loop0 /mnt

ファイルシステムを確認します。
ループバックデバイスが/mntにマウントされていることがわかります。

ファイルシステムの確認
[root@server ~]# df -hT -t xfs
ファイルシス   タイプ サイズ  使用  残り 使用% マウント位置
/dev/sda3      xfs       40G  4.2G   36G   11% /
/dev/sda1      xfs      253M  118M  135M   47% /boot
/dev/loop1     xfs       97M  5.3M   92M    6% /mnt

##5.2 パラメータの表示(xfs_info)

[root@server ~]# xfs_info /mnt/
meta-data=/dev/loop1             isize=512    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

##5.3 パラメータの変更(xfs_admin)
ここでは、ファイルシステムのラベルを変更してみます。

ラベルの確認(変更前)
[root@server ~]# xfs_admin -l /dev/loop0
label = ""

TESTという名前のラベルを設定してみます。

ラベルの変更
[root@server ~]# xfs_admin -L TEST /dev/loop0
writing all SBs
new label = "TEST"

TESTという名前のラベルが設定されたことがわかります。

ラベルの確認(変更後)
[root@server ~]# xfs_admin -l /dev/loop0
label = "TEST"

##5.4 エラーチェック(xfs_repair)
ファイルシステムが壊れていないかどうかをチェックします。
壊れていた場合、ファイルシステムの修復を行います。
ファイルシステムをアンマウントした状態で実行します。

実行結果
[root@server ~]# xfs_repair /dev/loop0
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 3
        - agno = 2
Phase 5 - rebuild AG headers and trees...
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done

##5.5 デバッグ(debugfs)

コマンドプロンプトに対してhelpと入力すると、オプションが表示されます。

helpの表示方法
[root@server ~]# xfs_db /dev/loop0
xfs_db> help
ablock filoff -- set address to file offset (attr fork)
addr [field-expression] -- set current address
agf [agno] -- set address to agf header
agfl [agno] -- set address to agfl block
-以下、略-

##5.6 ファイルシステムのデフラグ(xfs_fsr)

テスト用ファイルの作成
[root@server ~]# fallocate -l 80M /mnt/test.dat

ファイルシステムのデフラグを実行する。
ここでは、テスト用のファイルサイズが大きいためか、
デフラグするためのスペースが足りず、デフラグできませんでした。

実行結果
[root@server ~]# xfs_fsr -v /mnt
/mnt start inode=0
ino=67
insufficient freespace for: ino=67: size=83886080: ignoring

##5.7 ファイル断片化の確認(xfs_bmap)
テスト用ファイルを作成するファイルシステムを確認します。

[root@server ~]# df -hT -t xfs
ファイルシス   タイプ サイズ  使用  残り 使用% マウント位置
/dev/sda3      xfs       40G  4.2G   36G   11% /
/dev/sda1      xfs      253M  118M  135M   47% /boot
/dev/loop1     xfs       97M  5.3M   92M    6% /mnt

/mntにテスト用ファイル(80M)を作成してみます。

テスト用ファイルの作成
[root@server ~]# fallocate -l 80M /mnt/test.dat

ファイルの断片化を確認します。
ファイルの各部分がファイルシステム内のどのブロックにマッピングされているかがわかります。
ファイルが4つのエクステントに分割されていることがわかります。

実行結果
[root@server ~]# xfs_bmap -v /mnt/test.dat
/mnt/test.dat:
 EXT: FILE-OFFSET       BLOCK-RANGE      AG AG-OFFSET        TOTAL FLAGS
   0: [0..51063]:       128..51191        0 (128..51191)     51064 10000
   1: [51064..102191]:  51264..102391     1 (64..51191)      51128 10000
   2: [102192..146479]: 109304..153591    2 (6904..51191)    44288 10000
   3: [146480..163839]: 153664..171023    3 (64..17423)      17360 10000

フラグメントの状況は、xfs_dbコマンドを使っても確認できます。

xfs_dbの実行結果
[root@server ~]# xfs_db -r /dev/loop0
xfs_db> frag
actual 4, ideal 1, fragmentation factor 75.00%
Note, this number is largely meaningless.
Files on this filesystem average 4.00 extents per file

#Z 参考情報
Linux教科書 LPICレベル1 スピードマスター問題集 Version5.0対応
【 xfs_repair 】コマンド――XFSファイルシステムを検査、修復する
今から始めるRHEL 7 第2回 標準ファイルシステムとなったXFS
第3章 XFS ファイルシステム
【xfs_info】コマンドの見方, XFSファイルシステムの特徴
xfs_fsrを使ってXFSファイルシステムをベストの状態で使用する

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?