Linux форматировать диск через терминал

Terminal method of formatting storage drive

I want to know how to format a storage drive from the terminal. Helpful things to provide in the answer would be often used options for commands and base knowledge that one can use to extrapolate future uses. Specifically I would like to know how to format in the different file systems such as NTFS, FAT32, EXT4, etc. Information on how to partition the drive via terminal is also wanted. I am trying to format a high capacity external hard drive (EHDD) into NTFS — from the terminal. I know I can use gparted for this as well as other GUI programs, but I still want to now how to do it from the terminal.

To whoever will anser: make sure to explain the option -m on reserved space — it’s relevant, because it’s couterintuitive.

2 Answers 2

There are a few options available:

  1. fdisk .
  2. parted (the CLI brother of GParted).
  3. The various mkfs programs, if you already have partitions and wish to format.

fdisk and parted are interactive, and have help commands, so you can always look for help within the program. Both are also scriptable. The mkfs commands are not interactive.

fdisk

fdisk expects a device (such as /dev/sda ) as an argument. It has the following commands:

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) 

I don’t use fdisk that much. I’ll just focus on:

parted

parted doesn’t need an argument (it tries to «guess»), but you should always specify the disk. Given the choice, parted is the program you should prefer. It has the following commands:

 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 

The commands can be contracted to a unique prefix (e.g., h is short for help ).

Читайте также:  Linux перемонтировать root в режиме rw

I’m going to use a temporary file ( /tmp/part ) I created to show you the commands, so the sizes will be somewhat small. You should replace that with the device you need ( /dev/sda , for example).

First, if your disk doesn’t have a partition table, we must create one:

parted /tmp/part mklabel gpt 

or mklabel msdos , if you want the old-school 4-primary-partition thing (called MBR or MSDOS partition table). Then we make, say, an ext4 partition starting starting at 3GB (i.e., leaving the initial 3G free) and of size 2GB (i.e., ending at 5GB). parted expects locations in MB for mkpartfs , but we can specify the suffix:

parted /tmp/part mkpart primary ext4 3G 5G 

And another, now an NTFS partition of 1GB:

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 

Note how it uses SI prefixes, whereas GParted steadfastly uses binary prefixes (while dropping the silly i ). I’ll label the partitions:

# 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 

While parted can create partitions of filesystem ntfs just fine, it can’t format an existing partition (!) to 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". 

Indeed, parted will tell you that you should use it for manipulating partitions, not filesystems, which brings me to:

mkfs

mkfs , like fsck , is essentially a frontend to various filesystem-specific commands. On my system for example, mkfs.bfs , mkfs.cramfs , mkfs.ext2 , mkfs.ext3 , mkfs.ext4 , mkfs.ext4dev , mkfs.fat , mkfs.minix , mkfs.msdos , mkfs.ntfs , mkfs.vfat are available.

Now, unfortunately, while parted operates just fine on a file, like the one I used above, mkfs can’t go hunting for partitions in such files. In fact, it expects block devices, so if I’m going to use a new file /tmp/file for mkfs , I have to force it do so. You’ll use the block device corresponding to the partition you want to format, such as /dev/sda2 . The general syntax for mkfs is:

# 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). 

As you can see, the -t flag lets us pass filesystem-specific flags. For example, NTFS flags:

# 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 

So let’s make an NTFS partition, with quick formatting ( -Q ), forcing it to operate on a non-block-device file ( -F ), and setting a label ( -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. 

Clearly it didn’t enjoy working on a file. 🙂 Don’t worry, it should automatically detect most values when working on an actual disk. Even this «file» works fine as a filesystem:

# 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 

(See the weird permissions?)

Читайте также:  Monitor memory usage in linux

Notes:

  1. I haven’t used sudo anywhere in this answer yet. Since I was operating on files, and files owned by me, I didn’t need sudo . parted will warn you about this. For block devices, which are usually always owned by root , you will need sudo (or you’ll have to use a root shell via sudo -i or sudo su — ).
  2. parted is a GNU program, and like many GNU programs, has extensive documentation in the info format. Install parted-doc ( sudo apt-get install parted-doc ) and then run info parted . You can also checkout the online user’s manual.
  3. GParted is able to format a partition to NTFS as it calls the appropriate mkfs program directly ( mkntfs , in this case — mkfs.ntfs is just a link to mkntfs ). It also sets a number of parameters. In fact, for most operations, you can examine the details of the GParted messages to see which commands were run.
  4. I won’t go into the merits of GPT vs MBR/MSDOS partition tables, but GPT is likely to be found on new devices with UEFI, especially if you got Windows 8 on them. The state of partitioning tools? discusses what tools are available if you’re facing GPT.
  5. LVM, ZFS and btrfs are a whole another game. They all have their accompanying tools, and you should use them instead of parted or fdisk (except perhaps for an initial step of creating partitions for their use).

Note on parted usage:

The syntax of the parted program is:

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

When you run parted without a command, like:

Читайте также:  Линуксе флешку с виндоус

You’ll be presented a simple shell, where you can run the above commands. However, these commands can also be run directly using the parted program. So these three are equivalent:

# 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 

Note also that, when creating partitions with parted , a useful indicator of end of partitions is -1s (this is «1» between the hyphen and the «s»). This is useful if you want your partition to span from a specified start to the rest of the disk. To be more specific, running

parted /dev/sda -- mkpart primary ext4 3G -1s 

will create a partition of /dev/sda that starts at 3G and ends at the last sector of the /dev/sda disk (i.e. it spans from 3G to the whole remainder of the disk). Note that the — is necessary, for 1s not to be interpreted as an invalid option.

Источник

Оцените статью
Adblock
detector