我想知道如何从终端格式化一个存储驱动器。在答案中提供有用的东西是经常使用的命令选项和基础知识,可以用来推断未来的用途。具体来说,我想知道如何在不同的文件系统中格式化,如NTFS、FAT32、EXT4等。还想知道如何通过终端对驱动器进行分区的信息。
我正试图将一个大容量的外置硬盘(EHDD)格式化为NTFS--从终端。
我知道我可以用gparted来做这件事,也可以用其他GUI程序,但我还是想知道如何从终端做这件事。
fdisk
(比较老,不支持GPT4)。
2.2. parted
(GParted的CLI兄弟)。
3.各种mkfs
程序,如果你已经有了分区并希望进行格式化。
fdisk
和parted
是交互式的,并且有帮助命令,所以你可以随时在程序中寻找帮助。这两个程序也是可以编写脚本的。mkfs
命令不是交互式的。fdisk
fdisk
希望有一个设备(例如/dev/sda
)作为参数。它有以下命令。
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the DOS compatibility flag
d delete a partition
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
。我只关注一下。parted
parted
不*需要一个参数(它试图猜测),但你应该总是指定磁盘。如果有选择的话,parted
是你应该选择的程序。它有以下命令。
align-check TYPE N check partition N for TYPE(min|opt) alignment
check NUMBER do a simple check on the file system
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
resizepart NUMBER END resize partition NUMBER
move NUMBER START END move partition NUMBER
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resize NUMBER START END resize partition NUMBER and its file system
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
这些命令可以收缩为一个独特的前缀(例如,h
是help
的缩写)。
我将使用我创建的一个临时文件(/tmp/part
)来向你展示这些命令,所以尺寸会有些小。你应该用你需要的设备(例如/dev/sda
)来替换它。
首先,如果你的磁盘没有分区表,我们必须创建一个。
parted /tmp/part mklabel gpt
或者mklabel msdos
,如果你想要老式的4个主分区的东西(称为MBR或MSDOS分区表)。
然后我们做,比如说,一个ext4分区,从3GB开始(即留下初始的3G空间),大小为2GB(即在5GB结束)。parted
希望mkpartfs
的位置以MB为单位,但我们可以指定后缀。
parted /tmp/part mkpart primary ext4 3G 5G
还有一个,现在是1GB的NTFS分区。
parted /tmp/part mkpart primary ntfs 5G 6G
结果。
# parted /tmp/part print
Model: (file)
Disk /tmp/blah: 10.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 3000MB 5000MB 2000MB primary
2 5000MB 6000MB 1000MB primary msftdata
注意它是如何使用SI前缀的,而GParted则坚定地使用二进制前缀(同时放弃了愚蠢的i
)。
我将给这些分区贴上标签。
# parted /tmp/part name 1 hello
# parted /tmp/part name 2 world
# parted /tmp/part print
Model: (file)
Disk /tmp/blah: 10.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 3000MB 5000MB 2000MB hello
2 5000MB 6000MB 1000MB world msftdata
虽然parted
可以很好地创建文件系统ntfs
的分区,但它不能将现有分区(!)格式化为NTFS。
mkfs partition fs-type
Make a filesystem fs-type on partition. fs-type can be one
of "fat16", "fat32", "ext2", "linux-swap", or "reiserfs".
mkfs
,像fsck
一样,本质上是各种文件系统特定命令的前端。例如,在我的系统中,mkfs.bfs
, mkfs.cramfs
, mkfs.ext2
, mkfs.ext3
, mkfs.ext4
, mkfs.ext4dev
, mkfs.fat
, mkfs.minix
, mkfs.msdos
, mkfs.ntfs
, mkfs.vfat
都可以使用。
现在,不幸的是,虽然parted
在文件上操作得很好,就像我上面用的那个,但mkfs
不能在这种文件中寻找分区。事实上,它需要的是块状设备,所以如果我打算为mkfs
使用一个新文件/tmp/file
,我必须强迫它这么做。你将使用与你想格式化的分区相对应的块设备,如/dev/sda2
。
`mkfs'的一般语法是。
# mkfs --help
Usage: mkfs [options] [-t type fs-options] device [size]
Options:
-t, --type=TYPE file system type, when undefined ext2 is used
fs-options parameters to real file system builder
device path to a device
size number of blocks on the device
-V, --verbose explain what is done
defining -V more than once will cause a dry-run
-V, --version output version information and exit
-V as version must be only option
-h, --help display this help and exit
For more information, see mkfs(8).
正如你所看到的,-t
标志让我们传递文件系统特定的标志。例如,NTFS的标志。
# mkfs.ntfs --help
Usage: mkntfs [options] device [number-of-sectors]
Basic options:
-f, --fast Perform a quick format
-Q, --quick Perform a quick format
-L, --label STRING Set the volume label
-C, --enable-compression Enable compression on the volume
-I, --no-indexing Disable indexing on the volume
-n, --no-action Do not write to disk
Advanced options:
-c, --cluster-size BYTES Specify the cluster size for the volume
-s, --sector-size BYTES Specify the sector size for the device
-p, --partition-start SECTOR Specify the partition start sector
-H, --heads NUM Specify the number of heads
-S, --sectors-per-track NUM Specify the number of sectors per track
-z, --mft-zone-multiplier NUM Set the MFT zone multiplier
-T, --zero-time Fake the time to be 00:00 UTC, Jan 1, 1970
-F, --force Force execution despite errors
Output options:
-q, --quiet Quiet execution
-v, --verbose Verbose execution
--debug Very verbose execution
Help options:
-V, --version Display version
-l, --license Display licensing information
-h, --help Display this help
Developers' email address: [email protected]
News, support and information: http://tuxera.com
因此,让我们制作一个NTFS分区,并进行快速格式化(-Q
),强迫它在一个非块设备文件上操作(-F
),并设置一个标签(-L "hello world"
)。
# mkfs -t ntfs -F -Q -L "hello world" /tmp/file
/tmp/file is not a block device.
mkntfs forced anyway.
The sector size was not specified for /tmp/file and it could not be obtained automatically. It has been set to 512 bytes.
The partition start sector was not specified for /tmp/file and it could not be obtained automatically. It has been set to 0.
The number of sectors per track was not specified for /tmp/file and it could not be obtained automatically. It has been set to 0.
The number of heads was not specified for /tmp/file and it could not be obtained automatically. It has been set to 0.
Cluster size has been automatically set to 4096 bytes.
To boot from a device, Windows needs the 'partition start sector', the 'sectors per track' and the 'number of heads' to be set.
Windows will not be able to boot from this device.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.
显然,它并不喜欢在文件上工作。)别担心,当在实际磁盘上工作时,它应该会自动检测到大多数数值。甚至这个"文件"作为一个文件系统也能正常工作。
# mount -t ntfs-3g /tmp/file /mnt
# touch "/mnt/a file in mnt"
# ls -l /mnt
total 0
-rwxrwxrwx 1 root root 0 Aug 29 06:43 a file in mnt
# umount /mnt
# ls -l /mnt
total 0
1.在这个答案中,我还没有在任何地方使用sudo
。因为我是在文件上操作,而且是由我拥有的文件,我不需要sudo
。parted
会警告你这一点。对于块设备,通常总是由root
拥有,你将需要sudo
(或者你必须通过sudo -i
或sudo su -
使用root shell)。
2.2. parted
是一个GNU程序,和许多GNU程序一样,有大量的info
格式的文档。安装parted-doc
(sudo apt-get install parted-doc
),然后运行info parted
。你也可以查看一下在线用户手册。
3.GParted能够将一个分区格式化为NTFS,因为它直接调用相应的mkfs'程序(在这里是
mkntfs'--mkfs.ntfs'只是
mkntfs'的一个链接)。它还设置了一些参数。事实上,对于大多数操作,你可以检查GParted信息的细节,看看哪些命令被运行。
4. 我不会去讨论GPT与MBR/MSDOS分区表的优劣,但GPT很可能出现在带有UEFI的新设备上,特别是如果你的设备上有Windows 8。https://askubuntu.com/questions/340841/the-state-of-partitioning-tools,讨论如果你面临GPT,有哪些工具可用。
5.LVM、ZFS和btrfs是另一种游戏。它们都有相应的工具,你应该使用它们而不是 "parted "或 "fdisk"(也许除了为它们创建分区的初始步骤)。
parted
程序的语法是:
parted [options] [device [command [options...]...]]
当你运行 "parted "时,没有命令,比如。
parted /tmp/parted
你会看到一个简单的外壳,在那里你可以运行上述命令。然而,这些命令也可以直接使用parted
程序来运行。所以这三条是等价的。
# parted /tmp/parted
GNU Parted 2.3
Using /tmp/parted
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
和
# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /tmp/parted
Using /tmp/parted
(parted) mklabel gpt
和
parted /tmp/parted mklabel gpt
还请注意,当用 "parted "创建分区时,分区结束的一个有用指标是"-1s"(这是在连字符和 "s "之间的"1")。如果你想让你的分区从一个指定的开始跨越到磁盘的其他部分,这很有用。更具体地说,运行
parted /dev/sda -- mkpart primary ext4 3G -1s
将创建一个/dev/sda
的分区,从3G开始,到/dev/sda
磁盘的最后一个扇区结束(即从3G到整个磁盘的剩余部分)。注意"--"是必要的,因为 "1s "不会被解释为一个无效的选项。
首先,你如何用fdisk工具对你的硬盘进行实际分区。
Linux只允许4个主分区。你可以通过对一个主分区进行细分来拥有更多的逻辑分区。
只有其中一个主分区可以被细分。
在命令提示符下以root身份输入fdisk设备,就可以启动fdisk。
设备可能是/dev/sda或/dev/sdb之类的。
你所需要的基本fdisk命令是。
p print the partition table
n create a new partition
d delete a partition
q quit without saving changes
w write the new partition table and exit
你对分区表所做的修改在你发出写入(w)命令之前不会生效。
下面是一个分区表样本。
Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 184 370912+ 83 Linux
/dev/sdb2 185 368 370944 83 Linux
/dev/sdb3 369 552 370944 83 Linux
/dev/sdb4 553 621 139104 82 Linux swap
例子。
从 shell 提示符下启动 fdisk。
sudo su
fdisk /dev/sdb
这表明你正在使用你的SATA控制器上的第二个驱动器。
Command (m for help): p
Disk /dev/hdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
That makes for 384Mb per partition.
Now You get to work.
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-621, default 1):<RETURN>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-621, default 621): +384M
Next, You set up the partition You want to use for swap:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (197-621, default 197):<RETURN>
Using default value 197
Last cylinder or +size or +sizeM or +sizeK (197-621, default 621): +128M
现在,分区表看起来是这样的。
Device Boot Start End Blocks Id System
/dev/sdb1 1 196 395104 83 Linux
/dev/sdb2 197 262 133056 83 Linux
最后,你让第一个分区成为可启动分区。
Command (m for help): a
Partition number (1-4): 1
And You make the second partition of type swap:
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap)
Command (m for help): p
最终的结果。
Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 196 395104+ 83 Linux
/dev/sdb2 197 262 133056 82 Linux swap
最后,你发出写命令(w),把表写到磁盘上。
mkfs工具用于在Linux系统上创建文件系统(ext2、ext3、ext4等)。
你应该向mkfs指定要创建文件系统的设备名称。
查看可用的文件系统生成器命令
文件系统生成器(mkfs*命令)通常在/sbin/、/sbin/fs、/sbin/fs.d、/etc/fs和/etc等目录中搜索到。
如果没有找到,最后会搜索PATH变量中的目录。
下面的列表显示了系统中可用的mkfs*命令。
sudo su
cd /sbin
ls mkfs*
mkfs mkfs.bfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev
mkfs.minix mkfs.msdos mkfs.ntfs mkfs.vfat
在特定设备上建立一个文件系统
为了使用mkfs命令建立文件系统,需要的参数是设备-文件名和文件系统类型,如下所示。
下面的例子在/dev/sdb1分区上创建ext4文件系统。
sudo su
mkfs -t ext4 /dev/sdb1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1120112 inodes, 4476416 blocks
223820 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
137 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
请注意,mkfs命令的默认文件系统类型是ext2。
如果你不指定"-t "选项,它将创建ext2文件系统。
另外,你可以使用我们前面讨论的方法来识别你的文件系统是ext2还是ext3或ext4。
格式化一个NTFS驱动器
首先,你需要有创建NTFS文件系统的能力,所以安装ntfsprogs。
sudo su
apt-get install ntfs-3g
第二,你把分区炸掉,重新创建为NTFS。
sudo su
umount /dev/sdb1
fdisk /dev/sdb
Options to select:
‘d’ to delete the partition
‘n’ to create a new partition
‘p’ for primary
‘1’ for partition number
‘Enter’ for first cylinder (default 1)
‘Enter’ for last cylinder (default of max size)
‘t’ for type
‘L’ to list codes, and enter code for HPFS/NTFS. In my case, it’s ‘7’
‘w’ to write changes to disk, and exit
umount /dev/sdb1
在最后一步中,你要卸载该分区,因为Ubuntu又为你自动挂载了它。
现在,你需要创建文件系统。有两种方法:一种是没有耐心的方法(快速格式化),另一种是更好但更长的方法(完全格式化)。
快速格式化
这只是分配磁盘空间,但并不清除驱动器或检查坏扇区。这意味着它需要几秒钟。
sudo su
mkfs.ntfs -f /dev/sdb1
全格式化
如果你更关心数据的完整性,并且不介意等待,那就做一个完整的格式。
这可能需要几个小时才能把一个大的驱动器清空。
sudo su
mkfs.ntfs /dev/sdb1