格式化存储驱动器的终端方法

我想知道如何从终端格式化存储驱动器。 在答案中提供的有用的东西通常用于命令和基础知识的选项,可以用来推断未来的用途。 具体来说,我想知道如何在不同的文件系统中格式化,如NTFS,FAT32,EXT4等。还需要有关如何通过终端分区驱动器的信息。

我试图从终端格式化高容量外部硬盘驱动器(EHDD)到NTFS。

我知道我可以使用gparted以及其他GUI程序,但我现在仍想要从终端如何做到这一点。

有几个选项可供选择:

  1. fdisk (较旧,不支持GPT 4 )。
  2. parted (GParted的CLI兄弟)。
  3. 各种mkfs程序,如果您已经有分区并希望格式化。

fdiskparted是交互式的,并有帮助命令,因此您可以随时在程序中寻求帮助。 两者都是可编写脚本的。 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 

命令可以缩减为唯一的前缀(例如, hhelp简称)。

我将使用我创建的临时文件( /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". 

确实,parted会告诉你应该用它来操作分区, 而不是文件系统 ,这会让我:


mkfs

fsck一样, mkfs本质上是各种特定于文件系统的命令的前端。 在我的系统上,例如, mkfs.bfsmkfs.cramfsmkfs.ext2mkfs.ext3mkfs.ext4mkfs.ext4devmkfs.fatmkfs.minixmkfs.msdosmkfs.ntfsmkfs.vfat是可用。

现在,不幸的是,虽然parted在一个文件上运行得很好,就像我上面使用的那样, mkfs无法在这些文件中寻找分区。 实际上,它需要块设备,所以如果我要为mkfs使用新的文件/tmp/file ,我必须强制它这样做。 您将使用与要格式化的分区对应的块设备,例如/dev/sda2mkfs的一般语法是:

 # 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: ntfs-3g-devel@lists.sf.net 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 。 由于我操作文件和我拥有的文件,我不需要sudoparted会警告你这件事。 对于通常始终由root拥有的块设备,您将需要sudo (或者您必须通过sudo -isudo su -使用root shell)。
  2. parted是一个GNU程序,和许多GNU程序一样,在info格式中有大量文档。 安装parted-docsudo apt-get install parted-doc )然后运行info parted 。 您也可以查看在线用户手册 。
  3. GParted能够将分区格式化为NTFS,因为它直接调用适当的mkfs程序( mkntfs ,在这种情况下 – mkfs.ntfs只是mkfs.ntfs的链接)。 它还设置了许多参数。 实际上,对于大多数操作,您可以检查GParted消息的详细信息以查看运行的命令。
  4. 我不会介绍GPT与MBR / MSDOS分区表的优点,但可能会在具有UEFI的新设备上找到GPT,特别是如果你有它们的Windows 8。 分区工具的状态? 讨论了面对GPT时可用的工具。
  5. LVM,ZFS和btrfs是另一个游戏。 它们都有它们附带的工具,你应该使用它们而不是partedfdisk (除了为它们的使用创建分区的初始步骤)。

关于parted使用的注意事项:

parted程序的语法是:

 parted [options] [device [command [options...]...]] 

当您在没有命令的情况下运行parted ,例如:

 parted /tmp/parted 

您将看到一个简单的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 

 # 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 device启动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 

在发出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 

例:

从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): 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): 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 

最后,发出write命令(w)以在磁盘上写入表。


mkfs实用程序用于在Linux系统上创建文件系统(ext2,ext3,ext4等)。

您应该将设备名称指定为要在其上创建文件系统的mkfs。

查看可用的Filesystem Builder命令

文件系统构建器(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命令构建文件系统,所需的参数是device-filename和filesystem-type,如下所示。

以下示例在/ 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