見出し画像

【Linux】パーティション、ファイルシステム作成

機器のVer

CentOS7

作業ログ


ハードディスクの接続

パーティションを行う前にハードディスク(デバイスファイル)が検出されていること。外付けなどでHDDを増設するためには、システム電源を停止した状態で物理的に接続し、再び電源をONにすると自動的にデバイスを検出する。


パーティションの作成

パーティションとは、ハードディスクを論理的に分割した各領域のこと。MBR、GPT方式があり、主にGPTが使われる。詳細は以下の記事を参照。

パーティションの作成には3つのコマンドがある。どれも対話式で作成していく。今回は fdisk を使用。

parted:MBR、GPT方式共に使用可能。
gdisk:MBR、GPT方式共に使用可能。対話のためのコマンドが短く、オススメ
fdisk:MBR方式のみ使用可能。


現状のパーティションを確認

# fdisk -l /dev/sda

### fdisk -l [デバイス名] でパーティションテーブルを一覧表示
[root@test ~]# fdisk -l /dev/sda

Disk /dev/sda: 11.4 GB, 11437867008 bytes, 22339584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000adae3

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    22339583    10120192   8e  Linux LVM
[root@test ~]# 
[root@test ~]# 


パーティションの作成

fdisk後は、画面が偏移する。下記に記載しているコマンドの説明に詳細を記す。

### 書式
fdisk [options] device

device: デバイス名を指定。
options
 -l, --list: パーティションテーブルの情報を表示します。
 -n, --noauto-pt: パーティションテーブルの自動作成を抑制。
 -u, --sectors: セクター単位で表示。デフォルトでは、バイト単位で表示。
 -v, --version: バージョン情報を表示
。
 -h, --help: ヘルプ情報を表示

表示例

[root@test ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


コマンド (m でヘルプ): p

Disk /dev/sda: 11.4 GB, 11437867008 bytes, 22339584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000adae3

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    22339583    10120192   8e  Linux LVM

コマンド (m でヘルプ): 
コマンド (m でヘルプ): 
コマンド (m でヘルプ): d
パーティション番号 (1,2, default 2): 
Partition 2 is deleted

コマンド (m でヘルプ): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
パーティション番号 (2-4, default 2): 
最初 sector (2099200-22339583, 初期値 2099200): 
初期値 2099200 を使います
Last sector, +sectors or +size{K,M,G} (2099200-22339583, 初期値 22339583): 21000000
Partition 2 of type Linux and of size 9 GiB is set

コマンド (m でヘルプ): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
パーティション番号 (3,4, default 3): 
最初 sector (21000001-22339583, 初期値 21000192): 
初期値 21000192 を使います
Last sector, +sectors or +size{K,M,G} (21000192-22339583, 初期値 22339583): 21500000
Partition 3 of type Linux and of size 244 MiB is set

コマンド (m でヘルプ): t
パーティション番号 (1-3, default 3): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

コマンド (m でヘルプ): p

Disk /dev/sda: 11.4 GB, 11437867008 bytes, 22339584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000adae3

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    21000000     9450400+  83  Linux
/dev/sda3        21000192    21500000      249904+  8e  Linux LVM

コマンド (m でヘルプ): w
パーティションテーブルは変更されました!

ioctl() を呼び出してパーティションテーブルを再読込みします。

WARNING: Re-reading the partition table failed with error 16: デバイスもしくはリソースがビジー状態です.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
ディスクを同期しています。
[root@test ~]# 

コマンドの説明

  • m: ヘルプメニューを表示。

  • p: パーティションのテーブルを表示。

  • n: 新しいパーティションを作成。

  • t: パーティションのタイプを変更。
    よく使われるパーティションのID
     83: Linuxファイルシステム
     82: Linuxスワップ領域
     07: NTFS
     0c: FAT32(LBA)
     8e: Linux LVM

  • d: パーティションを削除。

  • w: パーティションテーブルの変更を保存。

コマンド (m でヘルプ): m
コマンドの動作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)


パーティションの有効化

抜けた後は再度、# fdisk -l /dev/sdaでパーティションが作成されたか確認し、partprobe /dev/sdaで作成したパーティションを有効化する。(パーティションテーブルを再読み込みして、新しいパーティションを認識する)

[root@test ~]# partprobe /dev/sda
[root@test ~]# 

### 成功していれば何も表示はされない


ファイルシステムの作成

ファイルシステムとは、ディレクトリの階層構造においてデータをファイルとして管理する方式のこと。ファイルシステムタイプにはext2、ext3、ext4、xfs、btrfsなどがあり、コマンドは mkfs.filetype や mkfs(mke2fs) -t filetype を用いる。どちらも同じ意味である。

コマンド

mkfs [options] device [size]

### size はファイルシステムのサイズを指定
 -t:ファイルシステムの種類を指定
 -n:ファイルシステムにラベルを付ける。
 -c:ファイルシステムの作成前にデバイス上のバッドブロックをスキャンして修正。
 -v:ファイルシステムの作成過程で詳細な情報を表示。
 -b:ブロックのサイズを指定します。このオプションを指定しない場合、自動的に決定。
 -L:ファイルシステムのサイズを指定。
 -I:ファイルシステムの inode レイアウトを指定。
 -m:ファイルシステムの使用可能なスペースの割合を指定。デフォルトは 5%
[root@test ~]# mkfs -t ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
62496 inodes, 249904 blocks
12495 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
31 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185

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

[root@test ~]# 


マウント

ディスク装置をLinuxのディレクトリ内に埋め込み、使えるようにする作業のこと。コマンドを使って行うと再起動などした時にマウントが解除される。永久設定は /etc/fstab で行う。

マウントの確認

[root@test ~]# mount
-----------------------------------------------------------
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=2347624k,nr_inodes=586906,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12479)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=471880k,mode=700)
-----------------------------------------------------------
[root@test ~]# 


マウント

必要であればマウント先のディレクトリを作成しておく。作業はroot権限で行う必要がある。

mount [-hV] [--help] [--version] [-t fstype] [-O optlist]
      [--source device] [--options opts] dest


### 上記に当てはめてマウントを行う
### options
 -t fstype : ファイルシステムの種類を指定
 -O optlist : マウントオプションをカンマ区切りで指定。例えば、rw(読み書き可能)、ro(読み込み専用)など。
 --source device : マウントするデバイスを指定。
 --options opts : マウントするオプションを指定。複数のオプションをカンマ区切りで指定することができる。
 dest : マウントポイントのパスを指定
オプション

表示例(最後尾)

[root@test ~]# mkdir /tmp/test
[root@test ~]# 
[root@test ~]# 
[root@test ~]# mount -t ext4 /dev/sda3 /tmp/test
[root@test ~]# 
[root@test ~]# 
[root@test ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=2347624k,nr_inodes=586906,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12479)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=471880k,mode=700)
/dev/sda3 on /tmp/test type ext4 (rw,relatime,seclabel,data=ordered)
[root@test ~]# 


アンマウント

作業はroot権限で行う必要がある。

# umount [オプション] [デバイスまたはマウントポイント]

### デバイスまたはマウントポイントを指定すれば両方ともアンマウントされる
オプション

表示例

[root@test ~]# umount -t ext4 /dev/sda3
[root@test ~]# 
[root@test ~]# 
[root@test ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=2347624k,nr_inodes=586906,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12479)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=471880k,mode=700)
[root@test ~]# 

自分がマウントしたディレクトリにいれば、busy表記になりアンマウントできない状態となるので注意が必要。
また、該当のファイルシステム内のすべてのプロセスを終了する必要があり、ディレクトリの使用も止める必要がある。これを行わなければアンマウント中にエラーが発生する。

よろしければサポートお願いします!よりいい情報を発信します。