📜 ⬆️ ⬇️

Arch Linux: Samba root partition

Greetings, friends!
Today I will not tell you why I love Arch Linux, you will see for yourself.

Task


- install Arch Linux so that the root partition is mounted over the network from a Samba server

Decision


To solve this problem, we need:
- installed Arch Linux (or LiveCD)
- mkinitcpio-mount-hook from AUR (http://aur.archlinux.org/packages.php?ID=40372)
- a couple more small hooks: mount.cifs and mount.loop
- extra / devtools (contains mkarchroot)
- core / mkinitcpio-nfs-utils (this package contains utilities and scripts to get the IP address)
- collecting script

1. And I will start with the last one.
')
build.sh
#!/bin/bash -e chroot_path=/tmp/chroot # Install base and some needed packages mkarchroot -f $chroot_path base mkinitcpio-nfs-utils # This adds cifs and loop modules to the initcpio image cp mount.cifs mount.loop -t $chroot_path/lib/initcpio/install # Add mount hook (http://aur.archlinux.org/packages.php?ID=40372) wget "http://people.oh14.de/andrej/mkinitcpio-mount-hook/mkinitcpio-mount-hook-0.3-stickbuild.tar.xz" -O - | tar -xJ --wildcards -C /tmp mkinitcpio-mount-hook-0.3/initcpio/*/mount cp -r /tmp/mkinitcpio-mount-hook-0.3/initcpio/* -t $chroot_path/lib/initcpio # Remove hard drives support and keep only needed hooks (added: net, mount, mount.cifs, mount.loop) sudo sed -i 's/^HOOKS=".*"/HOOKS="base udev net filesystems mount mount.cifs mount.loop"/' $chroot_path/etc/mkinitcpio.conf # Keep only one initcpio image (we don't need fallback) sed -i "s/^PRESETS=.*/PRESETS=('default')/" $chroot_path/etc/mkinitcpio.d/kernel26.preset # Adapt network configuration for network-based root sed -i 's/^DHCPCD_ARGS="-q"$/DHCPCD_ARGS="-q -p"/' $chroot_path/etc/conf.d/dhcpcd sed -i 's/^interface=$/interface=eth0/' $chroot_path/etc/rc.conf sed -i 's/^NETWORK_PERSIST="no"$/NETWORK_PERSIST="yes"/' $chroot_path/etc/rc.conf # Rebuild initcpio image mkarchroot -r "mkinitcpio -p kernel26" $chroot_path 


With the first command, we create a chroot environment and install packages from the base group, mkinitcpio-nfs-utils into it, and here you can add any other packages as desired. The second command adds support for cifs and loop devices in initrcpio. The third command extends the ability to mount the root partition. The fourth disables support for physical hard drives from initcpio and connects our hooks for cifs, loop and extended mount. The fifth command disables the fallback initcpio image, we do not need it, because we do not use autodetection when building. The sixth team sets up a new system so that the network, upon shutdown, does not turn off prematurely. And the last command reassembles initcpio, already with network support, cifs, loop, extended mount and no support for local hard drives.

2. The following two files include the modules needed for cifs (samba) and loop (mounting the disk image) in initcpio.

mount.cifs
 # vim:set ft=sh: install () { MODULES="cifs hmac md4 md5" BINARIES="" FILES="" SCRIPT="" } help () { cat <<HELPEOF This hook helps to mount cifs. HELPEOF } 


mount.loop
 # vim:set ft=sh: install () { MODULES="loop" BINARIES="" FILES="" SCRIPT="" } help () { cat <<HELPEOF This hook helps to mount loop images. HELPEOF } 


This is enough to build chroot and initcpio, but we need to put the chroot into a disk image. This is necessary in order not to puzzle over the support of acl, symlinks and hardlinks on the Samba server. Plus, the disk image is also in the fact that as a server we can use not only Linux, but also Windows.

3. Create a 1Gb file, format it, for example, under ext4 and mount it in / tmp / chroot:

 #!/bin/bash -e dd if=/dev/null of=/tmp/arch.img bs=1M seek=1024 mkfs.ext4 -F /tmp/arch.img mkdir /tmp/chroot mount /tmp/arch.img /tmp/chroot 


4. Now you can run build.sh. After the script has completed, we need to copy the files / tmp / chroot / boot / vmlinuz26 and /tmp/chroot/boot/kernel26.img (they are still useful to us) and unmount the image.

 #!/bin/bash -e cp /tmp/chroot/boot/{vmlinuz26,kernel26.img} /tmp umount /tmp/arch.img 


5. Is that all? Yes that's all. Well, almost everything. Now it remains to find out how to load all this. For this you need:
1) take our /tmp/arch.img and place it on some Samba server;
2) decide on the boot method (CD-ROM, PXE, Grub, or whatever you think of yourself);
3) copy / tmp / chroot / boot / vmlinuz26 and /tmp/chroot/boot/kernel26.img to where you will boot from.

Boot Parameters :
# grub
kernel /boot/vmlinuz26 ip=::::::dhcp mounts=cifs,root cifs_dev=//SERVER_IP/share cifs_target=/cifs cifs_type=cifs cifs_opts=username=guest root_dev=/cifs/arch.img root_type=ext4
initrd /boot/kernel26.img

# isolinux/pxelinux
kernel vmlinuz26
append initrd=kernel26.img ip=::::::dhcp mounts=cifs,root cifs_dev=//SERVER_IP/share cifs_target=/cifs cifs_type=cifs cifs_opts=username=guest root_dev=/cifs/arch.img root_type=ext4


Note : Pay attention to the parameter for username = guest. If you do not want the directory to be accessible to everyone, then instead of username = guest you can specify a specific account and password.

Result


We got a system that is loaded as follows:
1) vmlinuz26 is launched locally with kernel26.img (these files can be on a hard disk, on a CD, on a tftp server);
2) the network directory is mounted // SERVER_IP / share in the directory / cifs;
3) /cifs/arch.img is mounted as the root partition and the system continues to boot.

Approximately in the same way you can place an image on nfs. The experiment was also carried out with aufs, when the image file is mounted read-only, and the changes are written in tmpfs, but unfortunately, support for aufs is excluded in the current kernel.

Source: https://habr.com/ru/post/122708/


All Articles