How to use dd command in Linux disk management

  • backup partition table

  The partition table is very important, it is recommended to backup the partition table in the production environment

   Let's take a look at the partition table we want to backup first hexdump -C /dev/sda -n 512

  image.png

     We only backup the first 512 bytes

   Backup the partition table dd if=/dev/sda of=/data/mbr_bak bs=1 count=512 Back up the partition table of sda to mbr_bak of /data/

  image.png

  The backup file cannot be saved in the local machine, because the partition table is damaged and the machine cannot get up, and the backup file cannot be restored in the local machine.

image.png

  So the partition table backup file should exist in a relatively safe place, where we copy the backup file to centos7

image.png

Simulate the destruction of the partition table

    Use the dd command to overwrite the data in the partition table with 0 devices

 image.png 

   At this point the partition table has been destroyed by us

 image.png

 restore partition table

   1. The machine did not restart

    Use fdisk -l and lsblk commands to view partition information

 image.png

 At this time, we can see that there is a partition table in the memory (we destroy the partition table on the hard disk). If the partition table is found to be damaged in time, the machine does not restart; we can directly restore it with the files we backed up.

  Use the dd command to overwrite the first 512 bytes of the data we backed up

 image.png

 Check that the partition table has been restored

 image.png

 2. The machine has been restarted

  After the partition table is damaged, the machine cannot be restarted, and the recovery is performed below

 image.png

 In the BIOS, set the first boot item to CD-ROM

 image.png

 The boot item is set to CD-ROM boot, select item 3, enter rescue mode

image.png

 Enable network optionYes

image.png

 This option Skip

image.png

 Enter rescue mode, first check the IP address, and ping to see if it can connect to the remote host where the partition table backup file is stored

image.png

 Use the scp command to copy the partition table backup file on the remote host to the local

image.png

 Use the dd command to restore

image.png

 reboot after recovery

image.png

 Set the first startup item as the hard disk

image.png

  The system is started, and then check the partition table information

image.png

 Partition table restored

 If there is no network on the server where the backup files are stored, remove the hard disk with the lost data and install it on the machine where the backup partition table is restored.

Note: If the size is the same and the partition strategy is the same, you can use the same partition table to restore

  • Partition table clone

    fdisk -l can check that sda is more than 200 GB; sdb is more than 60 GB

image.png

  Write sda ​​data to sdb

image.png

   The end position of sdb4 is wrong, the total capacity is not that big

image.png

  sdb4 is not displayed because the partition start record of sdb4 is wrong

image.png

  Note: Two hard disks can achieve partition table cloning, the premise: the second hard disk is larger than the first hard disk or has the same capacity

  Only primary partitions can be cloned, but logical partitions cannot be cloned. Because the information of the logical partition is placed in the EBR, and the EBR is in the extended partition, it is not placed in the first MBR

  • delete flag

  Take sdb hard disk as an example

 image.png

 Check first if there is an identifier

 image.png

 clear flag

 image.png

Check the partition table, the flag is 0

image.png

 lsblk checks and finds that there are no partitions; we can also use this method to delete hard disk partitions

image.png

  • There are gaps in the partition space








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324653008&siteId=291194637