GoP*_*Pro 68 command-line partitioning ntfs hard-drive
我想知道如何从终端格式化存储驱动器。在答案中提供的有用的东西通常是用于命令和基础知识的选项,人们可以用它们来推断未来的用途。具体来说,我想知道如何在不同的文件系统中进行格式化,例如 NTFS、FAT32、EXT4 等。还需要有关如何通过终端对驱动器进行分区的信息。
我正在尝试从终端将高容量外部硬盘驱动器 (EHDD) 格式化为 NTFS。
我知道我可以为此使用 gparted 以及其他 GUI 程序,但我现在仍然想知道如何从终端执行此操作。
mur*_*uru 79
有几个选项可用:
fdisk并且parted是交互式的,并且具有帮助命令,因此您可以随时在程序中查找帮助。两者也是可编写脚本的。这些mkfs命令不是交互式的。
fdiskfdisk需要一个设备(例如/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)
Run Code Online (Sandbox Code Playgroud)
我用的fdisk不多。我只会专注于:
partedparted并不需要一个参数(它试图“猜”),但你应该总是指定磁盘。鉴于选择,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
Run Code Online (Sandbox Code Playgroud)
命令可以被压缩到一个唯一的前缀(例如,h是 的缩写help)。
我将使用/tmp/part我创建的临时文件 ( ) 来向您展示命令,因此大小会有点小。您应该将其替换为您需要的设备(/dev/sda例如)。
首先,如果您的磁盘没有分区表,我们必须创建一个:
parted /tmp/part mklabel gpt
Run Code Online (Sandbox Code Playgroud)
或者mklabel msdos,如果你想要老式的 4-primary-partition 东西(称为MBR 或 MSDOS 分区表)。然后我们创建一个 ext4 分区,从 3GB 开始(即,保留最初的 3G 空闲)和 2GB 大小(即,以 5GB 结束)。parted期望以 MB 为单位的位置mkpartfs,但我们可以指定后缀:
parted /tmp/part mkpart primary ext4 3G 5G
Run Code Online (Sandbox Code Playgroud)
另一个,现在是 1GB 的 NTFS 分区:
parted /tmp/part mkpart primary ntfs 5G 6G
Run Code Online (Sandbox Code Playgroud)
结果:
# 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
Run Code Online (Sandbox Code Playgroud)
请注意它如何使用 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
Run Code Online (Sandbox Code Playgroud)
虽然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".
Run Code Online (Sandbox Code Playgroud)
事实上, parted 会告诉你应该用它来操作分区,而不是 filesystems,这让我想到:
mkfsmkfs,如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但不能在这些文件中寻找分区。事实上,它希望块设备,所以如果我要使用一个新的文件/tmp/file的mkfs,我必须迫使它这样做。您将使用与要格式化的分区对应的块设备,例如/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).
Run Code Online (Sandbox Code Playgroud)
如您所见,该-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: ntfs-3g-devel@lists.sf.net
News, support and information: http://tuxera.com
Run Code Online (Sandbox Code Playgroud)
所以让我们做一个 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.
Run Code Online (Sandbox Code Playgroud)
显然它不喜欢处理文件。:) 别担心,在实际磁盘上工作时,它应该会自动检测大多数值。即使这个“文件”作为文件系统也能正常工作:
# 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
Run Code Online (Sandbox Code Playgroud)
(看到奇怪的权限了吗?)
sudo在这个答案中的任何地方使用过。由于我正在处理文件以及我拥有的文件,因此我不需要sudo. parted会警告你这一点。对于通常始终归 拥有的块设备,root您将需要sudo(或者您必须通过sudo -i或使用root shell sudo su -)。parted是一个 GNU 程序,并且像许多 GNU 程序一样,具有大量的info格式文档。安装parted-doc( sudo apt-get install parted-doc) 然后运行info parted。您还可以查看在线用户手册。mkfs直接调用适当的程序(mkntfs在这种情况下,mkfs.ntfs只是指向 的链接mkntfs)。它还设置了许多参数。事实上,对于大多数操作,您可以检查 GParted 消息的详细信息以查看运行了哪些命令。partedor fdisk(除了创建供它们使用的分区的初始步骤)。parted使用注意事项:parted程序的语法是:
parted [options] [device [command [options...]...]]
Run Code Online (Sandbox Code Playgroud)
当您在parted没有命令的情况下运行时,例如:
parted /tmp/parted
Run Code Online (Sandbox Code Playgroud)
您将看到一个简单的 shell,您可以在其中运行上述命令。但是,这些命令也可以直接使用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
Run Code Online (Sandbox Code Playgroud)
和
# 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
Run Code Online (Sandbox Code Playgroud)
和
parted /tmp/parted mklabel gpt
Run Code Online (Sandbox Code Playgroud)
另请注意,当使用 来创建分区时parted,一个有用的分区结束指示符是-1s(连字符和“s”之间的“1”)。如果您希望分区从指定的开始跨越到磁盘的其余部分,这将非常有用。更具体地说,运行
parted /dev/sda -- mkpart primary ext4 3G -1s
Run Code Online (Sandbox Code Playgroud)
将创建一个/dev/sda从 3G 开始并在/dev/sda磁盘的最后一个扇区结束的分区(即它从 3G 跨越到磁盘的整个剩余部分)。请注意,--是必要的,1s以免被解释为无效选项。
kyo*_*ake 12
首先,您将了解如何使用 fdisk 实用程序对硬盘进行实际分区。
Linux 只允许 4 个主分区。通过细分主分区之一,您可以拥有更多的逻辑分区。
只能细分一个主分区。
fdisk 是通过在命令提示符下键入 root fdisk device 来启动的。
设备可能类似于 /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
Run Code Online (Sandbox Code Playgroud)
在您发出 write (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
Run Code Online (Sandbox Code Playgroud)
例子:
从 shell 提示符启动 fdisk:
sudo su
fdisk /dev/sdb
Run Code Online (Sandbox Code Playgroud)
这表明您正在使用 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
Run Code Online (Sandbox Code Playgroud)
现在分区表如下所示:
Device Boot Start End Blocks Id System
/dev/sdb1 1 196 395104 83 Linux
/dev/sdb2 197 262 133056 83 Linux
Run Code Online (Sandbox Code Playgroud)
最后,您使第一个分区可启动:
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
Run Code Online (Sandbox Code Playgroud)
最终结果:
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
Run Code Online (Sandbox Code Playgroud)
最后,您发出写入命令 (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
Run Code Online (Sandbox Code Playgroud)
在特定设备上构建文件系统
为了使用 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
Run Code Online (Sandbox Code Playgroud)
请注意 mkfs 命令的默认文件系统类型是 ext2。
如果不指定“-t”选项,它将创建 ext2 文件系统。
此外,您可以使用我们之前讨论的方法来识别您是否拥有 ext2 或 ext3 或 ext4 文件系统。
格式化 NTFS 驱动器
首先,您需要能够创建 NTFS 文件系统,因此安装 ntfsprogs:
sudo su
apt-get install ntfs-3g
Run Code Online (Sandbox Code Playgroud)
其次,您删除分区并将其重新创建为 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
Run Code Online (Sandbox Code Playgroud)
在最后一步中,您卸载了分区,因为 Ubuntu 再次为您自动挂载了它。
现在,您需要创建文件系统。有两种方法可以解决这个问题:不耐烦的方式(快速格式化),或者更好但更长的方式(完整格式化)。
快速格式化
这只是分配磁盘空间,但不会将驱动器清零或检查坏扇区。这意味着它需要几秒钟。
sudo su
mkfs.ntfs -f /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
完整格式
如果您更关心数据完整性并且不介意等待,请执行完整格式。
这可能需要几个小时才能将大型驱动器归零!
sudo su
mkfs.ntfs /dev/sdb1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
563125 次 |
| 最近记录: |