OSX: diskutil command-convert to free space and then partition it

Source: Internet
Author: User
Tags float number

Disclaimer: the operations involved in this article are likely to damage your system files and cause data loss. Please imitate them carefully. The author shall not be liable for any consequences.


Purpose:

Although you are familiar with the diskutil command and Its GUI front-end program Disk utility, it is easier to use the disk tool on the front-end because you sometimes need to operate on disk partitions, and it is not prone to errors. But sometimes it needs to be processed remotely or through SSH, especially when multiple machines are simultaneously operated, it cannot seem as stupid as it is. Every company, even every office, is always so difficult to deal with. The old man asked for help. By the way, I would like to give you the administrator privilege, which I often do at home. He doesn't think he is awesome. You will, too, look down on us. If you don't want him to take the lead, let him get the handle! Besides, it can only be done through the GUI. I can't expect a young man to be either. When I was young, no one had ever played a computer. Therefore, only by mastering the skills that are not known by ordinary people can he take the services without knowing how to do it. No more. If you try it twice, he won't blow it up, so you can hold your feet.

That is all digress, and that is not the motivation for us to play with machines. This is to use the command line diskutil to complete two tasks:

First, convert a useless partition into idle space. In the disk tool, click "-" to delete a partition.

Second, allocate a free space on the disk to a partition. In the disk tool, the node + is used to generate a partition.

In addition, the principle is that data in other partitions and the data in the partition cannot be damaged.


Overview:

To use the diskutil command, let's see what it can do. The functions listed below are only useful and relevant to this article.

Features supported by the diskutil command:

Disk operations:

    • Erasedisk: erased the entire disk.
    • Partitiondisk: partition a disk.

Operations on partitions:

    • Erasevolume: deletes a partition/volume.
    • Reformat: Reformat a partition.
    • Resizevolume: changes the size of a partition.
    • Splitpartition: divides a partition into multiple small partitions.
    • Mergepartitions: Merge multiple partitions
You see, there is no function that can directly complete the above two tasks.


Common sense:

Sort out his basic knowledge concepts.

1. The partition types supported by the OS X system can be listed using commands:

$ diskutil listFilesystemsFormattable file systemsThese file system personalities can be used for erasing and partitioning.When specifying a personality as a parameter to a verb, case is not considered.Certain common aliases (also case-insensitive) are listed below as well.-------------------------------------------------------------------------------PERSONALITY                     USER VISIBLE NAME                               -------------------------------------------------------------------------------ExFAT                           ExFAT                                           Free Space                      Free Space                                        (or) freeMS-DOS                          MS-DOS (FAT)                                    MS-DOS FAT12                    MS-DOS (FAT12)                                  MS-DOS FAT16                    MS-DOS (FAT16)                                  MS-DOS FAT32                    MS-DOS (FAT32)                                    (or) fat32HFS+                            Mac OS Extended                                 Case-sensitive HFS+             Mac OS Extended (Case-sensitive)                  (or) hfsxCase-sensitive Journaled HFS+   Mac OS Extended (Case-sensitive, Journaled)       (or) jhfsxJournaled HFS+                  Mac OS Extended (Journaled)                       (or) jhfs+
It says that personalized names are case-insensitive.
The usage is also simple and clear, such as jhfs +, jhfs +, or "journaled HFS +. You can also use names that can be recognized by people, such as % applie_hfs %, but these names must be recognizable by the system. It is still unclear about the specific information.

Another way to use UUID is to specify the uuid of the partition type. This UUID can be defined by GPT or arbitrary. This method is more flexible.

Only Apple HFS partitions support lossless change of partition size, so jhfs + is used later.


2. The three parameters that describe the partition: Format (Format) name (name) size (size ). They are calledPartition triplicate(Triplet ).

Each part of the triplicate is separated by spaces. Here, the format has been mentioned earlier. The name is obviously a string, which can be described by "". The size can be in the following forms:

  • Floating Point followed by B, K (10 ^ 3), m (10 ^ 6), g (10 ^ 9), T (10 ^ 12 ), P (10 ^ 15), E (10 ^ 18), etc. (unit: byte), used to express the size of space for xxmb/GB/TB
  • Floating Point number followed by S (512 byte block), KI (2 ^ 10), mi (2 ^ 20), GI (2 ^ 30), Ti (2 ^ 40 ), pi (2 ^ 50), EI (2 ^ 60), etc. (unit: byte), used to express the space size of xxmb/GB/TB
  • DBS refers to the block size specified by the device, which is generally 512 bytes. However, it depends on the device, so it is not commonly used.
  • Float number followed by %, used to indicate the percentage of disk space occupied
  • R indicates all the remaining space. In the triplicate method of multiple partitions, it does not have to be the last one, but only one (it need not be in the last triplet. it must only appear in at most one triplet among all triplets .).

3. Common commands:

View the current disk partition: diskutil list

View the details of a partition: diskutil info disk1s2

4. device: it can be in four forms. Each form is applicable to different situations according to different command requirements.

  1. Device identifier: Looks like disk1s3
  2. Device node: It looks like/dev/disk1s3
  3. Volume mount point: the default value is similar to/volumes/my_partition.
  4. UUID (universal unique identifier): Looks like 11111111-2222-3333-4444-555555555555-


Work environment:

Try using any USB flash drive. In this example, a 1 gb usb flash drive is used. create three partitions and one free space. The first is the exfat partition dos of 25% space, followed by the free space of MIB, and then the jhfs + partition a of MB, finally, all the remaining space is allocated by jhfs + partition B.

$ diskutil partitionDisk disk1 GPT exFAT DOS 25% free whatIam 300Mi JHFS+ A 100M JHFS+ B RStarted partitioning on disk1Unmounting diskCreating the partition mapWaiting for the disks to reappearFormatting disk1s1 as ExFAT with name DOSVolume name      : DOSPartition offset : 2048 sectors (1048576 bytes)Volume size      : 507904 sectors (260046848 bytes)Bytes per sector : 512Bytes per cluster: 4096FAT offset       : 128 sectors (65536 bytes)# FAT sectors    : 512Number of FATs   : 1Cluster offset   : 640 sectors (327680 bytes)# Clusters       : 63408Volume Serial #  : 53c9fdc4Bitmap start     : 2Bitmap file size : 7926Upcase start     : 4Upcase file size : 5836Root start       : 6Mounting diskFormatting disk1s2 as Mac OS Extended (Journaled) with name AInitialized /dev/rdisk1s2 as a 95 MB HFS Plus volume with a 512k journalMounting diskFormatting disk1s3 as Mac OS Extended (Journaled) with name BInitialized /dev/rdisk1s3 as a 349 MB HFS Plus volume with a 8192k journalMounting diskFinished partitioning on disk1/dev/disk1   #:                       TYPE NAME                    SIZE       IDENTIFIER   0:      GUID_partition_scheme                        *1.0 GB     disk1   1:       Microsoft Basic Data DOS                     260.0 MB   disk1s1   2:                  Apple_HFS A                       100.0 MB   disk1s2   3:                  Apple_HFS B                       366.0 MB   disk1s3

Try 1:

Can I use resizevolume to change the size of a partition to 0 to make it free space?

Copy a file to partition a and try the following command:

$ diskutil resizeVolume disk1s3 0BStarted partitioning on disk1s3 AVerifying the diskChecking file systemChecking Journaled HFS Plus volumeChecking extents overflow fileChecking catalog fileChecking multi-linked filesChecking catalog hierarchyChecking extended attributes fileChecking volume bitmapChecking volume informationThe volume A appears to be OKResizingFinished partitioning on disk1s3 A/dev/disk1   #:                       TYPE NAME                    SIZE       IDENTIFIER   0:      GUID_partition_scheme                        *1.0 GB     disk1   1:       Microsoft Basic Data DOS                     260.0 MB   disk1s1   2:                  Apple_HFS A                       100.0 MB   disk1s2   3:                  Apple_HFS B                       366.0 MB   disk1s3

As you can see, it does not work at all.

As mentioned above, resizevolume is a non-destructive operation.


Operation 1:

Well, I sold a lawsuit before, but it was very easy to use this command.

$ diskutil eraseVolume free NONE disk1s3Started erase on disk1s2Unmounting diskError: 2: POSIX reports: No such file or directory

Although there seems to be an error, it is because free space cannot be loaded, so there is no problem.




Operation 2:

Now let's take a look at how to assign free space fee to an available shard.

One method is the easiest. If no data is needed on the disk, the simplest and most direct method is to repartition (diskutil partitiondisk.

But what if you want to save the data? After the above analysis, you can see that the free space will not be allocated to a device, that is, there is no UUID, no device descriptor, no node, no load point. If there is nothing, there is no direct method.

However, it is possible to "save the nation" by using resizevolume and then splitpartition. For example, you can allocate the allocation following partition a to partition B as follows:

#resizeVolume limits -> get Current SizenewVol="B"currentSize=`diskutil resizeVolume disk1s2 limits | grep "Current" | awk '{print $3$4}'`currentVol=`diskutil list disk1 |grep "disk1s2" | awk '{print $3}'`#resizeVolume to Rest:diskutil resizeVolume disk1s2 R#splitePartition:diskutil splitPartition disk1s2 2 JHFS+ $currentVol $currentSize JHFS+ $newVol R


GPT partition:

Using the diskutil list to view the disk partition status does not reflect the idle space, so you need to use another command to explain:

$ sudo gpt -r show disk1Password:    start     size  index  contents        0        1         PMBR        1        1         Pri GPT header        2       32         Pri GPT table       34     2014              2048   507904      1  GPT part - EBD0A0A2-B9E5-4433-87C0-68B6B72699C7   509952   612624           1122576   195312      2  GPT part - 48465300-0000-11AA-AA11-00306543ECAC  1317888   714784      3  GPT part - 48465300-0000-11AA-AA11-00306543ECAC  2032672        2           2032674       32         Sec GPT table  2032706        1         Sec GPT header


Operation 3:

What is the cause of this? For example, the idle space in the Front Partition after the DOS partition and before the partition is not easy to handle, because the former DOS partition cannot change the partition size, or other complicated situations, and is even more difficult to handle. With the knowledge of GPT partitioned tables, the real processing method is here.

The simplest method is:

diskutil unmountDisk force disk1sudo gpt add disk1diskutil unmountDisk force disk1sudo diskutil eraseVolume JHFS+ C disk1s4diskutil mountDisk disk1

In more complex cases, you may need to handle the problem as follows:
diskutil unmountDisk force disk1sudo gpt remove -b 1122576 disk1sudo gpt remove -b 1317888 disk1sudo gpt add -b   509952 -s 612624 disk1sudo gpt add -b 1122576 -s 195312 disk1sudo gpt add -b 1317888 -s 714784 disk1diskutil unmountDisk force disk1sudo diskutil eraseVolume JHFS+ C disk1s2diskutil mountDisk disk1
 

Ref:

Man diskutil

The chromium projects: disk format

Secrets of the GPT

Guid Partition Table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.