turtlechanのブログ

無知の私がLinuxをいじりながら書いていくブログ

【Linux】HDD を NTFS でフォーマット

先日、外付けHDD を NTFS でフォーマットした。
しばらくしたらやり方忘れるので、記事にまとめておく。

ラズパイでの操作を想定。Debian系なら基本的に同じはず。

はじめに

ざっくり手順は以下。

  1. ntfs-3g をインストール
  2. fdisk でパーティションを切る
  3. mkfs.ntfsパーティションをフォーマット
  4. マウントして確認

忘れた時のため、説明が無駄に長くなってます。

準備

バックアップを取る

HDD に入っているデータは消えるので事前にバックアップを取っておくこと。

必要ソフトをインストール

ラズパイにデフォでは NTFS を扱うツールが入っていない。

以下のコマンドで ntfs-3g をインストール

$ sudo apt-get install -y ntfs-3g

上記の ntfs-3g を入れれば、NTFS のフォーマット、マウント、書き込み、読み込みに必要なツール(ソフト)がインストールされます。

パーティションの設置

HDDにパーティションを設けます。
パーティションを作らずまるごとフォーマットしてもいいですが、作っておいた方が融通が効く。
特に理由がない限りパーティションを設けるべき。

以下の操作はアンマウントした状態で行う。すでにマウントしている場合はアンマウントしておく。

目的のHDDのデバイスファイルを探す

外付けHDDを接続して以下のコマンド。

$ sudo fdisk -l

以下の感じに表示されると思うので、容量等見て目的のデバイスファイルを探します。

Disk /dev/ram0: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

~ 中略 ~

Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 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
Disklabel type: dos
Disk identifier: 0xeff4f6a1

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk0p1       8192    96042    87851 42.9M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      98304 31116287 31017984 14.8G 83 Linux


Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: dos
Disk identifier: 0x00000000

今回私の場合は '/dev/sda' が目的のデバイスファイルです。
環境によって /dev/sdb だったりもするので必ず確認すること。

バイスファイルを指定してパーティションの設置

目的のデバイスファイルが分かったら、fdisk コマンドでパーティションを設けます。

以下のコマンド。

$ sudo fdisk /dev/sda

※デバイスファイルは環境に合わせて指定してください。

以下の感じなるんで、コマンドを打って操作していきます。
コマンドに 'w' を指定するまで書き込まれないので、間違えてもやり直せば大丈夫。

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): 

'p' で現在のパーティションを確認できます。不要なのがあれば 'd' で消しておきます。

Command (m for help): p
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: dos
Disk identifier: 0x00000000

Command (m for help): 

'n' で新たなパーティションを追加します。
今回はHDD全体を一つのパーティションにしますので、全部何も入力せず Enter してます。

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):  ← パーティションのタイプ、プライマリか拡張

Using default response p.
Partition number (1-4, default 1):  ← パーティション番号、通常はそのまま
First sector (2048-976773167, default 2048):  ← パーティションの先頭、通常はそのまま
Last sector, +sectors or +size{K,M,G,T,P} (2048-976773167, default 976773167):  パーティションの末尾、設けたいパーティションのサイズを入力。

Created a new partition 1 of type 'Linux' and of size 465.8 GiB.

Command (m for help): 

'p' で確認してみる。この段階ではまだ書き込まれていないのでやり直せる。

Command (m for help): p
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 976773167 976771120 465.8G 83 Linux

Command (m for help): 

/dev/sda1 とか言うのが作られているのが確認できるかと思います。
右っかわの Type が Linux になっているので変更します。

't' を入力して変更します。今回は NTFS にする予定なので'HPFS/NTFS/exFAT'にします。

Command (m for help): t
Selected partition 1  ← パーティション番号、一つの場合は勝手に選択される模様
Partition type (type L to list all types): l  ← 'l' でタイプのリストを表示

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Partition type (type L to list all types): 7  ← 上記のリストから目的のタイプ番号を入力
Changed type of partition 'Linux' to 'HPFS/NTFS/exFAT'.

Command (m for help): 

'p' で確認します。

Command (m for help): p
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 976773167 976771120 465.8G  7 HPFS/NTFS/exFAT

Command (m for help): 

'HPFS/NTFS/exFAT' になっているのが確認できると思います。

問題がなければ書き込みます。
'w' で書き込む。
※HDD内のデータは読み込めなくなるのでバックアップを取っていることを確認してから行って下さい。

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.  

書き込むと勝手にfdiskが終了してプロンプトに戻ってくる。
以下のコマンドでパーティションが作れたか確認します。

$ sudo fdisk -l
~ 省略 ~

Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 976773167 976771120 465.8G  7 HPFS/NTFS/exFAT

/dev/sda に /dev/sda1 が確認できました。

NTFS でフォーマット

パーティションが作れたら、次はフォーマット。

パーティションNTFS でフォーマットする

上記で新たに作ったパーティションNTFS でフォーマットします。
'/dev/sda1' が今回作った対象のデバイスファイルです。※環境によって違います。

以下のコマンド。

$ sudo mkfs.ntfs -Q /dev/sda1
Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

'-Q' はクイックフォーマット。指定しないとゼロ埋めするのでめちゃくちゃ時間が掛かる。

NTFS をマウントできるか確認

フォーマットが完了したら、ちゃんとマウントできるか確認しておきましょう。

以下のコマンド等で適当なところにマウントしてみます。

$ sudo mount /dev/sda1 /mnt

df コマンドで表示してみます。

$ df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/root         15G  2.3G   12G   17% /
devtmpfs         459M     0  459M    0% /dev
tmpfs            464M     0  464M    0% /dev/shm
tmpfs            464M   30M  434M    7% /run
tmpfs            5.0M  4.0K  5.0M    1% /run/lock
tmpfs            464M     0  464M    0% /sys/fs/cgroup
tmpfs             64M     0   64M    0% /tmp
tmpfs             16M     0   16M    0% /var/tmp
tmpfs             16M   16M     0  100% /var/log
/dev/mmcblk0p1    43M   23M   20M   53% /boot
tmpfs             93M     0   93M    0% /run/user/1001
/dev/sda1        466G   79M  466G    1% /mnt

認識されていますね。あとは煮るなり焼くなり好きに使えばいいです。

おわりに

長くて読む気力がなくなる出来になりましたが、私が忘れた時用なのでご容赦下さい。

 

何かの参考になれば幸いです。