1.\" Copyright (c) 2013, 2014 Juniper Networks, Inc. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24.\" 25.Dd June 25, 2025 26.Dt MKIMG 1 27.Os 28.Sh NAME 29.Nm mkimg 30.Nd "utility to make disk images" 31.Sh SYNOPSIS 32.Nm 33.Op Fl H Ar heads 34.Op Fl P Ar blksz 35.Op Fl S Ar secsz 36.Op Fl T Ar tracksz 37.Op Fl b Ar bootcode 38.Op Fl c Ar min_capacity 39.Op Fl C Ar max_capacity 40.Op Fl -capacity Ar capacity 41.Op Fl f Ar format 42.Op Fl o Ar outfile 43.Op Fl a Ar active 44.Op Fl t Ar timestamp 45.Op Fl v 46.Op Fl y 47.Op Fl s Ar scheme Op Fl p Ar partition ... 48.Nm 49.Fl -formats | Fl -schemes | Fl -version 50.Sh DESCRIPTION 51The 52.Nm 53utility creates a disk image from the raw partition contents specified with 54the 55.Ar partition 56argument(s) and using the partitioning scheme specified with the 57.Ar scheme 58argument. 59The disk image is written to 60.Ar stdout 61by default or the file specified with the 62.Ar outfile 63argument. 64The image file is a raw disk image by default, but the format of the 65image file can be specified with the 66.Ar format 67argument. 68Most formats require seekable output, except of raw disk image. 69.Pp 70The disk image can be made bootable by specifying the scheme-specific boot 71block contents with the 72.Ar bootcode 73argument and, 74depending on the scheme, 75with a boot partition. 76The contents of such a boot partition is provided like any other partition 77and the 78.Nm 79utility does not treat it any differently from other partitions. 80.Pp 81Some partitioning schemes need a disk geometry and for those the 82.Nm 83utility accepts the 84.Ar tracksz 85and 86.Ar heads 87arguments, specifying the number of sectors per track and the number of 88heads per cylinder (resp.) 89.Pp 90Both the logical and physical sector size can be specified and for that the 91.Nm 92utility 93accepts the 94.Ar secsz 95and 96.Ar blksz 97arguments. 98The 99.Ar secsz 100argument is used to specify the logical sector size. 101This is the sector size reported by a disk when queried for its capacity. 102Modern disks use a larger sector size internally, 103referred to as block size by the 104.Nm 105utility and this can be specified by the 106.Ar blksz 107argument. 108The 109.Nm 110utility will use the (physical) block size to determine the start of 111partitions and to round the size of the disk image. 112.Pp 113The 114.Fl c 115option can be used to specify a minimal capacity for the disk image. 116Use this option without the 117.Fl s 118and 119.Fl p 120options to create an empty disk image with the given (virtual) size. 121An empty partition table can be written to the disk when specifying a 122partitioning scheme with the 123.Fl s 124option, but without specifying any partitions. 125When the size required for all the partitions is larger than the 126given capacity, then the disk image will be larger than the capacity 127given. 128.Pp 129The 130.Fl C 131option specifies a maximum capacity for the disk image. 132If the combined sizes of the given partitions exceed the size given with 133.Fl C , 134image creation fails. 135.Pp 136The 137.Fl -capacity 138option is a shorthand to specify the minimum and maximum capacity at the 139same time. 140.Pp 141The 142.Fl t 143option causes any timestamps embedded in the output file to be set to the 144given time, specified in seconds since the epoch. 145.Pp 146The 147.Fl v 148option increases the level of output that the 149.Nm 150utility prints. 151.Pp 152The 153.Fl y 154option is used for testing purposes only and is not to be used in production. 155When present, the 156.Nm 157utility will generate predictable values for Universally Unique Identifiers 158(UUIDs) and time stamps so that consecutive runs of the 159.Nm 160utility will create images that are identical. 161.Pp 162The 163.Ar active 164option marks a partition as active, if the partitioning 165scheme supports it. 166Currently, only the 167.Ar mbr 168scheme supports this concept. 169By default, 170.Nm 171will only mark the first partition as active when boot code is 172specified. 173Use the 174.Ar active 175option to override the active partition. 176The number specified corresponds to the number after the 's' in the 177partition's 178.Xr geom 8 179name. 180No partitions are marked active when the value is 0. 181.Pp 182A set of long options exist to query about the 183.Nm 184utility itself. 185Options in this set should be given by themselves because the 186.Nm 187utility exits immediately after providing the requested information. 188The version of the 189.Nm 190utility is printed when the 191.Fl -version 192option is given. 193The list of supported output formats is printed when the 194.Fl -formats 195option is given and the list of supported partitioning schemes is printed 196when the 197.Fl -schemes 198option is given. 199Both the format and scheme lists a space-separated lists for easy handling 200in scripts. 201.Pp 202For a more descriptive list of supported partitioning schemes or supported 203output format, or for a detailed description of how to specify partitions, 204run the 205.Nm 206utility without any arguments. 207This will print a usage message with all the necessary details. 208.Sh DISK FORMATS 209The 210.Nm 211utility supports a number of output file formats. 212A short description of these is given below. 213.Ss QCOW and QCOW2 214QCOW stands for "QEMU Copy On Write". 215It's a sparse file format akin to VHD and VMDK and QCOW represents the 216first version. 217QCOW2 represents version 2 of the file format. 218Version 2 is not backward compatible with version 1 and adds support for 219snapshots among other things. 220The QCOW file formats are natively supported by QEMU and Xen. 221To write QCOW, specify 222.Fl f Ar qcow 223on the command line. 224To write version 2 QCOW, specify 225.Fl f Ar qcow2 226on the command line. 227The preferred file extension is ".qcow" and ".qcow2" for QCOW and QCOW2 228(resp.), but ".qcow" is sometimes used for version 2 files as well. 229.Ss RAW file format 230This file format is a sector by sector representation of an actual disk. 231There is no extra information that describes or relates to the format itself. 232The size of the file is the size of the (virtual) disk. 233This file format is suitable for being copied onto a disk with utilities 234like 235.Nm dd . 236To write a raw disk file, either omit the 237.Fl f 238option, or specify 239.Fl f Ar raw 240on the command line. 241The preferred file extension is one of ".img" or ".raw", but there's no 242real convention for it. 243.Ss Dynamic VHD and Fixed VHD 244Microsoft's "Virtual Hard Disk" file formats. 245The dynamic format is a sparse format akin to QCOW and VMDK. 246The fixed format is effectively a raw format with a footer appended to the 247file and as such it's often indistinguishable from the raw format. 248The fixed file format has been added to support Microsoft's Azure platform 249and due to inconsistencies in interpretation of the footer is not compatible 250with utilities like 251.Nm qemu 252when it is specifically instructed to interpreted the file as a VHD file. 253By default 254.Nm qemu 255will treat the file as a raw disk file, which mostly works fine. 256To have 257.Nm 258create a dynamic VHD file, specify 259.Fl f Ar vhd 260on the command line. 261To create a fixed VHD file for use by Azure, specify 262.Fl f Ar vhdf 263on the command line. 264The preferred file extension is ".vhd". 265.Ss Dynamic VHDX 266Microsoft's "Virtual Hard Disk v2" file formats, the 267successor to VHD. 268VHDX is the required format for the 2nd generation Hyper-V VMs. 269To have 270.Nm 271create a dynamic VHDX file, specify 272.Fl f Ar vhdx 273on the command line. 274The preferred file extension is ".vhdx". 275.Ss VMDK 276VMware's "Virtual Machine Disk" file format. 277It's a sparse file format akin to QCOW and VHD and supported by many 278virtualization solutions. 279To create a VMDK file, specify 280.Fl f Ar vmdk 281on the command line. 282The preferred file extension is ".vmdk". 283.Pp 284Not all virtualization solutions support all file formats, but often those 285virtualization environments have utilities to convert from one format to 286another. 287Note however that conversion may require that the virtual disk size is 288changed to match the constraints of the output format and this may invalidate 289the contents of the disk image. 290For example, the GUID Partition Table (GPT) scheme has a header in the last 291sector on the disk. 292When changing the disk size, the GPT must be changed so that the last header 293is moved accordingly. 294This is typically not part of the conversion process. 295If possible, use an output format specifically for the environment in which 296the file is intended to be used. 297.Sh PARTITION SPECIFICATION 298An option 299.Fl p 300may be used multiple times to specify a list of created partition entries. 301A specification that is a single dash indicates an unused partition entry. 302Otherwise, a partition specification has the following format: 303.Bd -literal -offset indent 304<type> ':' <kind> <contents> 305.Ed 306.Bl -tag -width indent 307.It Cm type 308the partition type alias (f.e.: freebsd-swap) 309that may be optionally followed by a '/' separator 310and a label for partitioning schemes that feature partition labels 311(see the 312.Sx EXAMPLES 313Section below) 314.It Cm kind 315the interpretation of the contents specification: 316.Bl -tag -width indent 317.It Cm ':' 318contents holds the size of an empty partition, 319a number that may be suffixed with one of K, M, G, T, P or E 320(either upper or lower case) following the SI power of two convention 321(see also 322.Xr expand_number 3 ) 323.It Cm '=' 324contents holds the name of a file to read 325.It Cm '-' 326contents holds a command to run; the output of which is the contents 327of the partition. 328Multi-word strings should be quoted according to the shell rules. 329.El 330.It Cm contents 331the specification of a partition's contents 332.El 333.Sh ENVIRONMENT 334.Bl -tag -width "TMPDIR" -compact 335.It Ev TMPDIR 336Directory to put temporary files in; default is 337.Pa /tmp . 338.El 339.Sh EXAMPLES 340To create a bootable disk image that is partitioned using the GPT scheme and 341containing a root file system that was previously created using 342.Xr makefs 8 343and also containing a swap partition, run the 344.Nm 345utility as follows: 346.Dl % mkimg -s gpt -b /boot/pmbr -p freebsd-boot:=/boot/gptboot \ 347-p freebsd-ufs:=root-file-system.ufs -p freebsd-swap::1G \ 348-o gpt.img 349.Pp 350The command line given above results in a raw image file. 351This is because no output format was given. 352To create a VMDK image for example, add the 353.Fl f Ar vmdk 354argument to the 355.Nm 356utility and name the output file accordingly. 357.Pp 358A nested partitioning scheme is created by running the 359.Nm 360utility twice. 361The output of the first will be fed as the contents of a partition to the 362second. 363This can be done using a temporary file, like so: 364.Dl % mkimg -s bsd -b /boot/boot -p freebsd-ufs:=root-file-system.ufs \ 365-p freebsd-swap::1G -o /tmp/bsd.img 366.Dl % mkimg -s mbr -b /boot/mbr -p freebsd:=/tmp/bsd.img -o mbr-bsd.img 367.Pp 368Alternatively, the 369.Nm 370utility can be run in a cascaded fashion, whereby the output of the 371first is fed directly into the second. 372To do this, run the 373.Nm 374utility as follows: 375.Dl % mkimg -s mbr -b /boot/mbr -p freebsd:-'mkimg -s bsd -b /boot/boot \ 376-p freebsd-ufs:=root-file-system.ufs -p freebsd-swap::1G' -o mbr-bsd.img 377.Pp 378To accommodate the need to have partitions named or numbered in a certain 379way, the 380.Nm 381utility allows for the specification of empty partitions. 382For example, to create an image that is compatible with partition layouts 383found in 384.Pa /etc/disktab , 385the 'd' partition often needs to be skipped. 386This is accomplished by inserting an unused partition after the first 2 387partition specifications. 388It is worth noting at this time that the BSD scheme will automatically 389skip the 'c' partition by virtue of it referring to the entire disk. 390To create an image that is compatible with the qp120at disk, use the 391.Nm 392utility as follows: 393.Dl % mkimg -s bsd -b /boot/boot -p freebsd-ufs:=root-file-system.ufs \ 394-p freebsd-swap::20M -p- -p- -p- -p- -p freebsd-ufs:=usr-file-system.ufs \ 395-o bsd.img 396.Pp 397For partitioning schemes that feature partition labels, the 398.Nm 399utility supports assigning labels to the partitions specified. 400In the following example the file system partition is labeled as 'backup': 401.Dl % mkimg -s gpt -p freebsd-ufs/backup:=file-system.ufs -o gpt.img 402.Sh SEE ALSO 403.Xr dd 1 , 404.Xr expand_number 3 , 405.Xr gpart 8 , 406.Xr makefs 8 , 407.Xr mdconfig 8 , 408.Xr newfs 8 409.Sh HISTORY 410The 411.Nm 412utility first appeared in 413.Fx 10.1 . 414.Sh AUTHORS 415The 416.Nm 417utility and manpage were written by 418.An Marcel Moolenaar Aq Mt marcel@FreeBSD.org . 419