s-sgdisk源码分析 “--set-alignment=value分区对齐参数”
生活随笔
收集整理的這篇文章主要介紹了
s-sgdisk源码分析 “--set-alignment=value分区对齐参数”
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 邊界對齊子命令使用
- 源碼分析
- sgdisk.cc main函數(shù)入口
- gptcl.cc DoOptions解析并執(zhí)行具體命令函數(shù)
- gpt.cc CreatePartition創(chuàng)建分區(qū)函數(shù),設(shè)置起始扇區(qū)對齊
- gpt.cc Align分區(qū)對齊函數(shù),設(shè)置起始扇區(qū)對齊
sgdisk命令是由
gdisk-0.8.6-4.el7.x86_64程序包安裝
sgdisk源碼路徑 https://github.com/Shihta/gdisk
邊界對齊子命令使用
本節(jié)主要描述sgdisk源碼中關(guān)于命令-a --set-alignment=value的邊界對齊邏輯描述,借此各位可以看到sgdisk命令集的其他參數(shù)源碼白編寫的邏輯
該參數(shù)使用方式一般是在創(chuàng)建分區(qū)過程中使用
sgdisk --set-alignment=8 --new=1:0:2G --mbrtogpt -- /dev/sdb
這個命令是在/dev/sdb上創(chuàng)建一個編號為1,大小為2G的gpt分區(qū),且start_sector是4K對齊的。sgdisk底層做分區(qū)是以扇區(qū)為單位,同時設(shè)置邊界對齊時單位是扇區(qū),默認(rèn)以1M(2048個扇區(qū))為單位進(jìn)行對齊,最小只能設(shè)置8個扇區(qū)對齊
源碼分析
sgdisk.cc main函數(shù)入口
int main(int argc, char *argv[]) {GPTDataCL theGPT;#主要執(zhí)行函數(shù),對參數(shù)進(jìn)行解析以及調(diào)用各個執(zhí)行函數(shù)執(zhí)行具體功能return theGPT.DoOptions(argc, argv);
}
gptcl.cc DoOptions解析并執(zhí)行具體命令函數(shù)
/*
該函數(shù)的返回值如下:
0 :執(zhí)行命令成功
1:執(zhí)行參數(shù)過少
2:讀磁盤分區(qū)表錯誤
3:格式化磁盤分區(qū)格式為gpt時沒有-g參數(shù)
4:無法保存sgdisk對磁盤的操作
8:備份磁盤分區(qū)表失敗,-R參數(shù)failed
*/
int GPTDataCL::DoOptions(int argc, char* argv[]) {...#將參數(shù)長項和短項以及對應(yīng)的描述封裝為結(jié)構(gòu)體struct poptOption theOptions[] ={{"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes","list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},{"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},{"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},{"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},{"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},{"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},{"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},{"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""},{"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""},{"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""},...}...// 確保最后一個參數(shù)一定是設(shè)備名稱(之前會做參數(shù)篩選)device = (char*) poptGetArg(poptCon);poptResetContext(poptCon);//如果設(shè)備名稱不為空的話開始進(jìn)行具體的參數(shù)解析if (device != NULL) {JustLooking(); // reset as necessaryBeQuiet(); // Tell called functions to be less verbose & interactive//加載磁盤分區(qū)表if (LoadPartitions((string) device)) {...//開始進(jìn)行具體的sgdisk參數(shù)解析while ((opt = poptGetNextOpt(poptCon)) > 0) {...//設(shè)置分區(qū)邊界對齊的短項參數(shù) 對應(yīng)--set-alignment=valuecase 'a'://初始化分區(qū)邊界對齊參數(shù) ,會為全局變量 sectorAlignment 賦值SetAlignment(alignment);break;...//創(chuàng)建分區(qū)參數(shù)短項,對應(yīng)--new=num:start_sector:end_sectorcase 'n':JustLooking(0);//獲取分區(qū)編號newPartNum = (int) GetInt(newPartInfo, 1) - 1;//如果分區(qū)編號小于零則選擇第一個沒有被使用的編號(最小的沒有被使用的)if (newPartNum < 0)newPartNum = FindFirstFreePart();//從磁盤的剩余空間中尋找最大的未被分配的塊,取該塊中的第一個可用的扇區(qū)作為起始扇區(qū)配置low = FindFirstInLargest();/*將該扇區(qū)調(diào)整為512字節(jié),因為不通磁盤有自己的扇區(qū)大小設(shè)置,這里會將這種類型的磁盤扇區(qū)大小設(shè)置為操作系統(tǒng)識別的512字節(jié)*/Align(&low);//依據(jù)該塊空間的起始扇區(qū)位置尋找終止扇區(qū)位置high = FindLastInFree(low);//這里做一個轉(zhuǎn)換,將我們創(chuàng)建分區(qū)在start和end處輸入的IEEE-1541-2002 value(K,M,G,T,P,E)轉(zhuǎn)換為扇區(qū)單位startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);//創(chuàng)建分區(qū):成功返回1,失敗返回0if (CreatePartition(newPartNum, startSector, endSector)) {saveData = 1;} else {cerr << "Could not create partition " << newPartNum + 1 << " from "<< startSector << " to " << endSector << "\n";neverSaveData = 1;} // if/else//釋放存放參數(shù)列表的char *空間free(newPartInfo);break;...}...}
gpt.cc CreatePartition創(chuàng)建分區(qū)函數(shù),設(shè)置起始扇區(qū)對齊
uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {int retval = 1; // assume there'll be no problemsuint64_t origSector = startSector;if (IsFreePartNum(partNum)) {//確保起始扇區(qū)是能夠被我們設(shè)置的邊界對齊參數(shù)整除(全局變量sectorAlignment)/*Align(&startSector)函數(shù)操作如下 1. 如果startSector % sectorAlignment == 0那么直接返回成功2. 否則 假設(shè)startSector=2049,sectorAlignment=8該函數(shù)會定位兩個區(qū)間[2048,2049]和[2049-2056],先從該第一個區(qū)間起始扇區(qū)++查看是否未被占用,如未被占用,則用作起始扇區(qū)否則從第二個區(qū)間2056扇區(qū)--查看是否未被占用,如未被占用,則用作起始扇區(qū)詳細(xì)可查看如下Align(&startSector)實現(xiàn)*/if (Align(&startSector)) {cout << "Information: Moved requested sector from " << origSector << " to "<< startSector << " in\norder to align on " << sectorAlignment<< "-sector boundaries.\n";} // ifif (IsFree(startSector) && (startSector <= endSector)) {if (FindLastInFree(startSector) >= endSector) {partitions[partNum].SetFirstLBA(startSector);partitions[partNum].SetLastLBA(endSector);partitions[partNum].SetType(DEFAULT_GPT_TYPE);partitions[partNum].RandomizeUniqueGUID();} else retval = 0; // if free space until endSector} else retval = 0; // if startSector is free} else retval = 0; // if legal partition numberreturn retval;
}
gpt.cc Align分區(qū)對齊函數(shù),設(shè)置起始扇區(qū)對齊
int GPTData::Align(uint64_t* sector) {int retval = 0, sectorOK = 0;uint64_t earlier, later, testSector;if ((*sector % sectorAlignment) != 0) {earlier = (*sector / sectorAlignment) * sectorAlignment;later = earlier + (uint64_t) sectorAlignment;// Check to see that every sector between the earlier one and the// requested one is clear, and that it's not too early....if (earlier >= mainHeader.firstUsableLBA) {sectorOK = 1;testSector = earlier;do {sectorOK = IsFree(testSector++);} while ((sectorOK == 1) && (testSector < *sector));if (sectorOK == 1) {*sector = earlier;retval = 1;} // if} // if firstUsableLBA check// If couldn't move the sector earlier, try to move it later instead....if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {sectorOK = 1;testSector = later;do {sectorOK = IsFree(testSector--);} while ((sectorOK == 1) && (testSector > *sector));if (sectorOK == 1) {*sector = later;retval = 1;} // if} // if} // ifreturn retval;
} // GPTData::Align()
總結(jié)
以上是生活随笔為你收集整理的s-sgdisk源码分析 “--set-alignment=value分区对齐参数”的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大家有没默默诅咒过一个人,然后那个人真的
- 下一篇: 轻微脑出血每天打的是什么针水?贵吗?输液