sfdisk有四个主要的功能:列出一个分区的大小(块为单位),一个设备的所有分区,检查一个设备的分区表,重新分区一个设备。
其实我在平时用的最多的是第二个–列出一个设备的分区表。有了fdisk命令,为什么还要有sfdisk命令呢?我第一次具体使用sfdisk命令,还是在HP
DL 系列机器上。HP机器使用了Smart
Array阵列卡,其链接的SCSI在linux中识别出来的不是平常我们常认为的sdx设备,而是cciss/c0d0px这样的样子。
而这样的分区信息,使用fdisk -l是没有办法打印出来的,这个时候就要使用sfdisk -l命令了。这就使用到了sfdisk的第二个功能了。
他的第三个功能和第四个没有使用过,不过刚才看man手册,发现其备份和恢复分区表的方式倒是很简单。
备份分区表:
sfdisk /dev/hdd -O hdd-partition-sectors.save
恢复分区表:
sfdisk /dev/hdd -I hdd-partition-sectors.save
下面是我的具体测试:
fdisk -l /dev/mdp0
[root@mlsx tools]# fdisk -l /dev/mdp0
Disk /dev/mdp0: 419 MB, 419299328 bytes
2 heads, 4 sectors/track, 102368 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Device Boot Start End Blocks Id System
/dev/mdp0p1 1 24415 97658 83 Linux
/dev/mdp0p2 24416 102368 311812 83 Linux
[root@mlsx tools]# ls /misc
boot.b
trd-2[root@mlsx tools]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mdp0p2 301959 50763 235606 18% /misc
[root@mlsx tools]#sfdisk /dev/mdp0 -O /tmp/mdp0-partition.save
[root@mlsx tools]# ls -l /tmp/mdp0-partition.save
-r–r–r– 1 root root 516 05-28 00:06 /tmp/mdp0-partition.save
[root@mlsx tools]# dd if=/dev/zero of=/dev/mdp0 bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 7.9619e-05 seconds, 6.4 MB/s
[root@mlsx tools]# fdisk -l /dev/mdp0
Disk /dev/mdp0: 419 MB, 419299328 bytes
2 heads, 4 sectors/track, 102368 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Disk /dev/mdp0 doesn't contain a valid partition table
[root@mlsx tools]# sfdisk /dev/mdp0 -I /tmp/mdp0-partition.save
Re-reading the partition table …
[root@mlsx tools]# fdisk -l /dev/mdp0
Disk /dev/mdp0: 419 MB, 419299328 bytes
2 heads, 4 sectors/track, 102368 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Device Boot Start End Blocks Id System
/dev/mdp0p1 1 24415 97658 83 Linux
/dev/mdp0p2 24416 102368 311812 83 Linux
[root@mlsx tools]# mount /dev/mdp0p2 /misc
[root@mlsx tools]# ls /misc
boot.b
从上面的例子可以看出,sfdisk确实可以做到分区的备份和恢复,而且分区已有数据并不会丢失。
这其实和拿着一片原始钥匙去配钥匙的地方复制一片出来,某天原始的丢了,用这片复制的打开房门,房子的摆设当然什么都没有改变。