linux command

fdisk

Linux Command – fdisk ใช้ในการบริหารจัดการ disk partition

 

คำสั่ง

1. แสดงข้อมูล disk partition ของ linux

$ fdisk -l
Disk /dev/sda: 74.5 GiB, 80000000000 bytes, 156250000 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
Disklabel type: dos
Disk identifier: 0x6e0c3dab

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 152344575 152342528 72.7G 83 Linux
/dev/sda2 * 152344576 152735743 391168 191M 83 Linux
/dev/sda3 152735744 156248063 3512320 1.7G 82 Linux swap / Solaris

 

2. แสดงข้อมูล partition ที่ระบุ

$ fdisk -l /dev/sda1
Disk /dev/sda1: 72.7 GiB, 77999374336 bytes, 152342528 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

 

3. แสดง command ภายใน fdisk (m)

$ fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): m
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)
Command (m for help):

 

4. แสดงข้อมูล disk partition ภายใน (p)

$ fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2624 20972857+ 83 Linux
/dev/sda3 2625 4582 15727635 83 Linux
/dev/sda4 4583 77541 586043167+ 5 Extended
/dev/sda5 4583 5887 10482381 83 Linux
/dev/sda6 5888 7192 10482381 83 Linux
/dev/sda7 7193 7845 5245191 83 Linux
/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris
/dev/sda9 8368 77541 555640123+ 8e Linux LVM
Command (m for help):

 

5. ลบ partition (d)

$ fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): d
Partition number (1-4): 4
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
You have new mail in /var/spool/mail/root

 

6. สร้าง partition (n)

$ fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e

 

7. แสดงขนาดของ partition

$ fdisk -s /dev/sda2
5194304

 

โครงสร้างคำสั่ง

 fdisk [options] device
 fdisk -l [device...]

 

รายละเอียด

เป็นคำสั่งที่ใช้ในการบริหารจัดการ disk partition เนื่องจากเป็นคำสั่งที่อันตราย และเกี่ยวข้องกับ file system จึงจำเป็นต้องมีสิทธิ์ระดับ root หรือ super user ในการใช้งาน command fdisk

 

Option

 -b, --sector-size sectorsize
 Specify the sector size of the disk. Valid values are 512, 1024, 2048, and 4096. (Recent kernels know the sector size. Use this option only on old kernels or to override
 the kernel's ideas.) Since util-linux-2.17, fdisk differentiates between logical and physical sector size. This option changes both sector sizes to sectorsize.

 -B, --protect-boot
 Don't erase the begin of the first disk sector when create a new disk label. This feature is supported for GPT and MBR.

 -c, --compatibility[=mode]
 Specify the compatibility mode, 'dos' or 'nondos'. The default is non-DOS mode. For backward compatibility, it is possible to use the option without the mode argument --
 then the default is used. Note that the optional mode argument cannot be separated from the -c option by a space, the correct form is for example '-c=dos'.

 -h, --help
 Display a help text and exit.

 -L, --color[=when]
 Colorize the output. The optional argument when can be auto, never or always. If the when argument is omitted, it defaults to auto. The colors can be disabled, for the
 current built-in default see --help output. See also the COLORS section.

 -l, --list
 List the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions (if that file exists) are used.

 -o, --output list
 Specify which output columns to print. Use --help to get a list of all supported columns.

 The default list of columns may be extended if list is specified in the format +list (e.g. -o +UUID).

 -s, --getsz
 Print the size in 512-byte sectors of each given block device. This option is DEPRECATED in favour of blockdev(1).

 -t, --type type
 Enable support only for disklabels of the specified type, and disable support for all other types.

 -u, --units[=unit]
 When listing partition tables, show sizes in 'sectors' or in 'cylinders'. The default is to show sizes in sectors. For backward compatibility, it is possible to use the
 option without the unit argument -- then the default is used. Note that the optional unit argument cannot be separated from the -u option by a space, the correct form is
 for example '-u=cylinders'.

 -C, --cylinders number
 Specify the number of cylinders of the disk. I have no idea why anybody would want to do so.

 -H, --heads number
 Specify the number of heads of the disk. (Not the physical number, of course, but the number used for partition tables.) Reasonable values are 255 and 16.

 -S, --sectors number
 Specify the number of sectors per track of the disk. (Not the physical number, of course, but the number used for partition tables.) A reasonable value is 63.

 -V, --version
 Display version information and exit.

 

กลุ่มคำสั่ง

cfdisk(8), sfdisk(8), mkfs(8), partx(8)

 

Reference:

คำสั่ง Unix – Linux Command

Linux, Unix

 

Author: Suphakit Annoppornchai

Credit: https://saixiii.com

Leave a Reply