gabrielgio.me @ aca23a0b0169571e405f29e96de358409a653f7f

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