硬盘分区

使用fdisk -l查看硬盘分区情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@kali:/tmp# fdisk -l
//共有两块硬盘 /dev/sdb /dev/sda
Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 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
Disklabel type: dos
Disk identifier: 0x293a4722
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 6143 4096 2M 83 Linux
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: dos
Disk identifier: 0xb2d1b90f
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 60262399 60260352 28.8G 83 Linux
/dev/sda2 60264446 62912511 2648066 1.3G 5 Extended
/dev/sda5 60264448 62912511 2648064 1.3G 82 Linux swap / Solaris

在Linux下,IDE硬盘设备名为hd*(a-d),SCSI、SATA、USB硬盘设备名为sd*(a-z)。
硬盘主分区最多4个,故主分区从sda1-sda4,扩展分区最多一个,且主分区加扩展分区最多为4个,逻辑分区从sda5开始。

现在我们为设备sdb新建主分区sdb2,大小位10M。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
root@kali:/tmp# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
//n添加分区
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free) //提示已经存在1个主分区,0个扩展分区,还可以添加3个
e extended (container for logical partitions) //扩展分区 逻辑分区的容器
//p 为主分区
Select (default p): p //主分区
Partition number (2-4, default 2): //分区编号 默认为2
First sector (6144-2097151, default 6144): //起始柱面编号
Last sector, +sectors or +size{K,M,G,T,P} (6144-2097151, default 2097151): +10M //结束柱面 可以以大小确定
Created a new partition 2 of type 'Linux' and of size 10 MiB.
Command (m for help): p //显示分区列表
Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 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
Disklabel type: dos
Disk identifier: 0x293a4722
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 6143 4096 2M 83 Linux
/dev/sdb2 6144 26623 20480 10M 83 Linux
Command (m for help): w //保存 不保存不生效
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
//使用fdisk -l查看 添加分区完成
....
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 6143 4096 2M 83 Linux
/dev/sdb2 6144 26623 20480 10M 83 Linux
....

硬盘格式化

添加完毕分区后,必须格式化才能使用

1
2
3
4
5
6
7
8
9
10
11
root@kali:/tmp# mkfs -t ext3 /dev/sdb2 //-t 指定文件系统类型
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 10240 1k blocks and 2560 inodes
Filesystem UUID: 330d0106-a723-4bae-8a00-d5d8105d4a0d
Superblock backups stored on blocks:
8193
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

挂载

格式化后的硬盘需要挂载,在生产环境下,需要设置为开机自动挂载,这里我们只做单次挂载。

1
2
3
4
5
root@kali:/tmp# mkdir test3 //创建挂载点
root@kali:/tmp# mount /dev/sdb2 test3 //挂载
root@kali:/tmp# cd test3
root@kali:/tmp/test3# ls
lost+found

备份指定分区

挂载好之后,我们就可以在挂载目录下使用了。为了方便起见,我们在该分区创建若干文件,然后备份该分区。

1
2
3
4
5
6
7
8
root@kali:/tmp/test3# ls
abc abd abe lost+found
root@kali:/tmp/test3# cd ..
root@kali:/tmp# umount /tmp/test3 //卸载硬盘
root@kali:/tmp# dd if=/dev/sdb2 of=disk-image1 //使用dd备份分区,当然可以备份整个硬盘
20480+0 records in
20480+0 records out
10485760 bytes (10 MB) copied, 0.289164 s, 36.3 MB/s

挂载硬盘镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@kali:/tmp# binwalk disk-image1 //通过binwalk可以看到文件
DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
0 0x0 Linux EXT filesystem, rev 1.0 ext3 filesystem data, UUID=330d0106-a723-4bae-8a00-d5d8105d105d
224256 0x36C00 Linux EXT filesystem, rev 1.0 ext3 filesystem data, UUID=330d0106-a723-4bae-8a00-d5d8105d105d
2098176 0x200400 PNG image, 164 x 371, 8-bit/color RGB, non-interlaced
2098256 0x200450 Zlib compressed data, default compression, uncompressed size >= 65536
8388608 0x800000 Linux EXT filesystem, rev 1.0 ext3 filesystem data, UUID=330d0106-a723-4bae-8a00-d5d8105d105d
root@kali:/tmp# file disk-image1 //查看文件类型
disk-image1: Linux rev 1.0 ext3 filesystem data, UUID=330d0106-a723-4bae-8a00-d5d8105d4a0d
root@kali:/tmp# mount disk-image1 test4
mount: mount point test4 does not exist
root@kali:/tmp# mkdir test4
root@kali:/tmp# mount disk-image1 test4
root@kali:/tmp# cd test4
root@kali:/tmp/test4# ls //显示正常的文件
abc abd abe lost+found test.png