歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下的分區工具

Linux下的分區工具

日期:2017/2/28 16:01:05   编辑:Linux教程

[root@localhost linuxidc]# df -Th
#查看已經掛載上的文件分區大小以及格式(當然你的系統必須有空閒的空間不用了)
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/sda1 ext3 2.9G 2.2G 505M 82% /
/dev/sda2 ext3 965M 22M 894M 3% /home
tmpfs tmpfs 345M 12K 345M 1% /dev/shm
[root@localhost linuxidc]# /sbin/fdisk /dev/sda
#fdisk命令來進行磁盤分區
#如果你有第二塊硬盤,那麼後面的設備符號是/dev/sdb,第三塊以此類推
The number of cylinders for this disk is set to 1044.
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): m #查看fdisk命令的幫助
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): p #打印/查看已有的分區表

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

Device Boot Start End Blocks Id System
/dev/sda1 * 1 382 3068383+ 83 Linux
/dev/sda2 383 509 1020127+ 83 Linux
/dev/sda3 510 636 1020127+ 82 Linux swap / Solaris
Command (m for help): n #開始增加新的分區
Command action
e extended #擴展分區
p primary partition (1-4) #主分區,一塊硬盤中可以有4個主分區
#例如:XP下C盤為主分區,DEF為邏輯分區(DEF合起來就是擴展分區)
e #增加擴展分區
Selected partition 4 #分區4作為擴展分區,分區4開始是未分配空閒空間
First cylinder (637-1044, default 637): <Enter>
Using default value 637 #分區的開頭,1044是塊結尾
Last cylinder or +size or +sizeM or +sizeK (637-1044, default 1044): <Enter>
#也可在這裡直接輸入想要建立分區的大小,比如+5000MB,表示建立5000MB大小的分區
Using default value 1044 #分區的結尾
Command (m for help): n #建立完擴展分區,然後建立邏輯分區
First cylinder (637-1044, default 637): <Enter>
Using default value 637
Last cylinder or +size or +sizeM or +sizeK (637-1044, default 1044): <Enter>
Using default value 1044
Command (m for help): p #建立完了邏輯分區,打印/查看
#可以看到增加了一個分區,邏輯分區是建立在擴展分區上的
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000e25d9
Device Boot Start End Blocks Id System
/dev/sda1 * 1 382 3068383+ 83 Linux
/dev/sda2 383 509 1020127+ 83 Linux
/dev/sda3 510 636 1020127+ 82 Linux swap / Solaris
/dev/sda4 637 1044 3277260 5 Extended
/dev/sda5 637 1044 3277228+ 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.
Syncing disks.
[root@localhost linuxidc]# df -Th
#因為這個時候還沒有掛載,所以在df命令下並不能看到新的分區
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/sda1 ext3 2.9G 2.2G 505M 82% /
/dev/sda2 ext3 965M 22M 894M 3% /home
tmpfs tmpfs 345M 12K 345M 1% /dev/shm
[root@localhost linuxidc]# /sbin/mkfs.ext3 /dev/sda5
#在掛載之前應該先格式化,比如ext2,ext3格式
#還可以使用/sbin/mkfs –t ext3 /dev/sda5
#其實我們已經看到在fdisk命令中已經自動將其格式化為ext3格式了
mke2fs 1.40.2 (12-Jul-2007)
warning: 107 blocks unused.
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
410400 inodes, 819200 blocks
40965 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=838860800
25 block groups
32768 blocks per group, 32768 fragments per group
16416 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (16384 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.
[root@localhost linuxidc]# /sbin/e2label /dev/sda5 backup #給新分區添加一個標簽
[root@localhost linuxidc]# mkdir /mnt/bak #創建目錄
[root@localhost linuxidc]# mount /dev/sda5 /mnt/bak #將新分區掛載到指定目錄上
[root@localhost linuxidc]# df -Th #這時候就可以看到掛載上了
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/sda1 ext3 2.9G 2.2G 505M 82% /
/dev/sda2 ext3 965M 22M 894M 3% /home
tmpfs tmpfs 345M 12K 345M 1% /dev/shm
/dev/sda5 ext3 3.1G 69M 2.9G 3% /mnt/bak
[root@localhost linuxidc]# vim /etc/fstab #添加一行到/etc/fstab中開機自動掛載
#其實不用設置,系統也可以開機自動掛載的,因為它的格式跟linux系統格式是一樣的
[root@localhost linuxidc]# cat /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/home /home ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
LABEL=backup /mnt/bak ext3 defaults 1 2

Copyright © Linux教程網 All Rights Reserved