gabrielgio.me @ c037259bc45446b2a7911a5abc3b930eea8c560b

  1---
  2title: "Running alpine in memory with data mode"
  3date: 2023-04-30
  4tags: ['alpine', 'linux']
  5---
  6
  7# Local testing
  8
  9To facilitate iteration and testing of this setup, we can use `qemu` and for the
 10whole setup  we will be using two disks: one for mounting the /var folder and
 11another for storing lbu files (more on that later). To create the two images,
 12follow these steps:
 13
 14```bash 
 15qemu-img create -f qcow2 var.cow2 10G
 16qemu-img create -f qcow2 media.cow2 5G
 17```
 18
 19Those images have arbitrary sizes and probably won't even required that much for
 20this setup so feel free to choose another size. 
 21
 22Once you have created the images go to alpine download page[^1] and grab the
 23latest virtual image.
 24
 25We all of that we can now start the virtual machine 
 26
 27```bash
 28qemu-system-x86_64 \
 29    -machine accel=kvm \
 30    -display gtk \
 31    -m 2048 \
 32    -boot d \
 33    -cdrom alpine-virt-3.17.3-x86_64.iso \
 34    -drive file=var.cow2,if=virtio \
 35    -drive file=media.cow2,if=virtio
 36```
 37
 38For more info about what are those parameters head to `qemu` documentation[^2].
 39Just pay attention to the `-boot d` option which will force cdrom to boot first
 40(more on that later as well).
 41
 42# Setting up environment
 43
 44Before we can run `setup-alpine` we need to mount a persistent media folder so
 45it can be picked up by the script and used to store the `lbu` files. To do so we
 46need to install some extra package that are not available in the live ISO.
 47
 48Run `setup-interfaces` to configure interfaces. The default values will do.
 49After that start the networking service `rc-service networking start`. Now we
 50have internet we can setup a repository. You could edit `/etc/apk/repositories`
 51directly but there is handy command for that already `setup-apkrepos`. Run it
 52and pick any option you see fit. I'd go for `f` but `1` also works.
 53
 54Now we can install some packages required for the remaining of the setup:
 55
 56```bash
 57apk add lsbkl e2fsprogs
 58```
 59
 60`lsblk` is useful to identify devices and `e2fsprogs` will provide `ext4`
 61support.
 62
 63Run `lsblk` and will display the device we attached, e.g.:
 64
 65```bash
 66vda     253:0   0   10G     0   disk
 67vdb     253:0   0    5G     0   disk
 68```
 69
 70Now let's format and mount `vdb` on `/media`.
 71
 72```bash
 73# formatting using ext4
 74mkfs.ext4 /dev/vdb
 75
 76# creating target folder for mouting
 77# the name is arbitrary, feel free to choose another one
 78mkdir /media/vdb
 79
 80# mouting 
 81mount -t ext4 /dev/vdb /media/vdb
 82```
 83
 84To confirm if device is mounted you can run `df -h /media/vdb`, it shows the
 85size and which device is mounted on that folder.
 86
 87# Setting up alpine on data mode
 88
 89Now we can run `setup-alpine`. Choose whatever options fits your need up to the
 90point where it asks to choose the device. It may repeat some of the step we
 91already did but it is be fine.
 92
 93When it asks to choose a disks to use enter the name of disk, which, in this
 94particularly setup, is `vda`. Then it will ask to choose how you would like to
 95run alpine[^3], pick `data`. 
 96
 97Now it will prompt to choose which media device we want to use for storing the
 98`lbu` files. By default it should the media folder we mounted in the previous
 99step, if not just enter `vdb`. Select place for cache. Default is fine.
100
101The cache folder is used to store the apk files we come to add. Since it does
102not have internet access when booting it needs to store those extra package in
103folder so later it can be restored.
104
105**Warning**, do not reboot now. We need to use `lbu` to make a backup of all
106changes we did, otherwise everything will be lost. Take a careful read of the
107`lbu` documentation[^4], it will provide the necessary information to understand
108how `lbu` works.
109
110Run `lbu commit` to backup it. You can check the `apkvol` file stored in the
111`/media/vdb/`. Now you changes as saved and you are good to reboot.
112
113The live ISO will look for `apkvol` files and try to restore it. That is why it
114is required the ISO to be the first to boot. There is no boot info store
115anywhere else since the one device is used to store `lbu` and the other one is
116mount on `/var` so we use live ISO to boot and restore the state.
117
118You can check here[^5] how that is possible and here [^6] how we can expand that
119idea and netboot using the apkvol to boot any machine to specific state.
120
121# Making changes
122
123After rebooting your system, you can now log into your fresh installation. You
124can then install a new package, such as vim, using the command `apk add vim`.
125However, if you reboot the system again, the vim package will be lost and you
126will need to reinstall it.
127
128If you run `lbu status` will show what was changed and in this case
129`/etc/apk/world`. The world file store all the package you have installed and
130since you have added a new packaged it has been modified. `lbu
131commit` to persist it.
132
133You can check the `/media/vdb/cache` folder to see that it has stored the vim
134package and its dependencies.
135
136# Why /var?
137
138The /var folder is a directory in Linux that is used to store variable data
139files as the contents of this folder can change by the OS. This folder contains
140files that are not critical to the basic operation of the system, but are
141instead used for tasks such as logging, spooling, and caching. For example
142postgresql store all its data on the var folder allowing us to use a database on
143data mode and still have its data persistent between boots.
144
145# In conclusion
146
147We can take advantage of speed boost provided by `tmpfs`, and we can still
148restore the system state even if the computer is rebooted. The only thing to
149keep in mind is to commit any changes made before rebooting  ;).
150
151[^1]: https://alpinelinux.org/downloads/
152[^2]: https://www.qemu.org/docs/master/system/invocation.html
153[^3]: https://wiki.alpinelinux.org/wiki/Installation
154[^4]: https://wiki.alpinelinux.org/wiki/Alpine_local_backup
155[^5]: https://bitfehler.srht.site/posts/2022-11-28_messing-with-your-initramfs---alpine-edition.html
156[^6]: https://www.apalrd.net/posts/2022/alpine_pxe/