使用sgdiske2fsck和resize2fs对分区进行扩容

Posted pengdonglin137

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用sgdiske2fsck和resize2fs对分区进行扩容相关的知识,希望对你有一定的参考价值。

邮箱: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

 

完。

以上是关于使用sgdiske2fsck和resize2fs对分区进行扩容的主要内容,如果未能解决你的问题,请参考以下文章

resize2fs命令使用

resize2fs命令详解

resize2fs

resize2fs:尝试打开时超级块中的魔术数字错误

resize2fs命令

resize2fs: Bad magic number in super-block while trying to open