注意: 分区的时候要umount磁盘之后才能分区。在Ubuntu用fdisk分析需要root用户权限。
第一步:先把设备卸载掉,解除占用:
caizd@ubuntu:/home/share$ lsblk //查看大小空间
caizd@ubuntu:/home/share$ umount /dev/sdb1
caizd@ubuntu:/home/share$ umount /dev/sdb2
第二步:对磁盘分区
1、首先执行fdisk /dev/sdb 进入该设备分区模式(设备名称根据你的实际设备,我这里是sdb)。此时出现:
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 注:l是列出分区类型,以供我们设置相应分区的类型;
m print this menu 注:m 是列出帮助信息;
n add a new partition 注:添加一个分区;
o create a new empty DOS partition table
p print the partition table 注:p列出分区表;
q quit without saving changes 注:不保存退出;
s create a new empty Sun disklabel
t change a partition’s system id 注:t 改变分区类型;
u change display/entry units
v verify the partition table
w write table to disk and exit 注:把分区表写入硬盘并退出;
x extra functionality (experts only) 注:扩展应用,专家功能;
2、列出当前操作硬盘的分区情况,输入p
Command (m for help): p
Disk /dev/sdb: 128.0 GB, 128035676160 bytes
255 heads, 63 sectors/track, 15566 cylinders, total 250069680 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
Disk identifier: 0xac341720
Device Boot Start End Blocks Id System
/dev/sdb1 2048 411647 204800 83 Linux
/dev/sdb2 411648 250069679 124829016 83 Linux
3、通过fdisk的d指令来删除一个分区,输入d
ommand (m for help): d //输入d删除分区操作
Partition number (1-4): 1 //输入1表示删除分区1
Command (m for help): p //输入p再次查看分区情况,是否已经删除了分区1
Disk /dev/sdb: 128.0 GB, 128035676160 bytes
255 heads, 63 sectors/track, 15566 cylinders, total 250069680 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
Disk identifier: 0xac341720
Device Boot Start End Blocks Id System
/dev/sdb2 411648 250069679 124829016 83 Linux
Command (m for help): d //输入d删除分区操作
Selected partition 2 //输入2表示删除分区2
Command (m for help): p //输入p再次查看分区情况,是否已经删除了分区2
Disk /dev/sdb: 128.0 GB, 128035676160 bytes
255 heads, 63 sectors/track, 15566 cylinders, total 250069680 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
Disk identifier: 0xac341720
Device Boot Start End Blocks Id System
Command (m for help):w
在删除分区操作之后,输入w保存退出!
如果自己操作错了,莫慌,用q不保存退出即可。
4、通过fdisk的n指令增加一个分区,输入n
root@ubuntu:/home/share# fdisk /dev/sdb //执行命令进入该设备分区模式
Command (m for help): n //增加一个分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1 //设置分区号为1
First sector (2048-250069679, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-250069679, default 250069679): 1048576 //设置分区空间大小为512M(1024x1024=1048576 )
Command (m for help): p //查看新建的分区是否生成
Disk /dev/sdb: 128.0 GB, 128035676160 bytes
255 heads, 63 sectors/track, 15566 cylinders, total 250069680 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
Disk identifier: 0xac341720
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1048576 523264+ 83 Linux
Command (m for help): w //输入w,写入并且保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
root@ubuntu:/home/share# fdisk /dev/sdb //执行命令进入该设备分区模式
Command (m for help): p //查看当前设备分区情况
Disk /dev/sdb: 128.0 GB, 128035676160 bytes
70 heads, 5 sectors/track, 714484 cylinders, total 250069680 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
Disk identifier: 0xac341720
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1048576 523264+ 83 Linux
Command (m for help): n //增加一个分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p //新增主分区
Partition number (1-4, default 2): 2 //设置分区编号为2,默认为2
Using default value 2
First sector (1048577-250069679, default 1048577): //直接回车选择默认default
Using default value 1048577
Last sector, +sectors or +size{K,M,G} (1048577-250069679, default 250069679): //直接回车选择默认default,相当于把剩余的硬盘空间分配给这个分区
Using default value 250069679
Command (m for help): w //写入保存退出。
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
root@ubuntu:/home/share#
5、对分区进行格式化,以及加载;
一般用 mkfs.bfs mkfs.ext2 mkfs.jfs mkfs.msdos mkfs.vfatmkfs.cramfs mkfs.ext3 mkfs.minix mkfs.reiserfs mkfs.xfs 等命令来格式化分区,比如我想格式化 sdb1为ext3文件系统,则输入;
root@ubuntu:/home/share# sudo mkfs.ext3 /dev/sdb1
6、查看磁盘分区是什么类型的文件系统格式
root@ubuntu:/home/caizd# file -s /dev/sda1 //查看磁盘分区是什么类型的文件系统格式
/dev/sda1: Linux rev 1.0 ext4 filesystem data, UUID=a5fc26a3-0c04-4356-af3f-cb887e5b366b (needs journal recovery) (extents) (large files) (huge files)
或者: blkid /dev/sda1 //查看磁盘分区是什么类型的文件系统格式
如果我想加载 sdb1到目前系统来存取文件,应该有mount 命令,但首先您得建一个挂载目录;比如 /mnt/sdb1 ;
root@ubuntu:/home/share# mkdir /mnt/sdb1
root@ubuntu:/home/share# mount /dev/sdb1 /mnt/sdb1
root@ubuntu:/home/share# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 254G 14G 228G 6% /
udev 987M 8.0K 987M 1% /dev
tmpfs 398M 1.1M 397M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 995M 200K 995M 1% /run/shm
/dev/loop0 3.0G 2.8G 71M 98% /home/share/loongson_img/bmc_img
/dev/loop1 3.0G 2.8G 116M 97% /home/share/loongson_img/sw_img
/dev/loop2 497M 228M 245M 49% /home/share/loongson_img/boot_img
/dev/sdb1 495M 11M 460M 3% /mnt/sdb1