![如何使用 BASH 脚本开头的分区扇区值 a、b、c 和 d 计算并重新分区三个分区?](https://linux22.com/image/220324/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20BASH%20%E8%84%9A%E6%9C%AC%E5%BC%80%E5%A4%B4%E7%9A%84%E5%88%86%E5%8C%BA%E6%89%87%E5%8C%BA%E5%80%BC%20a%E3%80%81b%E3%80%81c%20%E5%92%8C%20d%20%E8%AE%A1%E7%AE%97%E5%B9%B6%E9%87%8D%E6%96%B0%E5%88%86%E5%8C%BA%E4%B8%89%E4%B8%AA%E5%88%86%E5%8C%BA%EF%BC%9F.png)
假设 Linux 机器上有以下 bash 脚本,开头为:
#!/bin/bash
#This setsetcor script takes four arguments: setsector a b c d
#Both a, b, and c and d are integers
a=$1; #The end sector (in 512 byte sectors) of the sdx1 partition
b=$2; #The end sector (in 512 byte sectors) of the sdx2 partition
c=$3; #The end sector (in 512 byte sectors) of the sdx3 partition
d=$4; #The end sector (in 512 byte sectors) of the free space
我有一个小型 SSD 磁盘驱动器,我使用 dd 实用程序将其完整复制到较大的 SSD 或 HD 驱动器上,以制作初始文件相同的副本较小的驱动器到较大的驱动器。
之后,我想使用脚本中进行的计算来完成以下任务:
- 保持分区 1 和 2 完全相同。
- 使用命令将分区 3 的内容复制
dd
到的结束大磁盘驱动器空间。 - 使用
cfdisk
命令删除分区仅在分区表中,而不在其内容中对于分区 3。 - 展开分区 2,使其末尾位于分区 3 的副本开头之前。
- 使用命令
cfdisk
在磁盘驱动器的最末端以其原始大小重新创建分区 3。 - 使用命令
ntfsresize
扩展分区2。 - 我假设如果正确的话DD使用选项,不需要对驱动器末尾的分区号 3 进行任何 NTFS 大小调整。如果需要的话,我需要帮助来解决这个问题。
- 由于计算所有这些的复杂性,脚本应该打印出必要的步骤。
我继续尝试使用该命令cfdisk
来完成此操作,但仍然没有找到用于输入的正确公式起始分区扇区号和调整分区扇区大小重新创建分区时的值。
有什么建议吗如何使用变量给出的分区扇区末尾值来计算和重新分区三个分区a、b、c 和 d在 bash 脚本的开头?
答案1
下面的代码实现了问题的答案:
#!/bin/bash
#This setsetcor script takes four arguments: setsector a b c d
#Both a, b, and c and d are integers
a=$1; #The end sector (in 512 byte sectors) of the sdx1 partition
b=$2; #The end sector (in 512 byte sectors) of the sdx2 partition
c=$3; #The end sector (in 512 byte sectors) of the sdx3 partition
d=$4; #The end sector (in 512 byte sectors) of the free space
#Input quantities are printed out
printf "The end (in 512 byte sectors) of the sdx1 partition is a=%dS (512-Byte) Sectors.\n" $((a))
printf "The end (in 512 byte sectors) of the sdx2 partition is b=%dS (512-Byte) Sectors.\n" $((b))
printf "The end (in 512 byte sectors) of the sdx3 partition is c=%dS (512-Byte) Sectors.\n" $((c))
printf "The end (in 512 byte sectors) of the free space is d=%dS (512-Byte) Sectors.\n" $((d))
printf "\n"
#Simple derivative quantities are calculated and printed
sizeofp2=$(($b-$a))
sizeofp2GB=$((10**2*($b-$a)*512/1024/1024/1024))
sizeofp3=$(($c-$b));
sizeofp3MB=$((10**2*($c-$b)*512/1024/1024));
freespace=$(($d-$c));
freespaceGB=$((10**2*($d-$c)*512/1024/1024/1024));
printf "The size of Partition 2 equals %3.1fGB and %dS (512-Byte) Sectors. \n" $(($sizeofp2GB))e-2 $sizeofp2
printf "The size of Partition 3 equals %3.1fMB and %dS (512-Byte) Sectors. \n" $(($sizeofp3MB))e-2 $sizeofp3
printf "The Free Space equals %3.2fGB and %dS (512-Byte) Sectors.\n" $(($freespaceGB))e-2 $freespace
printf "\n"
#Calculate the new partition #2 and partition #3, Sectors and MB. Adjustments need to be made in Sectors to avoid rounding.
printf "Delete Partition 3; then delete the Partition 2 (without saving the partition table). \n\n"
printf "A new Partition 2 needs to be formed, (type 7) with the Sector size (of the origninal partition 2 Sectors \n"
printf "plus the original Free Space Sectors). \n\n"
printf "Then Partition 3 (type 27) is formed with its original Sector size, to avoid rounding errors:\n\n"
sizeofnewp2=$(($sizeofp2+$freespace))
sizeofnewp2GB=$((10**2*($sizeofp2+$freespace)*512/1024/1024/1024));
printf "The size of the new partition 2 equals %3.1fGB and %dS (512-Byte) Sectors. \n" $(($sizeofnewp2GB))e-2 $sizeofnewp2
printf "The size of the new partition 3 equals %3.1fMB and %dS (512-Byte) Sectors. \n\n" $(($sizeofp3MB))e-2 $sizeofp3
printf "After all of the ajustements are made (including setting the Partition types properly), save the Partition Table.\n"
在某些情况下,需要稍微减小分区 2 的大小,例如 0.1 GB 左右,以便为原始分区 3 的扇区腾出更多空间。除此之外,它似乎与cfdisk。 (我怀疑是一些舍入误差造成了这个小差异,但现在我有一些有用的东西。另请注意,我打印出了为了方便起见而使用的 cfdisk 分区类型。它应该根据最终用户的情况进行调整应用。)