linux磁盘分区命令及操作

分区管理fdisk
前言
在linux系统中我们来添加一块新硬盘进行分区.运用fdisk工具进行分区。
[root@mxl ~]# fdisk �Cl 是查看分区

Disk /dev/hda: 8589 MB, 8589934592 bytes这里是新加的硬盘的容量
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
这里是我们新加的一块IDE接口的硬盘,IDE 接口的硬盘都用hd表示这里是第一块
Disk /dev/hda doesn't contain a valid partition table  
这句话的大概意思是此块硬盘没有进行分区
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 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         144     1052257+  82  Linux swap / Solaris
/dev/sda3             145        1958    14570955   83  Linux
这些是我们以前的磁盘分区,这些不是以IDE接口进行连接的,
下面我们来对新加的硬盘进行分区,方法与步骤如下:
1、 创建分区
[root@mxl ~]# fdisk /dev/had  对新加的硬盘进行分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 17753.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
  (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):
Command (m for help): m
Command action
a   toggle a bootable flag   将分区设置为启动区
b   edit bsd disklabel    编辑bsd的disklabel
c   toggle the dos compatibility flag  设置该分区为dos分区
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   输入N后可以创建主分区和扩展分区
Command action
  e   extended
  p   primary partition (1-4)
说明一点必需建立扩展分区才能创建逻辑分区。
Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-17753, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-17753, default 17753): +2048M
第一主分区完毕
创建第二主分区
Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (4235-17753, default 4235):
Using default value 4235
Last cylinder or +size or +sizeM or +sizeK (4235-17753, default 17753): +1024M完毕
创建第三主分区
Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (6352-17753, default 6352):
Using default value 6352
Last cylinder or +size or +sizeM or +sizeK (6352-17753, default 17753): +1024M完毕
创建扩展分区和逻辑分区
Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
e
Selected partition 4
First cylinder (8469-17753, default 8469):
Using default value 8469
Last cylinder or +size or +sizeM or +sizeK (8469-17753, default 17753):
Using default value 17753

Command (m for help): p 查看分区信息

Disk /dev/hda: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1        4234     2000533+  83  Linux
/dev/hda2            4235        6351     1000282+  83  Linux
/dev/hda3            6352        8468     1000282+  83  Linux
/dev/hda4            8469       17753     4387162+   5  Extended
Command (m for help): n
First cylinder (8469-17753, default 8469):
Using default value 8469
Last cylinder or +size or +sizeM or +sizeK (8469-17753, default 17753):
创建扩展分区完毕我们把所有剩余的空间都给它
Command (m for help): p

Disk /dev/hda: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1        4234     2000533+  83  Linux
/dev/hda2            4235        6351     1000282+  83  Linux
/dev/hda3            6352        8468     1000282+  83  Linux
/dev/hda4            8469       17753     4387162+   5  Extended
/dev/hda5            8469       10585     1000251   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: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
保存退出
格式化文件系统
[root@mxl ~]# fdisk -l

Disk /dev/hda: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1        4234     2000533+  83  Linux
/dev/hda2            4235        6351     1000282+  83  Linux
/dev/hda3            6352        8468     1000282+  83  Linux
/dev/hda4            8469       17753     4387162+   5  Extended
/dev/hda5            8469       10585     1000251   83  Linux
/dev/hda6           10586       14819     2000533+  83  Linux
/dev/hda7           14820       16936     1000251   83  Linux
/dev/hda8           16937       17753      386001   83  Linux
下面对这些文件进行格式化
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
[root@mxl ~]# mkfs -t ext3 /dev/hdb1 格式化
mke2fs 1.39 (29-May-2006)
Could not stat /dev/hdb1 --- No such file or directory

The device apparently does not exist; did you specify it correctly?
[root@mxl ~]# mkfs -t ext3 /dev/hda1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
250368 inodes, 500133 blocks
25006 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=515899392
16 block groups
32768 blocks per group, 32768 fragments per group
15648 inodes per group
Superblock backups stored on blocks:
       32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
格式化完毕 如果对其它的分区进行格式化方法同上
现在我们只需要把把格式化的分区接载到系统的某个文件中就可以使用,下面是挂载的方法以apache为例
[root@mxl ~]# mkdir /mnt/apache   在/mnt下创建一个apache目录
[root@mxl ~]# mount /dev/hda1 /mnt/apache  这个就是挂载了
查看我们挂载的信息
[root@mxl ~]# df -h
Filesystem            Size  Used   Avail Use% Mounted on
文件系统              容量  已用   未用 已用% 挂载点
/dev/sda3              14G  3.6G  9.2G  29% /
/dev/sda1              99M   12M   83M  12% /boot
tmpfs                 128M     0  128M   0% /dev/shm
/dev/hda1             1.9G   35M  1.8G   2% /mnt/apache
查看文件目录大小
[root@mxl /]# du -h |more
208K    ./media/httpd-2.2.8/docs/conf/extra
320K    ./media/httpd-2.2.8/docs/conf
32K     ./media/httpd-2.2.8/docs/error/include
304K    ./media/httpd-2.2.8/docs/error
56K     ./media/httpd-2.2.8/docs/docroot
520K    ./media/httpd-2.2.8/docs/icons/small
1.8M    ./media/httpd-2.2.8/docs/icons
24K     ./media/httpd-2.2.8/docs/cgi-examples
140K    ./media/httpd-2.2.8/docs/man
320K    ./media/httpd-2.2.8/docs/manual/images
620K    ./media/httpd-2.2.8/docs/manual/vhosts
300K    ./media/httpd-2.2.8/docs/manual/developer
180K    ./media/httpd-2.2.8/docs/manual/rewrite
6.5M    ./media/httpd-2.2.8/docs/manual/mod
432K    ./media/httpd-2.2.8/docs/manual/misc
668K    ./media/httpd-2.2.8/docs/manual/programs
248K    ./media/httpd-2.2.8/docs/manual/ssl
28K     ./media/httpd-2.2.8/docs/manual/faq
16K     ./media/httpd-2.2.8/docs/manual/style/xsl/util
--More--
修改分区类型
[root@mxl ~]# fdisk /dev/hda

The number of cylinders for this disk is set to 17753.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
  (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): t
Command (m for help): t
Partition number (1-8): 3
Hex code (type L to list codes): L   l是查看分区类型

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

Disk /dev/hda: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1        4234     2000533+  83  Linux
/dev/hda2            4235        6351     1000282+  83  Linux
/dev/hda3            6352        8468     1000282+  82  Linux swap / Solaris
修改成功
下面我们进行挂载才能使其生效

使用parted进行分区管理
[root@mxl /]# parted
GNU Parted 1.8.1
Using /dev/hda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sdb      选择要配置的磁盘                                            
Using /dev/sdb
(parted)    

(parted) mkpart primary ext3 1 1024.
Error: Unable to open /dev/sdb - unrecognised disk label.  提示找不到磁盘标签
我们要创意标签即可
(parted) mklabel msdos
(parted) mkpart primary ext3 1 1024    
(parted) p                                                                

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
1      0.51kB  1024MB  1024MB  primary                    
主分区创建成功
创建扩展分区
(parted) mkpart extended 3072 8000
(parted) p                                                                

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
1      0.51kB  1024MB  1024MB  primary                    
2      1024MB  2048MB  1024MB  primary                    
3      2048MB  3072MB  1024MB  primary                    
4      3072MB  8000MB  4928MB  extended               lba
创建逻辑分区
(parted) mkpart logical 3072 4000                                        
(parted) p                                                                

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
1      0.51kB  1024MB  1024MB  primary                    
2      1024MB  2048MB  1024MB  primary                    
3      2048MB  3072MB  1024MB  primary                    
4      3072MB  8000MB  4928MB  extended               lba  
5      3072MB  4000MB  928MB   logical                    

(parted) mkpart logical 4000 5204
(parted) p                                                                

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
1      0.51kB  1024MB  1024MB  primary                    
2      1024MB  2048MB  1024MB  primary                    
3      2048MB  3072MB  1024MB  primary                    
4      3072MB  8000MB  4928MB  extended               lba  
5      3072MB  4000MB  928MB   logical                    
6      4000MB  5204MB  1204MB  logical      
将磁盘加载到系统中
方法一:手工加载
[root@mxl /]# mount -t ext3 /dev/hda3 /apache
[root@mxl /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              14G  3.6G  9.2G  29% /
/dev/sda1              99M   12M   83M  12% /boot
tmpfs                 128M     0  128M   0% /dev/shm
/dev/hda3             962M   18M  896M   2% /apache
方法二:系统启动自动加载
我们只需要对下面的文件进行添加即可
[root@mxl etc]# cat fstab
LABEL=/  设备        / 挂载点          文件类型ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
sysfs                   /sys                    sysfs   defaults        0 0
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
这里的0与1代表
前一个0代表不检查后一个0代表不运行,前一个1代表检查文件系统是否需要备份后一个1代表运行就是检查文件系统
这里的2代表非正常关机或者达到最大加载次数一定天数才运行。
如果让系统开机就加载某个分区我们要必需在/etc/fstab加入一行
[root@mxl etc]# vi fstab

LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
sysfs                   /sys                    sysfs   defaults        0 0
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
/dev/hda3               /apache                 ext3    defaults        0 0
重启系统再次查看
[root@mxl ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              14G  3.6G  9.2G  29% /
/dev/sda1              99M   12M   83M  12% /boot
tmpfs                 128M     0  128M   0% /dev/shm
/dev/hda3             962M   18M  896M   2% /apache
自动加载成功
交换空间管理
添加新内存后对虚拟内存进行的设置
查看一下内存的大小
[root@mxl /]# free
            total       used       free     shared    buffers     cached
Mem:        229540     200124      29416          0      31984      81168
-/+ buffers/cache:      86972     142568
Swap:      1534196          0    1534196实际虚拟内存大小
下面来对虚拟内存进行修改
[root@mxl /]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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         204     1534207+  82  Linux swap / Solaris
/dev/sda3             205        1305     8843782+  83  Linux

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

  Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         125     1004031   83  Linux
/dev/sdb2             126         250     1004062+  83  Linux
/dev/sdb3             251        1044     6377805    5  Extended
/dev/sdb5             251         500     2008093+  82  Linux swap / Solaris
/dev/sdb是我新加的一块硬盘关于对新加硬盘的分区挂载上面已经学过这里就不在详细介绍.我们就以为个新加的硬盘为来增加虚拟内存的大小
/dev/sdb5就是我要为给虚拟内存的分区,当然我们要让系统重启时自动加载就需要在/etc/fstab加上一行即,上面也介绍了加入的例子了
[root@mxl /]# mkswap /dev/sdb5
Setting up swapspace version 1, size = 2056282 kB
[root@mxl /]# swapon /dev/sdb5
[root@mxl /]# free
            total       used       free     shared    buffers     cached
Mem:        229540     200772      28768          0      31992      81188
-/+ buffers/cache:      87592     141948

Swap:      3542280          0    3542280 虚拟内存的变化

转:http://haoyou168.blog.51cto.com/284295/83021


你可能感兴趣的:(linux磁盘分区)