qemu-img.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625
  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. if (b->image_size == 0) {
  4031. b->offset = 0;
  4032. } else {
  4033. b->offset %= b->image_size;
  4034. }
  4035. if (b->write) {
  4036. acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
  4037. } else {
  4038. acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
  4039. }
  4040. if (!acb) {
  4041. error_report("Failed to issue request");
  4042. exit(EXIT_FAILURE);
  4043. }
  4044. }
  4045. }
  4046. static int img_bench(int argc, char **argv)
  4047. {
  4048. int c, ret = 0;
  4049. const char *fmt = NULL, *filename;
  4050. bool quiet = false;
  4051. bool image_opts = false;
  4052. bool is_write = false;
  4053. int count = 75000;
  4054. int depth = 64;
  4055. int64_t offset = 0;
  4056. size_t bufsize = 4096;
  4057. int pattern = 0;
  4058. size_t step = 0;
  4059. int flush_interval = 0;
  4060. bool drain_on_flush = true;
  4061. int64_t image_size;
  4062. BlockBackend *blk = NULL;
  4063. BenchData data = {};
  4064. int flags = 0;
  4065. bool writethrough = false;
  4066. struct timeval t1, t2;
  4067. int i;
  4068. bool force_share = false;
  4069. size_t buf_size = 0;
  4070. for (;;) {
  4071. static const struct option long_options[] = {
  4072. {"help", no_argument, 0, 'h'},
  4073. {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
  4074. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4075. {"pattern", required_argument, 0, OPTION_PATTERN},
  4076. {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
  4077. {"force-share", no_argument, 0, 'U'},
  4078. {0, 0, 0, 0}
  4079. };
  4080. c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options,
  4081. NULL);
  4082. if (c == -1) {
  4083. break;
  4084. }
  4085. switch (c) {
  4086. case ':':
  4087. missing_argument(argv[optind - 1]);
  4088. break;
  4089. case '?':
  4090. unrecognized_option(argv[optind - 1]);
  4091. break;
  4092. case 'h':
  4093. help();
  4094. break;
  4095. case 'c':
  4096. {
  4097. unsigned long res;
  4098. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
  4099. error_report("Invalid request count specified");
  4100. return 1;
  4101. }
  4102. count = res;
  4103. break;
  4104. }
  4105. case 'd':
  4106. {
  4107. unsigned long res;
  4108. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
  4109. error_report("Invalid queue depth specified");
  4110. return 1;
  4111. }
  4112. depth = res;
  4113. break;
  4114. }
  4115. case 'f':
  4116. fmt = optarg;
  4117. break;
  4118. case 'n':
  4119. flags |= BDRV_O_NATIVE_AIO;
  4120. break;
  4121. case 'i':
  4122. ret = bdrv_parse_aio(optarg, &flags);
  4123. if (ret < 0) {
  4124. error_report("Invalid aio option: %s", optarg);
  4125. ret = -1;
  4126. goto out;
  4127. }
  4128. break;
  4129. case 'o':
  4130. {
  4131. offset = cvtnum("offset", optarg);
  4132. if (offset < 0) {
  4133. return 1;
  4134. }
  4135. break;
  4136. }
  4137. break;
  4138. case 'q':
  4139. quiet = true;
  4140. break;
  4141. case 's':
  4142. {
  4143. int64_t sval;
  4144. sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
  4145. if (sval < 0) {
  4146. return 1;
  4147. }
  4148. bufsize = sval;
  4149. break;
  4150. }
  4151. case 'S':
  4152. {
  4153. int64_t sval;
  4154. sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
  4155. if (sval < 0) {
  4156. return 1;
  4157. }
  4158. step = sval;
  4159. break;
  4160. }
  4161. case 't':
  4162. ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
  4163. if (ret < 0) {
  4164. error_report("Invalid cache mode");
  4165. ret = -1;
  4166. goto out;
  4167. }
  4168. break;
  4169. case 'w':
  4170. flags |= BDRV_O_RDWR;
  4171. is_write = true;
  4172. break;
  4173. case 'U':
  4174. force_share = true;
  4175. break;
  4176. case OPTION_PATTERN:
  4177. {
  4178. unsigned long res;
  4179. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
  4180. error_report("Invalid pattern byte specified");
  4181. return 1;
  4182. }
  4183. pattern = res;
  4184. break;
  4185. }
  4186. case OPTION_FLUSH_INTERVAL:
  4187. {
  4188. unsigned long res;
  4189. if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
  4190. error_report("Invalid flush interval specified");
  4191. return 1;
  4192. }
  4193. flush_interval = res;
  4194. break;
  4195. }
  4196. case OPTION_NO_DRAIN:
  4197. drain_on_flush = false;
  4198. break;
  4199. case OPTION_IMAGE_OPTS:
  4200. image_opts = true;
  4201. break;
  4202. }
  4203. }
  4204. if (optind != argc - 1) {
  4205. error_exit("Expecting one image file name");
  4206. }
  4207. filename = argv[argc - 1];
  4208. if (!is_write && flush_interval) {
  4209. error_report("--flush-interval is only available in write tests");
  4210. ret = -1;
  4211. goto out;
  4212. }
  4213. if (flush_interval && flush_interval < depth) {
  4214. error_report("Flush interval can't be smaller than depth");
  4215. ret = -1;
  4216. goto out;
  4217. }
  4218. blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
  4219. force_share);
  4220. if (!blk) {
  4221. ret = -1;
  4222. goto out;
  4223. }
  4224. image_size = blk_getlength(blk);
  4225. if (image_size < 0) {
  4226. ret = image_size;
  4227. goto out;
  4228. }
  4229. data = (BenchData) {
  4230. .blk = blk,
  4231. .image_size = image_size,
  4232. .bufsize = bufsize,
  4233. .step = step ?: bufsize,
  4234. .nrreq = depth,
  4235. .n = count,
  4236. .offset = offset,
  4237. .write = is_write,
  4238. .flush_interval = flush_interval,
  4239. .drain_on_flush = drain_on_flush,
  4240. };
  4241. printf("Sending %d %s requests, %d bytes each, %d in parallel "
  4242. "(starting at offset %" PRId64 ", step size %d)\n",
  4243. data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
  4244. data.offset, data.step);
  4245. if (flush_interval) {
  4246. printf("Sending flush every %d requests\n", flush_interval);
  4247. }
  4248. buf_size = data.nrreq * data.bufsize;
  4249. data.buf = blk_blockalign(blk, buf_size);
  4250. memset(data.buf, pattern, data.nrreq * data.bufsize);
  4251. blk_register_buf(blk, data.buf, buf_size, &error_fatal);
  4252. data.qiov = g_new(QEMUIOVector, data.nrreq);
  4253. for (i = 0; i < data.nrreq; i++) {
  4254. qemu_iovec_init(&data.qiov[i], 1);
  4255. qemu_iovec_add(&data.qiov[i],
  4256. data.buf + i * data.bufsize, data.bufsize);
  4257. }
  4258. gettimeofday(&t1, NULL);
  4259. bench_cb(&data, 0);
  4260. while (data.n > 0) {
  4261. main_loop_wait(false);
  4262. }
  4263. gettimeofday(&t2, NULL);
  4264. printf("Run completed in %3.3f seconds.\n",
  4265. (t2.tv_sec - t1.tv_sec)
  4266. + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
  4267. out:
  4268. if (data.buf) {
  4269. blk_unregister_buf(blk, data.buf, buf_size);
  4270. }
  4271. qemu_vfree(data.buf);
  4272. blk_unref(blk);
  4273. if (ret) {
  4274. return 1;
  4275. }
  4276. return 0;
  4277. }
  4278. enum ImgBitmapAct {
  4279. BITMAP_ADD,
  4280. BITMAP_REMOVE,
  4281. BITMAP_CLEAR,
  4282. BITMAP_ENABLE,
  4283. BITMAP_DISABLE,
  4284. BITMAP_MERGE,
  4285. };
  4286. typedef struct ImgBitmapAction {
  4287. enum ImgBitmapAct act;
  4288. const char *src; /* only used for merge */
  4289. QSIMPLEQ_ENTRY(ImgBitmapAction) next;
  4290. } ImgBitmapAction;
  4291. static int img_bitmap(int argc, char **argv)
  4292. {
  4293. Error *err = NULL;
  4294. int c, ret = 1;
  4295. QemuOpts *opts = NULL;
  4296. const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL;
  4297. const char *filename, *bitmap;
  4298. BlockBackend *blk = NULL, *src = NULL;
  4299. BlockDriverState *bs = NULL, *src_bs = NULL;
  4300. bool image_opts = false;
  4301. int64_t granularity = 0;
  4302. bool add = false, merge = false;
  4303. QSIMPLEQ_HEAD(, ImgBitmapAction) actions;
  4304. ImgBitmapAction *act, *act_next;
  4305. const char *op;
  4306. int inactivate_ret;
  4307. QSIMPLEQ_INIT(&actions);
  4308. for (;;) {
  4309. static const struct option long_options[] = {
  4310. {"help", no_argument, 0, 'h'},
  4311. {"object", required_argument, 0, OPTION_OBJECT},
  4312. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4313. {"add", no_argument, 0, OPTION_ADD},
  4314. {"remove", no_argument, 0, OPTION_REMOVE},
  4315. {"clear", no_argument, 0, OPTION_CLEAR},
  4316. {"enable", no_argument, 0, OPTION_ENABLE},
  4317. {"disable", no_argument, 0, OPTION_DISABLE},
  4318. {"merge", required_argument, 0, OPTION_MERGE},
  4319. {"granularity", required_argument, 0, 'g'},
  4320. {"source-file", required_argument, 0, 'b'},
  4321. {"source-format", required_argument, 0, 'F'},
  4322. {0, 0, 0, 0}
  4323. };
  4324. c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL);
  4325. if (c == -1) {
  4326. break;
  4327. }
  4328. switch (c) {
  4329. case ':':
  4330. missing_argument(argv[optind - 1]);
  4331. break;
  4332. case '?':
  4333. unrecognized_option(argv[optind - 1]);
  4334. break;
  4335. case 'h':
  4336. help();
  4337. break;
  4338. case 'b':
  4339. src_filename = optarg;
  4340. break;
  4341. case 'f':
  4342. fmt = optarg;
  4343. break;
  4344. case 'F':
  4345. src_fmt = optarg;
  4346. break;
  4347. case 'g':
  4348. granularity = cvtnum("granularity", optarg);
  4349. if (granularity < 0) {
  4350. return 1;
  4351. }
  4352. break;
  4353. case OPTION_ADD:
  4354. act = g_new0(ImgBitmapAction, 1);
  4355. act->act = BITMAP_ADD;
  4356. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4357. add = true;
  4358. break;
  4359. case OPTION_REMOVE:
  4360. act = g_new0(ImgBitmapAction, 1);
  4361. act->act = BITMAP_REMOVE;
  4362. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4363. break;
  4364. case OPTION_CLEAR:
  4365. act = g_new0(ImgBitmapAction, 1);
  4366. act->act = BITMAP_CLEAR;
  4367. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4368. break;
  4369. case OPTION_ENABLE:
  4370. act = g_new0(ImgBitmapAction, 1);
  4371. act->act = BITMAP_ENABLE;
  4372. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4373. break;
  4374. case OPTION_DISABLE:
  4375. act = g_new0(ImgBitmapAction, 1);
  4376. act->act = BITMAP_DISABLE;
  4377. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4378. break;
  4379. case OPTION_MERGE:
  4380. act = g_new0(ImgBitmapAction, 1);
  4381. act->act = BITMAP_MERGE;
  4382. act->src = optarg;
  4383. QSIMPLEQ_INSERT_TAIL(&actions, act, next);
  4384. merge = true;
  4385. break;
  4386. case OPTION_OBJECT:
  4387. user_creatable_process_cmdline(optarg);
  4388. break;
  4389. case OPTION_IMAGE_OPTS:
  4390. image_opts = true;
  4391. break;
  4392. }
  4393. }
  4394. if (QSIMPLEQ_EMPTY(&actions)) {
  4395. error_report("Need at least one of --add, --remove, --clear, "
  4396. "--enable, --disable, or --merge");
  4397. goto out;
  4398. }
  4399. if (granularity && !add) {
  4400. error_report("granularity only supported with --add");
  4401. goto out;
  4402. }
  4403. if (src_fmt && !src_filename) {
  4404. error_report("-F only supported with -b");
  4405. goto out;
  4406. }
  4407. if (src_filename && !merge) {
  4408. error_report("Merge bitmap source file only supported with "
  4409. "--merge");
  4410. goto out;
  4411. }
  4412. if (optind != argc - 2) {
  4413. error_report("Expecting filename and bitmap name");
  4414. goto out;
  4415. }
  4416. filename = argv[optind];
  4417. bitmap = argv[optind + 1];
  4418. /*
  4419. * No need to open backing chains; we will be manipulating bitmaps
  4420. * directly in this image without reference to image contents.
  4421. */
  4422. blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING,
  4423. false, false, false);
  4424. if (!blk) {
  4425. goto out;
  4426. }
  4427. bs = blk_bs(blk);
  4428. if (src_filename) {
  4429. src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING,
  4430. false, false, false);
  4431. if (!src) {
  4432. goto out;
  4433. }
  4434. src_bs = blk_bs(src);
  4435. } else {
  4436. src_bs = bs;
  4437. }
  4438. QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) {
  4439. switch (act->act) {
  4440. case BITMAP_ADD:
  4441. qmp_block_dirty_bitmap_add(bs->node_name, bitmap,
  4442. !!granularity, granularity, true, true,
  4443. false, false, &err);
  4444. op = "add";
  4445. break;
  4446. case BITMAP_REMOVE:
  4447. qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err);
  4448. op = "remove";
  4449. break;
  4450. case BITMAP_CLEAR:
  4451. qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err);
  4452. op = "clear";
  4453. break;
  4454. case BITMAP_ENABLE:
  4455. qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err);
  4456. op = "enable";
  4457. break;
  4458. case BITMAP_DISABLE:
  4459. qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err);
  4460. op = "disable";
  4461. break;
  4462. case BITMAP_MERGE:
  4463. do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name,
  4464. act->src, &err);
  4465. op = "merge";
  4466. break;
  4467. default:
  4468. g_assert_not_reached();
  4469. }
  4470. if (err) {
  4471. error_reportf_err(err, "Operation %s on bitmap %s failed: ",
  4472. op, bitmap);
  4473. goto out;
  4474. }
  4475. g_free(act);
  4476. }
  4477. ret = 0;
  4478. out:
  4479. /*
  4480. * Manually inactivate the images first because this way we can know whether
  4481. * an error occurred. blk_unref() doesn't tell us about failures.
  4482. */
  4483. inactivate_ret = bdrv_inactivate_all();
  4484. if (inactivate_ret < 0) {
  4485. error_report("Error while closing the image: %s", strerror(-inactivate_ret));
  4486. ret = 1;
  4487. }
  4488. blk_unref(src);
  4489. blk_unref(blk);
  4490. qemu_opts_del(opts);
  4491. return ret;
  4492. }
  4493. #define C_BS 01
  4494. #define C_COUNT 02
  4495. #define C_IF 04
  4496. #define C_OF 010
  4497. #define C_SKIP 020
  4498. struct DdInfo {
  4499. unsigned int flags;
  4500. int64_t count;
  4501. };
  4502. struct DdIo {
  4503. int bsz; /* Block size */
  4504. char *filename;
  4505. uint8_t *buf;
  4506. int64_t offset;
  4507. };
  4508. struct DdOpts {
  4509. const char *name;
  4510. int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
  4511. unsigned int flag;
  4512. };
  4513. static int img_dd_bs(const char *arg,
  4514. struct DdIo *in, struct DdIo *out,
  4515. struct DdInfo *dd)
  4516. {
  4517. int64_t res;
  4518. res = cvtnum_full("bs", arg, 1, INT_MAX);
  4519. if (res < 0) {
  4520. return 1;
  4521. }
  4522. in->bsz = out->bsz = res;
  4523. return 0;
  4524. }
  4525. static int img_dd_count(const char *arg,
  4526. struct DdIo *in, struct DdIo *out,
  4527. struct DdInfo *dd)
  4528. {
  4529. dd->count = cvtnum("count", arg);
  4530. if (dd->count < 0) {
  4531. return 1;
  4532. }
  4533. return 0;
  4534. }
  4535. static int img_dd_if(const char *arg,
  4536. struct DdIo *in, struct DdIo *out,
  4537. struct DdInfo *dd)
  4538. {
  4539. in->filename = g_strdup(arg);
  4540. return 0;
  4541. }
  4542. static int img_dd_of(const char *arg,
  4543. struct DdIo *in, struct DdIo *out,
  4544. struct DdInfo *dd)
  4545. {
  4546. out->filename = g_strdup(arg);
  4547. return 0;
  4548. }
  4549. static int img_dd_skip(const char *arg,
  4550. struct DdIo *in, struct DdIo *out,
  4551. struct DdInfo *dd)
  4552. {
  4553. in->offset = cvtnum("skip", arg);
  4554. if (in->offset < 0) {
  4555. return 1;
  4556. }
  4557. return 0;
  4558. }
  4559. static int img_dd(int argc, char **argv)
  4560. {
  4561. int ret = 0;
  4562. char *arg = NULL;
  4563. char *tmp;
  4564. BlockDriver *drv = NULL, *proto_drv = NULL;
  4565. BlockBackend *blk1 = NULL, *blk2 = NULL;
  4566. QemuOpts *opts = NULL;
  4567. QemuOptsList *create_opts = NULL;
  4568. Error *local_err = NULL;
  4569. bool image_opts = false;
  4570. int c, i;
  4571. const char *out_fmt = "raw";
  4572. const char *fmt = NULL;
  4573. int64_t size = 0;
  4574. int64_t out_pos, in_pos;
  4575. bool force_share = false;
  4576. struct DdInfo dd = {
  4577. .flags = 0,
  4578. .count = 0,
  4579. };
  4580. struct DdIo in = {
  4581. .bsz = 512, /* Block size is by default 512 bytes */
  4582. .filename = NULL,
  4583. .buf = NULL,
  4584. .offset = 0
  4585. };
  4586. struct DdIo out = {
  4587. .bsz = 512,
  4588. .filename = NULL,
  4589. .buf = NULL,
  4590. .offset = 0
  4591. };
  4592. const struct DdOpts options[] = {
  4593. { "bs", img_dd_bs, C_BS },
  4594. { "count", img_dd_count, C_COUNT },
  4595. { "if", img_dd_if, C_IF },
  4596. { "of", img_dd_of, C_OF },
  4597. { "skip", img_dd_skip, C_SKIP },
  4598. { NULL, NULL, 0 }
  4599. };
  4600. const struct option long_options[] = {
  4601. { "help", no_argument, 0, 'h'},
  4602. { "object", required_argument, 0, OPTION_OBJECT},
  4603. { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4604. { "force-share", no_argument, 0, 'U'},
  4605. { 0, 0, 0, 0 }
  4606. };
  4607. while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
  4608. if (c == EOF) {
  4609. break;
  4610. }
  4611. switch (c) {
  4612. case 'O':
  4613. out_fmt = optarg;
  4614. break;
  4615. case 'f':
  4616. fmt = optarg;
  4617. break;
  4618. case ':':
  4619. missing_argument(argv[optind - 1]);
  4620. break;
  4621. case '?':
  4622. unrecognized_option(argv[optind - 1]);
  4623. break;
  4624. case 'h':
  4625. help();
  4626. break;
  4627. case 'U':
  4628. force_share = true;
  4629. break;
  4630. case OPTION_OBJECT:
  4631. user_creatable_process_cmdline(optarg);
  4632. break;
  4633. case OPTION_IMAGE_OPTS:
  4634. image_opts = true;
  4635. break;
  4636. }
  4637. }
  4638. for (i = optind; i < argc; i++) {
  4639. int j;
  4640. arg = g_strdup(argv[i]);
  4641. tmp = strchr(arg, '=');
  4642. if (tmp == NULL) {
  4643. error_report("unrecognized operand %s", arg);
  4644. ret = -1;
  4645. goto out;
  4646. }
  4647. *tmp++ = '\0';
  4648. for (j = 0; options[j].name != NULL; j++) {
  4649. if (!strcmp(arg, options[j].name)) {
  4650. break;
  4651. }
  4652. }
  4653. if (options[j].name == NULL) {
  4654. error_report("unrecognized operand %s", arg);
  4655. ret = -1;
  4656. goto out;
  4657. }
  4658. if (options[j].f(tmp, &in, &out, &dd) != 0) {
  4659. ret = -1;
  4660. goto out;
  4661. }
  4662. dd.flags |= options[j].flag;
  4663. g_free(arg);
  4664. arg = NULL;
  4665. }
  4666. if (!(dd.flags & C_IF && dd.flags & C_OF)) {
  4667. error_report("Must specify both input and output files");
  4668. ret = -1;
  4669. goto out;
  4670. }
  4671. blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
  4672. force_share);
  4673. if (!blk1) {
  4674. ret = -1;
  4675. goto out;
  4676. }
  4677. drv = bdrv_find_format(out_fmt);
  4678. if (!drv) {
  4679. error_report("Unknown file format");
  4680. ret = -1;
  4681. goto out;
  4682. }
  4683. proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
  4684. if (!proto_drv) {
  4685. error_report_err(local_err);
  4686. ret = -1;
  4687. goto out;
  4688. }
  4689. if (!drv->create_opts) {
  4690. error_report("Format driver '%s' does not support image creation",
  4691. drv->format_name);
  4692. ret = -1;
  4693. goto out;
  4694. }
  4695. if (!proto_drv->create_opts) {
  4696. error_report("Protocol driver '%s' does not support image creation",
  4697. proto_drv->format_name);
  4698. ret = -1;
  4699. goto out;
  4700. }
  4701. create_opts = qemu_opts_append(create_opts, drv->create_opts);
  4702. create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
  4703. opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
  4704. size = blk_getlength(blk1);
  4705. if (size < 0) {
  4706. error_report("Failed to get size for '%s'", in.filename);
  4707. ret = -1;
  4708. goto out;
  4709. }
  4710. if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
  4711. dd.count * in.bsz < size) {
  4712. size = dd.count * in.bsz;
  4713. }
  4714. /* Overflow means the specified offset is beyond input image's size */
  4715. if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
  4716. size < in.bsz * in.offset)) {
  4717. qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
  4718. } else {
  4719. qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
  4720. size - in.bsz * in.offset, &error_abort);
  4721. }
  4722. ret = bdrv_create(drv, out.filename, opts, &local_err);
  4723. if (ret < 0) {
  4724. error_reportf_err(local_err,
  4725. "%s: error while creating output image: ",
  4726. out.filename);
  4727. ret = -1;
  4728. goto out;
  4729. }
  4730. /* TODO, we can't honour --image-opts for the target,
  4731. * since it needs to be given in a format compatible
  4732. * with the bdrv_create() call above which does not
  4733. * support image-opts style.
  4734. */
  4735. blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
  4736. false, false, false);
  4737. if (!blk2) {
  4738. ret = -1;
  4739. goto out;
  4740. }
  4741. if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
  4742. size < in.offset * in.bsz)) {
  4743. /* We give a warning if the skip option is bigger than the input
  4744. * size and create an empty output disk image (i.e. like dd(1)).
  4745. */
  4746. error_report("%s: cannot skip to specified offset", in.filename);
  4747. in_pos = size;
  4748. } else {
  4749. in_pos = in.offset * in.bsz;
  4750. }
  4751. in.buf = g_new(uint8_t, in.bsz);
  4752. for (out_pos = 0; in_pos < size; ) {
  4753. int bytes = (in_pos + in.bsz > size) ? size - in_pos : in.bsz;
  4754. ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
  4755. if (ret < 0) {
  4756. error_report("error while reading from input image file: %s",
  4757. strerror(-ret));
  4758. goto out;
  4759. }
  4760. in_pos += bytes;
  4761. ret = blk_pwrite(blk2, out_pos, bytes, in.buf, 0);
  4762. if (ret < 0) {
  4763. error_report("error while writing to output image file: %s",
  4764. strerror(-ret));
  4765. goto out;
  4766. }
  4767. out_pos += bytes;
  4768. }
  4769. out:
  4770. g_free(arg);
  4771. qemu_opts_del(opts);
  4772. qemu_opts_free(create_opts);
  4773. blk_unref(blk1);
  4774. blk_unref(blk2);
  4775. g_free(in.filename);
  4776. g_free(out.filename);
  4777. g_free(in.buf);
  4778. g_free(out.buf);
  4779. if (ret) {
  4780. return 1;
  4781. }
  4782. return 0;
  4783. }
  4784. static void dump_json_block_measure_info(BlockMeasureInfo *info)
  4785. {
  4786. GString *str;
  4787. QObject *obj;
  4788. Visitor *v = qobject_output_visitor_new(&obj);
  4789. visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
  4790. visit_complete(v, &obj);
  4791. str = qobject_to_json_pretty(obj, true);
  4792. assert(str != NULL);
  4793. printf("%s\n", str->str);
  4794. qobject_unref(obj);
  4795. visit_free(v);
  4796. g_string_free(str, true);
  4797. }
  4798. static int img_measure(int argc, char **argv)
  4799. {
  4800. static const struct option long_options[] = {
  4801. {"help", no_argument, 0, 'h'},
  4802. {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
  4803. {"object", required_argument, 0, OPTION_OBJECT},
  4804. {"output", required_argument, 0, OPTION_OUTPUT},
  4805. {"size", required_argument, 0, OPTION_SIZE},
  4806. {"force-share", no_argument, 0, 'U'},
  4807. {0, 0, 0, 0}
  4808. };
  4809. OutputFormat output_format = OFORMAT_HUMAN;
  4810. BlockBackend *in_blk = NULL;
  4811. BlockDriver *drv;
  4812. const char *filename = NULL;
  4813. const char *fmt = NULL;
  4814. const char *out_fmt = "raw";
  4815. char *options = NULL;
  4816. char *snapshot_name = NULL;
  4817. bool force_share = false;
  4818. QemuOpts *opts = NULL;
  4819. QemuOpts *object_opts = NULL;
  4820. QemuOpts *sn_opts = NULL;
  4821. QemuOptsList *create_opts = NULL;
  4822. bool image_opts = false;
  4823. uint64_t img_size = UINT64_MAX;
  4824. BlockMeasureInfo *info = NULL;
  4825. Error *local_err = NULL;
  4826. int ret = 1;
  4827. int c;
  4828. while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
  4829. long_options, NULL)) != -1) {
  4830. switch (c) {
  4831. case '?':
  4832. case 'h':
  4833. help();
  4834. break;
  4835. case 'f':
  4836. fmt = optarg;
  4837. break;
  4838. case 'O':
  4839. out_fmt = optarg;
  4840. break;
  4841. case 'o':
  4842. if (accumulate_options(&options, optarg) < 0) {
  4843. goto out;
  4844. }
  4845. break;
  4846. case 'l':
  4847. if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
  4848. sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
  4849. optarg, false);
  4850. if (!sn_opts) {
  4851. error_report("Failed in parsing snapshot param '%s'",
  4852. optarg);
  4853. goto out;
  4854. }
  4855. } else {
  4856. snapshot_name = optarg;
  4857. }
  4858. break;
  4859. case 'U':
  4860. force_share = true;
  4861. break;
  4862. case OPTION_OBJECT:
  4863. user_creatable_process_cmdline(optarg);
  4864. break;
  4865. case OPTION_IMAGE_OPTS:
  4866. image_opts = true;
  4867. break;
  4868. case OPTION_OUTPUT:
  4869. if (!strcmp(optarg, "json")) {
  4870. output_format = OFORMAT_JSON;
  4871. } else if (!strcmp(optarg, "human")) {
  4872. output_format = OFORMAT_HUMAN;
  4873. } else {
  4874. error_report("--output must be used with human or json "
  4875. "as argument.");
  4876. goto out;
  4877. }
  4878. break;
  4879. case OPTION_SIZE:
  4880. {
  4881. int64_t sval;
  4882. sval = cvtnum("image size", optarg);
  4883. if (sval < 0) {
  4884. goto out;
  4885. }
  4886. img_size = (uint64_t)sval;
  4887. }
  4888. break;
  4889. }
  4890. }
  4891. if (argc - optind > 1) {
  4892. error_report("At most one filename argument is allowed.");
  4893. goto out;
  4894. } else if (argc - optind == 1) {
  4895. filename = argv[optind];
  4896. }
  4897. if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) {
  4898. error_report("--image-opts, -f, and -l require a filename argument.");
  4899. goto out;
  4900. }
  4901. if (filename && img_size != UINT64_MAX) {
  4902. error_report("--size N cannot be used together with a filename.");
  4903. goto out;
  4904. }
  4905. if (!filename && img_size == UINT64_MAX) {
  4906. error_report("Either --size N or one filename must be specified.");
  4907. goto out;
  4908. }
  4909. if (filename) {
  4910. in_blk = img_open(image_opts, filename, fmt, 0,
  4911. false, false, force_share);
  4912. if (!in_blk) {
  4913. goto out;
  4914. }
  4915. if (sn_opts) {
  4916. bdrv_snapshot_load_tmp(blk_bs(in_blk),
  4917. qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
  4918. qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
  4919. &local_err);
  4920. } else if (snapshot_name != NULL) {
  4921. bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
  4922. snapshot_name, &local_err);
  4923. }
  4924. if (local_err) {
  4925. error_reportf_err(local_err, "Failed to load snapshot: ");
  4926. goto out;
  4927. }
  4928. }
  4929. drv = bdrv_find_format(out_fmt);
  4930. if (!drv) {
  4931. error_report("Unknown file format '%s'", out_fmt);
  4932. goto out;
  4933. }
  4934. if (!drv->create_opts) {
  4935. error_report("Format driver '%s' does not support image creation",
  4936. drv->format_name);
  4937. goto out;
  4938. }
  4939. create_opts = qemu_opts_append(create_opts, drv->create_opts);
  4940. create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
  4941. opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
  4942. if (options) {
  4943. if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
  4944. error_report_err(local_err);
  4945. error_report("Invalid options for file format '%s'", out_fmt);
  4946. goto out;
  4947. }
  4948. }
  4949. if (img_size != UINT64_MAX) {
  4950. qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
  4951. }
  4952. info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
  4953. if (local_err) {
  4954. error_report_err(local_err);
  4955. goto out;
  4956. }
  4957. if (output_format == OFORMAT_HUMAN) {
  4958. printf("required size: %" PRIu64 "\n", info->required);
  4959. printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
  4960. if (info->has_bitmaps) {
  4961. printf("bitmaps size: %" PRIu64 "\n", info->bitmaps);
  4962. }
  4963. } else {
  4964. dump_json_block_measure_info(info);
  4965. }
  4966. ret = 0;
  4967. out:
  4968. qapi_free_BlockMeasureInfo(info);
  4969. qemu_opts_del(object_opts);
  4970. qemu_opts_del(opts);
  4971. qemu_opts_del(sn_opts);
  4972. qemu_opts_free(create_opts);
  4973. g_free(options);
  4974. blk_unref(in_blk);
  4975. return ret;
  4976. }
  4977. static const img_cmd_t img_cmds[] = {
  4978. #define DEF(option, callback, arg_string) \
  4979. { option, callback },
  4980. #include "qemu-img-cmds.h"
  4981. #undef DEF
  4982. { NULL, NULL, },
  4983. };
  4984. int main(int argc, char **argv)
  4985. {
  4986. const img_cmd_t *cmd;
  4987. const char *cmdname;
  4988. int c;
  4989. static const struct option long_options[] = {
  4990. {"help", no_argument, 0, 'h'},
  4991. {"version", no_argument, 0, 'V'},
  4992. {"trace", required_argument, NULL, 'T'},
  4993. {0, 0, 0, 0}
  4994. };
  4995. #ifdef CONFIG_POSIX
  4996. signal(SIGPIPE, SIG_IGN);
  4997. #endif
  4998. socket_init();
  4999. error_init(argv[0]);
  5000. module_call_init(MODULE_INIT_TRACE);
  5001. qemu_init_exec_dir(argv[0]);
  5002. qemu_init_main_loop(&error_fatal);
  5003. qcrypto_init(&error_fatal);
  5004. module_call_init(MODULE_INIT_QOM);
  5005. bdrv_init();
  5006. if (argc < 2) {
  5007. error_exit("Not enough arguments");
  5008. }
  5009. qemu_add_opts(&qemu_source_opts);
  5010. qemu_add_opts(&qemu_trace_opts);
  5011. while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
  5012. switch (c) {
  5013. case ':':
  5014. missing_argument(argv[optind - 1]);
  5015. return 0;
  5016. case '?':
  5017. unrecognized_option(argv[optind - 1]);
  5018. return 0;
  5019. case 'h':
  5020. help();
  5021. return 0;
  5022. case 'V':
  5023. printf(QEMU_IMG_VERSION);
  5024. return 0;
  5025. case 'T':
  5026. trace_opt_parse(optarg);
  5027. break;
  5028. }
  5029. }
  5030. cmdname = argv[optind];
  5031. /* reset getopt_long scanning */
  5032. argc -= optind;
  5033. if (argc < 1) {
  5034. return 0;
  5035. }
  5036. argv += optind;
  5037. qemu_reset_optind();
  5038. if (!trace_init_backends()) {
  5039. exit(1);
  5040. }
  5041. trace_init_file();
  5042. qemu_set_log(LOG_TRACE, &error_fatal);
  5043. /* find the command */
  5044. for (cmd = img_cmds; cmd->name != NULL; cmd++) {
  5045. if (!strcmp(cmdname, cmd->name)) {
  5046. return cmd->handler(argc, argv);
  5047. }
  5048. }
  5049. /* not found */
  5050. error_exit("Command not found: %s", cmdname);
  5051. }