Difference between revisions of "Creating Arch Linux disk image"

From ArchWiki
Jump to: navigation, search
(Partition the disk image and create filesystems)
(Install packages on the guest's filesystem)
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
[[Category:Virtualization]]
 
[[Category:Virtualization]]
 
[[Category:Getting and installing Arch]]
 
[[Category:Getting and installing Arch]]
{{i18n|Creating Arch Linux disk image}}
+
[[ar:Creating Arch Linux disk image]]
 
+
 
This page describes how to create a file that contains a disk image of Arch Linux.  This disk image can be run in a virtual machine using software such as [[QEMU]], [[VirtualBox]], or [[VMware]], and it can be customized however you want.
 
This page describes how to create a file that contains a disk image of Arch Linux.  This disk image can be run in a virtual machine using software such as [[QEMU]], [[VirtualBox]], or [[VMware]], and it can be customized however you want.
  
 
== Archiso ==
 
== Archiso ==
  
The [http://archlinux.org/download/ official installation media for Arch Linux] is a hybrid ISO/disk image, so it can already be booted either as a CD-ROM or as a disk.  However, it uses an ISO filesystem, so nothing on it can be changed without re-making the installation media.
+
The [https://archlinux.org/download/ official installation media for Arch Linux] is a hybrid ISO/disk image, so it can already be booted either as a CD-ROM or as a disk.  However, it uses an ISO filesystem, so nothing on it can be changed without re-making the installation media.
  
 
You can also create your own live Arch Linux systems using the [[Archiso]] tools.
 
You can also create your own live Arch Linux systems using the [[Archiso]] tools.
Line 14: Line 13:
 
== Install Arch Linux in a disk image using the installation media ==
 
== Install Arch Linux in a disk image using the installation media ==
  
Using [[QEMU]], [[VirtualBox]], or other virtualization software, you can install Arch Linux into a disk image by booting the virtual machine from [http://archlinux.org/download/ the installation media] with the disk image file attached as a virtual hard disk.  This is the preferred way to make a virtual disk image containing Arch Linux because other than starting up the virtual machine, the installation will proceed exactly as in the [[Official Installation Guide]].
+
Using [[QEMU]], [[VirtualBox]], or other virtualization software, you can install Arch Linux into a disk image by booting the virtual machine from [https://archlinux.org/download/ the installation media] with the disk image file attached as a virtual hard disk.  This is the preferred way to make a virtual disk image containing Arch Linux because other than starting up the virtual machine, the installation will proceed exactly as in the [[Official Installation Guide]].
  
 
== Install Arch Linux in a disk image without the installation media ==
 
== Install Arch Linux in a disk image without the installation media ==
  
 
It is also possible to create a disk image of Arch Linux directly from software and packages on a host Arch Linux system.  This has several advantages:
 
It is also possible to create a disk image of Arch Linux directly from software and packages on a host Arch Linux system.  This has several advantages:
* You do not need to have a copy of [http://archlinux.org/download/ the installation media].
+
* You do not need to have a copy of [https://archlinux.org/download/ the installation media].
 
* You can include the most up-to-date software packages in the disk image by installing them directly from the host's package manager, and you can do this before you have even booted up the guest for the first time.
 
* You can include the most up-to-date software packages in the disk image by installing them directly from the host's package manager, and you can do this before you have even booted up the guest for the first time.
 
* You can customize the disk image in ways that may not be supported by the official Arch Linux installer.
 
* You can customize the disk image in ways that may not be supported by the official Arch Linux installer.
Line 34: Line 33:
 
  $ fallocate -l 1G archlinux.raw
 
  $ fallocate -l 1G archlinux.raw
  
* If you have installed [[QEMU]], you may instead use {{ic|qemu-img create}} to create the raw disk image.  {{ic|qemu-img}} can also create a disk image in a non-raw format such as qcow2, provided that you export the image using {{ic|qemu-nbd}} and use {{ic|nbd-client}} from the {{Pkg|nbd}} package to set up a device that appears to contain the actual data of the disk image.
+
* If you have installed [[QEMU]], you may instead use {{ic|qemu-img create}} to create the raw disk image.  {{ic|qemu-img}} can also create a disk image in a non-raw format such as qcow2, provided that you export the image using {{ic|qemu-nbd}} to set up a device that appears to contain the actual data of the disk image.
  
 
=== Create filesystem(s) on the virtual disk ===
 
=== Create filesystem(s) on the virtual disk ===
Line 40: Line 39:
 
==== Use entire disk as one filesystem ====
 
==== Use entire disk as one filesystem ====
  
If you do not need multiple partitions in your Arch Linux guest, it's easiest to just leave the virtual disk unpartitioned and use the whole thing as a filesystem.
+
If you do not need multiple partitions in your Arch Linux guest, it's easiest to leave the virtual disk unpartitioned and use the whole thing as a filesystem.
  
 
* To make an [[Ext4|ext4 filesystem]]:
 
* To make an [[Ext4|ext4 filesystem]]:
Line 47: Line 46:
 
==== Partitioned disk ====
 
==== Partitioned disk ====
  
Or, if you want to, you can partition the disk.  In this simple example, it will be given only one partition, and it will be a bootable primary partition formatted as an [[Ext4|ext4 filesystem]] containing the guest's entire filesystem.  There will be no [[Swap|swap]] partition.
+
Or you can partition the disk.  In this simple example, it will be given only one partition, and it will be a bootable primary partition formatted as an [[Ext4|ext4 filesystem]] containing the guest's entire filesystem.  There will be no [[Swap|swap]] partition.
  
 
* Partition the disk.
 
* Partition the disk.
Line 66: Line 65:
 
  # kpartx -a /dev/loop0
 
  # kpartx -a /dev/loop0
  
{{Note|{{ic|kpartx}} is part of the {{Pkg|multipath-tools}} package.  See [[QEMU#Mounting_a_partition_inside_a_raw_disk_image]] for other ways to mount a partition inside a disk image.  But beware: [[GRUB2]] will not install correctly to the disk image unless the partition loopback device is created through the device mapper (using {{ic|kpartx}}).}}
+
{{Note|{{ic|kpartx}} is part of the {{AUR|multipath-tools-git}} package from the [[Arch User Repository|AUR]].  See [[QEMU#Mounting_a_partition_inside_a_raw_disk_image]] for other ways to mount a partition inside a disk image.  But beware: [[GRUB2]] will not install correctly to the disk image unless the partition loopback device is created through the device mapper (using {{ic|kpartx}}).}}
  
 
* Make the needed filesystems on the partitions.
 
* Make the needed filesystems on the partitions.
Line 76: Line 75:
  
 
  # TMPDIR=/full/path/to/temporary/directory
 
  # TMPDIR=/full/path/to/temporary/directory
 +
 +
For a partitioned disk:
 
  # mount /dev/mapper/loop0p1 $TMPDIR
 
  # mount /dev/mapper/loop0p1 $TMPDIR
 +
or, for an unpartitioned disk:
 +
# mount archlinux.raw $TMPDIR
  
* Create a directory that pacman needs.
+
Install arch-install-scripts and then install the packages you want on the system (like the base group):
# mkdir -p $TMPDIR/var/lib/pacman/sync
+
  
* Synchronize the package databases on the host system.
+
  # pacstrap $TMPDIR base
  # pacman -Sy
+
 
+
* Copy package databases to the guest's filesystem.
+
# cp /var/lib/pacman/sync/*  $TMPDIR/var/lib/pacman/sync
+
 
+
* Install packages using the guest's filesystem as an alternative installation root.  You can install whatever packages you want, and anything that is in the package cache on the host system is not re-downloaded.  A good idea would be to install the base system, although not everything in it is actually needed:
+
# pacman --root $TMPDIR -S base
+
 
+
You definitely will need essential packages such as {{Pkg|linux}} and {{Pkg|initscripts}} (they are included in ''base'').  Note that the post-installation hooks for {{Pkg|linux}} will fail because {{ic|/proc}}, {{ic|/sys}}, and {{ic|/dev}} are not mounted inside the guest.  We will generate the [[initramfs]] later.
+
 
+
For an even more minimal system, try installing only some of the base system:
+
# pacman --root $TMPDIR -S linux filesystem e2fsprogs initscripts syslog-ng sysvinit dhcpcd iputils
+
 
+
Or maybe you don't want a minimal system at all, but rather want all the non-[[Arch User Repository|AUR]] packages you have installed on your host (you likely will need a virtual disk larger than 1GB for this!):
+
 
+
# comm -23 <(pacman -Qq | sort) <(pacman -Qmq) | pacman --root $TMPDIR -S -
+
  
 
=== Write a fstab file for the guest ===
 
=== Write a fstab file for the guest ===
Line 119: Line 105:
 
=== Install bootloader on the guest ===
 
=== Install bootloader on the guest ===
  
For your bootloader, you can choose [[Extlinux]], [[GRUB2]], or another bootloader.
+
For your bootloader, you can choose [[Extlinux]], [[GRUB2]], or another bootloader.  Due to the possible problems installing [[#GRUB2|GRUB2]] in this setting, I suggest using [[Syslinux|Extlinux]].
  
 
==== Extlinux ====
 
==== Extlinux ====
 
Due to the possible problems installing [[#GRUB2|GRUB2]] in this setting, I suggest using [[Syslinux|Extlinux]].
 
  
 
* Install Extlinux on the guest's bootable partition.
 
* Install Extlinux on the guest's bootable partition.
 
  # extlinux --install $TMPDIR/boot
 
  # extlinux --install $TMPDIR/boot
* Install Syslinux's MBR in the guest's MBR.
+
* Install Syslinux's MBR in the guest's MBR (only for partitioned disks).
 
  # dd if=/usr/lib/syslinux/mbr.bin conv=notrunc bs=440 count=1 of=/dev/loop0
 
  # dd if=/usr/lib/syslinux/mbr.bin conv=notrunc bs=440 count=1 of=/dev/loop0
 
* Create a configuration file for Extlinux.  Replace $UUID with the UUID of the guest's root filesystem, which was set to the variable $UUID above.
 
* Create a configuration file for Extlinux.  Replace $UUID with the UUID of the guest's root filesystem, which was set to the variable $UUID above.
Line 139: Line 123:
 
</nowiki>}}
 
</nowiki>}}
  
{{Note|In order for Extlinux to be booted from the MBR, it must be installed on a partition marked as bootable.}}
+
{{Note|In order for Extlinux to be booted from the MBR on a partitioned disk, it must be installed on a partition marked as bootable.}}
  
 
==== GRUB2 ====
 
==== GRUB2 ====
  
In this example, we will install the [[GRUB2]] bootloader on the guest.  Make sure you have {{Pkg|grub2-bios}} installed on the host system.
+
In this example, we will install the [[GRUB2]] bootloader on the partitioned disk of the guest.  Make sure you have {{Pkg|grub2-bios}} installed on the host system.
  
{{Note|You may run into some problems when trying to install GRUB2 on a virtual disk.  It seems to only work on partition loopback devices created through the device mapper, not those created using the {{ic|loop}} module or setting up a loop device with an offset.  In addition, GRUB2 may fail to detect your ext4 filesystem for some reason.  I would suggest first trying [[#Extlinux|extlinux]], unless you need to use GRUB.}}
+
{{Note|You may run into some problems when trying to install GRUB2 on a virtual disk.  It seems to only work on partition loopback devices created through the device mapper, not those created using the {{ic|loop}} module or setting up a loop device with an offset.  In addition, GRUB2 may fail to detect your ext4 filesystem for some reason.  I would suggest first trying [[#Extlinux|Extlinux]], unless you need to use GRUB.}}
  
 
* Install GRUB2.  {{ic|--boot-directory}} must be set to the {{ic|/boot}} directory within the guest's root filesystem, while the given device must be the loopback device corresponding to the guest's entire disk image.  Be careful not to overwrite the bootloader of your host system!
 
* Install GRUB2.  {{ic|--boot-directory}} must be set to the {{ic|/boot}} directory within the guest's root filesystem, while the given device must be the loopback device corresponding to the guest's entire disk image.  Be careful not to overwrite the bootloader of your host system!
Line 180: Line 164:
 
  # qemu archlinux.raw
 
  # qemu archlinux.raw
  
{{Tip|If you have built the virtio block drivers into your guest's initramfs, then you can boot it using a paravirtualized block device.  Combine it with [[KVM]] and a minimal Arch Linux guest will boot up in as little as 3 seconds.
+
{{Tip|If you have built the virtio block drivers into your guest's initramfs, then you can boot it using a paravirtualized block device.  Combine it with [[KVM]] and a minimal Arch Linux guest will boot to a login prompt in as little as 3 seconds after starting QEMU.
 
  # qemu-system-x86_64 -enable-kvm -drive <nowiki>file=archlinux.raw,media=disk,if=virtio</nowiki>
 
  # qemu-system-x86_64 -enable-kvm -drive <nowiki>file=archlinux.raw,media=disk,if=virtio</nowiki>
 
}}
 
}}
Line 186: Line 170:
 
=== Other tips ===
 
=== Other tips ===
  
* If you use [[QEMU]], you can specify the guest's kernel and initramfs on the command line.  This means that you can avoid having a partitioned disk image with a bootloader entirely, and just have a raw filesystem image containing the root filesystem.
+
* If you use [[QEMU]], you can specify the guest's kernel and initramfs on the command line.  If you do this, you don't need to install a bootloader on the disk image, nor do you need to install the {{Pkg|linux}} package.
  
* Since you have full control over the guest's hardware, it is not too hard to skip installing the {{pkg|linux}} package and install compile a custom [[Kernels|kernel]] for the guest that has all the needed [[Kernel modules|modules]] built in, if you are familiar with configuring the Linux kernel.
+
* Since you have full control over the guest's hardware, it is not too hard compile a custom [[Kernels|kernel]] for the guest that has all the needed [[Kernel modules|modules]] built in, if you are familiar with configuring the Linux kernel.
  
 
* You can make copies of your disk image and run multiple Arch Linux virtual machines at the same time.
 
* You can make copies of your disk image and run multiple Arch Linux virtual machines at the same time.
  
* See [[Install from Existing Linux]] for some more general tips about installing Arch Linux from an existing Linux installation (that doesn't necessarily have to be Arch Linux).
+
* See [[Install from Existing Linux]] for some more general tips about installing Arch Linux from an existing Linux installation that doesn't necessarily have to be Arch Linux.

Revision as of 14:31, 13 October 2013

This page describes how to create a file that contains a disk image of Arch Linux. This disk image can be run in a virtual machine using software such as QEMU, VirtualBox, or VMware, and it can be customized however you want.

Archiso

The official installation media for Arch Linux is a hybrid ISO/disk image, so it can already be booted either as a CD-ROM or as a disk. However, it uses an ISO filesystem, so nothing on it can be changed without re-making the installation media.

You can also create your own live Arch Linux systems using the Archiso tools. This may be what you want, but Archiso is only designed for building live systems that boot from a read-only filesystem.

Install Arch Linux in a disk image using the installation media

Using QEMU, VirtualBox, or other virtualization software, you can install Arch Linux into a disk image by booting the virtual machine from the installation media with the disk image file attached as a virtual hard disk. This is the preferred way to make a virtual disk image containing Arch Linux because other than starting up the virtual machine, the installation will proceed exactly as in the Official Installation Guide.

Install Arch Linux in a disk image without the installation media

It is also possible to create a disk image of Arch Linux directly from software and packages on a host Arch Linux system. This has several advantages:

  • You do not need to have a copy of the installation media.
  • You can include the most up-to-date software packages in the disk image by installing them directly from the host's package manager, and you can do this before you have even booted up the guest for the first time.
  • You can customize the disk image in ways that may not be supported by the official Arch Linux installer.

However, this method is more difficult than using the installation media. In addition, it will not work if your host machine does not have pacman (i.e. is not running Arch Linux).

In these directions, the host system refers to the Arch Linux system you are currently running, while the guest system refers to the Arch Linux system you are creating as a disk image.

Make a file containing the disk image

  • Create a raw disk image for the virtual machine. In this example, it is made with a size of 1 GiB.
$ dd if=/dev/zero of=archlinux.raw bs=4096 count=262144

Or, on filesystems supporting the fallocate() system call:

$ fallocate -l 1G archlinux.raw
  • If you have installed QEMU, you may instead use qemu-img create to create the raw disk image. qemu-img can also create a disk image in a non-raw format such as qcow2, provided that you export the image using qemu-nbd to set up a device that appears to contain the actual data of the disk image.

Create filesystem(s) on the virtual disk

Use entire disk as one filesystem

If you do not need multiple partitions in your Arch Linux guest, it's easiest to leave the virtual disk unpartitioned and use the whole thing as a filesystem.

$ mkfs.ext4 -F archlinux.raw

Partitioned disk

Or you can partition the disk. In this simple example, it will be given only one partition, and it will be a bootable primary partition formatted as an ext4 filesystem containing the guest's entire filesystem. There will be no swap partition.

  • Partition the disk.
$ fdisk archlinux.raw <<< '
$ > o
$ > n
$ > p
$ > 1
$ >
$ >
$ > a
$ > 1
$ > w'
  • Make the partitions available as loopback devices. The rest of the instructions will assume that the loopback device created for archlinux.raw is /dev/loop0, but it will be a higher number if you already have set up loop devices.
# losetup -f --show archlinux.raw
# kpartx -a /dev/loop0
Note: kpartx is part of the multipath-tools-gitAUR package from the AUR. See QEMU#Mounting_a_partition_inside_a_raw_disk_image for other ways to mount a partition inside a disk image. But beware: GRUB2 will not install correctly to the disk image unless the partition loopback device is created through the device mapper (using kpartx).
  • Make the needed filesystems on the partitions.
# mkfs.ext4 /dev/mapper/loop0p1

Install packages on the guest's filesystem

  • Mount the guest's root filesystem on a temporary directory.
# TMPDIR=/full/path/to/temporary/directory

For a partitioned disk:

# mount /dev/mapper/loop0p1 $TMPDIR

or, for an unpartitioned disk:

# mount archlinux.raw $TMPDIR

Install arch-install-scripts and then install the packages you want on the system (like the base group):

# pacstrap $TMPDIR base

Write a fstab file for the guest

  • Add any mountpoints to the guest's fstab file. In this example, we just need a mountpoint for the guest's root filesystem. You do not have to specify it by UUID, but it is a good idea to do so because it guarantees that the root partition will be found regardless of what type of disk is emulated.
# UUID=$(blkid -s UUID -o value /dev/mapper/loop0p1)
# echo "UUID=$UUID   /   ext4   defaults   0   1" >> $TMPDIR/etc/fstab 

Generate initramfs for the guest

As noted earlier, initramfs generation failed when installing linux.

  • You may need to edit $TMPDIR/etc/mkinitcpio.conf to remove the autodetect hook, to stop your host system's hardware configuration from removing essential modules (e.g. those needed to access the root filesystem) from the initramfs of the guest, which is going to be running in a different environment in a virtual machine. In addition, it would be a good idea to have the MODULES line read
MODULES="virtio_blk virtio_pci"

so that it will be possible to boot your Arch Linux guest using a paravirtualized block device.

  • Generate the initramfs for the guest manually by running the following command:
# mkinitcpio -g $TMPDIR/boot/initramfs-linux.img -k $TMPDIR/boot/vmlinuz-linux -c $TMPDIR/etc/mkinitcpio.conf

Install bootloader on the guest

For your bootloader, you can choose Extlinux, GRUB2, or another bootloader. Due to the possible problems installing GRUB2 in this setting, I suggest using Extlinux.

Extlinux

  • Install Extlinux on the guest's bootable partition.
# extlinux --install $TMPDIR/boot
  • Install Syslinux's MBR in the guest's MBR (only for partitioned disks).
# dd if=/usr/lib/syslinux/mbr.bin conv=notrunc bs=440 count=1 of=/dev/loop0
  • Create a configuration file for Extlinux. Replace $UUID with the UUID of the guest's root filesystem, which was set to the variable $UUID above.
$TMPDIR/boot/extlinux.conf
DEFAULT archlinux
LABEL   archlinux
SAY     Booting Arch Linux
LINUX   /boot/vmlinuz-linux
APPEND  root=/dev/disk/by-uuid/$UUID ro
INITRD  /boot/initramfs-linux.img
Note: In order for Extlinux to be booted from the MBR on a partitioned disk, it must be installed on a partition marked as bootable.

GRUB2

In this example, we will install the GRUB2 bootloader on the partitioned disk of the guest. Make sure you have grub2-bios installed on the host system.

Note: You may run into some problems when trying to install GRUB2 on a virtual disk. It seems to only work on partition loopback devices created through the device mapper, not those created using the loop module or setting up a loop device with an offset. In addition, GRUB2 may fail to detect your ext4 filesystem for some reason. I would suggest first trying Extlinux, unless you need to use GRUB.
  • Install GRUB2. --boot-directory must be set to the /boot directory within the guest's root filesystem, while the given device must be the loopback device corresponding to the guest's entire disk image. Be careful not to overwrite the bootloader of your host system!
# grub-install --boot-directory=$TMPDIR/boot /dev/loop0
  • Write a grub.cfg file. Replace $UUID, in both places, with the UUID of the guest's root filesystem, which was set to the variable $UUID above.
$TMPDIR/boot/grub/grub.cfg
set default="0"
set timeout="3"
insmod msdospart
insmod ext2
set root='(/dev/sda, msdos1)'
search --no-floppy --fs-uuid --set=root $UUID
menuentry "Arch Linux" {
   linux /boot/vmlinuz-linux root=/dev/disk/by-uuid/$UUID ro
   initrd /boot/initramfs-linux.img
}

Cleanup

You should not boot the guest while its filesystem is still mounted; otherwise, the files on it may be corrupted. Unmount the guest's filesystem and get rid of the loopback devices first.

# umount $TMPDIR
# kpartx -d /dev/loop0
# losetup -d /dev/loop0

Boot the guest

Finally, boot the guest Arch Linux using your virtualization software of choice, such as QEMU:

# qemu archlinux.raw
Tip: If you have built the virtio block drivers into your guest's initramfs, then you can boot it using a paravirtualized block device. Combine it with KVM and a minimal Arch Linux guest will boot to a login prompt in as little as 3 seconds after starting QEMU.
# qemu-system-x86_64 -enable-kvm -drive file=archlinux.raw,media=disk,if=virtio

Other tips

  • If you use QEMU, you can specify the guest's kernel and initramfs on the command line. If you do this, you don't need to install a bootloader on the disk image, nor do you need to install the linux package.
  • Since you have full control over the guest's hardware, it is not too hard compile a custom kernel for the guest that has all the needed modules built in, if you are familiar with configuring the Linux kernel.
  • You can make copies of your disk image and run multiple Arch Linux virtual machines at the same time.
  • See Install from Existing Linux for some more general tips about installing Arch Linux from an existing Linux installation that doesn't necessarily have to be Arch Linux.