Difference between revisions of "Creating Arch Linux disk image"

From ArchWiki
Jump to: navigation, search
(simplification of wikilinks, fixing whitespace and capitalization, removing underscores (https://github.com/lahwaacz/wiki-scripts/blob/master/link-checker.py (interactive)))
(redirect page per template)
 
Line 1: Line 1:
[[Category:Virtualization]]
+
#REDIRECT: [[Installation guide]]
[[Category:Getting and installing Arch]]
 
[[ar:Creating Arch Linux disk image]]
 
{{Deletion|Info out of date. Now normal installation do exactly the same thing.}}
 
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]], [[OpenStack]], [[VirtualBox]], or [[VMware]], and it can be customized however you want.
 
 
 
== Archiso ==
 
 
 
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.
 
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 [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 [[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 [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 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.
 
 
 
On file systems supporting the {{ic|fallocate()}} system call:
 
 
 
$ fallocate -l 1G archlinux.raw
 
 
 
Otherwise, simply use [[dd]]:
 
 
 
$ dd of=archlinux.raw bs=1 seek=1G count=0
 
 
 
{{Note|If you have {{Pkg|qemu}} installed, using ''qemu-img'' is another alternative. See [[QEMU#Creating a hard disk image]] for details.}}
 
 
 
=== 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 is easiest to leave the virtual disk unpartitioned and use the whole thing as a filesystem.
 
 
 
To make an [[Ext4|ext4 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|ext4 filesystem]] containing the guest's entire filesystem. There will be no [[swap]] partition.
 
 
 
First partition the disk:
 
 
 
{{Poor writing|This instruction explains only how and not '''why''', is rather error-prone, and is hard to debug. Why not just link to [[Partitioning]]?}}
 
 
 
$ fdisk archlinux.raw <<< '
 
$ > o
 
$ > n
 
$ > p
 
$ > 1
 
$ >
 
$ >
 
$ > a
 
$ > 1
 
$ > w'
 
 
 
Then make the partitions available as loopback devices. The rest of the instructions will assume that the loopback device created for {{ic|archlinux.raw}} is {{ic|/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|{{ic|kpartx}} is part of the {{AUR|multipath-tools}} 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.}}
 
 
 
Finally 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. For convenience, set its path as {{ic|$TMPDIR}}j, which will also be used in the following instructions.
 
 
 
# TMPDIR=/full/path/to/temporary/directory
 
 
 
For an unpartitioned disk:
 
 
 
# mount archlinux.raw $TMPDIR
 
 
 
or, for a partitioned disk:
 
 
 
# mount /dev/mapper/loop0p1 $TMPDIR
 
 
 
Install {{Pkg|arch-install-scripts}} and then install the packages you want on the system (like the {{Grp|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.
 
 
 
# genfstab -U $TMPDIR >> $TMPDIR/etc/fstab
 
 
 
And then fix up the paths as necessary.
 
 
 
=== Generate initramfs for the guest ===
 
 
 
{{Accuracy|This step may be unnecessary, for me initramfs generation when pacstrap installed the kernel was successful. Perhaps just configure the bootloader to use {{ic|/boot/initramfs-linux-fallback.img}} until the initramfs is regenerated from the VM (using the {{ic|autodetect}} hook)?}}
 
 
 
As noted earlier, [[initramfs]] generation failed when installing {{Pkg|linux}}. 
 
 
 
You may need to edit {{ic|$TMPDIR/etc/mkinitcpio.conf}} to remove the {{ic|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.
 
 
 
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.
 
 
 
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. It is recommended to try [[#Extlinux|Extlinux]] first, unless you need to use GRUB.
 
 
 
==== 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
 
 
 
Finally, create a configuration file for Extlinux. Replace {{ic|''UUID''}} with the UUID of the guest's root filesystem, which can be obtained by running {{ic|lsblk -f}}.
 
 
 
{{hc|$TMPDIR/boot/extlinux.conf|2=
 
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.
 
 
 
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!
 
 
 
# grub-install --boot-directory=$TMPDIR/boot /dev/loop0
 
 
 
Then write a {{ic|grub.cfg}} file. Replace {{ic|''UUID''}}, in both places, with the UUID of the guest's root filesystem, which can be obtained by running {{ic|lsblk -f}}.
 
 
 
{{hc|$TMPDIR/boot/grub/grub.cfg|2=
 
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
 
}
 
}}
 
 
 
=== Configure cloud-init ===
 
This step is optional. Users planning to use the arch linux images on cloud like say to launch in OpenStack would want to perform some early initialization that can be done with cloud-init package. Follow instruction in [[cloud-init]] to configure the package.
 
 
 
=== 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
 
 
 
=== Other tips ===
 
 
 
* If you use [[QEMU]], you can specify the guest's kernel and initramfs on the command line.  If you do this, you do not 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 compile a custom [[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.
 
 
 
* See [[Install from existing Linux]] for some more general tips about installing Arch Linux from an existing Linux installation that does not necessarily have to be Arch Linux.
 

Latest revision as of 06:55, 24 August 2015

Redirect to: