郵箱:pengdonglin137@163.com
參考:
考慮下面一種場景:
編譯刷機包時,是按照2GB的eMMC制作gpt分區格式的鏡像,但是最終使用的eMMC的大小是4GB,如何讓最后一個分區占滿后面的2GB空間,並且保證分區文件系統原有的內容不丟失呢?
下面在本地模擬一下:
先創建兩個大小都為100MB的空文件origin.disk和empty.disk,其中origin.disk所模擬的就是上面2GB的eMMC,然后對其進行分區,並且創建文件系統,在最后一個分區里新建一個文件,用於驗證內容是否會丟失。接着將origin.disk和empty.disk進行拼接得到new.disk,此時這個new.disk所模擬的就是上面的4GB大小的eMMC。然后對這個new.disk進行處理,使最后一個分區占滿新增加的空間,最后會輸出擴展后的分區中的文件的內容,看是否跟原有文件相同。
下面是測試腳本:
#!/bin/bash # clear rm -rf origin.disk rm -rf new.disk ########################################################################################################### # Create origin.disk: 100MB dd if=/dev/zero of=./origin.disk bs=1024 count=102400 dd if=/dev/zero of=./empty.disk bs=1024 count=102400 # Create GPT and partition sgdisk -n 0:0:+30M -c 1:"1st" ./origin.disk sgdisk -n 0:0:+20M -c 2:"2nd" ./origin.disk sgdisk -n 0:0:0 -c 3:"3rd" ./origin.disk # losetup sudo losetup /dev/loop1 ./origin.disk sudo partprobe /dev/loop1 # Create fs echo "Create file system ..." sudo mkfs.ext4 /dev/loop1p1 sudo mkfs.ext4 /dev/loop1p2 sudo mkfs.ext4 /dev/loop1p3 # Create a file mkdir -p p3 sudo mount -t ext4 /dev/loop1p3 ./p3 sudo chmod 777 p3 sudo echo "Hello world" > ./p3/demo.txt sync df -h | tail -n 1 sudo umount ./p3 # Show Partition sudo sgdisk -p ./origin.disk # or sudo sgdisk -p /dev/loop1 # losetup deattach sudo losetup -d /dev/loop1 ########################################################################################################### # Create New disk echo "Create new disk ..." cat origin.disk empty.disk > new.disk # losetup echo "losetup ..." sudo losetup /dev/loop1 ./new.disk sudo partprobe /dev/loop1 # Move backup gpt to the end echo "Move back up gpt partition ..." sudo sgdisk -e /dev/loop1 # Delete last part echo "Delete last part ..." sudo sgdisk -d 3 /dev/loop1 # Recreate last part echo "Recreate last part ..." sudo sgdisk -n 0:0:0 -c 3:"New 3rd" /dev/loop1 sudo partprobe /dev/loop1 # Check echo "e2fsck -f /dev/loop1p3 ..." sudo e2fsck -f /dev/loop1p3 # Resize echo "resize2fs /dev/loop1p3 ..." sudo resize2fs /dev/loop1p3 # mount echo "mount -t ext4 /dev/loop1p3 ./p3 ..." sudo mount -t ext4 /dev/loop1p3 ./p3 df -h | tail -n 1 cat ./p3/demo.txt # umount echo "umount ..." sudo umount ./p3 echo "losetup detach ..." sudo losetup -d /dev/loop1 # end sudo sgdisk -p ./new.disk echo "End"
下面是輸出log:
1 102400+0 records in 2 102400+0 records out 3 104857600 bytes (105 MB, 100 MiB) copied, 0.299038 s, 351 MB/s 4 102400+0 records in 5 102400+0 records out 6 104857600 bytes (105 MB, 100 MiB) copied, 0.304469 s, 344 MB/s 7 Creating new GPT entries. 8 Setting name! 9 partNum is 0 10 REALLY setting name! 11 Warning: The kernel is still using the old partition table. 12 The new table will be used at the next reboot or after you 13 run partprobe(8) or kpartx(8) 14 The operation has completed successfully. 15 Setting name! 16 partNum is 1 17 REALLY setting name! 18 Warning: The kernel is still using the old partition table. 19 The new table will be used at the next reboot or after you 20 run partprobe(8) or kpartx(8) 21 The operation has completed successfully. 22 Setting name! 23 partNum is 2 24 REALLY setting name! 25 Warning: The kernel is still using the old partition table. 26 The new table will be used at the next reboot or after you 27 run partprobe(8) or kpartx(8) 28 The operation has completed successfully. 29 Create file system ... 30 mke2fs 1.42.13 (17-May-2015) 31 Discarding device blocks: done 32 Creating filesystem with 30720 1k blocks and 7680 inodes 33 Filesystem UUID: b71db7b0-1b0b-4b54-a3b3-4d642d8db8c6 34 Superblock backups stored on blocks: 35 8193, 24577 36 37 Allocating group tables: done 38 Writing inode tables: done 39 Creating journal (1024 blocks): done 40 Writing superblocks and filesystem accounting information: done 41 42 mke2fs 1.42.13 (17-May-2015) 43 Discarding device blocks: done 44 Creating filesystem with 20480 1k blocks and 5136 inodes 45 Filesystem UUID: da901017-8edc-4509-a535-3ce31bbf3af4 46 Superblock backups stored on blocks: 47 8193 48 49 Allocating group tables: done 50 Writing inode tables: done 51 Creating journal (1024 blocks): done 52 Writing superblocks and filesystem accounting information: done 53 54 mke2fs 1.42.13 (17-May-2015) 55 Discarding device blocks: done 56 Creating filesystem with 50156 1k blocks and 12544 inodes 57 Filesystem UUID: 149c00b8-8f2d-49ac-8307-a341e541d738 58 Superblock backups stored on blocks: 59 8193, 24577, 40961 60 61 Allocating group tables: done 62 Writing inode tables: done 63 Creating journal (4096 blocks): done 64 Writing superblocks and filesystem accounting information: done 65 66 /dev/loop1p3 44M 795K 40M 2% /home/peng_dl/work/A15/resize/p3 67 Disk ./origin.disk: 204800 sectors, 100.0 MiB 68 Logical sector size: 512 bytes 69 Disk identifier (GUID): 20C58E58-BC5D-4432-9BB8-B007981C614D 70 Partition table holds up to 128 entries 71 First usable sector is 34, last usable sector is 204766 72 Partitions will be aligned on 2048-sector boundaries 73 Total free space is 2014 sectors (1007.0 KiB) 74 75 Number Start (sector) End (sector) Size Code Name 76 1 2048 63487 30.0 MiB 8300 1st 77 2 63488 104447 20.0 MiB 8300 2nd 78 3 104448 204766 49.0 MiB 8300 3rd 79 Disk /dev/loop1: 204800 sectors, 100.0 MiB 80 Logical sector size: 512 bytes 81 Disk identifier (GUID): 20C58E58-BC5D-4432-9BB8-B007981C614D 82 Partition table holds up to 128 entries 83 First usable sector is 34, last usable sector is 204766 84 Partitions will be aligned on 2048-sector boundaries 85 Total free space is 2014 sectors (1007.0 KiB) 86 87 Number Start (sector) End (sector) Size Code Name 88 1 2048 63487 30.0 MiB 8300 1st 89 2 63488 104447 20.0 MiB 8300 2nd 90 3 104448 204766 49.0 MiB 8300 3rd 91 Create new disk ... 92 losetup ... 93 Warning: Not all of the space available to /dev/loop1 appears to be used, you can fix the GPT to use all of the space (an extra 204800 blocks) or continue with the current setting? 94 Move back up gpt partition ... 95 Warning: The kernel is still using the old partition table. 96 The new table will be used at the next reboot or after you 97 run partprobe(8) or kpartx(8) 98 The operation has completed successfully. 99 Delete last part ... 100 Warning: The kernel is still using the old partition table. 101 The new table will be used at the next reboot or after you 102 run partprobe(8) or kpartx(8) 103 The operation has completed successfully. 104 Recreate last part ... 105 Setting name! 106 partNum is 2 107 REALLY setting name! 108 Warning: The kernel is still using the old partition table. 109 The new table will be used at the next reboot or after you 110 run partprobe(8) or kpartx(8) 111 The operation has completed successfully. 112 e2fsck -f /dev/loop1p3 ... 113 e2fsck 1.42.13 (17-May-2015) 114 Pass 1: Checking inodes, blocks, and sizes 115 Pass 2: Checking directory structure 116 Pass 3: Checking directory connectivity 117 Pass 4: Checking reference counts 118 Pass 5: Checking group summary information 119 /dev/loop1p3: 12/12544 files (0.0% non-contiguous), 6482/50156 blocks 120 resize2fs /dev/loop1p3 ... 121 resize2fs 1.42.13 (17-May-2015) 122 Resizing the filesystem on /dev/loop1p3 to 152556 (1k) blocks. 123 The filesystem on /dev/loop1p3 is now 152556 (1k) blocks long. 124 125 mount -t ext4 /dev/loop1p3 ./p3 ... 126 /dev/loop1p3 141M 1.2M 130M 1% /home/peng_dl/work/A15/resize/p3 127 Hello world 128 umount ... 129 losetup detach ... 130 Disk ./new.disk: 409600 sectors, 200.0 MiB 131 Logical sector size: 512 bytes 132 Disk identifier (GUID): 20C58E58-BC5D-4432-9BB8-B007981C614D 133 Partition table holds up to 128 entries 134 First usable sector is 34, last usable sector is 409566 135 Partitions will be aligned on 2048-sector boundaries 136 Total free space is 2014 sectors (1007.0 KiB) 137 138 Number Start (sector) End (sector) Size Code Name 139 1 2048 63487 30.0 MiB 8300 1st 140 2 63488 104447 20.0 MiB 8300 2nd 141 3 104448 409566 149.0 MiB 8300 New 3rd 142 End
完。