linux下磁盘分区方法详解

[root@docker ~]# fdisk -l      #找出机器中所有硬盘个数及设备名称

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris

**fdisk -l 信息解析**(注意:Blocks的单位是byte)
1、Disk /dev/sda: 107.4 GB, 107374182400 bytes
块设备名称为/dev/sda,此设备的大小为107.4GB,107374182400 bytes这是转换成字节后的大小,即:107374182400/1024/1024/1024=100GB (注:bytes=B,表示“字节”,bit=b,表示“位”,所以1MB=1024KB,1KB=1024bytes)

2255 heads, 63 sectors/track, 13054 cylinders
255 heads:表示磁头数为255
63 sectors/track:表示每磁道上有63个扇区
13054 cylinders:表示共有13054个柱面,柱面是分区的最小单位

3、Units = cylinders of 16065 * 512 = 8225280 bytes
16065=255*63 因为每一个磁头都是在同一个柱面的,63表示每个磁道上的扇区数量,这两个数的乘积表示一个柱面上的扇区数量;所以16065*512表示一个柱面的大小是8225280字节

4、Sector size (logical/physical): 512 bytes / 512 bytes
表示一个扇区的大小是512字节

总结:所以一个磁盘大小=一个柱面大小*柱面的总数=磁头数量*每个磁道上的扇区数*一个扇区大小*柱面总数
磁盘大小=8225280*13054=107374182400bytes=100GB=255*63*512*13054

注意:根据如上信息可知,/dev/sda硬盘还有40G剩余空间

一、磁盘分区:(SCSI硬盘最多能分15个分区,IDE硬盘最多能划分63个分区)
主分区+扩展分区最多只能划分四个,想要更多的分区的话,只能在扩展分区上划分逻辑分区

[root@docker ~]# 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)
p
Partition number (1-4): 3
First cylinder (7650-13054, default 7650): 
Using default value 7650
Last cylinder, +cylinders or +size{K,M,G} (7650-13054, default 13054): +1024M

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris
/dev/sda3            7650        7781     1059858+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (7782-13054, default 7782): 
Using default value 7782
Last cylinder, +cylinders or +size{K,M,G} (7782-13054, default 13054): 
Using default value 13054

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris
/dev/sda3            7650        7781     1059858+  83  Linux
/dev/sda4            7782       13054    42355372+   5  Extended

Command (m for help): n
First cylinder (7782-13054, default 7782): 
Using default value 7782
Last cylinder, +cylinders or +size{K,M,G} (7782-13054, default 13054): +10G      

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris
/dev/sda3            7650        7781     1059858+  83  Linux
/dev/sda4            7782       13054    42355372+   5  Extended
/dev/sda5            7782        9087    10490413+  83  Linux

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: 设备或资源忙.
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.

注意:此处在之前的基础上,再添加了一个主分区和一个扩展分区,并在扩展分区上划分了一个逻辑分区
p显示分区表;n添加新的分区;t改变分区系统格式;L可以显示所有系统格式;d删除分区;w保存退出;q不保存退出

2、同步磁盘

[root@docker ~]# partx -a /dev/sda     
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2

[root@docker ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5

[root@docker ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris
/dev/sda3            7650        7781     1059858+  83  Linux
/dev/sda4            7782       13054    42355372+   5  Extended
/dev/sda5            7782        9087    10490413+  83  Linux
注意:扩展分区不能格式化,也不需要挂载,扩展分区最好是把剩下的所有的空间值都给它

3、分区格式化

[root@docker ~]# mkfs.ext4 /dev/sda3
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
66384 inodes, 264964 blocks
13248 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

正在写入inode表: 完成                            
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

4、挂载分区

[root@docker ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda1      ext4    48G  4.4G   42G  10% /
tmpfs          tmpfs  495M  192K  495M   1% /dev/shm
[root@docker ~]# mkdir /luyun /hello
[root@docker ~]# mount /dev/sda3 /luyun
[root@docker ~]# mount /dev/sda5 /hello
[root@docker ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda1      ext4    48G  4.4G   42G  10% /
tmpfs          tmpfs  495M  192K  495M   1% /dev/shm
/dev/sda3      ext4   987M  1.3M  918M   1% /luyun
/dev/sda5      ext4   9.8G   23M  9.2G   1% /hello

二、交换分区

1、查看交换分区

[root@docker ~]# cat /proc/swaps 
Filename                Type        Size    Used    Priority
/dev/sda2                               partition   10239996    11820   -1
[root@docker ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                               partition   10239996    11820   -1

2、若交换分区不够用了,那如何添加交换分区

方法一

[root@docker ~]# 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
First cylinder (9088-13054, default 9088): 
Using default value 9088
Last cylinder, +cylinders or +size{K,M,G} (9088-13054, default 13054): +2G

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris
/dev/sda3            7650        7781     1059858+  83  Linux
/dev/sda4            7782       13054    42355372+   5  Extended
/dev/sda5            7782        9087    10490413+  83  Linux
/dev/sda6            9088        9349     2104483+  83  Linux

Command (m for help): t     #更改分区的类型为swap
Partition number (1-6): 6
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap / Solaris)

Command (m for help): p   

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aeeec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        6375    51200000   83  Linux
/dev/sda2            6375        7650    10240000   82  Linux swap / Solaris
/dev/sda3            7650        7781     1059858+  83  Linux
/dev/sda4            7782       13054    42355372+   5  Extended
/dev/sda5            7782        9087    10490413+  83  Linux
/dev/sda6            9088        9349     2104483+  82  Linux swap / Solaris

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: 设备或资源忙.
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.

2、同步磁盘

[root@docker ~]# partx -a /dev/sda     #此命令敲2次

3、分区格式化

[root@docker ~]# mkswap /dev/sda6
Setting up swapspace version 1, size = 2104476 KiB
no label, UUID=5c54d9bb-cded-477a-aba0-bd5cac82751f

4、激活swap分区

[root@docker ~]# swapon /dev/sda6

5、查看swap分区

[root@docker ~]# cat /proc/swaps 
Filename                Type        Size        Used    Priority
/dev/sda2               partition   10239996    11820   -1
/dev/sda6               partition   2104476     0       -2     

方法二

1、生成一个大文件,把这个文件所占用的空间变成交换分区

[root@docker ~]# dd if=/dev/zero of=file bs=1M count=100
记录了100+0 的读入
记录了100+0 的写出
104857600字节(105 MB)已复制,2.35601 秒,44.5 MB/秒
[root@docker ~]# ls -lh file 
-rw-r--r-- 1 root root 100M 9月  20 14:16 file

[root@docker ~]# mkswap file
mkswap: file: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=b3be4784-90fd-4e35-8d1a-9da079a3ec55

[root@docker ~]# swapon file 

[root@docker ~]# cat /proc/swaps 
Filename                Type        Size        Used    Priority
/dev/sda2               partition   10239996    12244   -1
/dev/sda6               partition   2104476     0       -2
/root/file              file        102396      0       -3

2、若想停用一个交换分区,如下

[root@docker ~]# swapoff /root/file 
[root@docker ~]# cat /proc/swaps 
Filename                Type        Size        Used    Priority
/dev/sda2               partition   10239996    12244   -1
/dev/sda6               partition   2104476     0       -2

你可能感兴趣的:(Linux)