Linux 磁盘管理命令

 df   查看磁盘分区使用情况

root@liujie-desktop:/# df -h
文件系统            容量  已用 可用 已用% 挂载点
/dev/sda1              19G  4.6G   14G  26% /
none                  497M  316K  497M   1% /dev
none                  501M   24K  501M   1% /dev/shm
none                  501M   96K  501M   1% /var/run
none                  501M     0  501M   0% /var/lock
none                  501M     0  501M   0% /lib/init/rw
/dev/sdb1             3.4G  963M  2.5G  28% /media/disk
/dev/sdb1             3.4G  963M  2.5G  28% /mnt/usb
 du  统计磁盘文件的大小
 -b   以字节为单位

 -k 以KB为单位

 -m 以mb为单位

  -h  以1024进制显示

 -s  统计指定目标

root@liujie-desktop:/# du -sh etc
15M	etc

关于分区,

1. 主分区和扩展分区总数不超过4个;

2. 扩展分区最多只能有一个;

3. 扩展分区不能直接存取数据,必须将其划分为逻辑分区。

关于Linux系统磁盘

新硬盘添加到服务器中后需要创建分区;格式化分区;挂载分区之后才可以使用,这些步骤缺一不可。新添加的磁盘分区可以挂载到系统的任意位置。


给虚拟机vm添加一块硬盘

eidt virtual  mathine setting->add...->hard disk ->net ->next->ok

fdisk -l 列出当前分区列表(fdisk 只能给MBR分区

root@liujie-desktop:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0000303e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2497    20051968   83  Linux
/dev/sda2            2497        2611      916481    5  Extended
/dev/sda5            2497        2611      916480   82  Linux swap / Solaris

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table
由上一行可以看出sdb就是我们新添加的4G硬盘,不包含可用的分区表,即未分区,不能存取数据。
fdisk  /dev/sdb    进入分区模式

接着输入n

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
添加扩展分区和主分区
之后按照提示填写起始位和终止位

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): n
Command action
   e   extended
   p   primary partition (1-4)

创建分区

p
Partition number (1-4): 1
First cylinder (1-522, default 1):   # 输入的是块编号
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522): 256

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x02a6305f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         256     2056288+  83  Linux

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

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x02a6305f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         256     2056288+  83  Linux
/dev/sdb2             257         522     2136645    5  Extended

Command (m for help): 

将分区方案写入到分区表w

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
然后在fdisk -l 查看

root@liujie-desktop:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0000303e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2497    20051968   83  Linux
/dev/sda2            2497        2611      916481    5  Extended
/dev/sda5            2497        2611      916480   82  Linux swap / Solaris

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x02a6305f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         256     2056288+  83  Linux
/dev/sdb2             257         522     2136645    5  Extended

分区模式GPT

分区模式MBR :主分区不超过4个,单个容量不超过2TB

分区模式GPT:主分区个数几乎(128)没有限制,容量几乎没有限制(18EB),不支持X86架构

GPT现在利用 parted 命令(该指令既可以给MBR分又可以给GPT分)。

root@liujie-desktop:~# parted
GNU Parted 2.2
使用 /dev/sda
欢迎使用 GNU Parted! 输入 'help'可获得命令列表.
(parted) select /dev/sdc
使用 /dev/sdc

(parted) mklabel gpt          #msdos 就是为MBR分区
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sdc: 6442MB
Sector size (logical/physical): 512B/512B
分区表:gpt

数字  开始:  End  大小  文件系统  Name  标志

(parted) print all                                                        
Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
分区表:msdos

数字  开始:  End     大小    类型      文件系统        标志
 1    1049kB  20.5GB  20.5GB  primary   ext4            启动
 2    20.5GB  21.5GB  938MB   extended
 5    20.5GB  21.5GB  938MB   logical   linux-swap(v1)


Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sdb: 4295MB
Sector size (logical/physical): 512B/512B
分区表:msdos

数字  开始:  End     大小    类型      文件系统  标志
 1    32.3kB  2106MB  2106MB  primary
 2    2106MB  4294MB  2188MB  extended


Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sdc: 6442MB
Sector size (logical/physical): 512B/512B
分区表:gpt

数字  开始:  End  大小  文件系统  Name  标志


(parted)                        

分两种方式:交互模式和命令模式

交互模式:

(parted) mkpart
分区名称?  []? iliu
文件系统类型?  [ext2]?                                                   
起始点? 0                                                                
结束点? 2000                                                             
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? c
(parted) mkpart
分区名称?  []? iliu                                                      
文件系统类型?  [ext2]?                                                   
起始点? 1                                                                
结束点? 2000                                                             
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sdc: 6442MB
Sector size (logical/physical): 512B/512B
分区表:gpt

数字  开始:  End     大小    文件系统  Name  标志
 1    1049kB  2000MB  1999MB            iliu

(parted) 
命令模式:

mkpart ijie 2000 3000

(parted) mkpart ijie 2000 3000
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sdc: 6442MB
Sector size (logical/physical): 512B/512B
分区表:gpt

数字  开始:  End     大小    文件系统  Name  标志
 1    1049kB  2000MB  1999MB            iliu
 2    2000MB  3000MB  1000MB            ijie

(parted) mkpart iqian 2500 4000                                          
警告: You requested a partition from 2500MB to 4000MB.                    
The closest location we can manage is 3000MB to 4000MB.
Is this still acceptable to you?
是/Yes/否/No? yes                                                         
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sdc: 6442MB
Sector size (logical/physical): 512B/512B
分区表:gpt

数字  开始:  End     大小    文件系统  Name   标志
 1    1049kB  2000MB  1999MB            iliu
 2    2000MB  3000MB  1000MB            ijie
 3    3000MB  4000MB  1000MB            iqian
删除一个分区:

rm +数字

(parted) quit                                                             
信息: You may need to update /etc/fstab.

分区格式化

利用mkfs 命令
查看有哪些
root@liujie-desktop:~# ls -l /dev/sdb*
brw-rw---- 1 root disk 8, 16 2017-09-02 16:31 /dev/sdb
brw-rw---- 1 root disk 8, 17 2017-09-02 16:28 /dev/sdb1
brw-rw---- 1 root disk 8, 18 2017-09-02 16:28 /dev/sdb2
root@liujie-desktop:~# ls -l /dev/sdc*
brw-rw---- 1 root disk 8, 32 2017-09-02 16:45 /dev/sdc
brw-rw---- 1 root disk 8, 33 2017-09-02 16:45 /dev/sdc1
brw-rw---- 1 root disk 8, 34 2017-09-02 16:45 /dev/sdc2
brw-rw---- 1 root disk 8, 35 2017-09-02 16:45 /dev/sdc3
brw-rw---- 1 root disk 8, 36 2017-09-02 16:45 /dev/sdc4
root@liujie-desktop:~# ls -l /dev/sda*
brw-rw---- 1 root disk 8, 0 2017-09-02 16:31 /dev/sda
brw-rw---- 1 root disk 8, 1 2017-09-02 16:28 /dev/sda1
brw-rw---- 1 root disk 8, 2 2017-09-02 16:28 /dev/sda2
brw-rw---- 1 root disk 8, 5 2017-09-02 16:28 /dev/sda5
利用指令:

mkfs.ext3  + 设备名

root@liujie-desktop:~# mkfs.ext3 /dev/sdb1
mke2fs 1.41.11 (14-Mar-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
128768 inodes, 514072 blocks
25703 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=528482304
16 block groups
32768 blocks per group, 32768 fragments per group
8048 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入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.
或者:

mkfs -t ext4 + 设备名

挂载

然后对新添加的设备进行mount挂载到摸个已存在的目录。(如果需要永久挂载,需要编辑/etc/fstab.)

如何添加Swap分区

1. 建立一个普通Linux分区;

2. 修改分区类型的16进制编码;

3. 格式化交换分区;

4. 启用swap分区。


先进入fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x02a6305f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         256     2056288+  83  Linux
/dev/sdb2             257         522     2136645    5  Extended

Command (m for help): t
Partition number (1-5): 1
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 1 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x02a6305f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         256     2056288+  82  Linux swap / Solaris
/dev/sdb2             257         522     2136645    5  Extended

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
格式化交换分区
 mkswap  /dev/sdb1

启用

swapon  /dev/sdb1

停止

swapoff  /dev/sdb1


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值