Linux sfdisk 命令

NAME

        sfdisk - partition table manipulator for Linux

SYNOPSIS(sfdisk用法格式简要)
       sfdisk [options] device
       sfdisk -s [partition]

DESCRIPTION

       sfdisk  has  four  (main)  uses: list the size of a partition, list the partitions on a device, check the partitions on a device, and very dangerous repartition a device. (sfdisk 有四种用法)
       sfdisk doesn't understand the GUID Partition Table (GPT) format and it is not designed for large partitions.  In these cases  use  the  more advanced GNU parted(8).

   sfdisk四种用法:

  List sizes (列举大小)

       sfdisk  -s  partition gives the size of partition in blocks.  This may be useful in connection with programs like mkswap(8).  Here partition is usually something like /dev/hda1 or /dev/sdb12, but may also be an entire disk, like /dev/xda.
               % sfdisk -s /dev/hda9
              81599
       If the partition argument is omitted, sfdisk will list the sizes of all disks, and the total:
               % sfdisk -s
              /dev/hda: 208896
              /dev/hdb: 1025136
              /dev/hdc: 1031063
              /dev/sda: 8877895
              /dev/sdb: 1758927
              total: 12901917 blocks

   List partitions(列举分区)

       The second type of invocation: sfdisk -l device will list the partitions on the specified device.  If the device argument  is  omitted,  the  partitions on all hard disks are listed.
               % sfdisk -l /dev/hdc
              Disk /dev/hdc: 16 heads, 63 sectors, 2045 cylinders
              Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
                 Device Boot Start     End   #cyls   #blocks   Id  System
              /dev/hdc1          0+    406     407-   205096+  83  Linux native
              /dev/hdc2        407     813     407    205128   83  Linux native
              /dev/hdc3        814    2044    1231    620424   83  Linux native
              /dev/hdc4          0       -       0         0    0  Empty
       The  trailing  -  and  + signs indicate that rounding has taken place, and that the actual value is slightly less or more.  To see the exact values, ask for a listing with sectors as unit (-u S) .

数据尾部带上(- 和 + )表示取舍后的值比实际值略小后略大, 为查看实际值可以用下面命令

            wiwa@tech:~$ sudo sfdisk -l -u S /dev/sdb


   Check partitions(检查分区)

       The third type of invocation: sfdisk -V device will apply various consistency checks to the partition tables on device.  It prints  `OK'  or   complains.  The -V option can be used together with -l.  In a shell script one might use sfdisk -V -q device which only returns a status.

wiwa@tech:~$ sudo sfdisk -V /dev/sdb


   Create partitions(创建分区)

       The  fourth  type of invocation: sfdisk device will cause sfdisk to read the specification for the desired partitioning of device from standard input, and then to change the partition tables on that disk.  Thus it is possible to use sfdisk  from  a  shell  script.   When  sfdisk determines that its standard input is a terminal, it will be conversational; otherwise it will abort on any error.
       BE EXTREMELY CAREFUL - ONE TYPING MISTAKE AND ALL YOUR DATA IS LOST
       As a precaution, one can save the sectors changed by sfdisk:
              % sfdisk /dev/hdd -O hdd-partition-sectors.save
              ...
       Then,  if  you  discover  that you did something stupid before anything else has been written to disk, it may be possible to recover the old situation with:
              % sfdisk /dev/hdd -I hdd-partition-sectors.save
       (This is not the same as saving the old partition table: a readable version of the old partition table can be saved  using  the  -d  option. However, if you create logical partitions, the sectors describing them are located somewhere on disk, possibly on sectors that were not part  of the partition table before.  Thus, the information the -O option saves is not a binary version of the output of -d.)
       There are many options.

OPTIONS(sfdisk命令选项)

       -v, --version
              Print version number of sfdisk and exit immediately.

       -h, --help
              Print a usage message and exit immediately.

       -T, --list-types
              Print the recognized types (system Id's).

       -s, --show-size
              List the size of a partition.

       -g, --show-geometry
              List the kernel's idea of the geometry of the indicated disk(s).

      
-G, --show-pt-geometry
              List the geometry of the indicated disks guessed by looking at the partition table.

       -l, --list
              List the partitions of a device.

       -d, --dump
              Dump the partitions of a device in a format that is usable as input to /fBsfdisk/fR.  For example,
                   % sfdisk -d /dev/hda > hda.out
                  % sfdisk /dev/hda < hda.out

              will correct the bad last extended partition that the OS/2 fdisk creates.

       -V, --verify
              Test whether partitions seem correct.  (See the third invocation type above.)

       -i, --increment
              Number cylinders etc. starting from 1 instead of 0.

       -N number
              Change only the single partition indicated.  For example:
                  % sfdisk /dev/hdb -N5
                  ,,,*
              will make the fifth partition on /dev/hdb bootable (`active') and change nothing else.  (Probably  this  fifth  partition  is  called /dev/hdb5, but you are free to call it something else, like `/my_equipment/disks/2/5' or so).

       -A, --activate number
              Make the indicated partition(s) active, and all others inactive.

        -c, --id number [Id]
              If  no  Id  argument given: print the partition Id of the indicated partition.  If an Id argument is present: change the type (Id) of the indicated partition to the given value.  This option has two longer forms, --print-id and --change-id.  For example:
                  % sfdisk --print-id /dev/hdb 5
                  6
                  % sfdisk --change-id /dev/hdb 5 83
                  OK
              first reports that /dev/hdb5 has Id 6, and then changes that into 83.

       -u, --unit letter
              Interpret the input and show the output in the units specified by letter.  This letter can be one of S, C, B or M,  meaning  Sectors,  Cylinders, Blocks and Megabytes, respectively.  The default is cylinders, at least when the geometry is known.


       -x, --show-extended
              Also list non-primary extended partitions on output, and expect descriptors for them on input.

       -C, --cylinders cylinders
              Specify the number of cylinders, possibly overriding what the kernel thinks.

       -H, --heads heads
              Specify the number of heads, possibly overriding what the kernel thinks.

       -S, --sectors sectors
              Specify the number of sectors, possibly overriding what the kernel thinks.

       -f, --force
              Do what I say, even if it is stupid.

       -q, --quiet
              Suppress warning messages.

       -L, --Linux
              Do not complain about things irrelevant for Linux
.

       -D, --DOS
              For  DOS-compatibility:  waste a little space.  (More precisely: if a partition cannot contain sector 0, e.g. because that is the MBR of the device, or contains the partition table of an extended partition, then sfdisk would make it start the next  sector.   However, when this option is given it skips to the start of the next track, wasting for example 33 sectors (in case of 34 sectors/track), just like certain versions of DOS do.)  Certain Disk Managers and boot loaders (such as OSBS, but not LILO or the OS/2 Boot Manager)  also live in this empty space, so maybe you want this option if you use one.

       -E, --DOS-extended
              Take  the  starting  sector  numbers of "inner" extended partitions to be relative to the starting cylinder boundary of the outer one (like some versions of DOS do), rather than relative to the actual starting sector (like Linux does).  (The fact that there is a difference  here means that one should always let extended partitions start at cylinder boundaries if DOS and Linux should interpret the partition table in the same way.  Of course one can only know where cylinder boundaries are when one knows what geometry DOS will use for this disk.)

       --IBM, --leave-last
              Certain  IBM  diagnostic  programs  assume that they can use the last cylinder on a disk for disk-testing purposes.  If you think you might ever run such programs, use this option to tell sfdisk that it should not allocate  the  last  cylinder.   Sometimes  the  last  cylinder contains a bad sector table.

       -n     Go through all the motions, but do not actually write to disk.

       -R, --re-read
              Only  execute  the BLKRRPART ioctl (to make the kernel re-read the partition table).  This can be useful for checking in advance that the final BLKRRPART will be successful, and also when you changed the partition table `by hand' (e.g., using dd from a  backup).   If the kernel complains (`device busy for revalidation (usage = 2)') then something still uses the device, and you still have to unmount some file system, or say swapoff to some swap partition.

       --no-reread
              When starting a repartitioning of a disk, sfdisk checks that this disk is not mounted, or in use as a swap  device,  and  refuses  to continue if it is.  This option suppresses the test.  (On the other hand, the -f option would force sfdisk to continue even when this test fails.)

       --in-order
              Caution, see warning section.  To be documented.

       --not-in-order
              Caution, see warning section.  To be documented.

       --inside-outer
              Caution, see warning section.  Chaining order.

       --not-inside-outer
              Caution, see warning section.  Chaining order.

       --nested
              Caution, see warning section.  Every partition is contained in the surrounding partitions and is disjoint from all others.

       --chained
              Caution, see warning section.  Every data partition is contained in the surrounding partitions and  disjoint  from  all  others,  but extended partitions may lie outside (insofar as allowed by all_logicals_inside_outermost_extended).

       --onesector
              Caution,  see  warning section.  All data partitions are mutually disjoint; extended partitions each use one sector only (except perhaps for the outermost one).

       -O file
              Just before writing the new partition, output the sectors that are going to be overwritten to file (where hopefully file  resides on another disk, or on a floppy).

       -I file
              After  destroying  your filesystems with an unfortunate sfdisk command, you would have been able to restore the old situation if only  you had preserved it using the -O flag.

THEORY(disk分区理论)

       Block 0 of a disk (the Master Boot Record) contains among other things four partition descriptors. The partitions described here are  called primary partitions.

       A partition descriptor has 6 fields:
              struct partition {
                  unsigned char bootable;        /* 0 or 0x80 , 1btye */

                  hsc begin_hsc;                         /* 3 bytes  hsc 即 head sector cylinder*/

                  unsigned char id;                     /* 1byte*/
                  hsc end_hsc;                            /* 3bytes*/
                  unsigned int starting_sector;  /* 4bytes*/
                  unsigned int nr_of_sectors;    /* 4btyes*/

              }

下面是linux分区结构体的声明格式:

 68 struct partition {
 69         unsigned char boot_ind;         /* 0x80 - active */
 70         unsigned char head;             /* starting head */
 71         unsigned char sector;           /* starting sector */
 72         unsigned char cyl;              /* starting cylinder */
 73         unsigned char sys_ind;          /* What partition type */
 74         unsigned char end_head;         /* end head */
 75         unsigned char end_sector;       /* end sector */
 76         unsigned char end_cyl;          /* end cylinder */
 77         __le32 start_sect;      /* starting sector counting from 0 */
 78         __le32 nr_sects;                /* nr of sectors in partition */
 79 } __attribute__((packed));

       The  two  hsc  fields indicate head, sector and cylinder of the begin and the end of the partition. Since each hsc field only takes 3 bytes,  only 24 bits are available, which does not suffice for big disks (say > 8GB). In fact, due to the wasteful representation (that uses a  byte for  the  number of heads, which is typically 16), problems already start with 0.5GB.  However Linux does not use these fields, and problems   can arise only at boot time, before Linux has been started. For more details, see the lilo documentation.
       Each partition has a type, its `Id', and if this type is 5 or f (`extended partition') the starting sector of the partition again contains 4  partition  descriptors. MSDOS only uses the first two of these: the first one an actual data partition, and the second one again an extended partition (or empty).  In this way one gets a chain of extended partitions.  Other operating systems have  slightly  different  conventions.  Linux  also accepts type 85 as equivalent to 5 and f - this can be useful if one wants to have extended partitions under Linux past the 1024 cylinder boundary, without DOS FDISK hanging.  (If there is no good reason, you should just use 5, which is understood by other systems.)
       Partitions that are not primary or extended are called logical.  Often, one cannot boot from logical  partitions  (because  the  process  of finding  them  is more involved than just looking at the MBR).  Note that of an extended partition only the Id and the start are used. There  are various conventions about what to write in the other fields. One should not try to use extended partitions for data storage or swap.

INPUT FORMAT(sfdisk输入格式)

       sfdisk reads lines of the form
             
       where each line fills one partition descriptor.
       Fields are separated by whitespace, or comma or semicolon possibly followed by whitespace; initial and trailing whitespace is ignored.  Numbers can be octal, decimal or hexadecimal, decimal is default.  When a field is absent or empty, a default value is used.
       The  parts can (and probably should) be omitted - sfdisk computes them from and and the disk geometry as given by the kernel or specified using the -H, -S, -C flags. (c:cylinder柱面, h: header磁头, s:sector 扇区)
       Bootable is specified as [*|-], with as default not-bootable.  (The value of this field is irrelevant for Linux - when  Linux  runs  it  has been  booted  already - but might play a role for certain boot loaders and for other operating systems.  For example, when there are several  primary DOS partitions, DOS assigns C: to the first among these that is bootable.)

       Id is given in hex, without the 0x prefix, or is [E|S|L|X], where L (LINUX_NATIVE  (83))  is  the  default,  S  is  LINUX_SWAP  (82),  E  is  EXTENDED_PARTITION (5), and X is LINUX_EXTENDED (85).

id 块的值有[ E|S|L|X] 四种格式, E:(EXTENDED _PARTITION)扩展分区, S(LINUX_SWAP)linux转化分区, L(LINUX_NATIVE)linux 原始分区,


       The default value of start is the first nonassigned sector/cylinder/...

       The default value of size is as much as possible (until next partition or end-of-disk).

       However, for the four partitions inside an extended partition, the defaults are: Linux partition, Extended partition, Empty, Empty.

       But when the -N option (change a single partition only) is given, the default for each field is its previous value.

       A '+' can be specified instead of a number for size, which means as much as possible. This is useful with the -N option .

EXAMPLE(sfdisk 实例说明)

       The command
              sfdisk /dev/hdc << EOF
              0,407      /*开始为0cylinders,大小为407cylinders*/
              ,407
              ;
              ;
              EOF

       will partition /dev/hdc just as indicated above.

       The command
               sfdisk /dev/hdb << EOF      /* end of file (^D) 此指令没有声明单位,默认以cylinders单位分配大小 */
              ,3,L
              ,60,L
              ,19,S
              ,,E
              ,130,L
              ,130,L
              ,130,L
              ,,L
              EOF

       will  partition  /dev/hdb  into two Linux partitions of 3 and 60 cylinders, a swap space of 19 cylinders, and an extended partition covering the rest. Inside the extended partition there are four Linux logical partitions, three of 130 cylinders and one covering the rest.
       With the -x option, the number of input lines must be a multiple of 4: you have to list the two empty partitions that you never  want  using two  blank  lines.  Without  the -x option, you give one line for the partitions inside a extended partition, instead of four, and terminate with end-of-file (^D).  (And sfdisk will assume that your input line represents the first of four, that the second one is extended, and  the  3rd and 4th are empty.)

    1、手头有个8G的U盘, U盘的设备号为/dev/sdb,想对其分区为: 第一分区200MB且作为启动盘, 第二分区5GB,剩余容量作为第三分区,可以这样输入:

wiwa@tech:~$ sudo sfdisk --in-order --Linux --unit M /dev/sdb << EOF
,200,L,*
,5120,L
,,L
EOF

wiwa@tech:~$ lsblk /dev/sdb
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb      8:16   1   7.4G  0 disk 
├─sdb1   8:17   1   200M  0 part /media/wiwa/sdb1
├─sdb2   8:18   1     5G  0 part 
└─sdb3   8:19   1   2.2G  0 part 

wiwa@tech:~$ sudo fdisk -l  /dev/sdb

Disk /dev/sdb: 7948 MB, 7948206080 bytes
245 heads, 62 sectors/track, 1021 cylinders, total 15523840 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1      409599      204799+  83  Linux
/dev/sdb2          409600    10895359     5242880   83  Linux
/dev/sdb3        10895360    15523839     2314240   83  Linux

wiwa@tech:~$ sudo sfdisk -l  /dev/sdb

Disk /dev/sdb: 1021 cylinders, 245 heads, 62 sectors/track
Units = cylinders of 7777280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1   *      0+     26-     27-    204799+  83  Linux
/dev/sdb2         26+    717-    691-   5242880   83  Linux
/dev/sdb3        717+   1021-    305-   2314240   83  Linux
/dev/sdb4          0       -       0          0    0  Empty

wiwa@tech:~$ sudo sfdisk -l -u S /dev/sdb

Disk /dev/sdb: 1021 cylinders, 245 heads, 62 sectors/track
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *         1    409599     409599  83  Linux
/dev/sdb2        409600  10895359   10485760  83  Linux
/dev/sdb3      10895360  15523839    4628480  83  Linux
/dev/sdb4             0         -          0   0  Empty

wiwa@tech:~$ sudo sfdisk -V /dev/sdb
Warning: partition 1 does not end at a cylinder boundary
Warning: partition 2 does not start at a cylinder boundary
Warning: partition 2 does not end at a cylinder boundary
Warning: partition 3 does not start at a cylinder boundary
Warning: partition 3 does not end at a cylinder boundary
end of partition 3 has impossible value for cylinders: 1021 (should be in 0-1020)

 

    2、手头有个8G的U盘, 想对其分区为: 第一分区200MB且不作为启动盘, 第二分区5GB,剩余容量作为第三分区,可以这样输入:

wiwa@tech:~$ sudo sfdisk --in-order --Linux --unit M /dev/sdb << EOF
> ,200,L
> ,5120,L
> ,,L
> EOF

wiwa@tech:~$ sudo fdisk -l  /dev/sdb

Disk /dev/sdb: 7948 MB, 7948206080 bytes
245 heads, 62 sectors/track, 1021 cylinders, total 15523840 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      409599      204799+  83  Linux   /*此处”*"号不见了*/
/dev/sdb2          409600    10895359     5242880   83  Linux
/dev/sdb3        10895360    15523839     2314240   83  Linux

wiwa@tech:~$ sudo sfdisk -V /dev/sdb
Warning: partition 1 does not end at a cylinder boundary
Warning: partition 2 does not start at a cylinder boundary
Warning: partition 2 does not end at a cylinder boundary
Warning: partition 3 does not start at a cylinder boundary
Warning: partition 3 does not end at a cylinder boundary
Warning: no primary partition is marked bootable (active)
This does not matter for LILO, but the DOS MBR will not boot this disk.
end of partition 3 has impossible value for cylinders: 1021 (should be in 0-1020)

  3、手头有个8G的U盘, 想对其分区为: 第一分区200MB且作为启动盘(启动扇区从1MB处开始), 第二分区5GB,剩余容量作为第三分区,可以这样输入:

wiwa@tech:~$ sudo sfdisk --in-order --Linux --unit M /dev/sdb << EOF
1,200,L,*
,5120,L
,,L
EOF

 wiwa@tech:~$ sudo sfdisk -l -u S /dev/sdb

Disk /dev/sdb: 1021 cylinders, 245 heads, 62 sectors/track
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *      2048    411647     409600  83  Linux
/dev/sdb2        411648  10897407   10485760  83  Linux
/dev/sdb3      10897408  15523839    4626432  83  Linux
/dev/sdb4             0         -          0   0  Empty

wiwa@tech:~$ sudo sfdisk -l  /dev/sdb

Disk /dev/sdb: 1021 cylinders, 245 heads, 62 sectors/track
Units = cylinders of 7777280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1   *      0+     27-     27-    204800   83  Linux
/dev/sdb2         27+    717-    691-   5242880   83  Linux
/dev/sdb3        717+   1021-    305-   2313216   83  Linux
/dev/sdb4          0       -       0          0    0  Empty


AVAILABILITY(可获取出处)     

The sfdisk command is part of the util-linux package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/.*

备注:

本文的出处直接$ man sfdisk;

创建于20160203,wiwa

你可能感兴趣的:(Linux,Commands,Linux,Problems,Gallery)