qemu-img.c 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621
  1. /*
  2. * QEMU disk image utility
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include <getopt.h>
  26. #include "qemu/help-texts.h"
  27. #include "qemu/qemu-progress.h"
  28. #include "qemu-version.h"
  29. #include "qapi/error.h"
  30. #include "qapi/qapi-commands-block-core.h"
  31. #include "qapi/qapi-visit-block-core.h"
  32. #include "qapi/qobject-output-visitor.h"
  33. #include "qobject/qjson.h"
  34. #include "qobject/qdict.h"
  35. #include "qemu/cutils.h"
  36. #include "qemu/config-file.h"
  37. #include "qemu/option.h"
  38. #include "qemu/error-report.h"
  39. #include "qemu/log.h"
  40. #include "qemu/main-loop.h"
  41. #include "qemu/module.h"
  42. #include "qemu/sockets.h"
  43. #include "qemu/units.h"
  44. #include "qemu/memalign.h"
  45. #include "qom/object_interfaces.h"
  46. #include "system/block-backend.h"
  47. #include "block/block_int.h"
  48. #include "block/blockjob.h"
  49. #include "block/dirty-bitmap.h"
  50. #include "block/qapi.h"
  51. #include "crypto/init.h"
  52. #include "trace/control.h"
  53. #include "qemu/throttle.h"
  54. #include "block/throttle-groups.h"
  55. #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
  56. "\n" QEMU_COPYRIGHT "\n"
  57. typedef struct img_cmd_t {
  58. const char *name;
  59. int (*handler)(int argc, char **argv);
  60. } img_cmd_t;
  61. enum {
  62. OPTION_OUTPUT = 256,
  63. OPTION_BACKING_CHAIN = 257,
  64. OPTION_OBJECT = 258,
  65. OPTION_IMAGE_OPTS = 259,
  66. OPTION_PATTERN = 260,
  67. OPTION_FLUSH_INTERVAL = 261,
  68. OPTION_NO_DRAIN = 262,
  69. OPTION_TARGET_IMAGE_OPTS = 263,
  70. OPTION_SIZE = 264,
  71. OPTION_PREALLOCATION = 265,
  72. OPTION_SHRINK = 266,
  73. OPTION_SALVAGE = 267,
  74. OPTION_TARGET_IS_ZERO = 268,
  75. OPTION_ADD = 269,
  76. OPTION_REMOVE = 270,
  77. OPTION_CLEAR = 271,
  78. OPTION_ENABLE = 272,
  79. OPTION_DISABLE = 273,
  80. OPTION_MERGE = 274,
  81. OPTION_BITMAPS = 275,
  82. OPTION_FORCE = 276,
  83. OPTION_SKIP_BROKEN = 277,
  84. };
  85. typedef enum OutputFormat {
  86. OFORMAT_JSON,
  87. OFORMAT_HUMAN,
  88. } OutputFormat;
  89. /* Default to cache=writeback as data integrity is not important for qemu-img */
  90. #define BDRV_DEFAULT_CACHE "writeback"
  91. static void format_print(void *opaque, const char *name)
  92. {
  93. printf(" %s", name);
  94. }
  95. static G_NORETURN G_GNUC_PRINTF(1, 2)
  96. void error_exit(const char *fmt, ...)
  97. {
  98. va_list ap;
  99. va_start(ap, fmt);
  100. error_vreport(fmt, ap);
  101. va_end(ap);
  102. error_printf("Try 'qemu-img --help' for more information\n");
  103. exit(EXIT_FAILURE);
  104. }
  105. static G_NORETURN
  106. void missing_argument(const char *option)
  107. {
  108. error_exit("missing argument for option '%s'", option);
  109. }
  110. static G_NORETURN
  111. void unrecognized_option(const char *option)
  112. {
  113. error_exit("unrecognized option '%s'", option);
  114. }
  115. /* Please keep in synch with docs/tools/qemu-img.rst */
  116. static G_NORETURN
  117. void help(void)
  118. {
  119. const char *help_msg =
  120. QEMU_IMG_VERSION
  121. "usage: qemu-img [standard options] command [command options]\n"
  122. "QEMU disk image utility\n"
  123. "\n"
  124. " '-h', '--help' display this help and exit\n"
  125. " '-V', '--version' output version information and exit\n"
  126. " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  127. " specify tracing options\n"
  128. "\n"
  129. "Command syntax:\n"
  130. #define DEF(option, callback, arg_string) \
  131. " " arg_string "\n"
  132. #include "qemu-img-cmds.h"
  133. #undef DEF
  134. "\n"
  135. "Command parameters:\n"
  136. " 'filename' is a disk image filename\n"
  137. " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
  138. " manual page for a description of the object properties. The most common\n"
  139. " object type is a 'secret', which is used to supply passwords and/or\n"
  140. " encryption keys.\n"
  141. " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
  142. " 'cache' is the cache mode used to write the output disk image, the valid\n"
  143. " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
  144. " 'directsync' and 'unsafe' (default for convert)\n"
  145. " 'src_cache' is the cache mode used to read input disk images, the valid\n"
  146. " options are the same as for the 'cache' option\n"
  147. " 'size' is the disk image size in bytes. Optional suffixes\n"
  148. " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
  149. " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
  150. " supported. 'b' is ignored.\n"
  151. " 'output_filename' is the destination disk image filename\n"
  152. " 'output_fmt' is the destination format\n"
  153. " 'options' is a comma separated list of format specific options in a\n"
  154. " name=value format. Use -o help for an overview of the options supported by\n"
  155. " the used format\n"
  156. " 'snapshot_param' is param used for internal snapshot, format\n"
  157. " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
  158. " '[ID_OR_NAME]'\n"
  159. " '-c' indicates that target image must be compressed (qcow format only)\n"
  160. " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
  161. " new backing file match exactly. The image doesn't need a working\n"
  162. " backing file before rebasing in this case (useful for renaming the\n"
  163. " backing file). For image creation, allow creating without attempting\n"
  164. " to open the backing file.\n"
  165. " '-h' with or without a command shows this help and lists the supported formats\n"
  166. " '-p' show progress of command (only certain commands)\n"
  167. " '-q' use Quiet mode - do not print any output (except errors)\n"
  168. " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
  169. " contain only zeros for qemu-img to create a sparse image during\n"
  170. " conversion. If the number of bytes is 0, the source will not be scanned for\n"
  171. " unallocated or zero sectors, and the destination image will always be\n"
  172. " fully allocated\n"
  173. " '--output' takes the format in which the output must be done (human or json)\n"
  174. " '-n' skips the target volume creation (useful if the volume is created\n"
  175. " prior to running qemu-img)\n"
  176. "\n"
  177. "Parameters to bitmap subcommand:\n"
  178. " 'bitmap' is the name of the bitmap to manipulate, through one or more\n"
  179. " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
  180. " or '--merge source'\n"
  181. " '-g granularity' sets the granularity for '--add' actions\n"
  182. " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
  183. " bitmaps from an alternative file\n"
  184. "\n"
  185. "Parameters to check subcommand:\n"
  186. " '-r' tries to repair any inconsistencies that are found during the check.\n"
  187. " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
  188. " kinds of errors, with a higher risk of choosing the wrong fix or\n"
  189. " hiding corruption that has already occurred.\n"
  190. "\n"
  191. "Parameters to convert subcommand:\n"
  192. " '--bitmaps' copies all top-level persistent bitmaps to destination\n"
  193. " '-m' specifies how many coroutines work in parallel during the convert\n"
  194. " process (defaults to 8)\n"
  195. " '-W' allow to write to the target out of order rather than sequential\n"
  196. "\n"
  197. "Parameters to snapshot subcommand:\n"
  198. " 'snapshot' is the name of the snapshot to create, apply or delete\n"
  199. " '-a' applies a snapshot (revert disk to saved state)\n"
  200. " '-c' creates a snapshot\n"
  201. " '-d' deletes a snapshot\n"
  202. " '-l' lists all snapshots in the given image\n"
  203. "\n"
  204. "Parameters to compare subcommand:\n"
  205. " '-f' first image format\n"
  206. " '-F' second image format\n"
  207. " '-s' run in Strict mode - fail on different image size or sector allocation\n"
  208. "\n"
  209. "Parameters to dd subcommand:\n"
  210. " 'bs=BYTES' read and write up to BYTES bytes at a time "
  211. "(default: 512)\n"
  212. " 'count=N' copy only N input blocks\n"
  213. " 'if=FILE' read from FILE\n"
  214. " 'of=FILE' write to FILE\n"
  215. " 'skip=N' skip N bs-sized blocks at the start of input\n";
  216. printf("%s\nSupported formats:", help_msg);
  217. bdrv_iterate_format(format_print, NULL, false);
  218. printf("\n\n" QEMU_HELP_BOTTOM "\n");
  219. exit(EXIT_SUCCESS);
  220. }
  221. /*
  222. * Is @list safe for accumulate_options()?
  223. * It is when multiple of them can be joined together separated by ','.
  224. * To make that work, @list must not start with ',' (or else a
  225. * separating ',' preceding it gets escaped), and it must not end with
  226. * an odd number of ',' (or else a separating ',' following it gets
  227. * escaped), or be empty (or else a separating ',' preceding it can
  228. * escape a separating ',' following it).
  229. *
  230. */
  231. static bool is_valid_option_list(const char *list)
  232. {
  233. size_t len = strlen(list);
  234. size_t i;
  235. if (!list[0] || list[0] == ',') {
  236. return false;
  237. }
  238. for (i = len; i > 0 && list[i - 1] == ','; i--) {
  239. }
  240. if ((len - i) % 2) {
  241. return false;
  242. }
  243. return true;
  244. }
  245. static int accumulate_options(char **options, char *list)
  246. {
  247. char *new_options;
  248. if (!is_valid_option_list(list)) {
  249. error_report("Invalid option list: %s", list);
  250. return -1;
  251. }
  252. if (!*options) {
  253. *options = g_strdup(list);
  254. } else {
  255. new_options = g_strdup_printf("%s,%s", *options, list);
  256. g_free(*options);
  257. *options = new_options;
  258. }
  259. return 0;
  260. }
  261. static QemuOptsList qemu_source_opts = {
  262. .name = "source",
  263. .implied_opt_name = "file",
  264. .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
  265. .desc = {
  266. { }
  267. },
  268. };
  269. static int G_GNUC_PRINTF(2, 3) qprintf(bool quiet, const char *fmt, ...)
  270. {
  271. int ret = 0;
  272. if (!quiet) {
  273. va_list args;
  274. va_start(args, fmt);
  275. ret = vprintf(fmt, args);
  276. va_end(args);
  277. }
  278. return ret;
  279. }
  280. static int print_block_option_help(const char *filename, const char *fmt)
  281. {
  282. BlockDriver *drv, *proto_drv;
  283. QemuOptsList *create_opts = NULL;
  284. Error *local_err = NULL;
  285. /* Find driver and parse its options */
  286. drv = bdrv_find_format(fmt);
  287. if (!drv) {
  288. error_report("Unknown file format '%s'", fmt);
  289. return 1;
  290. }
  291. if (!drv->create_opts) {
  292. error_report("Format driver '%s' does not support image creation", fmt);
  293. return 1;
  294. }
  295. create_opts = qemu_opts_append(create_opts, drv->create_opts);
  296. if (filename) {
  297. proto_drv = bdrv_find_protocol(filename, true, &local_err);
  298. if (!proto_drv) {
  299. error_report_err(local_err);
  300. qemu_opts_free(create_opts);
  301. return 1;
  302. }
  303. if (!proto_drv->create_opts) {
  304. error_report("Protocol driver '%s' does not support image creation",
  305. proto_drv->format_name);
  306. qemu_opts_free(create_opts);
  307. return 1;
  308. }
  309. create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
  310. }
  311. if (filename) {
  312. printf("Supported options:\n");
  313. } else {
  314. printf("Supported %s options:\n", fmt);
  315. }
  316. qemu_opts_print_help(create_opts, false);
  317. qemu_opts_free(create_opts);
  318. if (!filename) {
  319. printf("\n"
  320. "The protocol level may support further options.\n"
  321. "Specify the target filename to include those options.\n");
  322. }
  323. return 0;
  324. }
  325. static BlockBackend *img_open_opts(const char *optstr,
  326. QemuOpts *opts, int flags, bool writethrough,
  327. bool quiet, bool force_share)
  328. {
  329. QDict *options;
  330. Error *local_err = NULL;
  331. BlockBackend *blk;
  332. options = qemu_opts_to_qdict(opts, NULL);
  333. if (force_share) {
  334. if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
  335. && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
  336. error_report("--force-share/-U conflicts with image options");
  337. qobject_unref(options);
  338. return NULL;
  339. }
  340. qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
  341. }
  342. blk = blk_new_open(NULL, NULL, options, flags, &local_err);
  343. if (!blk) {
  344. error_reportf_err(local_err, "Could not open '%s': ", optstr);
  345. return NULL;
  346. }
  347. blk_set_enable_write_cache(blk, !writethrough);
  348. return blk;
  349. }
  350. static BlockBackend *img_open_file(const char *filename,
  351. QDict *options,
  352. const char *fmt, int flags,
  353. bool writethrough, bool quiet,
  354. bool force_share)
  355. {
  356. BlockBackend *blk;
  357. Error *local_err = NULL;
  358. if (!options) {
  359. options = qdict_new();
  360. }
  361. if (fmt) {
  362. qdict_put_str(options, "driver", fmt);
  363. }
  364. if (force_share) {
  365. qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
  366. }
  367. blk = blk_new_open(filename, NULL, options, flags, &local_err);
  368. if (!blk) {
  369. error_reportf_err(local_err, "Could not open '%s': ", filename);
  370. return NULL;
  371. }
  372. blk_set_enable_write_cache(blk, !writethrough);
  373. return blk;
  374. }
  375. static int img_add_key_secrets(void *opaque,
  376. const char *name, const char *value,
  377. Error **errp)
  378. {
  379. QDict *options = opaque;
  380. if (g_str_has_suffix(name, "key-secret")) {
  381. qdict_put_str(options, name, value);
  382. }
  383. return 0;
  384. }
  385. static BlockBackend *img_open(bool image_opts,
  386. const char *filename,
  387. const char *fmt, int flags, bool writethrough,
  388. bool quiet, bool force_share)
  389. {
  390. BlockBackend *blk;
  391. if (image_opts) {
  392. QemuOpts *opts;
  393. if (fmt) {
  394. error_report("--image-opts and --format are mutually exclusive");
  395. return NULL;
  396. }
  397. opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
  398. filename, true);
  399. if (!opts) {
  400. return NULL;
  401. }
  402. blk = img_open_opts(filename, opts, flags, writethrough, quiet,
  403. force_share);
  404. } else {
  405. blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
  406. force_share);
  407. }
  408. if (blk) {
  409. blk_set_force_allow_inactivate(blk);
  410. }
  411. return blk;
  412. }
  413. static int add_old_style_options(const char *fmt, QemuOpts *opts,
  414. const char *base_filename,
  415. const char *base_fmt)
  416. {
  417. if (base_filename) {
  418. if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename,
  419. NULL)) {
  420. error_report("Backing file not supported for file format '%s'",
  421. fmt);
  422. return -1;
  423. }
  424. }
  425. if (base_fmt) {
  426. if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) {
  427. error_report("Backing file format not supported for file "
  428. "format '%s'", fmt);
  429. return -1;
  430. }
  431. }
  432. return 0;
  433. }
  434. static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
  435. int64_t max)
  436. {
  437. int err;
  438. uint64_t res;
  439. err = qemu_strtosz(value, NULL, &res);
  440. if (err < 0 && err != -ERANGE) {
  441. error_report("Invalid %s specified. You may use "
  442. "k, M, G, T, P or E suffixes for", name);
  443. error_report("kilobytes, megabytes, gigabytes, terabytes, "
  444. "petabytes and exabytes.");
  445. return err;
  446. }
  447. if (err == -ERANGE || res > max || res < min) {
  448. error_report("Invalid %s specified. Must be between %" PRId64
  449. " and %" PRId64 ".", name, min, max);
  450. return -ERANGE;
  451. }
  452. return res;
  453. }
  454. static int64_t cvtnum(const char *name, const char *value)
  455. {
  456. return cvtnum_full(name, value, 0, INT64_MAX);
  457. }
  458. static int img_create(int argc, char **argv)
  459. {
  460. int c;
  461. uint64_t img_size = -1;
  462. const char *fmt = "raw";
  463. const char *base_fmt = NULL;
  464. const char *filename;
  465. const char *base_filename = NULL;
  466. char *options = NULL;
  467. Error *local_err = NULL;
  468. bool quiet = false;
  469. int flags = 0;
  470. for(;;) {
  471. static const struct option long_options[] = {
  472. {"help", no_argument, 0, 'h'},
  473. {"object", required_argument, 0, OPTION_OBJECT},
  474. {0, 0, 0, 0}
  475. };
  476. c = getopt_long(argc, argv, ":F:b:f:ho:qu",
  477. long_options, NULL);
  478. if (c == -1) {
  479. break;
  480. }
  481. switch(c) {
  482. case ':':
  483. missing_argument(argv[optind - 1]);
  484. break;
  485. case '?':
  486. unrecognized_option(argv[optind - 1]);
  487. break;
  488. case 'h':
  489. help();
  490. break;
  491. case 'F':
  492. base_fmt = optarg;
  493. break;
  494. case 'b':
  495. base_filename = optarg;
  496. break;
  497. case 'f':
  498. fmt = optarg;
  499. break;
  500. case 'o':
  501. if (accumulate_options(&options, optarg) < 0) {
  502. goto fail;
  503. }
  504. break;
  505. case 'q':
  506. quiet = true;
  507. break;
  508. case 'u':
  509. flags |= BDRV_O_NO_BACKING;
  510. break;
  511. case OPTION_OBJECT:
  512. user_creatable_process_cmdline(optarg);
  513. break;
  514. }
  515. }
  516. /* Get the filename */
  517. filename = (optind < argc) ? argv[optind] : NULL;
  518. if (options && has_help_option(options)) {
  519. g_free(options);
  520. return print_block_option_help(filename, fmt);
  521. }
  522. if (optind >= argc) {
  523. error_exit("Expecting image file name");
  524. }
  525. optind++;
  526. /* Get image size, if specified */
  527. if (optind < argc) {
  528. int64_t sval;
  529. sval = cvtnum("image size", argv[optind++]);
  530. if (sval < 0) {
  531. goto fail;
  532. }
  533. img_size = (uint64_t)sval;
  534. }
  535. if (optind != argc) {
  536. error_exit("Unexpected argument: %s", argv[optind]);
  537. }
  538. bdrv_img_create(filename, fmt, base_filename, base_fmt,
  539. options, img_size, flags, quiet, &local_err);
  540. if (local_err) {
  541. error_reportf_err(local_err, "%s: ", filename);
  542. goto fail;
  543. }
  544. g_free(options);
  545. return 0;
  546. fail:
  547. g_free(options);
  548. return 1;
  549. }
  550. static void dump_json_image_check(ImageCheck *check, bool quiet)
  551. {
  552. GString *str;
  553. QObject *obj;
  554. Visitor *v = qobject_output_visitor_new(&obj);
  555. visit_type_ImageCheck(v, NULL, &check, &error_abort);
  556. visit_complete(v, &obj);
  557. str = qobject_to_json_pretty(obj, true);
  558. assert(str != NULL);
  559. qprintf(quiet, "%s\n", str->str);
  560. qobject_unref(obj);
  561. visit_free(v);
  562. g_string_free(str, true);
  563. }
  564. static void dump_human_image_check(ImageCheck *check, bool quiet)
  565. {
  566. if (!(check->corruptions || check->leaks || check->check_errors)) {
  567. qprintf(quiet, "No errors were found on the image.\n");
  568. } else {
  569. if (check->corruptions) {
  570. qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
  571. "Data may be corrupted, or further writes to the image "
  572. "may corrupt it.\n",
  573. check->corruptions);
  574. }
  575. if (check->leaks) {
  576. qprintf(quiet,
  577. "\n%" PRId64 " leaked clusters were found on the image.\n"
  578. "This means waste of disk space, but no harm to data.\n",
  579. check->leaks);
  580. }
  581. if (check->check_errors) {
  582. qprintf(quiet,
  583. "\n%" PRId64
  584. " internal errors have occurred during the check.\n",
  585. check->check_errors);
  586. }
  587. }
  588. if (check->total_clusters != 0 && check->allocated_clusters != 0) {
  589. qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
  590. "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
  591. check->allocated_clusters, check->total_clusters,
  592. check->allocated_clusters * 100.0 / check->total_clusters,
  593. check->fragmented_clusters * 100.0 / check->allocated_clusters,
  594. check->compressed_clusters * 100.0 /
  595. check->allocated_clusters);
  596. }
  597. if (check->image_end_offset) {
  598. qprintf(quiet,
  599. "Image end offset: %" PRId64 "\n", check->image_end_offset);
  600. }
  601. }
  602. static int collect_image_check(BlockDriverState *bs,
  603. ImageCheck *check,
  604. const char *filename,
  605. const char *fmt,
  606. int fix)
  607. {
  608. int ret;
  609. BdrvCheckResult result;
  610. ret = bdrv_check(bs, &result, fix);
  611. if (ret < 0) {
  612. return ret;
  613. }
  614. check->filename = g_strdup(filename);
  615. check->format = g_strdup(bdrv_get_format_name(bs));
  616. check->check_errors = result.check_errors;
  617. check->corruptions = result.corruptions;
  618. check->has_corruptions = result.corruptions != 0;
  619. check->leaks = result.leaks;
  620. check->has_leaks = result.leaks != 0;
  621. check->corruptions_fixed = result.corruptions_fixed;
  622. check->has_corruptions_fixed = result.corruptions_fixed != 0;
  623. check->leaks_fixed = result.leaks_fixed;
  624. check->has_leaks_fixed = result.leaks_fixed != 0;
  625. check->image_end_offset = result.image_end_offset;
  626. check->has_image_end_offset = result.image_end_offset != 0;
  627. check->total_clusters = result.bfi.total_clusters;
  628. check->has_total_clusters = result.bfi.total_clusters != 0;
  629. check->allocated_clusters = result.bfi.allocated_clusters;
  630. check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
  631. check->fragmented_clusters = result.bfi.fragmented_clusters;
  632. check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
  633. check->compressed_clusters = result.bfi.compressed_clusters;
  634. check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
  635. return 0;
  636. }
  637. /*
  638. * Checks an image for consistency. Exit codes:
  639. *
  640. * 0 - Check completed, image is good
  641. * 1 - Check not completed because of internal errors
  642. * 2 - Check completed, image is corrupted
  643. * 3 - Check completed, image has leaked clusters, but is good otherwise
  644. * 63 - Checks are not supported by the image format
  645. */
  646. static int img_check(int argc, char **argv)
  647. {
  648. int c, ret;
  649. OutputFormat output_format = OFORMAT_HUMAN;
  650. const char *filename, *fmt, *output, *cache;
  651. BlockBackend *blk;
  652. BlockDriverState *bs;
  653. int fix = 0;
  654. int flags = BDRV_O_CHECK;
  655. bool writethrough;
  656. ImageCheck *check;
  657. bool quiet = false;
  658. bool image_opts = false;
  659. bool force_share = false;
  660. fmt = NULL;
  661. output = NULL;
  662. cache = BDRV_DEFAULT_CACHE;
  663. for(;;) {
  664. int option_index = 0;
  665. static const struct option long_options[] = {
  666. {"help", no_argument, 0, 'h'},
  667. {"format", required_argument, 0, 'f'},
  668. {"repair", required_argument, 0, 'r'},
  669. {"output", required_argument, 0, OPTION_OUTPUT},
  670. {"object", required_argument, 0, OPTION_OBJECT},
  671. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  672. {"force-share", no_argument, 0, 'U'},
  673. {0, 0, 0, 0}
  674. };
  675. c = getopt_long(argc, argv, ":hf:r:T:qU",
  676. long_options, &option_index);
  677. if (c == -1) {
  678. break;
  679. }
  680. switch(c) {
  681. case ':':
  682. missing_argument(argv[optind - 1]);
  683. break;
  684. case '?':
  685. unrecognized_option(argv[optind - 1]);
  686. break;
  687. case 'h':
  688. help();
  689. break;
  690. case 'f':
  691. fmt = optarg;
  692. break;
  693. case 'r':
  694. flags |= BDRV_O_RDWR;
  695. if (!strcmp(optarg, "leaks")) {
  696. fix = BDRV_FIX_LEAKS;
  697. } else if (!strcmp(optarg, "all")) {
  698. fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
  699. } else {
  700. error_exit("Unknown option value for -r "
  701. "(expecting 'leaks' or 'all'): %s", optarg);
  702. }
  703. break;
  704. case OPTION_OUTPUT:
  705. output = optarg;
  706. break;
  707. case 'T':
  708. cache = optarg;
  709. break;
  710. case 'q':
  711. quiet = true;
  712. break;
  713. case 'U':
  714. force_share = true;
  715. break;
  716. case OPTION_OBJECT:
  717. user_creatable_process_cmdline(optarg);
  718. break;
  719. case OPTION_IMAGE_OPTS:
  720. image_opts = true;
  721. break;
  722. }
  723. }
  724. if (optind != argc - 1) {
  725. error_exit("Expecting one image file name");
  726. }
  727. filename = argv[optind++];
  728. if (output && !strcmp(output, "json")) {
  729. output_format = OFORMAT_JSON;
  730. } else if (output && !strcmp(output, "human")) {
  731. output_format = OFORMAT_HUMAN;
  732. } else if (output) {
  733. error_report("--output must be used with human or json as argument.");
  734. return 1;
  735. }
  736. ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
  737. if (ret < 0) {
  738. error_report("Invalid source cache option: %s", cache);
  739. return 1;
  740. }
  741. blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
  742. force_share);
  743. if (!blk) {
  744. return 1;
  745. }
  746. bs = blk_bs(blk);
  747. check = g_new0(ImageCheck, 1);
  748. ret = collect_image_check(bs, check, filename, fmt, fix);
  749. if (ret == -ENOTSUP) {
  750. error_report("This image format does not support checks");
  751. ret = 63;
  752. goto fail;
  753. }
  754. if (check->corruptions_fixed || check->leaks_fixed) {
  755. int corruptions_fixed, leaks_fixed;
  756. bool has_leaks_fixed, has_corruptions_fixed;
  757. leaks_fixed = check->leaks_fixed;
  758. has_leaks_fixed = check->has_leaks_fixed;
  759. corruptions_fixed = check->corruptions_fixed;
  760. has_corruptions_fixed = check->has_corruptions_fixed;
  761. if (output_format == OFORMAT_HUMAN) {
  762. qprintf(quiet,
  763. "The following inconsistencies were found and repaired:\n\n"
  764. " %" PRId64 " leaked clusters\n"
  765. " %" PRId64 " corruptions\n\n"
  766. "Double checking the fixed image now...\n",
  767. check->leaks_fixed,
  768. check->corruptions_fixed);
  769. }
  770. qapi_free_ImageCheck(check);
  771. check = g_new0(ImageCheck, 1);
  772. ret = collect_image_check(bs, check, filename, fmt, 0);
  773. check->leaks_fixed = leaks_fixed;
  774. check->has_leaks_fixed = has_leaks_fixed;
  775. check->corruptions_fixed = corruptions_fixed;
  776. check->has_corruptions_fixed = has_corruptions_fixed;
  777. }
  778. if (!ret) {
  779. switch (output_format) {
  780. case OFORMAT_HUMAN:
  781. dump_human_image_check(check, quiet);
  782. break;
  783. case OFORMAT_JSON:
  784. dump_json_image_check(check, quiet);
  785. break;
  786. }
  787. }
  788. if (ret || check->check_errors) {
  789. if (ret) {
  790. error_report("Check failed: %s", strerror(-ret));
  791. } else {
  792. error_report("Check failed");
  793. }
  794. ret = 1;
  795. goto fail;
  796. }
  797. if (check->corruptions) {
  798. ret = 2;
  799. } else if (check->leaks) {
  800. ret = 3;
  801. } else {
  802. ret = 0;
  803. }
  804. fail:
  805. qapi_free_ImageCheck(check);
  806. blk_unref(blk);
  807. return ret;
  808. }
  809. typedef struct CommonBlockJobCBInfo {
  810. BlockDriverState *bs;
  811. Error **errp;
  812. } CommonBlockJobCBInfo;
  813. static void common_block_job_cb(void *opaque, int ret)
  814. {
  815. CommonBlockJobCBInfo *cbi = opaque;
  816. if (ret < 0) {
  817. error_setg_errno(cbi->errp, -ret, "Block job failed");
  818. }
  819. }
  820. static void run_block_job(BlockJob *job, Error **errp)
  821. {
  822. uint64_t progress_current, progress_total;
  823. AioContext *aio_context = block_job_get_aio_context(job);
  824. int ret = 0;
  825. job_lock();
  826. job_ref_locked(&job->job);
  827. do {
  828. float progress = 0.0f;
  829. job_unlock();
  830. aio_poll(aio_context, true);
  831. progress_get_snapshot(&job->job.progress, &progress_current,
  832. &progress_total);
  833. if (progress_total) {
  834. progress = (float)progress_current / progress_total * 100.f;
  835. }
  836. qemu_progress_print(progress, 0);
  837. job_lock();
  838. } while (!job_is_ready_locked(&job->job) &&
  839. !job_is_completed_locked(&job->job));
  840. if (!job_is_completed_locked(&job->job)) {
  841. ret = job_complete_sync_locked(&job->job, errp);
  842. } else {
  843. ret = job->job.ret;
  844. }
  845. job_unref_locked(&job->job);
  846. job_unlock();
  847. /* publish completion progress only when success */
  848. if (!ret) {
  849. qemu_progress_print(100.f, 0);
  850. }
  851. }
  852. static int img_commit(int argc, char **argv)
  853. {
  854. int c, ret, flags;
  855. const char *filename, *fmt, *cache, *base;
  856. BlockBackend *blk;
  857. BlockDriverState *bs, *base_bs;
  858. BlockJob *job;
  859. bool progress = false, quiet = false, drop = false;
  860. bool writethrough;
  861. Error *local_err = NULL;
  862. CommonBlockJobCBInfo cbi;
  863. bool image_opts = false;
  864. int64_t rate_limit = 0;
  865. fmt = NULL;
  866. cache = BDRV_DEFAULT_CACHE;
  867. base = NULL;
  868. for(;;) {
  869. static const struct option long_options[] = {
  870. {"help", no_argument, 0, 'h'},
  871. {"object", required_argument, 0, OPTION_OBJECT},
  872. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  873. {0, 0, 0, 0}
  874. };
  875. c = getopt_long(argc, argv, ":f:ht:b:dpqr:",
  876. long_options, NULL);
  877. if (c == -1) {
  878. break;
  879. }
  880. switch(c) {
  881. case ':':
  882. missing_argument(argv[optind - 1]);
  883. break;
  884. case '?':
  885. unrecognized_option(argv[optind - 1]);
  886. break;
  887. case 'h':
  888. help();
  889. break;
  890. case 'f':
  891. fmt = optarg;
  892. break;
  893. case 't':
  894. cache = optarg;
  895. break;
  896. case 'b':
  897. base = optarg;
  898. /* -b implies -d */
  899. drop = true;
  900. break;
  901. case 'd':
  902. drop = true;
  903. break;
  904. case 'p':
  905. progress = true;
  906. break;
  907. case 'q':
  908. quiet = true;
  909. break;
  910. case 'r':
  911. rate_limit = cvtnum("rate limit", optarg);
  912. if (rate_limit < 0) {
  913. return 1;
  914. }
  915. break;
  916. case OPTION_OBJECT:
  917. user_creatable_process_cmdline(optarg);
  918. break;
  919. case OPTION_IMAGE_OPTS:
  920. image_opts = true;
  921. break;
  922. }
  923. }
  924. /* Progress is not shown in Quiet mode */
  925. if (quiet) {
  926. progress = false;
  927. }
  928. if (optind != argc - 1) {
  929. error_exit("Expecting one image file name");
  930. }
  931. filename = argv[optind++];
  932. flags = BDRV_O_RDWR | BDRV_O_UNMAP;
  933. ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
  934. if (ret < 0) {
  935. error_report("Invalid cache option: %s", cache);
  936. return 1;
  937. }
  938. blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
  939. false);
  940. if (!blk) {
  941. return 1;
  942. }
  943. bs = blk_bs(blk);
  944. qemu_progress_init(progress, 1.f);
  945. qemu_progress_print(0.f, 100);
  946. bdrv_graph_rdlock_main_loop();
  947. if (base) {
  948. base_bs = bdrv_find_backing_image(bs, base);
  949. if (!base_bs) {
  950. error_setg(&local_err,
  951. "Did not find '%s' in the backing chain of '%s'",
  952. base, filename);
  953. bdrv_graph_rdunlock_main_loop();
  954. goto done;
  955. }
  956. } else {
  957. /* This is different from QMP, which by default uses the deepest file in
  958. * the backing chain (i.e., the very base); however, the traditional
  959. * behavior of qemu-img commit is using the immediate backing file. */
  960. base_bs = bdrv_backing_chain_next(bs);
  961. if (!base_bs) {
  962. error_setg(&local_err, "Image does not have a backing file");
  963. bdrv_graph_rdunlock_main_loop();
  964. goto done;
  965. }
  966. }
  967. bdrv_graph_rdunlock_main_loop();
  968. cbi = (CommonBlockJobCBInfo){
  969. .errp = &local_err,
  970. .bs = bs,
  971. };
  972. commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit,
  973. BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
  974. &cbi, false, &local_err);
  975. if (local_err) {
  976. goto done;
  977. }
  978. /* When the block job completes, the BlockBackend reference will point to
  979. * the old backing file. In order to avoid that the top image is already
  980. * deleted, so we can still empty it afterwards, increment the reference
  981. * counter here preemptively. */
  982. if (!drop) {
  983. bdrv_ref(bs);
  984. }
  985. job = block_job_get("commit");
  986. assert(job);
  987. run_block_job(job, &local_err);
  988. if (local_err) {
  989. goto unref_backing;
  990. }
  991. if (!drop) {
  992. BlockBackend *old_backing_blk;
  993. old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL,
  994. &local_err);
  995. if (!old_backing_blk) {
  996. goto unref_backing;
  997. }
  998. ret = blk_make_empty(old_backing_blk, &local_err);
  999. blk_unref(old_backing_blk);
  1000. if (ret == -ENOTSUP) {
  1001. error_free(local_err);
  1002. local_err = NULL;
  1003. } else if (ret < 0) {
  1004. goto unref_backing;
  1005. }
  1006. }
  1007. unref_backing:
  1008. if (!drop) {
  1009. bdrv_unref(bs);
  1010. }
  1011. done:
  1012. qemu_progress_end();
  1013. /*
  1014. * Manually inactivate the image first because this way we can know whether
  1015. * an error occurred. blk_unref() doesn't tell us about failures.
  1016. */
  1017. ret = bdrv_inactivate_all();
  1018. if (ret < 0 && !local_err) {
  1019. error_setg_errno(&local_err, -ret, "Error while closing the image");
  1020. }
  1021. blk_unref(blk);
  1022. if (local_err) {
  1023. error_report_err(local_err);
  1024. return 1;
  1025. }
  1026. qprintf(quiet, "Image committed.\n");
  1027. return 0;
  1028. }
  1029. /*
  1030. * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
  1031. * of the first sector boundary within buf where the sector contains a
  1032. * non-zero byte. This function is robust to a buffer that is not
  1033. * sector-aligned.
  1034. */
  1035. static int64_t find_nonzero(const uint8_t *buf, int64_t n)
  1036. {
  1037. int64_t i;
  1038. int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
  1039. for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
  1040. if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
  1041. return i;
  1042. }
  1043. }
  1044. if (i < n && !buffer_is_zero(buf + i, n - end)) {
  1045. return i;
  1046. }
  1047. return -1;
  1048. }
  1049. /*
  1050. * Returns true iff the first sector pointed to by 'buf' contains at least
  1051. * a non-NUL byte.
  1052. *
  1053. * 'pnum' is set to the number of sectors (including and immediately following
  1054. * the first one) that are known to be in the same allocated/unallocated state.
  1055. * The function will try to align the end offset to alignment boundaries so
  1056. * that the request will at least end aligned and consecutive requests will
  1057. * also start at an aligned offset.
  1058. */
  1059. static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum,
  1060. int64_t sector_num, int alignment)
  1061. {
  1062. bool is_zero;
  1063. int i, tail;
  1064. if (n <= 0) {
  1065. *pnum = 0;
  1066. return 0;
  1067. }
  1068. is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE);
  1069. for(i = 1; i < n; i++) {
  1070. buf += BDRV_SECTOR_SIZE;
  1071. if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) {
  1072. break;
  1073. }
  1074. }
  1075. if (i == n) {
  1076. /*
  1077. * The whole buf is the same.
  1078. * No reason to split it into chunks, so return now.
  1079. */
  1080. *pnum = i;
  1081. return !is_zero;
  1082. }
  1083. tail = (sector_num + i) & (alignment - 1);
  1084. if (tail) {
  1085. if (is_zero && i <= tail) {
  1086. /*
  1087. * For sure next sector after i is data, and it will rewrite this
  1088. * tail anyway due to RMW. So, let's just write data now.
  1089. */
  1090. is_zero = false;
  1091. }
  1092. if (!is_zero) {
  1093. /* If possible, align up end offset of allocated areas. */
  1094. i += alignment - tail;
  1095. i = MIN(i, n);
  1096. } else {
  1097. /*
  1098. * For sure next sector after i is data, and it will rewrite this
  1099. * tail anyway due to RMW. Better is avoid RMW and write zeroes up
  1100. * to aligned bound.
  1101. */
  1102. i -= tail;
  1103. }
  1104. }
  1105. *pnum = i;
  1106. return !is_zero;
  1107. }
  1108. /*
  1109. * Like is_allocated_sectors, but if the buffer starts with a used sector,
  1110. * up to 'min' consecutive sectors containing zeros are ignored. This avoids
  1111. * breaking up write requests for only small sparse areas.
  1112. */
  1113. static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
  1114. int min, int64_t sector_num, int alignment)
  1115. {
  1116. int ret;
  1117. int num_checked, num_used;
  1118. if (n < min) {
  1119. min = n;
  1120. }
  1121. ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
  1122. if (!ret) {
  1123. return ret;
  1124. }
  1125. num_used = *pnum;
  1126. buf += BDRV_SECTOR_SIZE * *pnum;
  1127. n -= *pnum;
  1128. sector_num += *pnum;
  1129. num_checked = num_used;
  1130. while (n > 0) {
  1131. ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
  1132. buf += BDRV_SECTOR_SIZE * *pnum;
  1133. n -= *pnum;
  1134. sector_num += *pnum;
  1135. num_checked += *pnum;
  1136. if (ret) {
  1137. num_used = num_checked;
  1138. } else if (*pnum >= min) {
  1139. break;
  1140. }
  1141. }
  1142. *pnum = num_used;
  1143. return 1;
  1144. }
  1145. /*
  1146. * Compares two buffers chunk by chunk, where @chsize is the chunk size.
  1147. * If @chsize is 0, default chunk size of BDRV_SECTOR_SIZE is used.
  1148. * Returns 0 if the first chunk of each buffer matches, non-zero otherwise.
  1149. *
  1150. * @pnum is set to the size of the buffer prefix aligned to @chsize that
  1151. * has the same matching status as the first chunk.
  1152. */
  1153. static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
  1154. int64_t bytes, uint64_t chsize, int64_t *pnum)
  1155. {
  1156. bool res;
  1157. int64_t i;
  1158. assert(bytes > 0);
  1159. if (!chsize) {
  1160. chsize = BDRV_SECTOR_SIZE;
  1161. }
  1162. i = MIN(bytes, chsize);
  1163. res = !!memcmp(buf1, buf2, i);
  1164. while (i < bytes) {
  1165. int64_t len = MIN(bytes - i, chsize);
  1166. if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
  1167. break;
  1168. }
  1169. i += len;
  1170. }
  1171. *pnum = i;
  1172. return res;
  1173. }
  1174. #define IO_BUF_SIZE (2 * MiB)
  1175. /*
  1176. * Check if passed sectors are empty (not allocated or contain only 0 bytes)
  1177. *
  1178. * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
  1179. * filled with 0, 1 if sectors contain non-zero data (this is a comparison
  1180. * failure), and 4 on error (the exit status for read errors), after emitting
  1181. * an error message.
  1182. *
  1183. * @param blk: BlockBackend for the image
  1184. * @param offset: Starting offset to check
  1185. * @param bytes: Number of bytes to check
  1186. * @param filename: Name of disk file we are checking (logging purpose)
  1187. * @param buffer: Allocated buffer for storing read data
  1188. * @param quiet: Flag for quiet mode
  1189. */
  1190. static int check_empty_sectors(BlockBackend *blk, int64_t offset,
  1191. int64_t bytes, const char *filename,
  1192. uint8_t *buffer, bool quiet)
  1193. {
  1194. int ret = 0;
  1195. int64_t idx;
  1196. ret = blk_pread(blk, offset, bytes, buffer, 0);
  1197. if (ret < 0) {
  1198. error_report("Error while reading offset %" PRId64 " of %s: %s",
  1199. offset, filename, strerror(-ret));
  1200. return 4;
  1201. }
  1202. idx = find_nonzero(buffer, bytes);
  1203. if (idx >= 0) {
  1204. qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
  1205. offset + idx);
  1206. return 1;
  1207. }
  1208. return 0;
  1209. }
  1210. /*
  1211. * Compares two images. Exit codes:
  1212. *
  1213. * 0 - Images are identical or the requested help was printed
  1214. * 1 - Images differ
  1215. * >1 - Error occurred
  1216. */
  1217. static int img_compare(int argc, char **argv)
  1218. {
  1219. const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
  1220. BlockBackend *blk1, *blk2;
  1221. BlockDriverState *bs1, *bs2;
  1222. int64_t total_size1, total_size2;
  1223. uint8_t *buf1 = NULL, *buf2 = NULL;
  1224. int64_t pnum1, pnum2;
  1225. int allocated1, allocated2;
  1226. int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
  1227. bool progress = false, quiet = false, strict = false;
  1228. int flags;
  1229. bool writethrough;
  1230. int64_t total_size;
  1231. int64_t offset = 0;
  1232. int64_t chunk;
  1233. int c;
  1234. uint64_t progress_base;
  1235. bool image_opts = false;
  1236. bool force_share = false;
  1237. cache = BDRV_DEFAULT_CACHE;
  1238. for (;;) {
  1239. static const struct option long_options[] = {
  1240. {"help", no_argument, 0, 'h'},
  1241. {"object", required_argument, 0, OPTION_OBJECT},
  1242. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  1243. {"force-share", no_argument, 0, 'U'},
  1244. {0, 0, 0, 0}
  1245. };
  1246. c = getopt_long(argc, argv, ":hf:F:T:pqsU",
  1247. long_options, NULL);
  1248. if (c == -1) {
  1249. break;
  1250. }
  1251. switch (c) {
  1252. case ':':
  1253. missing_argument(argv[optind - 1]);
  1254. break;
  1255. case '?':
  1256. unrecognized_option(argv[optind - 1]);
  1257. break;
  1258. case 'h':
  1259. help();
  1260. break;
  1261. case 'f':
  1262. fmt1 = optarg;
  1263. break;
  1264. case 'F':
  1265. fmt2 = optarg;
  1266. break;
  1267. case 'T':
  1268. cache = optarg;
  1269. break;
  1270. case 'p':
  1271. progress = true;
  1272. break;
  1273. case 'q':
  1274. quiet = true;
  1275. break;
  1276. case 's':
  1277. strict = true;
  1278. break;
  1279. case 'U':
  1280. force_share = true;
  1281. break;
  1282. case OPTION_OBJECT:
  1283. {
  1284. Error *local_err = NULL;
  1285. if (!user_creatable_add_from_str(optarg, &local_err)) {
  1286. if (local_err) {
  1287. error_report_err(local_err);
  1288. exit(2);
  1289. } else {
  1290. /* Help was printed */
  1291. exit(EXIT_SUCCESS);
  1292. }
  1293. }
  1294. break;
  1295. }
  1296. case OPTION_IMAGE_OPTS:
  1297. image_opts = true;
  1298. break;
  1299. }
  1300. }
  1301. /* Progress is not shown in Quiet mode */
  1302. if (quiet) {
  1303. progress = false;
  1304. }
  1305. if (optind != argc - 2) {
  1306. error_exit("Expecting two image file names");
  1307. }
  1308. filename1 = argv[optind++];
  1309. filename2 = argv[optind++];
  1310. /* Initialize before goto out */
  1311. qemu_progress_init(progress, 2.0);
  1312. flags = 0;
  1313. ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
  1314. if (ret < 0) {
  1315. error_report("Invalid source cache option: %s", cache);
  1316. ret = 2;
  1317. goto out3;
  1318. }
  1319. blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
  1320. force_share);
  1321. if (!blk1) {
  1322. ret = 2;
  1323. goto out3;
  1324. }
  1325. blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
  1326. force_share);
  1327. if (!blk2) {
  1328. ret = 2;
  1329. goto out2;
  1330. }
  1331. bs1 = blk_bs(blk1);
  1332. bs2 = blk_bs(blk2);
  1333. buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
  1334. buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
  1335. total_size1 = blk_getlength(blk1);
  1336. if (total_size1 < 0) {
  1337. error_report("Can't get size of %s: %s",
  1338. filename1, strerror(-total_size1));
  1339. ret = 4;
  1340. goto out;
  1341. }
  1342. total_size2 = blk_getlength(blk2);
  1343. if (total_size2 < 0) {
  1344. error_report("Can't get size of %s: %s",
  1345. filename2, strerror(-total_size2));
  1346. ret = 4;
  1347. goto out;
  1348. }
  1349. total_size = MIN(total_size1, total_size2);
  1350. progress_base = MAX(total_size1, total_size2);
  1351. qemu_progress_print(0, 100);
  1352. if (strict && total_size1 != total_size2) {
  1353. ret = 1;
  1354. qprintf(quiet, "Strict mode: Image size mismatch!\n");
  1355. goto out;
  1356. }
  1357. while (offset < total_size) {
  1358. int status1, status2;
  1359. status1 = bdrv_block_status_above(bs1, NULL, offset,
  1360. total_size1 - offset, &pnum1, NULL,
  1361. NULL);
  1362. if (status1 < 0) {
  1363. ret = 3;
  1364. error_report("Sector allocation test failed for %s", filename1);
  1365. goto out;
  1366. }
  1367. allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
  1368. status2 = bdrv_block_status_above(bs2, NULL, offset,
  1369. total_size2 - offset, &pnum2, NULL,
  1370. NULL);
  1371. if (status2 < 0) {
  1372. ret = 3;
  1373. error_report("Sector allocation test failed for %s", filename2);
  1374. goto out;
  1375. }
  1376. allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
  1377. assert(pnum1 && pnum2);
  1378. chunk = MIN(pnum1, pnum2);
  1379. if (strict) {
  1380. if (status1 != status2) {
  1381. ret = 1;
  1382. qprintf(quiet, "Strict mode: Offset %" PRId64
  1383. " block status mismatch!\n", offset);
  1384. goto out;
  1385. }
  1386. }
  1387. if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
  1388. /* nothing to do */
  1389. } else if (allocated1 == allocated2) {
  1390. if (allocated1) {
  1391. int64_t pnum;
  1392. chunk = MIN(chunk, IO_BUF_SIZE);
  1393. ret = blk_pread(blk1, offset, chunk, buf1, 0);
  1394. if (ret < 0) {
  1395. error_report("Error while reading offset %" PRId64
  1396. " of %s: %s",
  1397. offset, filename1, strerror(-ret));
  1398. ret = 4;
  1399. goto out;
  1400. }
  1401. ret = blk_pread(blk2, offset, chunk, buf2, 0);
  1402. if (ret < 0) {
  1403. error_report("Error while reading offset %" PRId64
  1404. " of %s: %s",
  1405. offset, filename2, strerror(-ret));
  1406. ret = 4;
  1407. goto out;
  1408. }
  1409. ret = compare_buffers(buf1, buf2, chunk, 0, &pnum);
  1410. if (ret || pnum != chunk) {
  1411. qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
  1412. offset + (ret ? 0 : pnum));
  1413. ret = 1;
  1414. goto out;
  1415. }
  1416. }
  1417. } else {
  1418. chunk = MIN(chunk, IO_BUF_SIZE);
  1419. if (allocated1) {
  1420. ret = check_empty_sectors(blk1, offset, chunk,
  1421. filename1, buf1, quiet);
  1422. } else {
  1423. ret = check_empty_sectors(blk2, offset, chunk,
  1424. filename2, buf1, quiet);
  1425. }
  1426. if (ret) {
  1427. goto out;
  1428. }
  1429. }
  1430. offset += chunk;
  1431. qemu_progress_print(((float) chunk / progress_base) * 100, 100);
  1432. }
  1433. if (total_size1 != total_size2) {
  1434. BlockBackend *blk_over;
  1435. const char *filename_over;
  1436. qprintf(quiet, "Warning: Image size mismatch!\n");
  1437. if (total_size1 > total_size2) {
  1438. blk_over = blk1;
  1439. filename_over = filename1;
  1440. } else {
  1441. blk_over = blk2;
  1442. filename_over = filename2;
  1443. }
  1444. while (offset < progress_base) {
  1445. ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
  1446. progress_base - offset, &chunk,
  1447. NULL, NULL);
  1448. if (ret < 0) {
  1449. ret = 3;
  1450. error_report("Sector allocation test failed for %s",
  1451. filename_over);
  1452. goto out;
  1453. }
  1454. if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
  1455. chunk = MIN(chunk, IO_BUF_SIZE);
  1456. ret = check_empty_sectors(blk_over, offset, chunk,
  1457. filename_over, buf1, quiet);
  1458. if (ret) {
  1459. goto out;
  1460. }
  1461. }
  1462. offset += chunk;
  1463. qemu_progress_print(((float) chunk / progress_base) * 100, 100);
  1464. }
  1465. }
  1466. qprintf(quiet, "Images are identical.\n");
  1467. ret = 0;
  1468. out:
  1469. qemu_vfree(buf1);
  1470. qemu_vfree(buf2);
  1471. blk_unref(blk2);
  1472. out2:
  1473. blk_unref(blk1);
  1474. out3:
  1475. qemu_progress_end();
  1476. return ret;
  1477. }
  1478. /* Convenience wrapper around qmp_block_dirty_bitmap_merge */
  1479. static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name,
  1480. const char *src_node, const char *src_name,
  1481. Error **errp)
  1482. {
  1483. BlockDirtyBitmapOrStr *merge_src;
  1484. BlockDirtyBitmapOrStrList *list = NULL;
  1485. merge_src = g_new0(BlockDirtyBitmapOrStr, 1);
  1486. merge_src->type = QTYPE_QDICT;
  1487. merge_src->u.external.node = g_strdup(src_node);
  1488. merge_src->u.external.name = g_strdup(src_name);
  1489. QAPI_LIST_PREPEND(list, merge_src);
  1490. qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp);
  1491. qapi_free_BlockDirtyBitmapOrStrList(list);
  1492. }
  1493. enum ImgConvertBlockStatus {
  1494. BLK_DATA,
  1495. BLK_ZERO,
  1496. BLK_BACKING_FILE,
  1497. };
  1498. #define MAX_COROUTINES 16
  1499. #define CONVERT_THROTTLE_GROUP "img_convert"
  1500. typedef struct ImgConvertState {
  1501. BlockBackend **src;
  1502. int64_t *src_sectors;
  1503. int *src_alignment;
  1504. int src_num;
  1505. int64_t total_sectors;
  1506. int64_t allocated_sectors;
  1507. int64_t allocated_done;
  1508. int64_t sector_num;
  1509. int64_t wr_offs;
  1510. enum ImgConvertBlockStatus status;
  1511. int64_t sector_next_status;
  1512. BlockBackend *target;
  1513. bool has_zero_init;
  1514. bool compressed;
  1515. bool target_is_new;
  1516. bool target_has_backing;
  1517. int64_t target_backing_sectors; /* negative if unknown */
  1518. bool wr_in_order;
  1519. bool copy_range;
  1520. bool salvage;
  1521. bool quiet;
  1522. int min_sparse;
  1523. int alignment;
  1524. size_t cluster_sectors;
  1525. size_t buf_sectors;
  1526. long num_coroutines;
  1527. int running_coroutines;
  1528. Coroutine *co[MAX_COROUTINES];
  1529. int64_t wait_sector_num[MAX_COROUTINES];
  1530. CoMutex lock;
  1531. int ret;
  1532. } ImgConvertState;
  1533. static void convert_select_part(ImgConvertState *s, int64_t sector_num,
  1534. int *src_cur, int64_t *src_cur_offset)
  1535. {
  1536. *src_cur = 0;
  1537. *src_cur_offset = 0;
  1538. while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
  1539. *src_cur_offset += s->src_sectors[*src_cur];
  1540. (*src_cur)++;
  1541. assert(*src_cur < s->src_num);
  1542. }
  1543. }
  1544. static int coroutine_mixed_fn GRAPH_RDLOCK
  1545. convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
  1546. {
  1547. int64_t src_cur_offset;
  1548. int ret, n, src_cur;
  1549. bool post_backing_zero = false;
  1550. convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
  1551. assert(s->total_sectors > sector_num);
  1552. n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
  1553. if (s->target_backing_sectors >= 0) {
  1554. if (sector_num >= s->target_backing_sectors) {
  1555. post_backing_zero = true;
  1556. } else if (sector_num + n > s->target_backing_sectors) {
  1557. /* Split requests around target_backing_sectors (because
  1558. * starting from there, zeros are handled differently) */
  1559. n = s->target_backing_sectors - sector_num;
  1560. }
  1561. }
  1562. if (s->sector_next_status <= sector_num) {
  1563. uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
  1564. int64_t count;
  1565. int tail;
  1566. BlockDriverState *src_bs = blk_bs(s->src[src_cur]);
  1567. BlockDriverState *base;
  1568. if (s->target_has_backing) {
  1569. base = bdrv_cow_bs(bdrv_skip_filters(src_bs));
  1570. } else {
  1571. base = NULL;
  1572. }
  1573. do {
  1574. count = n * BDRV_SECTOR_SIZE;
  1575. ret = bdrv_block_status_above(src_bs, base, offset, count, &count,
  1576. NULL, NULL);
  1577. if (ret < 0) {
  1578. if (s->salvage) {
  1579. if (n == 1) {
  1580. if (!s->quiet) {
  1581. warn_report("error while reading block status at "
  1582. "offset %" PRIu64 ": %s", offset,
  1583. strerror(-ret));
  1584. }
  1585. /* Just try to read the data, then */
  1586. ret = BDRV_BLOCK_DATA;
  1587. count = BDRV_SECTOR_SIZE;
  1588. } else {
  1589. /* Retry on a shorter range */
  1590. n = DIV_ROUND_UP(n, 4);
  1591. }
  1592. } else {
  1593. error_report("error while reading block status at offset "
  1594. "%" PRIu64 ": %s", offset, strerror(-ret));
  1595. return ret;
  1596. }
  1597. }
  1598. } while (ret < 0);
  1599. n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
  1600. /*
  1601. * Avoid that s->sector_next_status becomes unaligned to the source
  1602. * request alignment and/or cluster size to avoid unnecessary read
  1603. * cycles.
  1604. */
  1605. tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur];
  1606. if (n > tail) {
  1607. n -= tail;
  1608. }
  1609. if (ret & BDRV_BLOCK_ZERO) {
  1610. s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
  1611. } else if (ret & BDRV_BLOCK_DATA) {
  1612. s->status = BLK_DATA;
  1613. } else {
  1614. s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
  1615. }
  1616. s->sector_next_status = sector_num + n;
  1617. }
  1618. n = MIN(n, s->sector_next_status - sector_num);
  1619. if (s->status == BLK_DATA) {
  1620. n = MIN(n, s->buf_sectors);
  1621. }
  1622. /* We need to write complete clusters for compressed images, so if an
  1623. * unallocated area is shorter than that, we must consider the whole
  1624. * cluster allocated. */
  1625. if (s->compressed) {
  1626. if (n < s->cluster_sectors) {
  1627. n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
  1628. s->status = BLK_DATA;
  1629. } else {
  1630. n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
  1631. }
  1632. }
  1633. return n;
  1634. }
  1635. static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
  1636. int nb_sectors, uint8_t *buf)
  1637. {
  1638. uint64_t single_read_until = 0;
  1639. int n, ret;
  1640. assert(nb_sectors <= s->buf_sectors);
  1641. while (nb_sectors > 0) {
  1642. BlockBackend *blk;
  1643. int src_cur;
  1644. int64_t bs_sectors, src_cur_offset;
  1645. uint64_t offset;
  1646. /* In the case of compression with multiple source files, we can get a
  1647. * nb_sectors that spreads into the next part. So we must be able to
  1648. * read across multiple BDSes for one convert_read() call. */
  1649. convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
  1650. blk = s->src[src_cur];
  1651. bs_sectors = s->src_sectors[src_cur];
  1652. offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
  1653. n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
  1654. if (single_read_until > offset) {
  1655. n = 1;
  1656. }
  1657. ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0);
  1658. if (ret < 0) {
  1659. if (s->salvage) {
  1660. if (n > 1) {
  1661. single_read_until = offset + (n << BDRV_SECTOR_BITS);
  1662. continue;
  1663. } else {
  1664. if (!s->quiet) {
  1665. warn_report("error while reading offset %" PRIu64
  1666. ": %s", offset, strerror(-ret));
  1667. }
  1668. memset(buf, 0, BDRV_SECTOR_SIZE);
  1669. }
  1670. } else {
  1671. return ret;
  1672. }
  1673. }
  1674. sector_num += n;
  1675. nb_sectors -= n;
  1676. buf += n * BDRV_SECTOR_SIZE;
  1677. }
  1678. return 0;
  1679. }
  1680. static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
  1681. int nb_sectors, uint8_t *buf,
  1682. enum ImgConvertBlockStatus status)
  1683. {
  1684. int ret;
  1685. while (nb_sectors > 0) {
  1686. int n = nb_sectors;
  1687. BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
  1688. switch (status) {
  1689. case BLK_BACKING_FILE:
  1690. /* If we have a backing file, leave clusters unallocated that are
  1691. * unallocated in the source image, so that the backing file is
  1692. * visible at the respective offset. */
  1693. assert(s->target_has_backing);
  1694. break;
  1695. case BLK_DATA:
  1696. /* If we're told to keep the target fully allocated (-S 0) or there
  1697. * is real non-zero data, we must write it. Otherwise we can treat
  1698. * it as zero sectors.
  1699. * Compressed clusters need to be written as a whole, so in that
  1700. * case we can only save the write if the buffer is completely
  1701. * zeroed. */
  1702. if (!s->min_sparse ||
  1703. (!s->compressed &&
  1704. is_allocated_sectors_min(buf, n, &n, s->min_sparse,
  1705. sector_num, s->alignment)) ||
  1706. (s->compressed &&
  1707. !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
  1708. {
  1709. ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
  1710. n << BDRV_SECTOR_BITS, buf, flags);
  1711. if (ret < 0) {
  1712. return ret;
  1713. }
  1714. break;
  1715. }
  1716. /* fall-through */
  1717. case BLK_ZERO:
  1718. if (s->has_zero_init) {
  1719. assert(!s->target_has_backing);
  1720. break;
  1721. }
  1722. ret = blk_co_pwrite_zeroes(s->target,
  1723. sector_num << BDRV_SECTOR_BITS,
  1724. n << BDRV_SECTOR_BITS,
  1725. BDRV_REQ_MAY_UNMAP);
  1726. if (ret < 0) {
  1727. return ret;
  1728. }
  1729. break;
  1730. }
  1731. sector_num += n;
  1732. nb_sectors -= n;
  1733. buf += n * BDRV_SECTOR_SIZE;
  1734. }
  1735. return 0;
  1736. }
  1737. static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
  1738. int nb_sectors)
  1739. {
  1740. int n, ret;
  1741. while (nb_sectors > 0) {
  1742. BlockBackend *blk;
  1743. int src_cur;
  1744. int64_t bs_sectors, src_cur_offset;
  1745. int64_t offset;
  1746. convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
  1747. offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
  1748. blk = s->src[src_cur];
  1749. bs_sectors = s->src_sectors[src_cur];
  1750. n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
  1751. ret = blk_co_copy_range(blk, offset, s->target,
  1752. sector_num << BDRV_SECTOR_BITS,
  1753. n << BDRV_SECTOR_BITS, 0, 0);
  1754. if (ret < 0) {
  1755. return ret;
  1756. }
  1757. sector_num += n;
  1758. nb_sectors -= n;
  1759. }
  1760. return 0;
  1761. }
  1762. static void coroutine_fn convert_co_do_copy(void *opaque)
  1763. {
  1764. ImgConvertState *s = opaque;
  1765. uint8_t *buf = NULL;
  1766. int ret, i;
  1767. int index = -1;
  1768. for (i = 0; i < s->num_coroutines; i++) {
  1769. if (s->co[i] == qemu_coroutine_self()) {
  1770. index = i;
  1771. break;
  1772. }
  1773. }
  1774. assert(index >= 0);
  1775. s->running_coroutines++;
  1776. buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
  1777. while (1) {
  1778. int n;
  1779. int64_t sector_num;
  1780. enum ImgConvertBlockStatus status;
  1781. bool copy_range;
  1782. qemu_co_mutex_lock(&s->lock);
  1783. if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
  1784. qemu_co_mutex_unlock(&s->lock);
  1785. break;
  1786. }
  1787. WITH_GRAPH_RDLOCK_GUARD() {
  1788. n = convert_iteration_sectors(s, s->sector_num);
  1789. }
  1790. if (n < 0) {
  1791. qemu_co_mutex_unlock(&s->lock);
  1792. s->ret = n;
  1793. break;
  1794. }
  1795. /* save current sector and allocation status to local variables */
  1796. sector_num = s->sector_num;
  1797. status = s->status;
  1798. if (!s->min_sparse && s->status == BLK_ZERO) {
  1799. n = MIN(n, s->buf_sectors);
  1800. }
  1801. /* increment global sector counter so that other coroutines can
  1802. * already continue reading beyond this request */
  1803. s->sector_num += n;
  1804. qemu_co_mutex_unlock(&s->lock);
  1805. if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
  1806. s->allocated_done += n;
  1807. qemu_progress_print(100.0 * s->allocated_done /
  1808. s->allocated_sectors, 0);
  1809. }
  1810. retry:
  1811. copy_range = s->copy_range && s->status == BLK_DATA;
  1812. if (status == BLK_DATA && !copy_range) {
  1813. ret = convert_co_read(s, sector_num, n, buf);
  1814. if (ret < 0) {
  1815. error_report("error while reading at byte %lld: %s",
  1816. sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
  1817. s->ret = ret;
  1818. }
  1819. } else if (!s->min_sparse && status == BLK_ZERO) {
  1820. status = BLK_DATA;
  1821. memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
  1822. }
  1823. if (s->wr_in_order) {
  1824. /* keep writes in order */
  1825. while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
  1826. s->wait_sector_num[index] = sector_num;
  1827. qemu_coroutine_yield();
  1828. }
  1829. s->wait_sector_num[index] = -1;
  1830. }
  1831. if (s->ret == -EINPROGRESS) {
  1832. if (copy_range) {
  1833. WITH_GRAPH_RDLOCK_GUARD() {
  1834. ret = convert_co_copy_range(s, sector_num, n);
  1835. }
  1836. if (ret) {
  1837. s->copy_range = false;
  1838. goto retry;
  1839. }
  1840. } else {
  1841. ret = convert_co_write(s, sector_num, n, buf, status);
  1842. }
  1843. if (ret < 0) {
  1844. error_report("error while writing at byte %lld: %s",
  1845. sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
  1846. s->ret = ret;
  1847. }
  1848. }
  1849. if (s->wr_in_order) {
  1850. /* reenter the coroutine that might have waited
  1851. * for this write to complete */
  1852. s->wr_offs = sector_num + n;
  1853. for (i = 0; i < s->num_coroutines; i++) {
  1854. if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
  1855. /*
  1856. * A -> B -> A cannot occur because A has
  1857. * s->wait_sector_num[i] == -1 during A -> B. Therefore
  1858. * B will never enter A during this time window.
  1859. */
  1860. qemu_coroutine_enter(s->co[i]);
  1861. break;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. qemu_vfree(buf);
  1867. s->co[index] = NULL;
  1868. s->running_coroutines--;
  1869. if (!s->running_coroutines && s->ret == -EINPROGRESS) {
  1870. /* the convert job finished successfully */
  1871. s->ret = 0;
  1872. }
  1873. }
  1874. static int convert_do_copy(ImgConvertState *s)
  1875. {
  1876. int ret, i, n;
  1877. int64_t sector_num = 0;
  1878. /* Check whether we have zero initialisation or can get it efficiently */
  1879. if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
  1880. !s->target_has_backing) {
  1881. bdrv_graph_rdlock_main_loop();
  1882. s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
  1883. bdrv_graph_rdunlock_main_loop();
  1884. }
  1885. /* Allocate buffer for copied data. For compressed images, only one cluster
  1886. * can be copied at a time. */
  1887. if (s->compressed) {
  1888. if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
  1889. error_report("invalid cluster size");
  1890. return -EINVAL;
  1891. }
  1892. s->buf_sectors = s->cluster_sectors;
  1893. }
  1894. while (sector_num < s->total_sectors) {
  1895. bdrv_graph_rdlock_main_loop();
  1896. n = convert_iteration_sectors(s, sector_num);
  1897. bdrv_graph_rdunlock_main_loop();
  1898. if (n < 0) {
  1899. return n;
  1900. }
  1901. if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
  1902. {
  1903. s->allocated_sectors += n;
  1904. }
  1905. sector_num += n;
  1906. }
  1907. /* Do the copy */
  1908. s->sector_next_status = 0;
  1909. s->ret = -EINPROGRESS;
  1910. qemu_co_mutex_init(&s->lock);
  1911. for (i = 0; i < s->num_coroutines; i++) {
  1912. s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
  1913. s->wait_sector_num[i] = -1;
  1914. qemu_coroutine_enter(s->co[i]);
  1915. }
  1916. while (s->running_coroutines) {
  1917. main_loop_wait(false);
  1918. }
  1919. if (s->compressed && !s->ret) {
  1920. /* signal EOF to align */
  1921. ret = blk_pwrite_compressed(s->target, 0, 0, NULL);
  1922. if (ret < 0) {
  1923. return ret;
  1924. }
  1925. }
  1926. return s->ret;
  1927. }
  1928. /* Check that bitmaps can be copied, or output an error */
  1929. static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken)
  1930. {
  1931. BdrvDirtyBitmap *bm;
  1932. if (!bdrv_supports_persistent_dirty_bitmap(src)) {
  1933. error_report("Source lacks bitmap support");
  1934. return -1;
  1935. }
  1936. FOR_EACH_DIRTY_BITMAP(src, bm) {
  1937. if (!bdrv_dirty_bitmap_get_persistence(bm)) {
  1938. continue;
  1939. }
  1940. if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
  1941. error_report("Cannot copy inconsistent bitmap '%s'",
  1942. bdrv_dirty_bitmap_name(bm));
  1943. error_printf("Try --skip-broken-bitmaps, or "
  1944. "use 'qemu-img bitmap --remove' to delete it\n");
  1945. return -1;
  1946. }
  1947. }
  1948. return 0;
  1949. }
  1950. static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst,
  1951. bool skip_broken)
  1952. {
  1953. BdrvDirtyBitmap *bm;
  1954. Error *err = NULL;
  1955. FOR_EACH_DIRTY_BITMAP(src, bm) {
  1956. const char *name;
  1957. if (!bdrv_dirty_bitmap_get_persistence(bm)) {
  1958. continue;
  1959. }
  1960. name = bdrv_dirty_bitmap_name(bm);
  1961. if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
  1962. warn_report("Skipping inconsistent bitmap '%s'", name);
  1963. continue;
  1964. }
  1965. qmp_block_dirty_bitmap_add(dst->node_name, name,
  1966. true, bdrv_dirty_bitmap_granularity(bm),
  1967. true, true,
  1968. true, !bdrv_dirty_bitmap_enabled(bm),
  1969. &err);
  1970. if (err) {
  1971. error_reportf_err(err, "Failed to create bitmap %s: ", name);
  1972. return -1;
  1973. }
  1974. do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name,
  1975. &err);
  1976. if (err) {
  1977. error_reportf_err(err, "Failed to populate bitmap %s: ", name);
  1978. qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL);
  1979. return -1;
  1980. }
  1981. }
  1982. return 0;
  1983. }
  1984. #define MAX_BUF_SECTORS 32768
  1985. static void set_rate_limit(BlockBackend *blk, int64_t rate_limit)
  1986. {
  1987. ThrottleConfig cfg;
  1988. throttle_config_init(&cfg);
  1989. cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit;
  1990. blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP);
  1991. blk_set_io_limits(blk, &cfg);
  1992. }
  1993. static int img_convert(int argc, char **argv)
  1994. {
  1995. int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE;
  1996. const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
  1997. *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
  1998. *out_filename, *out_baseimg_param, *snapshot_name = NULL,
  1999. *backing_fmt = NULL;
  2000. BlockDriver *drv = NULL, *proto_drv = NULL;
  2001. BlockDriverInfo bdi;
  2002. BlockDriverState *out_bs;
  2003. QemuOpts *opts = NULL, *sn_opts = NULL;
  2004. QemuOptsList *create_opts = NULL;
  2005. QDict *open_opts = NULL;
  2006. char *options = NULL;
  2007. Error *local_err = NULL;
  2008. bool writethrough, src_writethrough, image_opts = false,
  2009. skip_create = false, progress = false, tgt_image_opts = false;
  2010. int64_t ret = -EINVAL;
  2011. bool force_share = false;
  2012. bool explict_min_sparse = false;
  2013. bool bitmaps = false;
  2014. bool skip_broken = false;
  2015. int64_t rate_limit = 0;
  2016. ImgConvertState s = (ImgConvertState) {
  2017. /* Need at least 4k of zeros for sparse detection */
  2018. .min_sparse = 8,
  2019. .copy_range = false,
  2020. .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
  2021. .wr_in_order = true,
  2022. .num_coroutines = 8,
  2023. };
  2024. for(;;) {
  2025. static const struct option long_options[] = {
  2026. {"help", no_argument, 0, 'h'},
  2027. {"object", required_argument, 0, OPTION_OBJECT},
  2028. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  2029. {"force-share", no_argument, 0, 'U'},
  2030. {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
  2031. {"salvage", no_argument, 0, OPTION_SALVAGE},
  2032. {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
  2033. {"bitmaps", no_argument, 0, OPTION_BITMAPS},
  2034. {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN},
  2035. {0, 0, 0, 0}
  2036. };
  2037. c = getopt_long(argc, argv, ":hf:O:B:CcF:o:l:S:pt:T:qnm:WUr:",
  2038. long_options, NULL);
  2039. if (c == -1) {
  2040. break;
  2041. }
  2042. switch(c) {
  2043. case ':':
  2044. missing_argument(argv[optind - 1]);
  2045. break;
  2046. case '?':
  2047. unrecognized_option(argv[optind - 1]);
  2048. break;
  2049. case 'h':
  2050. help();
  2051. break;
  2052. case 'f':
  2053. fmt = optarg;
  2054. break;
  2055. case 'O':
  2056. out_fmt = optarg;
  2057. break;
  2058. case 'B':
  2059. out_baseimg = optarg;
  2060. break;
  2061. case 'C':
  2062. s.copy_range = true;
  2063. break;
  2064. case 'c':
  2065. s.compressed = true;
  2066. break;
  2067. case 'F':
  2068. backing_fmt = optarg;
  2069. break;
  2070. case 'o':
  2071. if (accumulate_options(&options, optarg) < 0) {
  2072. goto fail_getopt;
  2073. }
  2074. break;
  2075. case 'l':
  2076. if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
  2077. sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
  2078. optarg, false);
  2079. if (!sn_opts) {
  2080. error_report("Failed in parsing snapshot param '%s'",
  2081. optarg);
  2082. goto fail_getopt;
  2083. }
  2084. } else {
  2085. snapshot_name = optarg;
  2086. }
  2087. break;
  2088. case 'S':
  2089. {
  2090. int64_t sval;
  2091. sval = cvtnum("buffer size for sparse output", optarg);
  2092. if (sval < 0) {
  2093. goto fail_getopt;
  2094. } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
  2095. sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
  2096. error_report("Invalid buffer size for sparse output specified. "
  2097. "Valid sizes are multiples of %llu up to %llu. Select "
  2098. "0 to disable sparse detection (fully allocates output).",
  2099. BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
  2100. goto fail_getopt;
  2101. }
  2102. s.min_sparse = sval / BDRV_SECTOR_SIZE;
  2103. explict_min_sparse = true;
  2104. break;
  2105. }
  2106. case 'p':
  2107. progress = true;
  2108. break;
  2109. case 't':
  2110. cache = optarg;
  2111. break;
  2112. case 'T':
  2113. src_cache = optarg;
  2114. break;
  2115. case 'q':
  2116. s.quiet = true;
  2117. break;
  2118. case 'n':
  2119. skip_create = true;
  2120. break;
  2121. case 'm':
  2122. if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
  2123. s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
  2124. error_report("Invalid number of coroutines. Allowed number of"
  2125. " coroutines is between 1 and %d", MAX_COROUTINES);
  2126. goto fail_getopt;
  2127. }
  2128. break;
  2129. case 'W':
  2130. s.wr_in_order = false;
  2131. break;
  2132. case 'U':
  2133. force_share = true;
  2134. break;
  2135. case 'r':
  2136. rate_limit = cvtnum("rate limit", optarg);
  2137. if (rate_limit < 0) {
  2138. goto fail_getopt;
  2139. }
  2140. break;
  2141. case OPTION_OBJECT:
  2142. user_creatable_process_cmdline(optarg);
  2143. break;
  2144. case OPTION_IMAGE_OPTS:
  2145. image_opts = true;
  2146. break;
  2147. case OPTION_SALVAGE:
  2148. s.salvage = true;
  2149. break;
  2150. case OPTION_TARGET_IMAGE_OPTS:
  2151. tgt_image_opts = true;
  2152. break;
  2153. case OPTION_TARGET_IS_ZERO:
  2154. /*
  2155. * The user asserting that the target is blank has the
  2156. * same effect as the target driver supporting zero
  2157. * initialisation.
  2158. */
  2159. s.has_zero_init = true;
  2160. break;
  2161. case OPTION_BITMAPS:
  2162. bitmaps = true;
  2163. break;
  2164. case OPTION_SKIP_BROKEN:
  2165. skip_broken = true;
  2166. break;
  2167. }
  2168. }
  2169. if (!out_fmt && !tgt_image_opts) {
  2170. out_fmt = "raw";
  2171. }
  2172. if (skip_broken && !bitmaps) {
  2173. error_report("Use of --skip-broken-bitmaps requires --bitmaps");
  2174. goto fail_getopt;
  2175. }
  2176. if (s.compressed && s.copy_range) {
  2177. error_report("Cannot enable copy offloading when -c is used");
  2178. goto fail_getopt;
  2179. }
  2180. if (explict_min_sparse && s.copy_range) {
  2181. error_report("Cannot enable copy offloading when -S is used");
  2182. goto fail_getopt;
  2183. }
  2184. if (s.copy_range && s.salvage) {
  2185. error_report("Cannot use copy offloading in salvaging mode");
  2186. goto fail_getopt;
  2187. }
  2188. if (tgt_image_opts && !skip_create) {
  2189. error_report("--target-image-opts requires use of -n flag");
  2190. goto fail_getopt;
  2191. }
  2192. if (skip_create && options) {
  2193. error_report("-o has no effect when skipping image creation");
  2194. goto fail_getopt;
  2195. }
  2196. if (s.has_zero_init && !skip_create) {
  2197. error_report("--target-is-zero requires use of -n flag");
  2198. goto fail_getopt;
  2199. }
  2200. s.src_num = argc - optind - 1;
  2201. out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
  2202. if (options && has_help_option(options)) {
  2203. if (out_fmt) {
  2204. ret = print_block_option_help(out_filename, out_fmt);
  2205. goto fail_getopt;
  2206. } else {
  2207. error_report("Option help requires a format be specified");
  2208. goto fail_getopt;
  2209. }
  2210. }
  2211. if (s.src_num < 1) {
  2212. error_report("Must specify image file name");
  2213. goto fail_getopt;
  2214. }
  2215. /* ret is still -EINVAL until here */
  2216. ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
  2217. if (ret < 0) {
  2218. error_report("Invalid source cache option: %s", src_cache);
  2219. goto fail_getopt;
  2220. }
  2221. /* Initialize before goto out */
  2222. if (s.quiet) {
  2223. progress = false;
  2224. }
  2225. qemu_progress_init(progress, 1.0);
  2226. qemu_progress_print(0, 100);
  2227. s.src = g_new0(BlockBackend *, s.src_num);
  2228. s.src_sectors = g_new(int64_t, s.src_num);
  2229. s.src_alignment = g_new(int, s.src_num);
  2230. for (bs_i = 0; bs_i < s.src_num; bs_i++) {
  2231. BlockDriverState *src_bs;
  2232. s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
  2233. fmt, src_flags, src_writethrough, s.quiet,
  2234. force_share);
  2235. if (!s.src[bs_i]) {
  2236. ret = -1;
  2237. goto out;
  2238. }
  2239. s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
  2240. if (s.src_sectors[bs_i] < 0) {
  2241. error_report("Could not get size of %s: %s",
  2242. argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
  2243. ret = -1;
  2244. goto out;
  2245. }
  2246. src_bs = blk_bs(s.src[bs_i]);
  2247. s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment,
  2248. BDRV_SECTOR_SIZE);
  2249. if (!bdrv_get_info(src_bs, &bdi)) {
  2250. s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i],
  2251. bdi.cluster_size / BDRV_SECTOR_SIZE);
  2252. }
  2253. s.total_sectors += s.src_sectors[bs_i];
  2254. }
  2255. if (sn_opts) {
  2256. bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
  2257. qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
  2258. qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
  2259. &local_err);
  2260. } else if (snapshot_name != NULL) {
  2261. if (s.src_num > 1) {
  2262. error_report("No support for concatenating multiple snapshot");
  2263. ret = -1;
  2264. goto out;
  2265. }
  2266. bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
  2267. &local_err);
  2268. }
  2269. if (local_err) {
  2270. error_reportf_err(local_err, "Failed to load snapshot: ");
  2271. ret = -1;
  2272. goto out;
  2273. }
  2274. if (!skip_create) {
  2275. /* Find driver and parse its options */
  2276. drv = bdrv_find_format(out_fmt);
  2277. if (!drv) {
  2278. error_report("Unknown file format '%s'", out_fmt);
  2279. ret = -1;
  2280. goto out;
  2281. }
  2282. proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
  2283. if (!proto_drv) {
  2284. error_report_err(local_err);
  2285. ret = -1;
  2286. goto out;
  2287. }
  2288. if (!drv->create_opts) {
  2289. error_report("Format driver '%s' does not support image creation",
  2290. drv->format_name);
  2291. ret = -1;
  2292. goto out;
  2293. }
  2294. if (!proto_drv->create_opts) {
  2295. error_report("Protocol driver '%s' does not support image creation",
  2296. proto_drv->format_name);
  2297. ret = -1;
  2298. goto out;
  2299. }
  2300. create_opts = qemu_opts_append(create_opts, drv->create_opts);
  2301. create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
  2302. opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
  2303. if (options) {
  2304. if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
  2305. error_report_err(local_err);
  2306. ret = -1;
  2307. goto out;
  2308. }
  2309. }
  2310. qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
  2311. s.total_sectors * BDRV_SECTOR_SIZE, &error_abort);
  2312. ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt);
  2313. if (ret < 0) {
  2314. goto out;
  2315. }
  2316. }
  2317. /* Get backing file name if -o backing_file was used */
  2318. out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
  2319. if (out_baseimg_param) {
  2320. out_baseimg = out_baseimg_param;
  2321. }
  2322. s.target_has_backing = (bool) out_baseimg;
  2323. if (s.has_zero_init && s.target_has_backing) {
  2324. error_report("Cannot use --target-is-zero when the destination "
  2325. "image has a backing file");
  2326. goto out;
  2327. }
  2328. if (s.src_num > 1 && out_baseimg) {
  2329. error_report("Having a backing file for the target makes no sense when "
  2330. "concatenating multiple input images");
  2331. ret = -1;
  2332. goto out;
  2333. }
  2334. if (out_baseimg_param) {
  2335. if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) {
  2336. error_report("Use of backing file requires explicit "
  2337. "backing format");
  2338. ret = -1;
  2339. goto out;
  2340. }
  2341. }
  2342. /* Check if compression is supported */
  2343. if (s.compressed) {
  2344. bool encryption =
  2345. qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
  2346. const char *encryptfmt =
  2347. qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
  2348. const char *preallocation =
  2349. qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
  2350. if (drv && !block_driver_can_compress(drv)) {
  2351. error_report("Compression not supported for this file format");
  2352. ret = -1;
  2353. goto out;
  2354. }
  2355. if (encryption || encryptfmt) {
  2356. error_report("Compression and encryption not supported at "
  2357. "the same time");
  2358. ret = -1;
  2359. goto out;
  2360. }
  2361. if (preallocation
  2362. && strcmp(preallocation, "off"))
  2363. {
  2364. error_report("Compression and preallocation not supported at "
  2365. "the same time");
  2366. ret = -1;
  2367. goto out;
  2368. }
  2369. }
  2370. /* Determine if bitmaps need copying */
  2371. if (bitmaps) {
  2372. if (s.src_num > 1) {
  2373. error_report("Copying bitmaps only possible with single source");
  2374. ret = -1;
  2375. goto out;
  2376. }
  2377. ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken);
  2378. if (ret < 0) {
  2379. goto out;
  2380. }
  2381. }
  2382. /*
  2383. * The later open call will need any decryption secrets, and
  2384. * bdrv_create() will purge "opts", so extract them now before
  2385. * they are lost.
  2386. */
  2387. if (!skip_create) {
  2388. open_opts = qdict_new();
  2389. qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
  2390. /* Create the new image */
  2391. ret = bdrv_create(drv, out_filename, opts, &local_err);
  2392. if (ret < 0) {
  2393. error_reportf_err(local_err, "%s: error while converting %s: ",
  2394. out_filename, out_fmt);
  2395. goto out;
  2396. }
  2397. }
  2398. s.target_is_new = !skip_create;
  2399. flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
  2400. ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
  2401. if (ret < 0) {
  2402. error_report("Invalid cache option: %s", cache);
  2403. goto out;
  2404. }
  2405. if (flags & BDRV_O_NOCACHE) {
  2406. /*
  2407. * If we open the target with O_DIRECT, it may be necessary to
  2408. * extend its size to align to the physical sector size.
  2409. */
  2410. flags |= BDRV_O_RESIZE;
  2411. }
  2412. if (skip_create) {
  2413. s.target = img_open(tgt_image_opts, out_filename, out_fmt,
  2414. flags, writethrough, s.quiet, false);
  2415. } else {
  2416. /* TODO ultimately we should allow --target-image-opts
  2417. * to be used even when -n is not given.
  2418. * That has to wait for bdrv_create to be improved
  2419. * to allow filenames in option syntax
  2420. */
  2421. s.target = img_open_file(out_filename, open_opts, out_fmt,
  2422. flags, writethrough, s.quiet, false);
  2423. open_opts = NULL; /* blk_new_open will have freed it */
  2424. }
  2425. if (!s.target) {
  2426. ret = -1;
  2427. goto out;
  2428. }
  2429. out_bs = blk_bs(s.target);
  2430. if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) {
  2431. error_report("Format driver '%s' does not support bitmaps",
  2432. out_bs->drv->format_name);
  2433. ret = -1;
  2434. goto out;
  2435. }
  2436. if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
  2437. error_report("Compression not supported for this file format");
  2438. ret = -1;
  2439. goto out;
  2440. }
  2441. /* increase bufsectors from the default 4096 (2M) if opt_transfer
  2442. * or discard_alignment of the out_bs is greater. Limit to
  2443. * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
  2444. s.buf_sectors = MIN(MAX_BUF_SECTORS,
  2445. MAX(s.buf_sectors,
  2446. MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
  2447. out_bs->bl.pdiscard_alignment >>
  2448. BDRV_SECTOR_BITS)));
  2449. /* try to align the write requests to the destination to avoid unnecessary
  2450. * RMW cycles. */
  2451. s.alignment = MAX(pow2floor(s.min_sparse),
  2452. DIV_ROUND_UP(out_bs->bl.request_alignment,
  2453. BDRV_SECTOR_SIZE));
  2454. assert(is_power_of_2(s.alignment));
  2455. if (skip_create) {
  2456. int64_t output_sectors = blk_nb_sectors(s.target);
  2457. if (output_sectors < 0) {
  2458. error_report("unable to get output image length: %s",
  2459. strerror(-output_sectors));
  2460. ret = -1;
  2461. goto out;
  2462. } else if (output_sectors < s.total_sectors) {
  2463. error_report("output file is smaller than input file");
  2464. ret = -1;
  2465. goto out;
  2466. }
  2467. }
  2468. if (s.target_has_backing && s.target_is_new) {
  2469. /* Errors are treated as "backing length unknown" (which means
  2470. * s.target_backing_sectors has to be negative, which it will
  2471. * be automatically). The backing file length is used only
  2472. * for optimizations, so such a case is not fatal. */
  2473. bdrv_graph_rdlock_main_loop();
  2474. s.target_backing_sectors =
  2475. bdrv_nb_sectors(bdrv_backing_chain_next(out_bs));
  2476. bdrv_graph_rdunlock_main_loop();
  2477. } else {
  2478. s.target_backing_sectors = -1;
  2479. }
  2480. ret = bdrv_get_info(out_bs, &bdi);
  2481. if (ret < 0) {
  2482. if (s.compressed) {
  2483. error_report("could not get block driver info");
  2484. goto out;
  2485. }
  2486. } else {
  2487. s.compressed = s.compressed || bdi.needs_compressed_writes;
  2488. s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
  2489. }
  2490. if (rate_limit) {
  2491. set_rate_limit(s.target, rate_limit);
  2492. }
  2493. ret = convert_do_copy(&s);
  2494. /* Now copy the bitmaps */
  2495. if (bitmaps && ret == 0) {
  2496. ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs, skip_broken);
  2497. }
  2498. out:
  2499. if (!ret) {
  2500. qemu_progress_print(100, 0);
  2501. }
  2502. qemu_progress_end();
  2503. qemu_opts_del(opts);
  2504. qemu_opts_free(create_opts);
  2505. qobject_unref(open_opts);
  2506. blk_unref(s.target);
  2507. if (s.src) {
  2508. for (bs_i = 0; bs_i < s.src_num; bs_i++) {
  2509. blk_unref(s.src[bs_i]);
  2510. }
  2511. g_free(s.src);
  2512. }
  2513. g_free(s.src_sectors);
  2514. g_free(s.src_alignment);
  2515. fail_getopt:
  2516. qemu_opts_del(sn_opts);
  2517. g_free(options);
  2518. return !!ret;
  2519. }
  2520. static void dump_snapshots(BlockDriverState *bs)
  2521. {
  2522. QEMUSnapshotInfo *sn_tab, *sn;
  2523. int nb_sns, i;
  2524. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  2525. if (nb_sns <= 0)
  2526. return;
  2527. printf("Snapshot list:\n");
  2528. bdrv_snapshot_dump(NULL);
  2529. printf("\n");
  2530. for(i = 0; i < nb_sns; i++) {
  2531. sn = &sn_tab[i];
  2532. bdrv_snapshot_dump(sn);
  2533. printf("\n");
  2534. }
  2535. g_free(sn_tab);
  2536. }
  2537. static void dump_json_block_graph_info_list(BlockGraphInfoList *list)
  2538. {
  2539. GString *str;
  2540. QObject *obj;
  2541. Visitor *v = qobject_output_visitor_new(&obj);
  2542. visit_type_BlockGraphInfoList(v, NULL, &list, &error_abort);
  2543. visit_complete(v, &obj);
  2544. str = qobject_to_json_pretty(obj, true);
  2545. assert(str != NULL);
  2546. printf("%s\n", str->str);
  2547. qobject_unref(obj);
  2548. visit_free(v);
  2549. g_string_free(str, true);
  2550. }
  2551. static void dump_json_block_graph_info(BlockGraphInfo *info)
  2552. {
  2553. GString *str;
  2554. QObject *obj;
  2555. Visitor *v = qobject_output_visitor_new(&obj);
  2556. visit_type_BlockGraphInfo(v, NULL, &info, &error_abort);
  2557. visit_complete(v, &obj);
  2558. str = qobject_to_json_pretty(obj, true);
  2559. assert(str != NULL);
  2560. printf("%s\n", str->str);
  2561. qobject_unref(obj);
  2562. visit_free(v);
  2563. g_string_free(str, true);
  2564. }
  2565. static void dump_human_image_info(BlockGraphInfo *info, int indentation,
  2566. const char *path)
  2567. {
  2568. BlockChildInfoList *children_list;
  2569. bdrv_node_info_dump(qapi_BlockGraphInfo_base(info), indentation,
  2570. info->children == NULL);
  2571. for (children_list = info->children; children_list;
  2572. children_list = children_list->next)
  2573. {
  2574. BlockChildInfo *child = children_list->value;
  2575. g_autofree char *child_path = NULL;
  2576. printf("%*sChild node '%s%s':\n",
  2577. indentation * 4, "", path, child->name);
  2578. child_path = g_strdup_printf("%s%s/", path, child->name);
  2579. dump_human_image_info(child->info, indentation + 1, child_path);
  2580. }
  2581. }
  2582. static void dump_human_image_info_list(BlockGraphInfoList *list)
  2583. {
  2584. BlockGraphInfoList *elem;
  2585. bool delim = false;
  2586. for (elem = list; elem; elem = elem->next) {
  2587. if (delim) {
  2588. printf("\n");
  2589. }
  2590. delim = true;
  2591. dump_human_image_info(elem->value, 0, "/");
  2592. }
  2593. }
  2594. static gboolean str_equal_func(gconstpointer a, gconstpointer b)
  2595. {
  2596. return strcmp(a, b) == 0;
  2597. }
  2598. /**
  2599. * Open an image file chain and return an BlockGraphInfoList
  2600. *
  2601. * @filename: topmost image filename
  2602. * @fmt: topmost image format (may be NULL to autodetect)
  2603. * @chain: true - enumerate entire backing file chain
  2604. * false - only topmost image file
  2605. *
  2606. * Returns a list of BlockNodeInfo objects or NULL if there was an error
  2607. * opening an image file. If there was an error a message will have been
  2608. * printed to stderr.
  2609. */
  2610. static BlockGraphInfoList *collect_image_info_list(bool image_opts,
  2611. const char *filename,
  2612. const char *fmt,
  2613. bool chain, bool force_share)
  2614. {
  2615. BlockGraphInfoList *head = NULL;
  2616. BlockGraphInfoList **tail = &head;
  2617. GHashTable *filenames;
  2618. Error *err = NULL;
  2619. filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
  2620. while (filename) {
  2621. BlockBackend *blk;
  2622. BlockDriverState *bs;
  2623. BlockGraphInfo *info;
  2624. if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
  2625. error_report("Backing file '%s' creates an infinite loop.",
  2626. filename);
  2627. goto err;
  2628. }
  2629. g_hash_table_insert(filenames, (gpointer)filename, NULL);
  2630. blk = img_open(image_opts, filename, fmt,
  2631. BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
  2632. force_share);
  2633. if (!blk) {
  2634. goto err;
  2635. }
  2636. bs = blk_bs(blk);
  2637. /*
  2638. * Note that the returned BlockGraphInfo object will not have
  2639. * information about this image's backing node, because we have opened
  2640. * it with BDRV_O_NO_BACKING. Printing this object will therefore not
  2641. * duplicate the backing chain information that we obtain by walking
  2642. * the chain manually here.
  2643. */
  2644. bdrv_graph_rdlock_main_loop();
  2645. bdrv_query_block_graph_info(bs, &info, &err);
  2646. bdrv_graph_rdunlock_main_loop();
  2647. if (err) {
  2648. error_report_err(err);
  2649. blk_unref(blk);
  2650. goto err;
  2651. }
  2652. QAPI_LIST_APPEND(tail, info);
  2653. blk_unref(blk);
  2654. /* Clear parameters that only apply to the topmost image */
  2655. filename = fmt = NULL;
  2656. image_opts = false;
  2657. if (chain) {
  2658. if (info->full_backing_filename) {
  2659. filename = info->full_backing_filename;
  2660. } else if (info->backing_filename) {
  2661. error_report("Could not determine absolute backing filename,"
  2662. " but backing filename '%s' present",
  2663. info->backing_filename);
  2664. goto err;
  2665. }
  2666. if (info->backing_filename_format) {
  2667. fmt = info->backing_filename_format;
  2668. }
  2669. }
  2670. }
  2671. g_hash_table_destroy(filenames);
  2672. return head;
  2673. err:
  2674. qapi_free_BlockGraphInfoList(head);
  2675. g_hash_table_destroy(filenames);
  2676. return NULL;
  2677. }
  2678. static int img_info(int argc, char **argv)
  2679. {
  2680. int c;
  2681. OutputFormat output_format = OFORMAT_HUMAN;
  2682. bool chain = false;
  2683. const char *filename, *fmt, *output;
  2684. BlockGraphInfoList *list;
  2685. bool image_opts = false;
  2686. bool force_share = false;
  2687. fmt = NULL;
  2688. output = NULL;
  2689. for(;;) {
  2690. int option_index = 0;
  2691. static const struct option long_options[] = {
  2692. {"help", no_argument, 0, 'h'},
  2693. {"format", required_argument, 0, 'f'},
  2694. {"output", required_argument, 0, OPTION_OUTPUT},
  2695. {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
  2696. {"object", required_argument, 0, OPTION_OBJECT},
  2697. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  2698. {"force-share", no_argument, 0, 'U'},
  2699. {0, 0, 0, 0}
  2700. };
  2701. c = getopt_long(argc, argv, ":f:hU",
  2702. long_options, &option_index);
  2703. if (c == -1) {
  2704. break;
  2705. }
  2706. switch(c) {
  2707. case ':':
  2708. missing_argument(argv[optind - 1]);
  2709. break;
  2710. case '?':
  2711. unrecognized_option(argv[optind - 1]);
  2712. break;
  2713. case 'h':
  2714. help();
  2715. break;
  2716. case 'f':
  2717. fmt = optarg;
  2718. break;
  2719. case 'U':
  2720. force_share = true;
  2721. break;
  2722. case OPTION_OUTPUT:
  2723. output = optarg;
  2724. break;
  2725. case OPTION_BACKING_CHAIN:
  2726. chain = true;
  2727. break;
  2728. case OPTION_OBJECT:
  2729. user_creatable_process_cmdline(optarg);
  2730. break;
  2731. case OPTION_IMAGE_OPTS:
  2732. image_opts = true;
  2733. break;
  2734. }
  2735. }
  2736. if (optind != argc - 1) {
  2737. error_exit("Expecting one image file name");
  2738. }
  2739. filename = argv[optind++];
  2740. if (output && !strcmp(output, "json")) {
  2741. output_format = OFORMAT_JSON;
  2742. } else if (output && !strcmp(output, "human")) {
  2743. output_format = OFORMAT_HUMAN;
  2744. } else if (output) {
  2745. error_report("--output must be used with human or json as argument.");
  2746. return 1;
  2747. }
  2748. list = collect_image_info_list(image_opts, filename, fmt, chain,
  2749. force_share);
  2750. if (!list) {
  2751. return 1;
  2752. }
  2753. switch (output_format) {
  2754. case OFORMAT_HUMAN:
  2755. dump_human_image_info_list(list);
  2756. break;
  2757. case OFORMAT_JSON:
  2758. if (chain) {
  2759. dump_json_block_graph_info_list(list);
  2760. } else {
  2761. dump_json_block_graph_info(list->value);
  2762. }
  2763. break;
  2764. }
  2765. qapi_free_BlockGraphInfoList(list);
  2766. return 0;
  2767. }
  2768. static int dump_map_entry(OutputFormat output_format, MapEntry *e,
  2769. MapEntry *next)
  2770. {
  2771. switch (output_format) {
  2772. case OFORMAT_HUMAN:
  2773. if (e->data && !e->has_offset) {
  2774. error_report("File contains external, encrypted or compressed clusters.");
  2775. return -1;
  2776. }
  2777. if (e->data && !e->zero) {
  2778. printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
  2779. e->start, e->length,
  2780. e->has_offset ? e->offset : 0,
  2781. e->filename ?: "");
  2782. }
  2783. /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
  2784. * Modify the flags here to allow more coalescing.
  2785. */
  2786. if (next && (!next->data || next->zero)) {
  2787. next->data = false;
  2788. next->zero = true;
  2789. }
  2790. break;
  2791. case OFORMAT_JSON:
  2792. printf("{ \"start\": %"PRId64", \"length\": %"PRId64","
  2793. " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s,"
  2794. " \"data\": %s, \"compressed\": %s",
  2795. e->start, e->length, e->depth,
  2796. e->present ? "true" : "false",
  2797. e->zero ? "true" : "false",
  2798. e->data ? "true" : "false",
  2799. e->compressed ? "true" : "false");
  2800. if (e->has_offset) {
  2801. printf(", \"offset\": %"PRId64"", e->offset);
  2802. }
  2803. putchar('}');
  2804. if (next) {
  2805. puts(",");
  2806. }
  2807. break;
  2808. }
  2809. return 0;
  2810. }
  2811. static int get_block_status(BlockDriverState *bs, int64_t offset,
  2812. int64_t bytes, MapEntry *e)
  2813. {
  2814. int ret;
  2815. int depth;
  2816. BlockDriverState *file;
  2817. bool has_offset;
  2818. int64_t map;
  2819. char *filename = NULL;
  2820. GLOBAL_STATE_CODE();
  2821. GRAPH_RDLOCK_GUARD_MAINLOOP();
  2822. /* As an optimization, we could cache the current range of unallocated
  2823. * clusters in each file of the chain, and avoid querying the same
  2824. * range repeatedly.
  2825. */
  2826. depth = 0;
  2827. for (;;) {
  2828. bs = bdrv_skip_filters(bs);
  2829. ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
  2830. if (ret < 0) {
  2831. return ret;
  2832. }
  2833. assert(bytes);
  2834. if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
  2835. break;
  2836. }
  2837. bs = bdrv_cow_bs(bs);
  2838. if (bs == NULL) {
  2839. ret = 0;
  2840. break;
  2841. }
  2842. depth++;
  2843. }
  2844. has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
  2845. if (file && has_offset) {
  2846. bdrv_refresh_filename(file);
  2847. filename = file->filename;
  2848. }
  2849. *e = (MapEntry) {
  2850. .start = offset,
  2851. .length = bytes,
  2852. .data = !!(ret & BDRV_BLOCK_DATA),
  2853. .zero = !!(ret & BDRV_BLOCK_ZERO),
  2854. .compressed = !!(ret & BDRV_BLOCK_COMPRESSED),
  2855. .offset = map,
  2856. .has_offset = has_offset,
  2857. .depth = depth,
  2858. .present = !!(ret & BDRV_BLOCK_ALLOCATED),
  2859. .filename = filename,
  2860. };
  2861. return 0;
  2862. }
  2863. static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
  2864. {
  2865. if (curr->length == 0) {
  2866. return false;
  2867. }
  2868. if (curr->zero != next->zero ||
  2869. curr->data != next->data ||
  2870. curr->compressed != next->compressed ||
  2871. curr->depth != next->depth ||
  2872. curr->present != next->present ||
  2873. !curr->filename != !next->filename ||
  2874. curr->has_offset != next->has_offset) {
  2875. return false;
  2876. }
  2877. if (curr->filename && strcmp(curr->filename, next->filename)) {
  2878. return false;
  2879. }
  2880. if (curr->has_offset && curr->offset + curr->length != next->offset) {
  2881. return false;
  2882. }
  2883. return true;
  2884. }
  2885. static int img_map(int argc, char **argv)
  2886. {
  2887. int c;
  2888. OutputFormat output_format = OFORMAT_HUMAN;
  2889. BlockBackend *blk;
  2890. BlockDriverState *bs;
  2891. const char *filename, *fmt, *output;
  2892. int64_t length;
  2893. MapEntry curr = { .length = 0 }, next;
  2894. int ret = 0;
  2895. bool image_opts = false;
  2896. bool force_share = false;
  2897. int64_t start_offset = 0;
  2898. int64_t max_length = -1;
  2899. fmt = NULL;
  2900. output = NULL;
  2901. for (;;) {
  2902. int option_index = 0;
  2903. static const struct option long_options[] = {
  2904. {"help", no_argument, 0, 'h'},
  2905. {"format", required_argument, 0, 'f'},
  2906. {"output", required_argument, 0, OPTION_OUTPUT},
  2907. {"object", required_argument, 0, OPTION_OBJECT},
  2908. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  2909. {"force-share", no_argument, 0, 'U'},
  2910. {"start-offset", required_argument, 0, 's'},
  2911. {"max-length", required_argument, 0, 'l'},
  2912. {0, 0, 0, 0}
  2913. };
  2914. c = getopt_long(argc, argv, ":f:s:l:hU",
  2915. long_options, &option_index);
  2916. if (c == -1) {
  2917. break;
  2918. }
  2919. switch (c) {
  2920. case ':':
  2921. missing_argument(argv[optind - 1]);
  2922. break;
  2923. case '?':
  2924. unrecognized_option(argv[optind - 1]);
  2925. break;
  2926. case 'h':
  2927. help();
  2928. break;
  2929. case 'f':
  2930. fmt = optarg;
  2931. break;
  2932. case 'U':
  2933. force_share = true;
  2934. break;
  2935. case OPTION_OUTPUT:
  2936. output = optarg;
  2937. break;
  2938. case 's':
  2939. start_offset = cvtnum("start offset", optarg);
  2940. if (start_offset < 0) {
  2941. return 1;
  2942. }
  2943. break;
  2944. case 'l':
  2945. max_length = cvtnum("max length", optarg);
  2946. if (max_length < 0) {
  2947. return 1;
  2948. }
  2949. break;
  2950. case OPTION_OBJECT:
  2951. user_creatable_process_cmdline(optarg);
  2952. break;
  2953. case OPTION_IMAGE_OPTS:
  2954. image_opts = true;
  2955. break;
  2956. }
  2957. }
  2958. if (optind != argc - 1) {
  2959. error_exit("Expecting one image file name");
  2960. }
  2961. filename = argv[optind];
  2962. if (output && !strcmp(output, "json")) {
  2963. output_format = OFORMAT_JSON;
  2964. } else if (output && !strcmp(output, "human")) {
  2965. output_format = OFORMAT_HUMAN;
  2966. } else if (output) {
  2967. error_report("--output must be used with human or json as argument.");
  2968. return 1;
  2969. }
  2970. blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
  2971. if (!blk) {
  2972. return 1;
  2973. }
  2974. bs = blk_bs(blk);
  2975. if (output_format == OFORMAT_HUMAN) {
  2976. printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
  2977. } else if (output_format == OFORMAT_JSON) {
  2978. putchar('[');
  2979. }
  2980. length = blk_getlength(blk);
  2981. if (length < 0) {
  2982. error_report("Failed to get size for '%s'", filename);
  2983. return 1;
  2984. }
  2985. if (max_length != -1) {
  2986. length = MIN(start_offset + max_length, length);
  2987. }
  2988. curr.start = start_offset;
  2989. while (curr.start + curr.length < length) {
  2990. int64_t offset = curr.start + curr.length;
  2991. int64_t n = length - offset;
  2992. ret = get_block_status(bs, offset, n, &next);
  2993. if (ret < 0) {
  2994. error_report("Could not read file metadata: %s", strerror(-ret));
  2995. goto out;
  2996. }
  2997. if (entry_mergeable(&curr, &next)) {
  2998. curr.length += next.length;
  2999. continue;
  3000. }
  3001. if (curr.length > 0) {
  3002. ret = dump_map_entry(output_format, &curr, &next);
  3003. if (ret < 0) {
  3004. goto out;
  3005. }
  3006. }
  3007. curr = next;
  3008. }
  3009. ret = dump_map_entry(output_format, &curr, NULL);
  3010. if (output_format == OFORMAT_JSON) {
  3011. puts("]");
  3012. }
  3013. out:
  3014. blk_unref(blk);
  3015. return ret < 0;
  3016. }
  3017. #define SNAPSHOT_LIST 1
  3018. #define SNAPSHOT_CREATE 2
  3019. #define SNAPSHOT_APPLY 3
  3020. #define SNAPSHOT_DELETE 4
  3021. static int img_snapshot(int argc, char **argv)
  3022. {
  3023. BlockBackend *blk;
  3024. BlockDriverState *bs;
  3025. QEMUSnapshotInfo sn;
  3026. char *filename, *snapshot_name = NULL;
  3027. int c, ret = 0, bdrv_oflags;
  3028. int action = 0;
  3029. bool quiet = false;
  3030. Error *err = NULL;
  3031. bool image_opts = false;
  3032. bool force_share = false;
  3033. int64_t rt;
  3034. bdrv_oflags = BDRV_O_RDWR;
  3035. /* Parse commandline parameters */
  3036. for(;;) {
  3037. static const struct option long_options[] = {
  3038. {"help", no_argument, 0, 'h'},
  3039. {"object", required_argument, 0, OPTION_OBJECT},
  3040. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  3041. {"force-share", no_argument, 0, 'U'},
  3042. {0, 0, 0, 0}
  3043. };
  3044. c = getopt_long(argc, argv, ":la:c:d:hqU",
  3045. long_options, NULL);
  3046. if (c == -1) {
  3047. break;
  3048. }
  3049. switch(c) {
  3050. case ':':
  3051. missing_argument(argv[optind - 1]);
  3052. break;
  3053. case '?':
  3054. unrecognized_option(argv[optind - 1]);
  3055. break;
  3056. case 'h':
  3057. help();
  3058. return 0;
  3059. case 'l':
  3060. if (action) {
  3061. error_exit("Cannot mix '-l', '-a', '-c', '-d'");
  3062. return 0;
  3063. }
  3064. action = SNAPSHOT_LIST;
  3065. bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
  3066. break;
  3067. case 'a':
  3068. if (action) {
  3069. error_exit("Cannot mix '-l', '-a', '-c', '-d'");
  3070. return 0;
  3071. }
  3072. action = SNAPSHOT_APPLY;
  3073. snapshot_name = optarg;
  3074. break;
  3075. case 'c':
  3076. if (action) {
  3077. error_exit("Cannot mix '-l', '-a', '-c', '-d'");
  3078. return 0;
  3079. }
  3080. action = SNAPSHOT_CREATE;
  3081. snapshot_name = optarg;
  3082. break;
  3083. case 'd':
  3084. if (action) {
  3085. error_exit("Cannot mix '-l', '-a', '-c', '-d'");
  3086. return 0;
  3087. }
  3088. action = SNAPSHOT_DELETE;
  3089. snapshot_name = optarg;
  3090. break;
  3091. case 'q':
  3092. quiet = true;
  3093. break;
  3094. case 'U':
  3095. force_share = true;
  3096. break;
  3097. case OPTION_OBJECT:
  3098. user_creatable_process_cmdline(optarg);
  3099. break;
  3100. case OPTION_IMAGE_OPTS:
  3101. image_opts = true;
  3102. break;
  3103. }
  3104. }
  3105. if (optind != argc - 1) {
  3106. error_exit("Expecting one image file name");
  3107. }
  3108. filename = argv[optind++];
  3109. /* Open the image */
  3110. blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
  3111. force_share);
  3112. if (!blk) {
  3113. return 1;
  3114. }
  3115. bs = blk_bs(blk);
  3116. /* Perform the requested action */
  3117. switch(action) {
  3118. case SNAPSHOT_LIST:
  3119. dump_snapshots(bs);
  3120. break;
  3121. case SNAPSHOT_CREATE:
  3122. memset(&sn, 0, sizeof(sn));
  3123. pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
  3124. rt = g_get_real_time();
  3125. sn.date_sec = rt / G_USEC_PER_SEC;
  3126. sn.date_nsec = (rt % G_USEC_PER_SEC) * 1000;
  3127. bdrv_graph_rdlock_main_loop();
  3128. ret = bdrv_snapshot_create(bs, &sn);
  3129. bdrv_graph_rdunlock_main_loop();
  3130. if (ret) {
  3131. error_report("Could not create snapshot '%s': %s",
  3132. snapshot_name, strerror(-ret));
  3133. }
  3134. break;
  3135. case SNAPSHOT_APPLY:
  3136. ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
  3137. if (ret) {
  3138. error_reportf_err(err, "Could not apply snapshot '%s': ",
  3139. snapshot_name);
  3140. }
  3141. break;
  3142. case SNAPSHOT_DELETE:
  3143. bdrv_graph_rdlock_main_loop();
  3144. ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
  3145. if (ret < 0) {
  3146. error_report("Could not delete snapshot '%s': snapshot not "
  3147. "found", snapshot_name);
  3148. ret = 1;
  3149. } else {
  3150. ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
  3151. if (ret < 0) {
  3152. error_reportf_err(err, "Could not delete snapshot '%s': ",
  3153. snapshot_name);
  3154. ret = 1;
  3155. }
  3156. }
  3157. bdrv_graph_rdunlock_main_loop();
  3158. break;
  3159. }
  3160. /* Cleanup */
  3161. blk_unref(blk);
  3162. if (ret) {
  3163. return 1;
  3164. }
  3165. return 0;
  3166. }
  3167. static int img_rebase(int argc, char **argv)
  3168. {
  3169. BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
  3170. uint8_t *buf_old = NULL;
  3171. uint8_t *buf_new = NULL;
  3172. BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
  3173. BlockDriverState *unfiltered_bs, *unfiltered_bs_cow;
  3174. BlockDriverInfo bdi = {0};
  3175. char *filename;
  3176. const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
  3177. int c, flags, src_flags, ret;
  3178. BdrvRequestFlags write_flags = 0;
  3179. bool writethrough, src_writethrough;
  3180. int unsafe = 0;
  3181. bool force_share = false;
  3182. int progress = 0;
  3183. bool quiet = false;
  3184. bool compress = false;
  3185. Error *local_err = NULL;
  3186. bool image_opts = false;
  3187. int64_t write_align;
  3188. /* Parse commandline parameters */
  3189. fmt = NULL;
  3190. cache = BDRV_DEFAULT_CACHE;
  3191. src_cache = BDRV_DEFAULT_CACHE;
  3192. out_baseimg = NULL;
  3193. out_basefmt = NULL;
  3194. for(;;) {
  3195. static const struct option long_options[] = {
  3196. {"help", no_argument, 0, 'h'},
  3197. {"object", required_argument, 0, OPTION_OBJECT},
  3198. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  3199. {"force-share", no_argument, 0, 'U'},
  3200. {"compress", no_argument, 0, 'c'},
  3201. {0, 0, 0, 0}
  3202. };
  3203. c = getopt_long(argc, argv, ":hf:F:b:upt:T:qUc",
  3204. long_options, NULL);
  3205. if (c == -1) {
  3206. break;
  3207. }
  3208. switch(c) {
  3209. case ':':
  3210. missing_argument(argv[optind - 1]);
  3211. break;
  3212. case '?':
  3213. unrecognized_option(argv[optind - 1]);
  3214. break;
  3215. case 'h':
  3216. help();
  3217. return 0;
  3218. case 'f':
  3219. fmt = optarg;
  3220. break;
  3221. case 'F':
  3222. out_basefmt = optarg;
  3223. break;
  3224. case 'b':
  3225. out_baseimg = optarg;
  3226. break;
  3227. case 'u':
  3228. unsafe = 1;
  3229. break;
  3230. case 'p':
  3231. progress = 1;
  3232. break;
  3233. case 't':
  3234. cache = optarg;
  3235. break;
  3236. case 'T':
  3237. src_cache = optarg;
  3238. break;
  3239. case 'q':
  3240. quiet = true;
  3241. break;
  3242. case OPTION_OBJECT:
  3243. user_creatable_process_cmdline(optarg);
  3244. break;
  3245. case OPTION_IMAGE_OPTS:
  3246. image_opts = true;
  3247. break;
  3248. case 'U':
  3249. force_share = true;
  3250. break;
  3251. case 'c':
  3252. compress = true;
  3253. break;
  3254. }
  3255. }
  3256. if (quiet) {
  3257. progress = 0;
  3258. }
  3259. if (optind != argc - 1) {
  3260. error_exit("Expecting one image file name");
  3261. }
  3262. if (!unsafe && !out_baseimg) {
  3263. error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
  3264. }
  3265. filename = argv[optind++];
  3266. qemu_progress_init(progress, 2.0);
  3267. qemu_progress_print(0, 100);
  3268. flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
  3269. ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
  3270. if (ret < 0) {
  3271. error_report("Invalid cache option: %s", cache);
  3272. goto out;
  3273. }
  3274. src_flags = 0;
  3275. ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
  3276. if (ret < 0) {
  3277. error_report("Invalid source cache option: %s", src_cache);
  3278. goto out;
  3279. }
  3280. /* The source files are opened read-only, don't care about WCE */
  3281. assert((src_flags & BDRV_O_RDWR) == 0);
  3282. (void) src_writethrough;
  3283. /*
  3284. * Open the images.
  3285. *
  3286. * Ignore the old backing file for unsafe rebase in case we want to correct
  3287. * the reference to a renamed or moved backing file.
  3288. */
  3289. blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
  3290. false);
  3291. if (!blk) {
  3292. ret = -1;
  3293. goto out;
  3294. }
  3295. bs = blk_bs(blk);
  3296. bdrv_graph_rdlock_main_loop();
  3297. unfiltered_bs = bdrv_skip_filters(bs);
  3298. unfiltered_bs_cow = bdrv_cow_bs(unfiltered_bs);
  3299. bdrv_graph_rdunlock_main_loop();
  3300. if (compress && !block_driver_can_compress(unfiltered_bs->drv)) {
  3301. error_report("Compression not supported for this file format");
  3302. ret = -1;
  3303. goto out;
  3304. } else if (compress) {
  3305. write_flags |= BDRV_REQ_WRITE_COMPRESSED;
  3306. }
  3307. if (out_basefmt != NULL) {
  3308. if (bdrv_find_format(out_basefmt) == NULL) {
  3309. error_report("Invalid format name: '%s'", out_basefmt);
  3310. ret = -1;
  3311. goto out;
  3312. }
  3313. }
  3314. /*
  3315. * We need overlay subcluster size (or cluster size in case writes are
  3316. * compressed) to make sure write requests are aligned.
  3317. */
  3318. ret = bdrv_get_info(unfiltered_bs, &bdi);
  3319. if (ret < 0) {
  3320. error_report("could not get block driver info");
  3321. goto out;
  3322. } else if (bdi.subcluster_size == 0) {
  3323. bdi.cluster_size = bdi.subcluster_size = 1;
  3324. }
  3325. write_align = compress ? bdi.cluster_size : bdi.subcluster_size;
  3326. /* For safe rebasing we need to compare old and new backing file */
  3327. if (!unsafe) {
  3328. QDict *options = NULL;
  3329. BlockDriverState *base_bs;
  3330. bdrv_graph_rdlock_main_loop();
  3331. base_bs = bdrv_cow_bs(unfiltered_bs);
  3332. bdrv_graph_rdunlock_main_loop();
  3333. if (base_bs) {
  3334. blk_old_backing = blk_new(qemu_get_aio_context(),
  3335. BLK_PERM_CONSISTENT_READ,
  3336. BLK_PERM_ALL);
  3337. ret = blk_insert_bs(blk_old_backing, base_bs,
  3338. &local_err);
  3339. if (ret < 0) {
  3340. error_reportf_err(local_err,
  3341. "Could not reuse old backing file '%s': ",
  3342. base_bs->filename);
  3343. goto out;
  3344. }
  3345. } else {
  3346. blk_old_backing = NULL;
  3347. }
  3348. if (out_baseimg[0]) {
  3349. const char *overlay_filename;
  3350. char *out_real_path;
  3351. options = qdict_new();
  3352. if (out_basefmt) {
  3353. qdict_put_str(options, "driver", out_basefmt);
  3354. }
  3355. if (force_share) {
  3356. qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
  3357. }
  3358. bdrv_graph_rdlock_main_loop();
  3359. bdrv_refresh_filename(bs);
  3360. bdrv_graph_rdunlock_main_loop();
  3361. overlay_filename = bs->exact_filename[0] ? bs->exact_filename
  3362. : bs->filename;
  3363. out_real_path =
  3364. bdrv_get_full_backing_filename_from_filename(overlay_filename,
  3365. out_baseimg,
  3366. &local_err);
  3367. if (local_err) {
  3368. qobject_unref(options);
  3369. error_reportf_err(local_err,
  3370. "Could not resolve backing filename: ");
  3371. ret = -1;
  3372. goto out;
  3373. }
  3374. /*
  3375. * Find out whether we rebase an image on top of a previous image
  3376. * in its chain.
  3377. */
  3378. prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
  3379. if (prefix_chain_bs) {
  3380. qobject_unref(options);
  3381. g_free(out_real_path);
  3382. blk_new_backing = blk_new(qemu_get_aio_context(),
  3383. BLK_PERM_CONSISTENT_READ,
  3384. BLK_PERM_ALL);
  3385. ret = blk_insert_bs(blk_new_backing, prefix_chain_bs,
  3386. &local_err);
  3387. if (ret < 0) {
  3388. error_reportf_err(local_err,
  3389. "Could not reuse backing file '%s': ",
  3390. out_baseimg);
  3391. goto out;
  3392. }
  3393. } else {
  3394. blk_new_backing = blk_new_open(out_real_path, NULL,
  3395. options, src_flags, &local_err);
  3396. g_free(out_real_path);
  3397. if (!blk_new_backing) {
  3398. error_reportf_err(local_err,
  3399. "Could not open new backing file '%s': ",
  3400. out_baseimg);
  3401. ret = -1;
  3402. goto out;
  3403. }
  3404. }
  3405. }
  3406. }
  3407. /*
  3408. * Check each unallocated cluster in the COW file. If it is unallocated,
  3409. * accesses go to the backing file. We must therefore compare this cluster
  3410. * in the old and new backing file, and if they differ we need to copy it
  3411. * from the old backing file into the COW file.
  3412. *
  3413. * If qemu-img crashes during this step, no harm is done. The content of
  3414. * the image is the same as the original one at any time.
  3415. */
  3416. if (!unsafe) {
  3417. int64_t size;
  3418. int64_t old_backing_size = 0;
  3419. int64_t new_backing_size = 0;
  3420. uint64_t offset;
  3421. int64_t n, n_old = 0, n_new = 0;
  3422. float local_progress = 0;
  3423. if (blk_old_backing && bdrv_opt_mem_align(blk_bs(blk_old_backing)) >
  3424. bdrv_opt_mem_align(blk_bs(blk))) {
  3425. buf_old = blk_blockalign(blk_old_backing, IO_BUF_SIZE);
  3426. } else {
  3427. buf_old = blk_blockalign(blk, IO_BUF_SIZE);
  3428. }
  3429. buf_new = blk_blockalign(blk_new_backing, IO_BUF_SIZE);
  3430. size = blk_getlength(blk);
  3431. if (size < 0) {
  3432. error_report("Could not get size of '%s': %s",
  3433. filename, strerror(-size));
  3434. ret = -1;
  3435. goto out;
  3436. }
  3437. if (blk_old_backing) {
  3438. old_backing_size = blk_getlength(blk_old_backing);
  3439. if (old_backing_size < 0) {
  3440. char backing_name[PATH_MAX];
  3441. bdrv_get_backing_filename(bs, backing_name,
  3442. sizeof(backing_name));
  3443. error_report("Could not get size of '%s': %s",
  3444. backing_name, strerror(-old_backing_size));
  3445. ret = -1;
  3446. goto out;
  3447. }
  3448. }
  3449. if (blk_new_backing) {
  3450. new_backing_size = blk_getlength(blk_new_backing);
  3451. if (new_backing_size < 0) {
  3452. error_report("Could not get size of '%s': %s",
  3453. out_baseimg, strerror(-new_backing_size));
  3454. ret = -1;
  3455. goto out;
  3456. }
  3457. }
  3458. if (size != 0) {
  3459. local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
  3460. }
  3461. for (offset = 0; offset < size; offset += n) {
  3462. bool old_backing_eof = false;
  3463. int64_t n_alloc;
  3464. /* How many bytes can we handle with the next read? */
  3465. n = MIN(IO_BUF_SIZE, size - offset);
  3466. /* If the cluster is allocated, we don't need to take action */
  3467. ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n);
  3468. if (ret < 0) {
  3469. error_report("error while reading image metadata: %s",
  3470. strerror(-ret));
  3471. goto out;
  3472. }
  3473. if (ret) {
  3474. continue;
  3475. }
  3476. if (prefix_chain_bs) {
  3477. uint64_t bytes = n;
  3478. /*
  3479. * If cluster wasn't changed since prefix_chain, we don't need
  3480. * to take action
  3481. */
  3482. ret = bdrv_is_allocated_above(unfiltered_bs_cow,
  3483. prefix_chain_bs, false,
  3484. offset, n, &n);
  3485. if (ret < 0) {
  3486. error_report("error while reading image metadata: %s",
  3487. strerror(-ret));
  3488. goto out;
  3489. }
  3490. if (!ret && n) {
  3491. continue;
  3492. }
  3493. if (!n) {
  3494. /*
  3495. * If we've reached EOF of the old backing, it means that
  3496. * offsets beyond the old backing size were read as zeroes.
  3497. * Now we will need to explicitly zero the cluster in
  3498. * order to preserve that state after the rebase.
  3499. */
  3500. n = bytes;
  3501. }
  3502. }
  3503. /*
  3504. * At this point we know that the region [offset; offset + n)
  3505. * is unallocated within the target image. This region might be
  3506. * unaligned to the target image's (sub)cluster boundaries, as
  3507. * old backing may have smaller clusters (or have subclusters).
  3508. * We extend it to the aligned boundaries to avoid CoW on
  3509. * partial writes in blk_pwrite(),
  3510. */
  3511. n += offset - QEMU_ALIGN_DOWN(offset, write_align);
  3512. offset = QEMU_ALIGN_DOWN(offset, write_align);
  3513. n += QEMU_ALIGN_UP(offset + n, write_align) - (offset + n);
  3514. n = MIN(n, size - offset);
  3515. assert(!bdrv_is_allocated(unfiltered_bs, offset, n, &n_alloc) &&
  3516. n_alloc == n);
  3517. /*
  3518. * Much like with the target image, we'll try to read as much
  3519. * of the old and new backings as we can.
  3520. */
  3521. n_old = MIN(n, MAX(0, old_backing_size - (int64_t) offset));
  3522. n_new = MIN(n, MAX(0, new_backing_size - (int64_t) offset));
  3523. /*
  3524. * Read old and new backing file and take into consideration that
  3525. * backing files may be smaller than the COW image.
  3526. */
  3527. memset(buf_old + n_old, 0, n - n_old);
  3528. if (!n_old) {
  3529. old_backing_eof = true;
  3530. } else {
  3531. ret = blk_pread(blk_old_backing, offset, n_old, buf_old, 0);
  3532. if (ret < 0) {
  3533. error_report("error while reading from old backing file");
  3534. goto out;
  3535. }
  3536. }
  3537. memset(buf_new + n_new, 0, n - n_new);
  3538. if (n_new) {
  3539. ret = blk_pread(blk_new_backing, offset, n_new, buf_new, 0);
  3540. if (ret < 0) {
  3541. error_report("error while reading from new backing file");
  3542. goto out;
  3543. }
  3544. }
  3545. /* If they differ, we need to write to the COW file */
  3546. uint64_t written = 0;
  3547. while (written < n) {
  3548. int64_t pnum;
  3549. if (compare_buffers(buf_old + written, buf_new + written,
  3550. n - written, write_align, &pnum))
  3551. {
  3552. if (old_backing_eof) {
  3553. ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0);
  3554. } else {
  3555. assert(written + pnum <= IO_BUF_SIZE);
  3556. ret = blk_pwrite(blk, offset + written, pnum,
  3557. buf_old + written, write_flags);
  3558. }
  3559. if (ret < 0) {
  3560. error_report("Error while writing to COW image: %s",
  3561. strerror(-ret));
  3562. goto out;
  3563. }
  3564. }
  3565. written += pnum;
  3566. if (offset + written >= old_backing_size) {
  3567. old_backing_eof = true;
  3568. }
  3569. }
  3570. qemu_progress_print(local_progress, 100);
  3571. }
  3572. }
  3573. /*
  3574. * Change the backing file. All clusters that are different from the old
  3575. * backing file are overwritten in the COW file now, so the visible content
  3576. * doesn't change when we switch the backing file.
  3577. */
  3578. if (out_baseimg && *out_baseimg) {
  3579. ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt,
  3580. true);
  3581. } else {
  3582. ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false);
  3583. }
  3584. if (ret == -ENOSPC) {
  3585. error_report("Could not change the backing file to '%s': No "
  3586. "space left in the file header", out_baseimg);
  3587. } else if (ret == -EINVAL && out_baseimg && !out_basefmt) {
  3588. error_report("Could not change the backing file to '%s': backing "
  3589. "format must be specified", out_baseimg);
  3590. } else if (ret < 0) {
  3591. error_report("Could not change the backing file to '%s': %s",
  3592. out_baseimg, strerror(-ret));
  3593. }
  3594. qemu_progress_print(100, 0);
  3595. /*
  3596. * TODO At this point it is possible to check if any clusters that are
  3597. * allocated in the COW file are the same in the backing file. If so, they
  3598. * could be dropped from the COW file. Don't do this before switching the
  3599. * backing file, in case of a crash this would lead to corruption.
  3600. */
  3601. out:
  3602. qemu_progress_end();
  3603. /* Cleanup */
  3604. if (!unsafe) {
  3605. blk_unref(blk_old_backing);
  3606. blk_unref(blk_new_backing);
  3607. }
  3608. qemu_vfree(buf_old);
  3609. qemu_vfree(buf_new);
  3610. blk_unref(blk);
  3611. if (ret) {
  3612. return 1;
  3613. }
  3614. return 0;
  3615. }
  3616. static int img_resize(int argc, char **argv)
  3617. {
  3618. Error *err = NULL;
  3619. int c, ret, relative;
  3620. const char *filename, *fmt, *size;
  3621. int64_t n, total_size, current_size;
  3622. bool quiet = false;
  3623. BlockBackend *blk = NULL;
  3624. PreallocMode prealloc = PREALLOC_MODE_OFF;
  3625. QemuOpts *param;
  3626. static QemuOptsList resize_options = {
  3627. .name = "resize_options",
  3628. .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
  3629. .desc = {
  3630. {
  3631. .name = BLOCK_OPT_SIZE,
  3632. .type = QEMU_OPT_SIZE,
  3633. .help = "Virtual disk size"
  3634. }, {
  3635. /* end of list */
  3636. }
  3637. },
  3638. };
  3639. bool image_opts = false;
  3640. bool shrink = false;
  3641. /* Remove size from argv manually so that negative numbers are not treated
  3642. * as options by getopt. */
  3643. if (argc < 3) {
  3644. error_exit("Not enough arguments");
  3645. return 1;
  3646. }
  3647. size = argv[--argc];
  3648. /* Parse getopt arguments */
  3649. fmt = NULL;
  3650. for(;;) {
  3651. static const struct option long_options[] = {
  3652. {"help", no_argument, 0, 'h'},
  3653. {"object", required_argument, 0, OPTION_OBJECT},
  3654. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  3655. {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
  3656. {"shrink", no_argument, 0, OPTION_SHRINK},
  3657. {0, 0, 0, 0}
  3658. };
  3659. c = getopt_long(argc, argv, ":f:hq",
  3660. long_options, NULL);
  3661. if (c == -1) {
  3662. break;
  3663. }
  3664. switch(c) {
  3665. case ':':
  3666. missing_argument(argv[optind - 1]);
  3667. break;
  3668. case '?':
  3669. unrecognized_option(argv[optind - 1]);
  3670. break;
  3671. case 'h':
  3672. help();
  3673. break;
  3674. case 'f':
  3675. fmt = optarg;
  3676. break;
  3677. case 'q':
  3678. quiet = true;
  3679. break;
  3680. case OPTION_OBJECT:
  3681. user_creatable_process_cmdline(optarg);
  3682. break;
  3683. case OPTION_IMAGE_OPTS:
  3684. image_opts = true;
  3685. break;
  3686. case OPTION_PREALLOCATION:
  3687. prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
  3688. PREALLOC_MODE__MAX, NULL);
  3689. if (prealloc == PREALLOC_MODE__MAX) {
  3690. error_report("Invalid preallocation mode '%s'", optarg);
  3691. return 1;
  3692. }
  3693. break;
  3694. case OPTION_SHRINK:
  3695. shrink = true;
  3696. break;
  3697. }
  3698. }
  3699. if (optind != argc - 1) {
  3700. error_exit("Expecting image file name and size");
  3701. }
  3702. filename = argv[optind++];
  3703. /* Choose grow, shrink, or absolute resize mode */
  3704. switch (size[0]) {
  3705. case '+':
  3706. relative = 1;
  3707. size++;
  3708. break;
  3709. case '-':
  3710. relative = -1;
  3711. size++;
  3712. break;
  3713. default:
  3714. relative = 0;
  3715. break;
  3716. }
  3717. /* Parse size */
  3718. param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
  3719. if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) {
  3720. error_report_err(err);
  3721. ret = -1;
  3722. qemu_opts_del(param);
  3723. goto out;
  3724. }
  3725. n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
  3726. qemu_opts_del(param);
  3727. blk = img_open(image_opts, filename, fmt,
  3728. BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
  3729. false);
  3730. if (!blk) {
  3731. ret = -1;
  3732. goto out;
  3733. }
  3734. current_size = blk_getlength(blk);
  3735. if (current_size < 0) {
  3736. error_report("Failed to inquire current image length: %s",
  3737. strerror(-current_size));
  3738. ret = -1;
  3739. goto out;
  3740. }
  3741. if (relative) {
  3742. total_size = current_size + n * relative;
  3743. } else {
  3744. total_size = n;
  3745. }
  3746. if (total_size <= 0) {
  3747. error_report("New image size must be positive");
  3748. ret = -1;
  3749. goto out;
  3750. }
  3751. if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
  3752. error_report("Preallocation can only be used for growing images");
  3753. ret = -1;
  3754. goto out;
  3755. }
  3756. if (total_size < current_size && !shrink) {
  3757. error_report("Use the --shrink option to perform a shrink operation.");
  3758. warn_report("Shrinking an image will delete all data beyond the "
  3759. "shrunken image's end. Before performing such an "
  3760. "operation, make sure there is no important data there.");
  3761. ret = -1;
  3762. goto out;
  3763. }
  3764. /*
  3765. * The user expects the image to have the desired size after
  3766. * resizing, so pass @exact=true. It is of no use to report
  3767. * success when the image has not actually been resized.
  3768. */
  3769. ret = blk_truncate(blk, total_size, true, prealloc, 0, &err);
  3770. if (!ret) {
  3771. qprintf(quiet, "Image resized.\n");
  3772. } else {
  3773. error_report_err(err);
  3774. }
  3775. out:
  3776. blk_unref(blk);
  3777. if (ret) {
  3778. return 1;
  3779. }
  3780. return 0;
  3781. }
  3782. static void amend_status_cb(BlockDriverState *bs,
  3783. int64_t offset, int64_t total_work_size,
  3784. void *opaque)
  3785. {
  3786. qemu_progress_print(100.f * offset / total_work_size, 0);
  3787. }
  3788. static int print_amend_option_help(const char *format)
  3789. {
  3790. BlockDriver *drv;
  3791. GRAPH_RDLOCK_GUARD_MAINLOOP();
  3792. /* Find driver and parse its options */
  3793. drv = bdrv_find_format(format);
  3794. if (!drv) {
  3795. error_report("Unknown file format '%s'", format);
  3796. return 1;
  3797. }
  3798. if (!drv->bdrv_amend_options) {
  3799. error_report("Format driver '%s' does not support option amendment",
  3800. format);
  3801. return 1;
  3802. }
  3803. /* Every driver supporting amendment must have amend_opts */
  3804. assert(drv->amend_opts);
  3805. printf("Amend options for '%s':\n", format);
  3806. qemu_opts_print_help(drv->amend_opts, false);
  3807. return 0;
  3808. }
  3809. static int img_amend(int argc, char **argv)
  3810. {
  3811. Error *err = NULL;
  3812. int c, ret = 0;
  3813. char *options = NULL;
  3814. QemuOptsList *amend_opts = NULL;
  3815. QemuOpts *opts = NULL;
  3816. const char *fmt = NULL, *filename, *cache;
  3817. int flags;
  3818. bool writethrough;
  3819. bool quiet = false, progress = false;
  3820. BlockBackend *blk = NULL;
  3821. BlockDriverState *bs = NULL;
  3822. bool image_opts = false;
  3823. bool force = false;
  3824. cache = BDRV_DEFAULT_CACHE;
  3825. for (;;) {
  3826. static const struct option long_options[] = {
  3827. {"help", no_argument, 0, 'h'},
  3828. {"object", required_argument, 0, OPTION_OBJECT},
  3829. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  3830. {"force", no_argument, 0, OPTION_FORCE},
  3831. {0, 0, 0, 0}
  3832. };
  3833. c = getopt_long(argc, argv, ":ho:f:t:pq",
  3834. long_options, NULL);
  3835. if (c == -1) {
  3836. break;
  3837. }
  3838. switch (c) {
  3839. case ':':
  3840. missing_argument(argv[optind - 1]);
  3841. break;
  3842. case '?':
  3843. unrecognized_option(argv[optind - 1]);
  3844. break;
  3845. case 'h':
  3846. help();
  3847. break;
  3848. case 'o':
  3849. if (accumulate_options(&options, optarg) < 0) {
  3850. ret = -1;
  3851. goto out_no_progress;
  3852. }
  3853. break;
  3854. case 'f':
  3855. fmt = optarg;
  3856. break;
  3857. case 't':
  3858. cache = optarg;
  3859. break;
  3860. case 'p':
  3861. progress = true;
  3862. break;
  3863. case 'q':
  3864. quiet = true;
  3865. break;
  3866. case OPTION_OBJECT:
  3867. user_creatable_process_cmdline(optarg);
  3868. break;
  3869. case OPTION_IMAGE_OPTS:
  3870. image_opts = true;
  3871. break;
  3872. case OPTION_FORCE:
  3873. force = true;
  3874. break;
  3875. }
  3876. }
  3877. if (!options) {
  3878. error_exit("Must specify options (-o)");
  3879. }
  3880. if (quiet) {
  3881. progress = false;
  3882. }
  3883. qemu_progress_init(progress, 1.0);
  3884. filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
  3885. if (fmt && has_help_option(options)) {
  3886. /* If a format is explicitly specified (and possibly no filename is
  3887. * given), print option help here */
  3888. ret = print_amend_option_help(fmt);
  3889. goto out;
  3890. }
  3891. if (optind != argc - 1) {
  3892. error_report("Expecting one image file name");
  3893. ret = -1;
  3894. goto out;
  3895. }
  3896. flags = BDRV_O_RDWR;
  3897. ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
  3898. if (ret < 0) {
  3899. error_report("Invalid cache option: %s", cache);
  3900. goto out;
  3901. }
  3902. blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
  3903. false);
  3904. if (!blk) {
  3905. ret = -1;
  3906. goto out;
  3907. }
  3908. bs = blk_bs(blk);
  3909. fmt = bs->drv->format_name;
  3910. if (has_help_option(options)) {
  3911. /* If the format was auto-detected, print option help here */
  3912. ret = print_amend_option_help(fmt);
  3913. goto out;
  3914. }
  3915. bdrv_graph_rdlock_main_loop();
  3916. if (!bs->drv->bdrv_amend_options) {
  3917. error_report("Format driver '%s' does not support option amendment",
  3918. fmt);
  3919. bdrv_graph_rdunlock_main_loop();
  3920. ret = -1;
  3921. goto out;
  3922. }
  3923. /* Every driver supporting amendment must have amend_opts */
  3924. assert(bs->drv->amend_opts);
  3925. amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts);
  3926. opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort);
  3927. if (!qemu_opts_do_parse(opts, options, NULL, &err)) {
  3928. /* Try to parse options using the create options */
  3929. amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts);
  3930. qemu_opts_del(opts);
  3931. opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort);
  3932. if (qemu_opts_do_parse(opts, options, NULL, NULL)) {
  3933. error_append_hint(&err,
  3934. "This option is only supported for image creation\n");
  3935. }
  3936. bdrv_graph_rdunlock_main_loop();
  3937. error_report_err(err);
  3938. ret = -1;
  3939. goto out;
  3940. }
  3941. /* In case the driver does not call amend_status_cb() */
  3942. qemu_progress_print(0.f, 0);
  3943. ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err);
  3944. qemu_progress_print(100.f, 0);
  3945. bdrv_graph_rdunlock_main_loop();
  3946. if (ret < 0) {
  3947. error_report_err(err);
  3948. goto out;
  3949. }
  3950. out:
  3951. qemu_progress_end();
  3952. out_no_progress:
  3953. blk_unref(blk);
  3954. qemu_opts_del(opts);
  3955. qemu_opts_free(amend_opts);
  3956. g_free(options);
  3957. if (ret) {
  3958. return 1;
  3959. }
  3960. return 0;
  3961. }
  3962. typedef struct BenchData {
  3963. BlockBackend *blk;
  3964. uint64_t image_size;
  3965. bool write;
  3966. int bufsize;
  3967. int step;
  3968. int nrreq;
  3969. int n;
  3970. int flush_interval;
  3971. bool drain_on_flush;
  3972. uint8_t *buf;
  3973. QEMUIOVector *qiov;
  3974. int in_flight;
  3975. bool in_flush;
  3976. uint64_t offset;
  3977. } BenchData;
  3978. static void bench_undrained_flush_cb(void *opaque, int ret)
  3979. {
  3980. if (ret < 0) {
  3981. error_report("Failed flush request: %s", strerror(-ret));
  3982. exit(EXIT_FAILURE);
  3983. }
  3984. }
  3985. static void bench_cb(void *opaque, int ret)
  3986. {
  3987. BenchData *b = opaque;
  3988. BlockAIOCB *acb;
  3989. if (ret < 0) {
  3990. error_report("Failed request: %s", strerror(-ret));
  3991. exit(EXIT_FAILURE);
  3992. }
  3993. if (b->in_flush) {
  3994. /* Just finished a flush with drained queue: Start next requests */
  3995. assert(b->in_flight == 0);
  3996. b->in_flush = false;
  3997. } else if (b->in_flight > 0) {
  3998. int remaining = b->n - b->in_flight;
  3999. b->n--;
  4000. b->in_flight--;
  4001. /* Time for flush? Drain queue if requested, then flush */
  4002. if (b->flush_interval && remaining % b->flush_interval == 0) {
  4003. if (!b->in_flight || !b->drain_on_flush) {
  4004. BlockCompletionFunc *cb;
  4005. if (b->drain_on_flush) {
  4006. b->in_flush = true;
  4007. cb = bench_cb;
  4008. } else {
  4009. cb = bench_undrained_flush_cb;
  4010. }
  4011. acb = blk_aio_flush(b->blk, cb, b);
  4012. if (!acb) {
  4013. error_report("Failed to issue flush request");
  4014. exit(EXIT_FAILURE);
  4015. }
  4016. }
  4017. if (b->drain_on_flush) {
  4018. return;
  4019. }
  4020. }
  4021. }
  4022. while (b->n > b->in_flight && b->in_flight < b->nrreq) {
  4023. int64_t offset = b->offset;
  4024. /* blk_aio_* might look for completed I/Os and kick bench_cb
  4025. * again, so make sure this operation is counted by in_flight
  4026. * and b->offset is ready for the next submission.
  4027. */
  4028. b->in_flight++;
  4029. b->offset += b->step;
  4030. b->offset %= b->image_size;
  4031. if (b->write) {
  4032. acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
  4033. } else {
  4034. acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
  4035. }
  4036. if (!acb) {
  4037. error_report("Failed to issue request");
  4038. exit(EXIT_FAILURE);
  4039. }
  4040. }
  4041. }
  4042. static int img_bench(int argc, char **argv)
  4043. {
  4044. int c, ret = 0;
  4045. const char *fmt = NULL, *filename;
  4046. bool quiet = false;
  4047. bool image_opts = false;
  4048. bool is_write = false;
  4049. int count = 75000;
  4050. int depth = 64;
  4051. int64_t offset = 0;
  4052. size_t bufsize = 4096;
  4053. int pattern = 0;
  4054. size_t step = 0;
  4055. int flush_interval = 0;
  4056. bool drain_on_flush = true;
  4057. int64_t image_size;
  4058. BlockBackend *blk = NULL;
  4059. BenchData data = {};
  4060. int flags = 0;
  4061. bool writethrough = false;
  4062. struct timeval t1, t2;
  4063. int i;
  4064. bool force_share = false;
  4065. size_t buf_size = 0;
  4066. for (;;) {
  4067. static const struct option long_options[] = {
  4068. {"help", no_argument, 0, 'h'},
  4069. {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
  4070. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4071. {"pattern", required_argument, 0, OPTION_PATTERN},
  4072. {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
  4073. {"force-share", no_argument, 0, 'U'},
  4074. {0, 0, 0, 0}
  4075. };
  4076. c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options,
  4077. NULL);
  4078. if (c == -1) {
  4079. break;
  4080. }
  4081. switch (c) {
  4082. case ':':
  4083. missing_argument(argv[optind - 1]);
  4084. break;
  4085. case '?':
  4086. unrecognized_option(argv[optind - 1]);
  4087. break;
  4088. case 'h':
  4089. help();
  4090. break;
  4091. case 'c':
  4092. {
  4093. unsigned long res;
  4094. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
  4095. error_report("Invalid request count specified");
  4096. return 1;
  4097. }
  4098. count = res;
  4099. break;
  4100. }
  4101. case 'd':
  4102. {
  4103. unsigned long res;
  4104. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
  4105. error_report("Invalid queue depth specified");
  4106. return 1;
  4107. }
  4108. depth = res;
  4109. break;
  4110. }
  4111. case 'f':
  4112. fmt = optarg;
  4113. break;
  4114. case 'n':
  4115. flags |= BDRV_O_NATIVE_AIO;
  4116. break;
  4117. case 'i':
  4118. ret = bdrv_parse_aio(optarg, &flags);
  4119. if (ret < 0) {
  4120. error_report("Invalid aio option: %s", optarg);
  4121. ret = -1;
  4122. goto out;
  4123. }
  4124. break;
  4125. case 'o':
  4126. {
  4127. offset = cvtnum("offset", optarg);
  4128. if (offset < 0) {
  4129. return 1;
  4130. }
  4131. break;
  4132. }
  4133. break;
  4134. case 'q':
  4135. quiet = true;
  4136. break;
  4137. case 's':
  4138. {
  4139. int64_t sval;
  4140. sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
  4141. if (sval < 0) {
  4142. return 1;
  4143. }
  4144. bufsize = sval;
  4145. break;
  4146. }
  4147. case 'S':
  4148. {
  4149. int64_t sval;
  4150. sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
  4151. if (sval < 0) {
  4152. return 1;
  4153. }
  4154. step = sval;
  4155. break;
  4156. }
  4157. case 't':
  4158. ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
  4159. if (ret < 0) {
  4160. error_report("Invalid cache mode");
  4161. ret = -1;
  4162. goto out;
  4163. }
  4164. break;
  4165. case 'w':
  4166. flags |= BDRV_O_RDWR;
  4167. is_write = true;
  4168. break;
  4169. case 'U':
  4170. force_share = true;
  4171. break;
  4172. case OPTION_PATTERN:
  4173. {
  4174. unsigned long res;
  4175. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
  4176. error_report("Invalid pattern byte specified");
  4177. return 1;
  4178. }
  4179. pattern = res;
  4180. break;
  4181. }
  4182. case OPTION_FLUSH_INTERVAL:
  4183. {
  4184. unsigned long res;
  4185. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
  4186. error_report("Invalid flush interval specified");
  4187. return 1;
  4188. }
  4189. flush_interval = res;
  4190. break;
  4191. }
  4192. case OPTION_NO_DRAIN:
  4193. drain_on_flush = false;
  4194. break;
  4195. case OPTION_IMAGE_OPTS:
  4196. image_opts = true;
  4197. break;
  4198. }
  4199. }
  4200. if (optind != argc - 1) {
  4201. error_exit("Expecting one image file name");
  4202. }
  4203. filename = argv[argc - 1];
  4204. if (!is_write && flush_interval) {
  4205. error_report("--flush-interval is only available in write tests");
  4206. ret = -1;
  4207. goto out;
  4208. }
  4209. if (flush_interval && flush_interval < depth) {
  4210. error_report("Flush interval can't be smaller than depth");
  4211. ret = -1;
  4212. goto out;
  4213. }
  4214. blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
  4215. force_share);
  4216. if (!blk) {
  4217. ret = -1;
  4218. goto out;
  4219. }
  4220. image_size = blk_getlength(blk);
  4221. if (image_size < 0) {
  4222. ret = image_size;
  4223. goto out;
  4224. }
  4225. data = (BenchData) {
  4226. .blk = blk,
  4227. .image_size = image_size,
  4228. .bufsize = bufsize,
  4229. .step = step ?: bufsize,
  4230. .nrreq = depth,
  4231. .n = count,
  4232. .offset = offset,
  4233. .write = is_write,
  4234. .flush_interval = flush_interval,
  4235. .drain_on_flush = drain_on_flush,
  4236. };
  4237. printf("Sending %d %s requests, %d bytes each, %d in parallel "
  4238. "(starting at offset %" PRId64 ", step size %d)\n",
  4239. data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
  4240. data.offset, data.step);
  4241. if (flush_interval) {
  4242. printf("Sending flush every %d requests\n", flush_interval);
  4243. }
  4244. buf_size = data.nrreq * data.bufsize;
  4245. data.buf = blk_blockalign(blk, buf_size);
  4246. memset(data.buf, pattern, data.nrreq * data.bufsize);
  4247. blk_register_buf(blk, data.buf, buf_size, &error_fatal);
  4248. data.qiov = g_new(QEMUIOVector, data.nrreq);
  4249. for (i = 0; i < data.nrreq; i++) {
  4250. qemu_iovec_init(&data.qiov[i], 1);
  4251. qemu_iovec_add(&data.qiov[i],
  4252. data.buf + i * data.bufsize, data.bufsize);
  4253. }
  4254. gettimeofday(&t1, NULL);
  4255. bench_cb(&data, 0);
  4256. while (data.n > 0) {
  4257. main_loop_wait(false);
  4258. }
  4259. gettimeofday(&t2, NULL);
  4260. printf("Run completed in %3.3f seconds.\n",
  4261. (t2.tv_sec - t1.tv_sec)
  4262. + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
  4263. out:
  4264. if (data.buf) {
  4265. blk_unregister_buf(blk, data.buf, buf_size);
  4266. }
  4267. qemu_vfree(data.buf);
  4268. blk_unref(blk);
  4269. if (ret) {
  4270. return 1;
  4271. }
  4272. return 0;
  4273. }
  4274. enum ImgBitmapAct {
  4275. BITMAP_ADD,
  4276. BITMAP_REMOVE,
  4277. BITMAP_CLEAR,
  4278. BITMAP_ENABLE,
  4279. BITMAP_DISABLE,
  4280. BITMAP_MERGE,
  4281. };
  4282. typedef struct ImgBitmapAction {
  4283. enum ImgBitmapAct act;
  4284. const char *src; /* only used for merge */
  4285. QSIMPLEQ_ENTRY(ImgBitmapAction) next;
  4286. } ImgBitmapAction;
  4287. static int img_bitmap(int argc, char **argv)
  4288. {
  4289. Error *err = NULL;
  4290. int c, ret = 1;
  4291. QemuOpts *opts = NULL;
  4292. const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL;
  4293. const char *filename, *bitmap;
  4294. BlockBackend *blk = NULL, *src = NULL;
  4295. BlockDriverState *bs = NULL, *src_bs = NULL;
  4296. bool image_opts = false;
  4297. int64_t granularity = 0;
  4298. bool add = false, merge = false;
  4299. QSIMPLEQ_HEAD(, ImgBitmapAction) actions;
  4300. ImgBitmapAction *act, *act_next;
  4301. const char *op;
  4302. int inactivate_ret;
  4303. QSIMPLEQ_INIT(&actions);
  4304. for (;;) {
  4305. static const struct option long_options[] = {
  4306. {"help", no_argument, 0, 'h'},
  4307. {"object", required_argument, 0, OPTION_OBJECT},
  4308. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4309. {"add", no_argument, 0, OPTION_ADD},
  4310. {"remove", no_argument, 0, OPTION_REMOVE},
  4311. {"clear", no_argument, 0, OPTION_CLEAR},
  4312. {"enable", no_argument, 0, OPTION_ENABLE},
  4313. {"disable", no_argument, 0, OPTION_DISABLE},
  4314. {"merge", required_argument, 0, OPTION_MERGE},
  4315. {"granularity", required_argument, 0, 'g'},
  4316. {"source-file", required_argument, 0, 'b'},
  4317. {"source-format", required_argument, 0, 'F'},
  4318. {0, 0, 0, 0}
  4319. };
  4320. c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL);
  4321. if (c == -1) {
  4322. break;
  4323. }
  4324. switch (c) {
  4325. case ':':
  4326. missing_argument(argv[optind - 1]);
  4327. break;
  4328. case '?':
  4329. unrecognized_option(argv[optind - 1]);
  4330. break;
  4331. case 'h':
  4332. help();
  4333. break;
  4334. case 'b':
  4335. src_filename = optarg;
  4336. break;
  4337. case 'f':
  4338. fmt = optarg;
  4339. break;
  4340. case 'F':
  4341. src_fmt = optarg;
  4342. break;
  4343. case 'g':
  4344. granularity = cvtnum("granularity", optarg);
  4345. if (granularity < 0) {
  4346. return 1;
  4347. }
  4348. break;
  4349. case OPTION_ADD:
  4350. act = g_new0(ImgBitmapAction, 1);
  4351. act->act = BITMAP_ADD;
  4352. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4353. add = true;
  4354. break;
  4355. case OPTION_REMOVE:
  4356. act = g_new0(ImgBitmapAction, 1);
  4357. act->act = BITMAP_REMOVE;
  4358. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4359. break;
  4360. case OPTION_CLEAR:
  4361. act = g_new0(ImgBitmapAction, 1);
  4362. act->act = BITMAP_CLEAR;
  4363. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4364. break;
  4365. case OPTION_ENABLE:
  4366. act = g_new0(ImgBitmapAction, 1);
  4367. act->act = BITMAP_ENABLE;
  4368. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4369. break;
  4370. case OPTION_DISABLE:
  4371. act = g_new0(ImgBitmapAction, 1);
  4372. act->act = BITMAP_DISABLE;
  4373. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4374. break;
  4375. case OPTION_MERGE:
  4376. act = g_new0(ImgBitmapAction, 1);
  4377. act->act = BITMAP_MERGE;
  4378. act->src = optarg;
  4379. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4380. merge = true;
  4381. break;
  4382. case OPTION_OBJECT:
  4383. user_creatable_process_cmdline(optarg);
  4384. break;
  4385. case OPTION_IMAGE_OPTS:
  4386. image_opts = true;
  4387. break;
  4388. }
  4389. }
  4390. if (QSIMPLEQ_EMPTY(&actions)) {
  4391. error_report("Need at least one of --add, --remove, --clear, "
  4392. "--enable, --disable, or --merge");
  4393. goto out;
  4394. }
  4395. if (granularity && !add) {
  4396. error_report("granularity only supported with --add");
  4397. goto out;
  4398. }
  4399. if (src_fmt && !src_filename) {
  4400. error_report("-F only supported with -b");
  4401. goto out;
  4402. }
  4403. if (src_filename && !merge) {
  4404. error_report("Merge bitmap source file only supported with "
  4405. "--merge");
  4406. goto out;
  4407. }
  4408. if (optind != argc - 2) {
  4409. error_report("Expecting filename and bitmap name");
  4410. goto out;
  4411. }
  4412. filename = argv[optind];
  4413. bitmap = argv[optind + 1];
  4414. /*
  4415. * No need to open backing chains; we will be manipulating bitmaps
  4416. * directly in this image without reference to image contents.
  4417. */
  4418. blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING,
  4419. false, false, false);
  4420. if (!blk) {
  4421. goto out;
  4422. }
  4423. bs = blk_bs(blk);
  4424. if (src_filename) {
  4425. src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING,
  4426. false, false, false);
  4427. if (!src) {
  4428. goto out;
  4429. }
  4430. src_bs = blk_bs(src);
  4431. } else {
  4432. src_bs = bs;
  4433. }
  4434. QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) {
  4435. switch (act->act) {
  4436. case BITMAP_ADD:
  4437. qmp_block_dirty_bitmap_add(bs->node_name, bitmap,
  4438. !!granularity, granularity, true, true,
  4439. false, false, &err);
  4440. op = "add";
  4441. break;
  4442. case BITMAP_REMOVE:
  4443. qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err);
  4444. op = "remove";
  4445. break;
  4446. case BITMAP_CLEAR:
  4447. qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err);
  4448. op = "clear";
  4449. break;
  4450. case BITMAP_ENABLE:
  4451. qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err);
  4452. op = "enable";
  4453. break;
  4454. case BITMAP_DISABLE:
  4455. qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err);
  4456. op = "disable";
  4457. break;
  4458. case BITMAP_MERGE:
  4459. do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name,
  4460. act->src, &err);
  4461. op = "merge";
  4462. break;
  4463. default:
  4464. g_assert_not_reached();
  4465. }
  4466. if (err) {
  4467. error_reportf_err(err, "Operation %s on bitmap %s failed: ",
  4468. op, bitmap);
  4469. goto out;
  4470. }
  4471. g_free(act);
  4472. }
  4473. ret = 0;
  4474. out:
  4475. /*
  4476. * Manually inactivate the images first because this way we can know whether
  4477. * an error occurred. blk_unref() doesn't tell us about failures.
  4478. */
  4479. inactivate_ret = bdrv_inactivate_all();
  4480. if (inactivate_ret < 0) {
  4481. error_report("Error while closing the image: %s", strerror(-inactivate_ret));
  4482. ret = 1;
  4483. }
  4484. blk_unref(src);
  4485. blk_unref(blk);
  4486. qemu_opts_del(opts);
  4487. return ret;
  4488. }
  4489. #define C_BS 01
  4490. #define C_COUNT 02
  4491. #define C_IF 04
  4492. #define C_OF 010
  4493. #define C_SKIP 020
  4494. struct DdInfo {
  4495. unsigned int flags;
  4496. int64_t count;
  4497. };
  4498. struct DdIo {
  4499. int bsz; /* Block size */
  4500. char *filename;
  4501. uint8_t *buf;
  4502. int64_t offset;
  4503. };
  4504. struct DdOpts {
  4505. const char *name;
  4506. int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
  4507. unsigned int flag;
  4508. };
  4509. static int img_dd_bs(const char *arg,
  4510. struct DdIo *in, struct DdIo *out,
  4511. struct DdInfo *dd)
  4512. {
  4513. int64_t res;
  4514. res = cvtnum_full("bs", arg, 1, INT_MAX);
  4515. if (res < 0) {
  4516. return 1;
  4517. }
  4518. in->bsz = out->bsz = res;
  4519. return 0;
  4520. }
  4521. static int img_dd_count(const char *arg,
  4522. struct DdIo *in, struct DdIo *out,
  4523. struct DdInfo *dd)
  4524. {
  4525. dd->count = cvtnum("count", arg);
  4526. if (dd->count < 0) {
  4527. return 1;
  4528. }
  4529. return 0;
  4530. }
  4531. static int img_dd_if(const char *arg,
  4532. struct DdIo *in, struct DdIo *out,
  4533. struct DdInfo *dd)
  4534. {
  4535. in->filename = g_strdup(arg);
  4536. return 0;
  4537. }
  4538. static int img_dd_of(const char *arg,
  4539. struct DdIo *in, struct DdIo *out,
  4540. struct DdInfo *dd)
  4541. {
  4542. out->filename = g_strdup(arg);
  4543. return 0;
  4544. }
  4545. static int img_dd_skip(const char *arg,
  4546. struct DdIo *in, struct DdIo *out,
  4547. struct DdInfo *dd)
  4548. {
  4549. in->offset = cvtnum("skip", arg);
  4550. if (in->offset < 0) {
  4551. return 1;
  4552. }
  4553. return 0;
  4554. }
  4555. static int img_dd(int argc, char **argv)
  4556. {
  4557. int ret = 0;
  4558. char *arg = NULL;
  4559. char *tmp;
  4560. BlockDriver *drv = NULL, *proto_drv = NULL;
  4561. BlockBackend *blk1 = NULL, *blk2 = NULL;
  4562. QemuOpts *opts = NULL;
  4563. QemuOptsList *create_opts = NULL;
  4564. Error *local_err = NULL;
  4565. bool image_opts = false;
  4566. int c, i;
  4567. const char *out_fmt = "raw";
  4568. const char *fmt = NULL;
  4569. int64_t size = 0;
  4570. int64_t out_pos, in_pos;
  4571. bool force_share = false;
  4572. struct DdInfo dd = {
  4573. .flags = 0,
  4574. .count = 0,
  4575. };
  4576. struct DdIo in = {
  4577. .bsz = 512, /* Block size is by default 512 bytes */
  4578. .filename = NULL,
  4579. .buf = NULL,
  4580. .offset = 0
  4581. };
  4582. struct DdIo out = {
  4583. .bsz = 512,
  4584. .filename = NULL,
  4585. .buf = NULL,
  4586. .offset = 0
  4587. };
  4588. const struct DdOpts options[] = {
  4589. { "bs", img_dd_bs, C_BS },
  4590. { "count", img_dd_count, C_COUNT },
  4591. { "if", img_dd_if, C_IF },
  4592. { "of", img_dd_of, C_OF },
  4593. { "skip", img_dd_skip, C_SKIP },
  4594. { NULL, NULL, 0 }
  4595. };
  4596. const struct option long_options[] = {
  4597. { "help", no_argument, 0, 'h'},
  4598. { "object", required_argument, 0, OPTION_OBJECT},
  4599. { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4600. { "force-share", no_argument, 0, 'U'},
  4601. { 0, 0, 0, 0 }
  4602. };
  4603. while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
  4604. if (c == EOF) {
  4605. break;
  4606. }
  4607. switch (c) {
  4608. case 'O':
  4609. out_fmt = optarg;
  4610. break;
  4611. case 'f':
  4612. fmt = optarg;
  4613. break;
  4614. case ':':
  4615. missing_argument(argv[optind - 1]);
  4616. break;
  4617. case '?':
  4618. unrecognized_option(argv[optind - 1]);
  4619. break;
  4620. case 'h':
  4621. help();
  4622. break;
  4623. case 'U':
  4624. force_share = true;
  4625. break;
  4626. case OPTION_OBJECT:
  4627. user_creatable_process_cmdline(optarg);
  4628. break;
  4629. case OPTION_IMAGE_OPTS:
  4630. image_opts = true;
  4631. break;
  4632. }
  4633. }
  4634. for (i = optind; i < argc; i++) {
  4635. int j;
  4636. arg = g_strdup(argv[i]);
  4637. tmp = strchr(arg, '=');
  4638. if (tmp == NULL) {
  4639. error_report("unrecognized operand %s", arg);
  4640. ret = -1;
  4641. goto out;
  4642. }
  4643. *tmp++ = '\0';
  4644. for (j = 0; options[j].name != NULL; j++) {
  4645. if (!strcmp(arg, options[j].name)) {
  4646. break;
  4647. }
  4648. }
  4649. if (options[j].name == NULL) {
  4650. error_report("unrecognized operand %s", arg);
  4651. ret = -1;
  4652. goto out;
  4653. }
  4654. if (options[j].f(tmp, &in, &out, &dd) != 0) {
  4655. ret = -1;
  4656. goto out;
  4657. }
  4658. dd.flags |= options[j].flag;
  4659. g_free(arg);
  4660. arg = NULL;
  4661. }
  4662. if (!(dd.flags & C_IF && dd.flags & C_OF)) {
  4663. error_report("Must specify both input and output files");
  4664. ret = -1;
  4665. goto out;
  4666. }
  4667. blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
  4668. force_share);
  4669. if (!blk1) {
  4670. ret = -1;
  4671. goto out;
  4672. }
  4673. drv = bdrv_find_format(out_fmt);
  4674. if (!drv) {
  4675. error_report("Unknown file format");
  4676. ret = -1;
  4677. goto out;
  4678. }
  4679. proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
  4680. if (!proto_drv) {
  4681. error_report_err(local_err);
  4682. ret = -1;
  4683. goto out;
  4684. }
  4685. if (!drv->create_opts) {
  4686. error_report("Format driver '%s' does not support image creation",
  4687. drv->format_name);
  4688. ret = -1;
  4689. goto out;
  4690. }
  4691. if (!proto_drv->create_opts) {
  4692. error_report("Protocol driver '%s' does not support image creation",
  4693. proto_drv->format_name);
  4694. ret = -1;
  4695. goto out;
  4696. }
  4697. create_opts = qemu_opts_append(create_opts, drv->create_opts);
  4698. create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
  4699. opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
  4700. size = blk_getlength(blk1);
  4701. if (size < 0) {
  4702. error_report("Failed to get size for '%s'", in.filename);
  4703. ret = -1;
  4704. goto out;
  4705. }
  4706. if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
  4707. dd.count * in.bsz < size) {
  4708. size = dd.count * in.bsz;
  4709. }
  4710. /* Overflow means the specified offset is beyond input image's size */
  4711. if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
  4712. size < in.bsz * in.offset)) {
  4713. qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
  4714. } else {
  4715. qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
  4716. size - in.bsz * in.offset, &error_abort);
  4717. }
  4718. ret = bdrv_create(drv, out.filename, opts, &local_err);
  4719. if (ret < 0) {
  4720. error_reportf_err(local_err,
  4721. "%s: error while creating output image: ",
  4722. out.filename);
  4723. ret = -1;
  4724. goto out;
  4725. }
  4726. /* TODO, we can't honour --image-opts for the target,
  4727. * since it needs to be given in a format compatible
  4728. * with the bdrv_create() call above which does not
  4729. * support image-opts style.
  4730. */
  4731. blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
  4732. false, false, false);
  4733. if (!blk2) {
  4734. ret = -1;
  4735. goto out;
  4736. }
  4737. if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
  4738. size < in.offset * in.bsz)) {
  4739. /* We give a warning if the skip option is bigger than the input
  4740. * size and create an empty output disk image (i.e. like dd(1)).
  4741. */
  4742. error_report("%s: cannot skip to specified offset", in.filename);
  4743. in_pos = size;
  4744. } else {
  4745. in_pos = in.offset * in.bsz;
  4746. }
  4747. in.buf = g_new(uint8_t, in.bsz);
  4748. for (out_pos = 0; in_pos < size; ) {
  4749. int bytes = (in_pos + in.bsz > size) ? size - in_pos : in.bsz;
  4750. ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
  4751. if (ret < 0) {
  4752. error_report("error while reading from input image file: %s",
  4753. strerror(-ret));
  4754. goto out;
  4755. }
  4756. in_pos += bytes;
  4757. ret = blk_pwrite(blk2, out_pos, bytes, in.buf, 0);
  4758. if (ret < 0) {
  4759. error_report("error while writing to output image file: %s",
  4760. strerror(-ret));
  4761. goto out;
  4762. }
  4763. out_pos += bytes;
  4764. }
  4765. out:
  4766. g_free(arg);
  4767. qemu_opts_del(opts);
  4768. qemu_opts_free(create_opts);
  4769. blk_unref(blk1);
  4770. blk_unref(blk2);
  4771. g_free(in.filename);
  4772. g_free(out.filename);
  4773. g_free(in.buf);
  4774. g_free(out.buf);
  4775. if (ret) {
  4776. return 1;
  4777. }
  4778. return 0;
  4779. }
  4780. static void dump_json_block_measure_info(BlockMeasureInfo *info)
  4781. {
  4782. GString *str;
  4783. QObject *obj;
  4784. Visitor *v = qobject_output_visitor_new(&obj);
  4785. visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
  4786. visit_complete(v, &obj);
  4787. str = qobject_to_json_pretty(obj, true);
  4788. assert(str != NULL);
  4789. printf("%s\n", str->str);
  4790. qobject_unref(obj);
  4791. visit_free(v);
  4792. g_string_free(str, true);
  4793. }
  4794. static int img_measure(int argc, char **argv)
  4795. {
  4796. static const struct option long_options[] = {
  4797. {"help", no_argument, 0, 'h'},
  4798. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4799. {"object", required_argument, 0, OPTION_OBJECT},
  4800. {"output", required_argument, 0, OPTION_OUTPUT},
  4801. {"size", required_argument, 0, OPTION_SIZE},
  4802. {"force-share", no_argument, 0, 'U'},
  4803. {0, 0, 0, 0}
  4804. };
  4805. OutputFormat output_format = OFORMAT_HUMAN;
  4806. BlockBackend *in_blk = NULL;
  4807. BlockDriver *drv;
  4808. const char *filename = NULL;
  4809. const char *fmt = NULL;
  4810. const char *out_fmt = "raw";
  4811. char *options = NULL;
  4812. char *snapshot_name = NULL;
  4813. bool force_share = false;
  4814. QemuOpts *opts = NULL;
  4815. QemuOpts *object_opts = NULL;
  4816. QemuOpts *sn_opts = NULL;
  4817. QemuOptsList *create_opts = NULL;
  4818. bool image_opts = false;
  4819. uint64_t img_size = UINT64_MAX;
  4820. BlockMeasureInfo *info = NULL;
  4821. Error *local_err = NULL;
  4822. int ret = 1;
  4823. int c;
  4824. while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
  4825. long_options, NULL)) != -1) {
  4826. switch (c) {
  4827. case '?':
  4828. case 'h':
  4829. help();
  4830. break;
  4831. case 'f':
  4832. fmt = optarg;
  4833. break;
  4834. case 'O':
  4835. out_fmt = optarg;
  4836. break;
  4837. case 'o':
  4838. if (accumulate_options(&options, optarg) < 0) {
  4839. goto out;
  4840. }
  4841. break;
  4842. case 'l':
  4843. if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
  4844. sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
  4845. optarg, false);
  4846. if (!sn_opts) {
  4847. error_report("Failed in parsing snapshot param '%s'",
  4848. optarg);
  4849. goto out;
  4850. }
  4851. } else {
  4852. snapshot_name = optarg;
  4853. }
  4854. break;
  4855. case 'U':
  4856. force_share = true;
  4857. break;
  4858. case OPTION_OBJECT:
  4859. user_creatable_process_cmdline(optarg);
  4860. break;
  4861. case OPTION_IMAGE_OPTS:
  4862. image_opts = true;
  4863. break;
  4864. case OPTION_OUTPUT:
  4865. if (!strcmp(optarg, "json")) {
  4866. output_format = OFORMAT_JSON;
  4867. } else if (!strcmp(optarg, "human")) {
  4868. output_format = OFORMAT_HUMAN;
  4869. } else {
  4870. error_report("--output must be used with human or json "
  4871. "as argument.");
  4872. goto out;
  4873. }
  4874. break;
  4875. case OPTION_SIZE:
  4876. {
  4877. int64_t sval;
  4878. sval = cvtnum("image size", optarg);
  4879. if (sval < 0) {
  4880. goto out;
  4881. }
  4882. img_size = (uint64_t)sval;
  4883. }
  4884. break;
  4885. }
  4886. }
  4887. if (argc - optind > 1) {
  4888. error_report("At most one filename argument is allowed.");
  4889. goto out;
  4890. } else if (argc - optind == 1) {
  4891. filename = argv[optind];
  4892. }
  4893. if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) {
  4894. error_report("--image-opts, -f, and -l require a filename argument.");
  4895. goto out;
  4896. }
  4897. if (filename && img_size != UINT64_MAX) {
  4898. error_report("--size N cannot be used together with a filename.");
  4899. goto out;
  4900. }
  4901. if (!filename && img_size == UINT64_MAX) {
  4902. error_report("Either --size N or one filename must be specified.");
  4903. goto out;
  4904. }
  4905. if (filename) {
  4906. in_blk = img_open(image_opts, filename, fmt, 0,
  4907. false, false, force_share);
  4908. if (!in_blk) {
  4909. goto out;
  4910. }
  4911. if (sn_opts) {
  4912. bdrv_snapshot_load_tmp(blk_bs(in_blk),
  4913. qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
  4914. qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
  4915. &local_err);
  4916. } else if (snapshot_name != NULL) {
  4917. bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
  4918. snapshot_name, &local_err);
  4919. }
  4920. if (local_err) {
  4921. error_reportf_err(local_err, "Failed to load snapshot: ");
  4922. goto out;
  4923. }
  4924. }
  4925. drv = bdrv_find_format(out_fmt);
  4926. if (!drv) {
  4927. error_report("Unknown file format '%s'", out_fmt);
  4928. goto out;
  4929. }
  4930. if (!drv->create_opts) {
  4931. error_report("Format driver '%s' does not support image creation",
  4932. drv->format_name);
  4933. goto out;
  4934. }
  4935. create_opts = qemu_opts_append(create_opts, drv->create_opts);
  4936. create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
  4937. opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
  4938. if (options) {
  4939. if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
  4940. error_report_err(local_err);
  4941. error_report("Invalid options for file format '%s'", out_fmt);
  4942. goto out;
  4943. }
  4944. }
  4945. if (img_size != UINT64_MAX) {
  4946. qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
  4947. }
  4948. info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
  4949. if (local_err) {
  4950. error_report_err(local_err);
  4951. goto out;
  4952. }
  4953. if (output_format == OFORMAT_HUMAN) {
  4954. printf("required size: %" PRIu64 "\n", info->required);
  4955. printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
  4956. if (info->has_bitmaps) {
  4957. printf("bitmaps size: %" PRIu64 "\n", info->bitmaps);
  4958. }
  4959. } else {
  4960. dump_json_block_measure_info(info);
  4961. }
  4962. ret = 0;
  4963. out:
  4964. qapi_free_BlockMeasureInfo(info);
  4965. qemu_opts_del(object_opts);
  4966. qemu_opts_del(opts);
  4967. qemu_opts_del(sn_opts);
  4968. qemu_opts_free(create_opts);
  4969. g_free(options);
  4970. blk_unref(in_blk);
  4971. return ret;
  4972. }
  4973. static const img_cmd_t img_cmds[] = {
  4974. #define DEF(option, callback, arg_string) \
  4975. { option, callback },
  4976. #include "qemu-img-cmds.h"
  4977. #undef DEF
  4978. { NULL, NULL, },
  4979. };
  4980. int main(int argc, char **argv)
  4981. {
  4982. const img_cmd_t *cmd;
  4983. const char *cmdname;
  4984. int c;
  4985. static const struct option long_options[] = {
  4986. {"help", no_argument, 0, 'h'},
  4987. {"version", no_argument, 0, 'V'},
  4988. {"trace", required_argument, NULL, 'T'},
  4989. {0, 0, 0, 0}
  4990. };
  4991. #ifdef CONFIG_POSIX
  4992. signal(SIGPIPE, SIG_IGN);
  4993. #endif
  4994. socket_init();
  4995. error_init(argv[0]);
  4996. module_call_init(MODULE_INIT_TRACE);
  4997. qemu_init_exec_dir(argv[0]);
  4998. qemu_init_main_loop(&error_fatal);
  4999. qcrypto_init(&error_fatal);
  5000. module_call_init(MODULE_INIT_QOM);
  5001. bdrv_init();
  5002. if (argc < 2) {
  5003. error_exit("Not enough arguments");
  5004. }
  5005. qemu_add_opts(&qemu_source_opts);
  5006. qemu_add_opts(&qemu_trace_opts);
  5007. while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
  5008. switch (c) {
  5009. case ':':
  5010. missing_argument(argv[optind - 1]);
  5011. return 0;
  5012. case '?':
  5013. unrecognized_option(argv[optind - 1]);
  5014. return 0;
  5015. case 'h':
  5016. help();
  5017. return 0;
  5018. case 'V':
  5019. printf(QEMU_IMG_VERSION);
  5020. return 0;
  5021. case 'T':
  5022. trace_opt_parse(optarg);
  5023. break;
  5024. }
  5025. }
  5026. cmdname = argv[optind];
  5027. /* reset getopt_long scanning */
  5028. argc -= optind;
  5029. if (argc < 1) {
  5030. return 0;
  5031. }
  5032. argv += optind;
  5033. qemu_reset_optind();
  5034. if (!trace_init_backends()) {
  5035. exit(1);
  5036. }
  5037. trace_init_file();
  5038. qemu_set_log(LOG_TRACE, &error_fatal);
  5039. /* find the command */
  5040. for (cmd = img_cmds; cmd->name != NULL; cmd++) {
  5041. if (!strcmp(cmdname, cmd->name)) {
  5042. return cmd->handler(argc, argv);
  5043. }
  5044. }
  5045. /* not found */
  5046. error_exit("Command not found: %s", cmdname);
  5047. }