Share
Embed
Transcript
31 slides
-
自己紹介 大西 和貴 出身: 京都 Twitter: _k_onishi_ Linux / Kernel
/ CPU / File System / Container / Virtualization C / Assembly / Nim SAKURA internet Inc. アプリケーショングループ / レンタルサーバーチーム -
環境 $ uname -a Linux test 4.15.0-44-generic #47-Ubuntu SMP Mon
Jan 14 11:26:59 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux $ cat /etc/os-release NAME="Ubuntu" VERSION="18.04.1 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.1 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic -
使用ディスク $ sudo fdisk -l Disk /dev/vdb: 20 GiB, 21474836480
bytes, 41943040 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes -
パーティション作成 $ sudo fdisk /dev/vdb # パーティションの作成 Command (m for
help): n # パーティションタイプ Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p # プライマリ # パーティション番号 Partition number (1-4, default 1): # 1 # パーティションサイズ First sector (2048-41943039, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G # 2GB Created a new partition 1 of type 'Linux' and of size 2 GiB. # 保存 Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. -
パーティション作成 # ext2~4への後方互換性を守るためにinodeのサイズがデフォルトで256バイトになるので # ext2で必要最低限のサイズである128バイトを指定する。 $ sudo mke2fs -I 128
/dev/vdb1 mke2fs 1.44.1 (24-Mar-2018) Creating filesystem with 524288 4k blocks and 131072 inodes Filesystem UUID: 0382c937-89bb-497c-b768-504c72bef867 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done -
MBR(Master Boot Record) ディスクの先頭セクタ(512byte)にはマスターブートレコードというどのパーティションにも属さない領 域が存在する。 名前 サイズ 説明 ブートローダ 446
byte OS起動用プログラム パーティションテーブル 1 16 byte パーティション情報 パーティションテーブル 2 16 byte パーティション情報 パーティションテーブル 3 16 byte パーティション情報 パーティションテーブル 4 16 byte パーティション情報 シグネチャ 2 byte 0xAA55 -
パーティションテーブル パーティション情報を保持するテーブル。 CHSは使用されておらず現在はLBAが主流。 名前 サイズ 説明 ブートフラグ 1 byte 起動の能否(0x00:
ブート不可, 0x80: ブート可能) パーティション開始位置 (CHS) 3 byte CHS: Cylinder Head Sector パーティションタイプ 1 byte パーティション終了位置 (CHS) 3 byte CHS: Cylinder Head Sector パーティション開始位置 (LBA) 4 byte LBA: Logical Block Addressing セクタ数 4 byte セクタ数 -
ブロックグループ 名前 サイズ 説明 Super Block* 1 block ブロックのサイズや総ブロック数、グループ毎の inode数など
Block Group Descriptor Table* n block 各ブロックグループの inode総数や空きinode数、 空きブロック数などを Reserved GDT Blocks* n block GDTのために予約されたブロック Block Bitmap 1 block ブロックの使用状況 Inode Bitmap 1 block inodeの使用状況 Inode Table n block このブロックグループが保持する inode情報 Data Blocks n block 実際のデータを含むブロック -
ブロックグループ Super Block及びBlock Group Descriptor、Reserved GDT Blocksはこのパーティション全体の情 報で、ブロックグループの0, 1及び3, 5,
7の累乗番目にのみ存在する。ブロックグループの 0番目が プライマリ、残りはバックアップとなる。 (Ext2の初期バージョンでは全てのブロックグループがそれら を保持していた) Reserved GDT Blocksはグループディスクリプタテーブルのための拡張領域、すなわち予約スペー スとして確保されている。 -
Super Block スーパーブロックはそのパーティション全体の情報を保持しており、主なものとしては以下が挙げられ る(ブロックの構造体はある程度大きいため全ては列挙しない )。 • 総inode数 • 総ブロック数 •
空きブロック数 • 空きinode数 • ブロックサイズ • グループ毎のブロック数 • グループ毎のinode数 • マウント時間 • 書き込み時間 • Etc... -
Block Group Descriptor Table ディスク上にあるブロックグループの情報を保持するブロックグループディスクリプタのテーブル。当 該ディスクリプタは以下のような構造になっている。 名前 サイズ ブロックビットマップの先頭ブロック 4
byte inodeビットマップの先頭ブロック 4 byte 未使用ブロック数 2 byte 未使用inode数 2 byte ディレクトリ数 2 byte パディング 2 byte 予約 4 byte * 3 -
Inode Table ファイルやディレクトリなどの情報を保持する inodeというデータ構造のテーブル。当該構造体は主に 以下のような情報を保持する。 • ファイル種別 • UID •
アクセス時刻 • 作成時刻 • 変更時刻 • 削除時刻 • リンクカウント • ファイルフラグ • データブロック番号 • Etc... -
ルートディレクトリを読んでみる。 ルートディレクトリ(/)の情報は必ずinode番号2に格納されるというルールがある。ここからブロックグ ループ0内にあるinodeテーブルの2番目のエントリを参照すればよいことがわかる。 # (自作プログラムの出力を一部抜粋) block group descriptor table: no.0
(0 ~ 32767): primary super block: 0 group descriptor table: 1 reserved gdt block: 2 block bitmap block: 233 inodes bitmap block: 234 inodes table block: 235 data block: 490 free blocks count: 32272 free inodes count: 8148 used dirs count: 2 : -
ルートディレクトリを読んでみる。 inodeテーブルはブロック番号235から始まることがわかる。次にそのテーブルの 2番目のエントリを 読む。 # (自作プログラムの出力を一部抜粋) inode table (8160) inode
no.1 file mode: uid : 0 access time : 1566179823 creation time : 1566179823 link count : 0 first data block : 0 inode no.2 file mode: socket / block device / directory / user read / user write / user execute / group read / group execute / others read uid : 0 access time : 1566219174 creation time : 1566219086 link count : 4 first data block : 490 : -
ディレクトリエントリ inodeテーブルはブロック番号235から始まることがわかる。次にそのテーブルの 2番目のエントリを 読む。 ドキュメントなどには記載が無いが名前の長さは 4バイトでアライメントされていることが多く、エントリ の長さから名前以外のデータ長の合計を引いたサイズが実際に名前の長さのために確保されいて るデータ領域のサイズとなる。 名前 サイズ
inode番号 4 byte エントリ長 2 byte 名前の長さ 1 byte ファイル種別 1 byte 名前 可変 -
# (自作プログラムの出力を一部抜粋) inodeNumber: 2 entryLength: 12 nameLength: 1 fileType: directory
fileName: . inodeNumber: 2 entryLength: 12 nameLength: 2 fileType: directory fileName: .. inodeNumber: 11 entryLength: 20 nameLength: 10 fileType: directory fileName: lost+found ルートディレクトリを読んでみる。 -
# (自作プログラムの出力を一部抜粋)(続き) inodeNumber: 16321 entryLength: 20 nameLength: 10 fileType: directory
fileName: sample_dir inodeNumber: 12 entryLength: 4032 nameLength: 10 fileType: regular fileName: sample.txt ルートディレクトリを読んでみる。 -
ルートディレクトリを読んでみる。 lsコマンドの結果と比較しても正しいことがわかる。 こうしてinodeの情報を追っていくことで特定のディレクトリやファイルのデータを抽出することができ る。 $ sudo ls -la /mnt/sde1/ total
28 drwxr-xr-x 4 root root 4096 Aug 19 21:51 . drwxr-xr-x 5 root root 4096 Aug 21 00:04 .. drwx------ 2 root root 16384 Aug 19 10:57 lost+found drwxr-xr-x 2 root root 4096 Aug 19 22:11 sample_dir -rw-r--r-- 1 root root 0 Aug 19 21:51 sample.txt