swap与dd命令使用详解

时间:2022-11-24 09:20:27

处理交换文件和分区

    交换分区是系统RAM 的补充

基本设置包括:

    创建交换分区或者文件

    使用mkswap 写入特殊签名

    在/etc/fstab 文件中添加适当的条目

    使用swapon -a

挂载交换分区

启用:swapon

swapon [OPTION]... [DEVICE]

-a :激活所有的交换分区;

-p PRIORITY :指定优先级

/etc/fstab:pri=value

禁用:swapoff [OPTION]... [DEVICE]

[root@station29 ~]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop1    7:1    0   200M  0 loop
loop7    7:7    0   200M  0 loop
sr0     11:0    1   3.7G  0 rom  
sda      8:0    0   200G  0 disk
├─sda1   8:1    0   200M  0 part /boot
├─sda2   8:2    0    20G  0 part /data
├─sda3   8:3    0    10G  0 part /home
├─sda4   8:4    0     1K  0 part
├─sda5   8:5    0     2G  0 part [SWAP]
└─sda6   8:6    0 167.8G  0 part /
sdb      8:16   0    30G  0 disk
sdc      8:32   0    20G  0 disk
├─sdc1   8:33   0     1G  0 part
├─sdc2   8:34   0     2G  0 part
├─sdc3   8:35   0     3G  0 part
└─sdc4   8:36   0 109.8M  0 part
[root@station29 ~]# free
             total       used       free     shared    buffers     cached
Mem:       1906252     365240    1541012       1176      24060     126452
-/+ buffers/cache:     214728    1691524
Swap:      2097148          0    2097148
[root@station29 ~]# fdisk /dev/sdb    (分区)
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xdd4e370a.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): n
Command action
   e   extended    (逻辑分区)
   p   primary partition (1-4)    (主分区)
p
Partition number (1-4): 1
First cylinder (1-3916, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916): +2G
 
Command (m for help): p
 
Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdd4e370a
 
Device Boot      Start       End      Blocks        Id     System
/dev/sdb1        1         262     2104483+      83     Linux
 
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel 编辑BSD磁盘标签
   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 创建一个新的空DOS分区表
   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): t
Selected partition 1
Hex code (type L to list codes): l
 
 0  Empty 24  NEC DOS 81  Minix / old Lin bf  Solaris        
 1  FAT12    39  Plan 9 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root    3c  PartitionMagic    83  Linux         c4  DRDOS/sec (FAT-
 3  XENIX usr    40  Venix 80286    84  OS/2 hidden C:  c6 DRDOS/sec (FAT-
 4  FAT16 <32M    41  PPC PReP Boot    85  Linux extended  c7  Syrinx         
 5  Extended    42  SFS    86  NTFS volume set da  Non-FS data    
 6  FAT16    4d  QNX4.x    87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM    93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32    52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA)     54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS    56  Golden Bow      a7  NeXTSTEP      f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep    70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)
 
Command (m for help): p
 
Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdd4e370a
 
Device Boot      Start         End      Blocks       Id     System
/dev/sdb1          1         262     2104483+     82     Linux swap / Solaris
 
Command (m for help): w
The partition table has been altered!    分区表已更改
 
Calling ioctl() to re-read partition table.
Syncing disks.    同步磁盘

SWAP 的优先级

可以指定swap 分区0 到32767 的优先级,值越大优先级越高

如果用户没有指定,那么核心会自动给swap 指定一个优先级,这个优先级从-1 开始,每加入一个新的没有用户指定优先级的swap ,会给这个优先级减一。

先添加的swap 的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级( 是正数) 永远高于核心缺省指定的优先级( 是负数)。

       优化性能:分布存放,高性能磁盘存放

[root@station29 ~]# mkswap /dev/sdb1 -L swap2  (格式化文件系统)
Setting up swapspace version 1, size = 2104476 KiB
LABEL=swap2, UUID=0c53f5bd-a767-4c19-8962-260758d1c4f8
[root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2: UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc4: UUID="5f4df3af-f8a8-42cb-a391-79974b5c1aa2" TYPE="ext4"
/dev/loop1: SEC_TYPE="msdos" UUID="5926-C9E7" TYPE="vfat"
/dev/loop7: SEC_TYPE="msdos" UUID="5926-C9E7" TYPE="vfat"
/dev/sdb1: LABEL="swap2" UUID="0c53f5bd-a767-4c19-8962-260758d1c4f8" TYPE="swap"
 
[root@station29 ~]# vim /etc/fstab
UUID=53e082c2-f1fc-4b96-aa31-d9071dc6355c /home                   ext4    defaults        1 2
UUID=7a8216c5-6781-4494-b2da-fda672f117ad swap                    swap    defaults        0 0
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
UUID="0c53f5bd-a767-4c19-8962-260758d1c4f8" swap swap   defaults        0 0  (加优先级)
 
[root@station29 ~]# swapon -a    (激活交换空间)
[root@station29 ~]# free
             total       used       free     shared    buffers     cached
Mem:       1906252     368604    1537648       1180      24888     126628
-/+ buffers/cache:     217088    1689164
Swap:      4201624          0    4201624
[root@station29 ~]# swapon -s           (查看)
Filename    Type    Size    Used    Priority
/dev/sda5                               partition    2097148    0    -1    (优先级高)
 
/dev/sdb1                               partition    2104476    0    -2
 
[root@station29 ~]# vim /etc/fstab                                                                                                       
输入   :r!blkid /dev/sda1
proc                    /proc                   proc    defaults        0 0
UUID="0c53f5bd-a767-4c19-8962-260758d1c4f8" swap swap   defaults,pri=100        0 0
 
[root@station29 ~]# vim /etc/fstab
[root@station29 ~]# swapon -s
Filename    Type    Size    Used    Priority
/dev/sda5                               partition    2097148    0    -1
/dev/sdb1                               partition    2104476    0    -2
[root@station29 ~]# swapoff /dev/sdb1 (禁用/dev/sdb1)
[root@station29 ~]# swapon -a
[root@station29 ~]# swapon -s
Filename    Type    Size    Used    Priority
/dev/sda5                               partition    2097148    0    -1
/dev/sdb1                               partition 2104476 0 100

 禁用swap

[root@station29 ~]# swapoff /dev/sdb1
[root@station29 ~]# free
             total       used       free     shared    buffers     cached
Mem:       1906252     367332    1538920       1180      25188     126624
-/+ buffers/cache:     215520    1690732
Swap:      2097148          0    2097148
 
[root@station29 ~]# vim /etc/fstab
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
 
[root@station29 ~]# fdisk /dev/sdb
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): d
Selected partition 1
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
 
[root@station29 ~]# free
             total       used       free     shared    buffers     cached
Mem:       1906252     368000    1538252       1176      25624     127168
-/+ buffers/cache:     215208    1691044
Swap:      2097148          0    2097148
 
root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2: UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc4: UUID="5f4df3af-f8a8-42cb-a391-79974b5c1aa2" TYPE="ext4"
/dev/loop1: SEC_TYPE="msdos" UUID="5926-C9E7" TYPE="vfat"
/dev/loop7: SEC_TYPE="msdos" UUID="5926-C9E7" TYPE="vfat"

没有内存情况下添加文件swap

[root@station29 ~]# dd if=/dev/zero of=/swapfile bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 58.5699 s, 36.7 MB/s
[root@station29 ~]# mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=3af09d92-ec2d-4052-b000-6057b81a6ff8
[root@station29 ~]# blkid /swapfile
/swapfile: UUID="3af09d92-ec2d-4052-b000-6057b81a6ff8" TYPE="swap"
[root@station29 ~]# vim /etc/fstab
proc                    /proc                   proc    defaults        0 0
/swapfile               swap                    swap    defaults        0 0
[root@station29 ~]# swapon -a
[root@station29 ~]# swapon -s    查看
Filename    Type    Size    Used    Priority
/dev/sda5                               partition    2097148    0    -1
/swapfile                               file 2097148 0 -2

删除文件swap

[root@station29 ~]# swapoff /swapfile
[root@station29 ~]# swapon -s
Filename    Type    Size    Used    Priority
/dev/sda5                               partition    2097148    0    -1
[root@station29 ~]# vim /etc/fstab
proc                    /proc                   proc    defaults        0 0
 
[root@station29 ~]# rm -f /swapfile

移动介质

挂载意味着使外来的文件系统看起来如同是主目录树的一部分

访问前、介质必须被挂载

摘除时,介质必须被卸载

按照默认设置,非根用户只能挂载某些设备(光盘、DVD 、软盘、USB 等等)

挂载点通常在/media  或/mnt下

 

挂载USB 介质

查看USB 设备是否识别

lsusb

被内核探测为SCSI 设备

/dev/sdaX 、/dev/sdbX 、或类似的设备文件

在图形环境中自动挂载

图标在[ 计算机] 窗口中创建

挂载在/run/media/<user>/<label>

手动挂载

mount /dev/sdb1 /mnt

插入U盘

[root@station29 ~]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdc      8:32   0    20G  0 disk
├─sdc1   8:33   0     1G  0 part
├─sdc2   8:34   0     2G  0 part
├─sdc3   8:35   0     3G  0 part
└─sdc4   8:36   0 109.8M  0 part
sdd      8:48   1  28.9G  0 disk
└─sdd1   8:49   1  28.9G  0 part
 
[root@station29 ~]# tail /var/log/messages    或从日志中查看
Apr 25 11:53:28 station29 kernel: USB Mass Storage support registered.
Apr 25 11:53:29 station29 kernel: scsi 3:0:0:0: Direct-Access     Kingston DataTraveler 3.0 PMAP PQ: 0 ANSI: 6
Apr 25 11:53:29 station29 kernel: sd 3:0:0:0: Attached scsi generic sg4 type 0
Apr 25 11:53:29 station29 kernel: sd 3:0:0:0: [sdd] 60632064 512-byte logical blocks: (31.0 GB/28.9 GiB)
Apr 25 11:53:29 station29 kernel: sd 3:0:0:0: [sdd] Write Protect is off
[root@station29 ~]# lsusb 查看USB设备是否识别
Bus 001 Device 002: ID 0951:1666 Kingston Technology DataTraveler G4
 
[root@station29 mnt]# mount /dev/sdd1 /mnt/file
[root@station29 mnt]# cd /mnt/file
[root@station29 file]# ls
01??????.pdf  3.22  3.24  3.27  summary  System Volume Information  ??vmware?3?????.pdf
[root@station29 file]# cd
[root@station29 ~]# sync       (强制写磁盘)
[root@station29 ~]# sync
[root@station29 ~]# sync
[root@station29 ~]# umount /mnt/file (取消挂载)

使用光盘

在图形环境下自动启动挂载/run/media/<user>/<label>

否则就必须被手工挂载

mount /dev/cdrom /mnt/

eject 命令卸载或弹出磁盘

创建ISO 文件

cp /dev/cdrom /root/centos7.iso

mkisofs -r -o /root/etc.iso /etc

刻录光盘

wodim –v –eject centos.iso

cdrecord  也可用作刻录光盘

 

生产iso镜像

[root@station29 ~]# cp /dev/sr0 /root/centos6.iso

可将镜像传至U盘

[root@station29 ~]# dd if=centos6.iso of=/dev/sdd

机房服务器过多,且有一台硬盘出现问题时,可用eject弹出光驱,以便查找故障机器

[root@station29 ~]# eject  /dev/  sr0            (弹出光驱)
[root@station29 ~]# eject  -t  /dev/  sr0    (弹入光驱)

常见工具

内存空间使用状态:

free [OPTION]

[root@station29 ~]# free 以K为单位
             total       used       free     shared    buffers     cached
Mem:       1906252     371860    1534392       1172      32788     125292
-/+ buffers/cache:     213780    1692472
Swap:      2097148          0    2097148 

-m:  以MB 为单位

[root@station29 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:       1861        362        1499       1        31        122
-/+ buffers/cache:         208        1653
Swap:       2047         0        2047

-g:  以GB 为单位

[root@station29 ~]# free -g
             total       used       free     shared    buffers     cached
Mem:          1         0          1        0          0          0
-/+ buffers/cache:          0          1
Swap:          1        0          1

文件系统空间占用等信息的查看工具:

df [OPTION]... [FILE]...

-H  以1000 为单位 10次方

[root@station29 ~]# df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda6       178G  4.2G  165G   3% /
tmpfs           976M   74k  976M   1% /dev/shm
/dev/sda1       199M   37M  153M  20% /boot
/dev/sda2        22G   47M   20G   1% /data
/dev/sda3        11G   42M  9.9G   1% /home

-T  文件系统类型

[root@station29 ~]# df -T
Filesystem     Type  1K-blocks    Used Available Use% Mounted on
/dev/sda6      ext4  173059896 4064916 160197336   3% /
tmpfs          tmpfs    953124      72    953052   1% /dev/shm
/dev/sda1      ext4     194241   35220    148781  20% /boot
/dev/sda2      ext4   20511356   45032  19417748   1% /data
/dev/sda3      ext4   10190136   40540   9625308   1% /home

-h: human-readable 人类可读的方式 2次方

[root@station29 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda6       166G  3.9G  153G   3% /
tmpfs           931M   72K  931M   1% /dev/shm
/dev/sda1       190M   35M  146M  20% /boot
/dev/sda2        20G   44M   19G   1% /data
/dev/sda3       9.8G   40M  9.2G   1% /home

-i :inodes instead of blocks

[root@station29 ~]# df -i 查看inode节点使用
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
/dev/sda6      11001856 103315 10898541    1% /
tmpfs            238281      3   238278    1% /dev/shm
/dev/sda1         51200     39    51161    1% /boot
/dev/sda2       1310720     21  1310699    1% /data
/dev/sda3        655360   1063   654297    1% /home
[root@station29 ~]# df -ih
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/sda6         11M  101K   11M    1% /
tmpfs            233K     3  233K    1% /dev/shm
/dev/sda1         50K    39   50K    1% /boot
/dev/sda2        1.3M    21  1.3M    1% /data
/dev/sda3        640K  1.1K  639K    1% /home

-P:  以Posix 兼容的格式输出

[root@station29 ~]# df -P
Filesystem     1024-blocks    Used Available Capacity Mounted on
/dev/sda6        173059896 4064916 160197336       3% /
tmpfs               953124      72    953052       1% /dev/shm
/dev/sda1           194241   35220    148781      20% /boot
/dev/sda2         20511356   45032  19417748       1% /data
/dev/sda3         10190136   40540   9625308       1% /home 

查看某目录总体空间占用状态:每个子文件夹的大小

du [OPTION]... DIR

-h: human-readable

-s: summary 汇总

[root@station29 ~]# du -sh 统计整个目录大小
401M    .

统计根下每个文件夹的大小

[root@station29 ~]# du  -sh  /*
11M    /apps
7.7M    /bin
33M    /boot
60K    /data
336K    /dev
41M    /etc
18M    /home

工具dd

dd 命令:convert and copy a file 转换和复制文件

用法:

dd if=/PATH/FROM/SRC of=/PATH/TO/DEST

[root@station29 ~]# dd if=/etc/issue of=f1
0+1 records in
0+1 records out
47 bytes (47 B) copied, 0.000178632 s, 263 kB/s
[root@station29 ~]# cat f1
CentOS release 6.8 (Final)
Kernel \r on an \m

bs=# :block size,  复制单元大小

count=# :复制多少个bs

of=file 写到所命名的文件而不是到标准输出

if=file 从所命名文件读取而不是从标准输入

[root@station29 ~]# cat a1
abcdef
[root@station29 ~]# cat a2
123456789
[root@station29 ~]# dd if=a1 of=a2    a1中的内容将把a2内容全覆盖
0+1 records in
0+1 records out
7 bytes (7 B) copied, 0.000252869 s, 27.7 kB/s
[root@station29 ~]# cat a2
abcdef

bs=size 指定块大小(既是是ibs 也是obs)

ibs=size 一次读size 个byte

obs=size 一次写size 个byte

cbs=size 一次转化size 个byte

skip=blocks 从开头忽略blocks 个ibs 大小的块

seek=blocks 从开头忽略blocks 个obs 大小的块

count=n 只拷贝n 个记录

[root@station29 ~]# cat a1
abcdef
[root@station29 ~]# cat a2
123456789
[root@station29 ~]# dd if=a1 of=a2 bs=1 count=2 skip=3 seek=4
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000143496 s, 13.9 kB/s
[root@station29 ~]# cat a2
1234de

conv=conversion[,conversion...]  用指定的参数转换文件

转换参数:

ascii 换 转换 EBCDIC 为 为 ASCII

ebcdic 换 转换 ASCII 为 为 EBCDIC

block  转换一行数据为长度为 cbs  的记录,不足部分用空格填充。

unblock 替代cbs 长度的每一行尾的空格为新行

lcase  把大写字符转换为小写字符

ucase  把小写字符转换为大写字符

[root@station29 ~]# cat a1
abcdef
[root@station29 ~]# cat a2
1234de789
[root@station29 ~]# dd if=a1 of=a2 conv=ucase
0+1 records in
0+1 records out
7 bytes (7 B) copied, 0.00020576 s, 34.0 kB/s
[root@station29 ~]# cat a2
ABCDEF 

nocreat  不创建输出文件

noerror  出错时不停止

notrunc  不截短输出文件

[root@station29 ~]# cat a1
abcdef
[root@station29 ~]# cat a2
123456789
[root@station29 ~]# dd if=a1 of=a2 bs=1 count=2 skip=3 seek=4 conv=notrunc
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000428027 s, 4.7 kB/s
[root@station29 ~]# cat a1
abcdef
[root@station29 ~]# cat a2
1234de789

sync  把每个输入块填充到ibs 个字节,不足部分用空(NUL) 字符补齐

 

备份MBR: 

dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1

    破坏MBR 中的bootloader:

dd if=/dev/zero of=/dev/sda bs=64 count=1seek=446

    有一个大与2K 的二进制文件fileA。现在想从第64 个字节位置开始读取,需要读取的大小是128Byts 。又有fileB,  想把上面读取到的128Bytes 写到第32个字节开始的位置,替换128Bytes ,请问如何实现?

#dd if=fileA of=fileB bs=1 count=128 skip=63 seek=31 conv=notrunc

 

备份:

dd if=/dev/sdx of=/dev/sdy

将本地的/dev/sdx 整盘备份到/dev/sdy

dd if=/dev/sdx of=/path/to/image

将/dev/sdx 全盘数据备份到指定路径的image 文件

dd if=/dev/sdx | gzip >/path/to/image.gz

备份/dev/sdx 全盘数据,并利用gzip 工具进行压缩,保存到指定路径

恢复:

dd if=/path/to/image of=/dev/sdx

将备份文件恢复到指定盘

gzip -dc /path/to/image.gz | dd of=/dev/sdx

将压缩的备份文件恢复到指定盘

 

拷贝内存资料到硬盘

dd if=/dev/mem of=/root/mem.bin bs=1024

将内存里的数据拷贝到root 目录下的mem.bin 文件

从光盘拷贝iso 镜像

dd if=/dev/cdrom of=/root/cd.iso

拷贝光盘数据到root 文件夹下,并保存为cd.iso 文件

销毁磁盘数据

dd if=/dev/urandom of=/dev/sda1

利用随机的数据填充硬盘,在某些必要的场合可以用来销毁数据,执行此操作以后,/dev/sda1 将无法挂载,创建和拷贝操作无法执行

 

得到最恰当的block size

dd if=/dev/zero bs=1024 count=1000000

of=/root/1Gb.file

dd if=/dev/zero bs=2048 count=500000

of=/root/1Gb.file

dd if=/dev/zero bs=4096 count=250000

of=/root/1Gb.file

通过比较dd 指令输出中所显示的命令执行时间,即可确定系统最佳的block size

 

测试硬盘读写速度

dd if=/dev/zero of=/root/1Gb.file bs=1024 count=1000000

dd if=/root/1Gb.file bs=64k | dd of=/dev/null

通过上两个命令输出的执行时间,可以计算出测试硬盘的写/读/速度

 

修复硬盘

dd if=/dev/sda of=/dev/sda

当硬盘较长时间(比如1,2 年)放置不使用后,磁盘上会产生消磁点。当磁头读到这些区域时会遇到困难,并可能导致I/O 错误。当这种情况影响到硬盘的第一个扇区时,

可能导致硬盘报废。上边的命令有可能使这些数据起死回生,且这个过程是安全高效的。