DEV Community

loading...
Cover image for Install Alpine Linux in Termux using Qemu

Install Alpine Linux in Termux using Qemu

starchaser137 profile image Alien AI Updated on ・4 min read

Alpine Linux is a security-oriented, lightweight Linux distribution.

If you didn't know already, Termux is an Android app that provides you a small Linux environment and now, you can use different versions of the famous qemu emulator in Terux.

We will be emulating the x86 virtual machine image of alpine linux which has a slimmed-down kernel for VMs, and apparently also works well with the qemu -nographic option.

First, setup storage in Termux using:

termux-setup-storage.

First, download the image from here.

To make things easier, you could download the iso directly into Termux using wget. Or if you downloaded using the normal way, don't worry. Just cd into the storage directory that now appears in Termux and navigate to the downloaded file and move it into the Termux home directory.

Now, run the following to install the required qemu and qemu utilities:

pkg install qemu-system-i386 qemu-utils

Now, create a virtual hard disk for your machine using:

qemu-img create -f qcow2 disk.img 5G

The installation is minimal as you might have noticed from the iso size, so 5 or even 3 gb size is sufficient.

Now we are ready to boot up our vm and begin the installation.

Run:

qemu-system-i386 -m 512 -hda disk.img -cdrom alpine-virt-3.13.0-x86.iso -boot d -nographic

What This Does:
-m specifies the size of RAM allocated to the VM.
-hda points to the previously created hard disk file.
-cdrom points to the disk image file.
-boot d tells the VM to boot from cdrom.
-nographic tells the VM to direct the output to Termux terminal. So you don't need to leave Termux or use VNC during the whole process.

Fun Fact: The -nographic option works properly only with the virtual machine version of the iso. My guess is that this is because of the "slimmed down" kernel as Alpine Linux guys say. Do tell me your views on this.

The vm will start showing output on your terminal screen. It will display some text, go blank for a few minutes and then show some more logs. After the boot has completed, you will get a login prompt.

Login into Alpine as root. Note that this wont affect your phone in any manner. It will log you in as soon as you enter "root" into the prompt. No default password or anything required.

Now run setup-alpine

It will ask you for keyboard settings. If you use the default english(us) keyboard, just enter us into both.

Then it will ask you for a system hostname. I entered termux but you can enter whatever you like.

Then it will ask you for a network interface to use. You can just press enter on these ones.

Then it will ask you to change root password. Enter whatever password you want, but remember it ( obv )

After entering password it asks you for timezone. I just went with the default one (UTC) for this.

Then it asks you if you want to use any proxy. For me, it is again, default option, "NO".

After that it asks you to choose mirrors. Just enter f for this one to automatically select the fastest mirrors. Or you could enter r to just enter any random mirrors.

Then it will ask you to select the ssh server. The default is openssh but I don't want it so I am selecting none. Note that if you select openssh, you will have to set it up during installation only.

Now, we arrive at the actual installation.

Alpine will now show you a list of disks available. If you are following this guide, just enter sda. (The only one.)

Then it asks you what you want to use the disk for. Enter sys.

Finally, it tells you that all data on the disk will be erased if you continue. Enter y.

Now just sit back and relax. All has been done from your side. The system will now install onto the virtual hard disk. In the meantime you can check out the alpine linux FAQs or howtos. or maybe follow me? ( 😅).

After the installation has completed, the system tells you to reboot and gives you back the live shell. Now don't type reboot, but type poweroff, because there is still some final work to do.

When the VM powers off, the iso file is no longer of use to us. To boot into our newly installed system, we need to change the qemu command.

Just remove the -cdrom alpine-virt-3.13.0-x86.iso -boot d part and it wil boot from the virtual hard disk from now on.

The system will now boot as earlier, but now when you login as root, you will need to enter the password you set up earlier.

So that's it. A fully functional alpine linux VM on Android. Cool, right?

Extra Notes

Try not to scroll much. I noticed that scrolling in -nographic mode of qemu really messes things up and overlaps text.

Note that you can't just exit the VM. You will have to always enter poweroff and wait for it to shut down properly.

Discussion (0)

pic
Editor guide