0

I have download alpine netboot distribution from this url:

https://dl-cdn.alpinelinux.org/alpine/v3.16/releases/x86/alpine-netboot-3.16.1-x86.tar.gz

I have run a virtual machine with qemu this way:

qemu-system-i386 -m 256 -kernel boot/vmlinuz-lts -initrd boot/initramfs-lts -append "console=ttyS0 ip=dhcp alpine_repo=http://dl-cdn.alpinelinux.org/alpine/edge/main/" 

With this command, a virtual machine is created and 22 apk pacakges are installed.

I am able to install additionnal apk packages, but i have to do it by hand (apk add command).

How can i script packages installation in qemu command line ?

Please note i can replace http://dl-cdn.alpinelinux.org/ by a local mirror. So i can also change files on the repository

Thanks

larsks
  • 41,276
  • 13
  • 117
  • 170
Bob5421
  • 337
  • 2
  • 8
  • 13

1 Answers1

2

It's not quite "scripting" package installation, but after manually installing packages you can create an overlay file using the lbu command, and then apply that on subsequent boots. See https://wiki.alpinelinux.org/wiki/Alpine_local_backup and https://wiki.alpinelinux.org/wiki/PXE_boot for details.

For example, if I boot using your command and then subsequently run:

apk add --update curl git

I can create a local backup file by running lbu package. This will create an apkovl file in my current directory:

localhost:~# ls
localhost.apkovl.tar.gz

If I place this file somewhere on my local system where it's available via http, I can boot the image like this:

qemu-system-i386 -m 256 -kernel boot/vmlinuz-lts \
  -initrd boot/initramfs-lts \
  -append "console=ttyS0 ip=dhcp alpine_repo=http://dl-cdn.alpinelinux.org/alpine/edge/main/ apkovl=http://10.0.2.2:8080/localhost/apkovl.tar.gz" 

When alpine finishes booting, I will find that curl and git are already installed.


If you want to explicitly run scripts at boot, rather than simply installing packages, you can:

  1. Enable the local service:

    rc-update add local default
    
  2. Place your script(s) into /etc/local.d/<something>.start and make sure it's executable.

  3. Follow the previous instructions for generating and using an overlay.

larsks
  • 41,276
  • 13
  • 117
  • 170
  • In fact what i want to do is running a command after vm is up… – Bob5421 Aug 06 '22 at 07:20
  • That doesn't substantially change the instructions; see the update. – larsks Aug 06 '22 at 12:09
  • The overlay is a snapshot with a copy of files. It will take space disk. There are no way to run a script instead ? Thanks – Bob5421 Aug 06 '22 at 14:01
  • The overlay is *tiny*; most of the files are zero length (they're just symlinks in `/etc/runlevels`). I'm not aware of any other way to get the netboot image to run a custom script at boot. – larsks Aug 06 '22 at 14:08
  • Can i simply put a file script in this « overlay » ? – Bob5421 Aug 06 '22 at 16:48
  • If you can place it somewhere it will run automatically, sure. That's why we're enabling the `local` service in this example -- so that there's a useful place to put the script. – larsks Aug 06 '22 at 17:34
  • And do you think i can only put my additional script file on the overlay ? Or lbu command copy all files ? Thanks – Bob5421 Aug 06 '22 at 19:01
  • The lbu is just a tar file that is extracted over the root filesystem. You could probably provide your own tar file that contains only a single file. Try it and see what happens! – larsks Aug 06 '22 at 19:12
  • upon netboot, overlay loads... the file is in /etc/local.d/script.start, the file is executable, the local service is started in rcstatus... but yet script does not run! I see no errors in dmesg and nothing related in /var/log/*. the script works perfectly run after login, but can't figure out why it doesn't run in the overlay. – josh Sep 24 '22 at 13:44