Migrate the data in the memory card (including boot record and system partition) that is not fully utilized to a memory card with a smaller capacity (while maintaining the function of the system disk)

Migrate the data in the memory card (including boot record and system partition) that is not fully utilized to a memory card with a smaller capacity (while maintaining the function of the system disk)

Recently, I encountered a demand like the title. There is a 64 GB system microSD card (TF card) of NVIDIA Jetson Nano, which contains 14 GPT partitions, 13 of which are very small and all less than 1 MB, and one partition has a large capacity, with a total capacity of about 32 GB. When using the dd command in Linux to back up the image file obtained by the above TF card to make a new TF card system disk, it is found that:

1. It is impossible for the new TF card to be 32 GB, because the image file has 64 GB, although there is no partition in the second half, but because the GPT backup partition table (Secondary Header) exists in the last sector of the original 64 GB TF card ( Last Sector) is the end of the image file, the GPT backup partition table will be lost, so that the creation of the new system disk will fail;

2. Even if you use a new 64 GB TF card, sometimes the same problem as above will occur because the capacity of the new card is slightly smaller than the capacity of the original card;

3. We had to use a new 128 GB TF card to make a new system disk based on the backup image.

In order to be able to use a 64 GB TF card to create a new system disk, my method is roughly to create the same partition table as the original 64 GB TF card on a 32 GB TF card and partition the original 64 GB card partition by partition. Copy the contents of the 32 GB card to the 32 GB card, and use the backup image obtained from the 32 GB card as the source to create a new system disk. The whole process is divided into 5 steps, which are described in detail below.

1. Use fdisk to view the information of the 32 GB card and the original 64 GB card

Connect both TF cards to Linux PC and execute in the terminal

sudo fdisk -l

Can list the information of all disks connected to the system, and determine the device description files of both according to the capacity. In this example, the device description file of the 32 GB TF card is /dev/sdb, and that of the 64 GB TF card is /dev/sdc. implement

sudo fdisk -l /dev/sdb

Only view the information of the 32 GB TF card, as shown in the figure below

1It can be seen that this is an empty card. implement

sudo fdisk -l /dev/sdc

Only view the information of the 64 GB TF card, as shown in the figure below

2It can be seen that it contains 14 GPT partitions, of which only /dev/sdc1 is a large-capacity partition.

2. Use gparted to shrink the largest partition on the original 64 GB card

In the previous step, we know that the capacity of /dev/sdc1 is 29.7 GiB, which exceeds the total capacity of some 32 GB TF cards, although the capacity of 32 GB TF cards in this example is 29.8 GiB is large enough, but for readers The /dev/sdc1 partition on the undercapacity 32 GB TF card or the original 64 GB card is larger (for example, 48 GiB, and the space occupied by all files in it is still less than 29 GiB). Here I use gparted to shrink /dev /sdc1 partition to 29 GiB. implement

sudo apt install gparted
sudo umount /dev/sdc1
sudo gparted

Install gparted, unmount the /dev/sdc1 partition (otherwise it can only be extended but not reduced) and start gparted. gparted is a GUI program, in which the intuitive operation, the result is as follows

3
gparted mainly does two steps:
1. Call resize2fs to adjust the size of the file system from 29.71 GiB to 29.00 GiB;
2. Adjust the partition capacity to 29.00 GiB. The first step is to change the file allocation table and may adjust the distribution of some files on the partition, that is, to move the content between 29.00 GiB and 29.71 GiB forward. The second step is to manipulate the partition table to set the capacity of /dev/sdc1 to 29.00 GiB. This step can be done manually in fdisk or gdisk (described in the next step).

4
The above figure uses fdisk to list the partitions of the 64 GB card after the partition capacity has been reduced. Compared with Figure 2, it can be seen that the size of /dev/sdc1 has indeed changed.

3. Use gdisk to create the same partition table on the 32 GB card as on the original 64 GB card

install gdisk

sudo apt-get install gdisk

Execute after installing gdisk

sudo gdisk /dev/sdc

Run gdisk interactively on /dev/sdc, the operation process is as shown below (Caution! Secondary header was placed beyond the disk's limits! Moving the header, but other problems may occur! in the figure indicates that the location of the GPT backup partition table has been changed from The 64 GB at the end of the original card is adjusted to the 32 GB at the end of the new card.)

5
Perform a reboot after the above process

sudo reboot

to make the operating system recognize the new partition table. Then we use fdisk to view /dev/sdb again and compare it with Figure 4. It can be seen that the same GPT partition table is established on the 32 GB card /dev/sdb as on the 64 GB card /dev/sdc after the partition capacity is reduced, as shown in the figure below

6

4. Use dd to copy the contents of the original 64 GB card to the 32 GB card partition by partition

Execute the following 14 commands to back up all partitions on the original 64 GB card to files by partition

sudo dd if=/dev/sdc1 status=progress | gzip -c > 1.img.gz
sudo dd if=/dev/sdc14 status=progress | gzip -c > 14.img.gz

The operation process is as follows

7Then execute the following 14 commands to restore the data from the backup file to the corresponding partition on the 32 GB card

gunzip -c 1.img.gz | sudo dd of=/dev/sdb1 status=progress
gunzip -c 1.img.gz | sudo dd of=/dev/sdb1 status=progress

The operation process is as follows

8Then execute partprobe or reboot. So we have created a new 32 GB Jetson Nano system disk

5. Get the backup image based on the 32 GB card and use it to make a new system disk

Next we can execute

sudo dd if=/dev/sdb status=progress | gzip -c > nano_backup.img.gz

Backup the 32 GB system disk to the file nano_backup.img.gz, and then execute

gunzip -c nano_backup.img.gz | sudo dd of=/dev/sdd status=progress

Restore from backup files to future TF cards to make more system disks. At this point we can use a 64 GB TF card instead of having to use a 128 GB TF card.

Guess you like

Origin blog.csdn.net/weixin_44355653/article/details/131847187