Difference between revisions of "Install Linux in a virtual machine"

From Computer Science
Jump to navigationJump to search
Line 46: Line 46:
 
Use the `fdisk` program to partition the disk, referred to as `/dev/sda`.
 
Use the `fdisk` program to partition the disk, referred to as `/dev/sda`.
  
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
<pre style="color: silver; background: black;">
 
root@archiso ~ # fdisk /dev/sda
 
root@archiso ~ # fdisk /dev/sda
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
</pre>
  
 
You'll see some mildly informative messages and then a different prompt
 
You'll see some mildly informative messages and then a different prompt
Line 55: Line 55:
 
by Enter.
 
by Enter.
  
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
<pre style="color: silver; background: black;">
 
Command (m for help): _
 
Command (m for help): _
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
</pre>
  
 
Enter the command "n" to create a new partition; select "p" to make it a
 
Enter the command "n" to create a new partition; select "p" to make it a
Line 65: Line 65:
 
transcript from my run through these steps:
 
transcript from my run through these steps:
  
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
<pre style="color: silver; background: black;">
Command (m for help): n
 
 
Partition type
 
Partition type
 
   p  primary (0 primary, 0 extended, 4 free)
 
   p  primary (0 primary, 0 extended, 4 free)
Line 76: Line 75:
  
 
Created a new partition 1 of type 'Linux' and of size 16 GiB.
 
Created a new partition 1 of type 'Linux' and of size 16 GiB.
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
</pre>
  
 
Note that for first sector and last sector I just hit enter to pick the
 
Note that for first sector and last sector I just hit enter to pick the
Line 85: Line 84:
 
partition table; the list of partitions at the end should look very similar to
 
partition table; the list of partitions at the end should look very similar to
 
this:
 
this:
 
+
<pre style="color: silver; background: black;">
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
 
 
Device    Boot Start      End  Sectors Size Id Type
 
Device    Boot Start      End  Sectors Size Id Type
 
/dev/sda1        2048 33554431 33552384  16G 83 Linux
 
/dev/sda1        2048 33554431 33552384  16G 83 Linux
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
</pre>
  
 
If it doesn't, enter the command "d" to delete the partition and re-create it
 
If it doesn't, enter the command "d" to delete the partition and re-create it
Line 99: Line 97:
 
Now format the partition you just created:
 
Now format the partition you just created:
  
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
<pre style="color: silver; background: black;">
 
root@archiso ~ # mkfs.ext4 /dev/sda1
 
root@archiso ~ # mkfs.ext4 /dev/sda1
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
</pre>
  
 
And then make that formatted partition available to the running system:
 
And then make that formatted partition available to the running system:
 
+
<pre style="color: silver; background: black;">
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
 
 
root@archiso ~ # mount /dev/sda1 /mnt
 
root@archiso ~ # mount /dev/sda1 /mnt
[[User:Robert Lichenstein|Robert Lichenstein]] ([[User talk:Robert Lichenstein|talk]]) 15:56, 9 February 2020 (EST)
+
</pre>
 
 
  
 
=== Install and configure ===
 
=== Install and configure ===

Revision as of 16:58, 9 February 2020


NOTE: These instructions are not canonical. There are many ways of installing Linux, but this will get you a running system that you can play with.

Download the install media [here](middlinux-2020.01.05-x86_64.iso).


Boot the virtual install media

Select your virtual machine from the list. Click "Settings". Select "Storage". Under the "Controller: IDE" heading, select the stylized DVD labeled "Empty". In the right pane (labeled Attributes), on the line labeled "Optical Drive", click the button with the stylized DVD. Select "Choose Virtual Optical Disk File..." Navigate to and select the install media you just downloaded. Click Ok.

Click the big green arrow labeled "Start".

You'll see a menu show up: hit Enter to select the default option, "Boot Arch Linux (x86_64)".

A bunch of opaque nonsense will fly by and eventually you'll be presented with a prompt that looks something like this:

Arch Linux 5.4.8-arch1-1-ARCH (tty1)

archiso login: root (automatic login)

root@archiso ~ # _

This tells you that you've been automatically logged in as the user "root" on the machine called "archiso" and that your current working directory is "~" (i.e., root's home directory). The "#" is the prompt awaiting input. The "\_" is the cursor. (The name of the program awaiting input is not Linux itself, but rather a program Linux has started for you, precisely to accept your input and do your bidding. This program is called the *shell*.)

Prepare disk

Before you can install anything on the virtual disk inside your virtual machine, you need to partition it and format the partition.

Use the `fdisk` program to partition the disk, referred to as `/dev/sda`.

root@archiso ~ # fdisk /dev/sda

You'll see some mildly informative messages and then a different prompt indicating that `fdisk` (rather than the shell) is awaiting input. Like the shell, `fdisk` expects you to enter a command (most are one letter) followed by Enter.

Command (m for help): _

Enter the command "n" to create a new partition; select "p" to make it a primary partition; with the partition number "1"; the default first sector and the default last sector. (For any of these, you can press enter to pick the default instead of actually typing the option explicitly.) Here's a transcript from my run through these steps:

Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-33554431, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-33554431, default 33554431):

Created a new partition 1 of type 'Linux' and of size 16 GiB.

Note that for first sector and last sector I just hit enter to pick the default. Some of the exact numbers might be different depending on the size of the virtual hard drive you created previously.

You should now be back at the `fdisk` prompt. Enter "p" to print the partition table; the list of partitions at the end should look very similar to this:

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1        2048 33554431 33552384  16G 83 Linux

If it doesn't, enter the command "d" to delete the partition and re-create it according to the instructions in the previous pair of paragraphs.

Once all is well, enter the command "w" to write the partition table to disk and exit `fdisk`. Upon doing so, you should once again see the shell prompt.

Now format the partition you just created:

root@archiso ~ # mkfs.ext4 /dev/sda1

And then make that formatted partition available to the running system:

root@archiso ~ # mount /dev/sda1 /mnt

Install and configure

Install the base set of packages into the virtual drive. This is the bare minimum of software required for a running system (base), along with the graphical environment (XFCE), some tools to make running inside VirtualBox easier (virtualbox-guest-utils), code that gets the system up and running (grub), and the program that lets you run *other* programs with elevated privileges (sudo).

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) root@archiso ~ # pacstrap -c /mnt base xfce4 xorg-server virtualbox-guest-modules-arch virtualbox-guest-utils ttf-dejavu dhcpcd grub sudo man man-pages Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

The preceding command will produce an error if you mistype any of the package names. For instance, if I misspell "sudo" as "sud" it will complain at me like so:

Robert Lichenstein (talk) error: target not found: sud ==> ERROR: Failed to install packages to new root 1 root@archiso # _ Robert Lichenstein (talk)

If you see such an error, press the up arrow to bring up the previous command, fix the typo, and re-run the command. When successfully complete, it will print a message telling you how long it took, followed by a new prompt, e.g.:

Robert Lichenstein (talk) pacstrap -c /mnt base xfce4 xorg-server virtualbox-guest-modules-arch grub 38.73s user 16.03s system 80% cpu 1:07.99 total root@archiso # _ Robert Lichenstein (talk)

Now you're ready to make sure the partition you've just created gets mounted at runtime:

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) root@archiso ~ # genfstab -p /mnt >> /mnt/etc/fstab Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Your new installation is located at /mnt. Start up a new shell that thinks its whole world is the collection of files under /mnt, called a *chroot*. (Normally you'd just use the `chroot` program, but for this particular purpose, there are some other things that need to happen, so the Arch Linux maintainers have wrapped the raw chroot command up in the bigger script you'll be running.)

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) root@archiso ~ # arch-chroot /mnt Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

On success, your prompt should change to a stripped-down version (because this is a shell running in a stripped-down environment, and thus hasn't been customized to show things like username, hostname, and current working directory):

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Replace "middlinux" in the following command to set the computer name to anything you like (though limit yourself to lowercase letters, numbers, and underscore).

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# echo middlinux > /etc/hostname Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Set the timezone. (This assumes the hardware clock on the host machine is set to UTC rather than localtime. It's entirely probable this assumption is invalid; we can deal with it later, it's not a big deal even if it's never fixed.) The `date` command should show the correct timezone.

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# ln -snf /usr/share/zoneinfo/US/Eastern /etc/localtime [root@archiso /]# date Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Set up localization. The sed command uncomments all lines that contain the string "en_US", thus enabling US-centric English-language localization, as opposed to British (en_UK), Australian (en_AU), or Canadian (en_CA); or, alternatively, completely different locales like China (zh_CN, zh_HK, zh_TW, etc).

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# mv /etc/locale.gen /etc/locale.gen.pacnew [root@archiso /]# sed -ne '/^#en_US/ s/^#//p' /etc/locale.gen.pacnew > /etc/locale.gen [root@archiso /]# locale-gen Generating locales...

 en_US.UTF-8... done
 en_US.IDO-8859-1... done

Generation complete. [root@archiso /]# echo "LANG=en_US.UTF-8" > /etc/locale.conf Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Create the initial boot environment, customized to your particular system. (You will probably see a couple warnings about possibly missing firmware: you can ignore these.)

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# mkinitcpio -p linux (lots of messages) Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Install and configure the bootloader.

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# grub-install /dev/sda Installing for i386-pc platform. Installation finished. No error reported. [root@archiso /]# grub-mkconfig -o /boot/grub/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-linux Found initrd image(s) in /boot: initramfs-linux.img Found fallback initrd image(s) in /boot: initramfs-linux-fallback.img done Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Make sure networking starts at boot. (The last letter of the first word is the letter ell, *not* the number one.)

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# systemctl enable dhcpcd@enp0s3 Created symlink /etc/systemd/system/multi-user.target.wants/dhcpcd@enp0s3.service -> /usr/lib/systemd/system/dhcpcd@.service Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Enable some VirtualBox-specific services.

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# systemctl enable vboxservice /etc/systemd/system/multi-user.target.wants/vboxservice.service -> /usr/lib/systemd/system/vboxservice.service [root@archiso /]# echo vboxsf > /etc/modules-load.d/vbox.conf Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Configure sudo, which allows one user to run a program using the privileges of another. (Usually used to perform administrative activities without having to log out and log back in as root.) The following `sed` command reads the file `/etc/sudoers`, searches for all lines that contain the string within the first pair of forward slashes, and removes the leading # and space from those lines.

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# sed -i -e '/wheel ALL=(ALL) ALL$/ s/^# //' /etc/sudoers Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Set the root (superuser, administrator) password and exit from the chroot. (Note that it won't even print asterisks to let you know you're typing.)

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) [root@archiso /]# passwd Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully [root@archiso /]# exit Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

It should print an account of the time you spent in the chroot and then the prompt should now revert to what it was before:

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) exit arch-chroot /mnt 10.96s user 2.74s system 5% cpu 4:26.84 total root@archiso ~ # Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)


Remove the virtual install media from the virtual drive

Shut down the system:

Robert Lichenstein (talk) 15:56, 9 February 2020 (EST) root@archiso ~ # halt -p Robert Lichenstein (talk) 15:56, 9 February 2020 (EST)

Now, in the VirtualBox UI, select your virtual machine from the list. Click "Settings". Select "Storage". Under the "Controller: IDE" heading, select the stylized DVD labeled with the name of the install media. To the right, click the stylized DVD with the little down-arrow. Select "Remove Disk from Virtual Drive". Click Ok.


Congratulations

Proceed to Your First Boot.