mirror of
https://github.com/linka-cloud/d2vm.git
synced 2025-12-14 15:03:12 +00:00
add fat32 boot partition support
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
80
builder.go
80
builder.go
@@ -67,6 +67,7 @@ type builder struct {
|
||||
|
||||
splitBoot bool
|
||||
bootSize uint64
|
||||
bootFS BootFS
|
||||
|
||||
loDevice string
|
||||
bootPart string
|
||||
@@ -83,7 +84,7 @@ type builder struct {
|
||||
cmdLineExtra string
|
||||
}
|
||||
|
||||
func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64, osRelease OSRelease, format string, cmdLineExtra string, splitBoot bool, bootSize uint64, luksPassword string, bootLoader string) (Builder, error) {
|
||||
func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64, osRelease OSRelease, format string, cmdLineExtra string, splitBoot bool, bootFS BootFS, bootSize uint64, luksPassword string, bootLoader string) (Builder, error) {
|
||||
if err := checkDependencies(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -123,6 +124,19 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if splitBoot {
|
||||
config.Kernel = strings.TrimPrefix(config.Kernel, "/boot")
|
||||
config.Initrd = strings.TrimPrefix(config.Initrd, "/boot")
|
||||
}
|
||||
|
||||
if bootFS == "" {
|
||||
bootFS = FSExt4
|
||||
}
|
||||
|
||||
if err := bootFS.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blp, err := BootloaderByName(bootLoader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -164,6 +178,7 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
|
||||
cmdLineExtra: cmdLineExtra,
|
||||
splitBoot: splitBoot,
|
||||
bootSize: bootSize,
|
||||
bootFS: bootFS,
|
||||
luksPassword: luksPassword,
|
||||
}
|
||||
if err := os.MkdirAll(b.mntPoint, os.ModePerm); err != nil {
|
||||
@@ -303,7 +318,12 @@ func (b *builder) mountImg(ctx context.Context) error {
|
||||
if err := os.MkdirAll(filepath.Join(b.mntPoint, "boot"), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := exec.Run(ctx, "mkfs.ext4", b.bootPart); err != nil {
|
||||
if b.bootFS.IsFat() {
|
||||
err = exec.Run(ctx, "mkfs.fat", "-F32", b.bootPart)
|
||||
} else {
|
||||
err = exec.Run(ctx, "mkfs.ext4", b.bootPart)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := exec.Run(ctx, "mount", b.bootPart, filepath.Join(b.mntPoint, "boot")); err != nil {
|
||||
@@ -363,7 +383,7 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fstab = fmt.Sprintf("UUID=%s / ext4 errors=remount-ro 0 1\nUUID=%s /boot ext4 errors=remount-ro 0 2\n", b.rootUUID, b.bootUUID)
|
||||
fstab = fmt.Sprintf("UUID=%s / ext4 errors=remount-ro 0 1\nUUID=%s /boot %s errors=remount-ro 0 2\n", b.rootUUID, b.bootUUID, b.bootFS.linux())
|
||||
} else {
|
||||
b.bootUUID = b.rootUUID
|
||||
fstab = fmt.Sprintf("UUID=%s / ext4 errors=remount-ro 0 1\n", b.bootUUID)
|
||||
@@ -387,13 +407,7 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
|
||||
if err := os.RemoveAll(b.chPath("/.dockerenv")); err != nil {
|
||||
return err
|
||||
}
|
||||
// create a symlink to /boot for non-alpine images in order to have a consistent path
|
||||
// even if the image is not split
|
||||
if _, err := os.Stat(b.chPath("/boot/boot")); os.IsNotExist(err) {
|
||||
if err := os.Symlink(".", b.chPath("/boot/boot")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
switch b.osRelease.ID {
|
||||
case ReleaseAlpine:
|
||||
by, err := os.ReadFile(b.chPath("/etc/inittab"))
|
||||
@@ -408,48 +422,26 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
case ReleaseUbuntu:
|
||||
if b.osRelease.VersionID >= "20.04" {
|
||||
return nil
|
||||
}
|
||||
fallthrough
|
||||
case ReleaseDebian, ReleaseKali:
|
||||
t, err := os.Readlink(b.chPath("/vmlinuz"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Symlink(t, b.chPath("/boot/vmlinuz")); err != nil {
|
||||
return err
|
||||
}
|
||||
t, err = os.Readlink(b.chPath("/initrd.img"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Symlink(t, b.chPath("/boot/initrd.img")); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *builder) cmdline(_ context.Context) string {
|
||||
if b.isLuksEnabled() {
|
||||
switch b.osRelease.ID {
|
||||
case ReleaseAlpine:
|
||||
return b.config.Cmdline(RootUUID(b.rootUUID), "root=/dev/mapper/root", "cryptdm=root", "cryptroot=UUID="+b.cryptUUID, b.cmdLineExtra)
|
||||
case ReleaseCentOS:
|
||||
return b.config.Cmdline(RootUUID(b.rootUUID), "rd.luks.name=UUID="+b.rootUUID+" rd.luks.uuid="+b.cryptUUID+" rd.luks.crypttab=0", b.cmdLineExtra)
|
||||
default:
|
||||
// for some versions of debian, the cryptopts parameter MUST contain all the following: target,source,key,opts...
|
||||
// see https://salsa.debian.org/cryptsetup-team/cryptsetup/-/blob/debian/buster/debian/functions
|
||||
// and https://cryptsetup-team.pages.debian.net/cryptsetup/README.initramfs.html
|
||||
return b.config.Cmdline(nil, "root=/dev/mapper/root", "cryptopts=target=root,source=UUID="+b.cryptUUID+",key=none,luks", b.cmdLineExtra)
|
||||
}
|
||||
} else {
|
||||
if !b.isLuksEnabled() {
|
||||
return b.config.Cmdline(RootUUID(b.rootUUID), b.cmdLineExtra)
|
||||
}
|
||||
switch b.osRelease.ID {
|
||||
case ReleaseAlpine:
|
||||
return b.config.Cmdline(RootUUID(b.rootUUID), "root=/dev/mapper/root", "cryptdm=root", "cryptroot=UUID="+b.cryptUUID, b.cmdLineExtra)
|
||||
case ReleaseCentOS:
|
||||
return b.config.Cmdline(RootUUID(b.rootUUID), "rd.luks.name=UUID="+b.rootUUID+" rd.luks.uuid="+b.cryptUUID+" rd.luks.crypttab=0", b.cmdLineExtra)
|
||||
default:
|
||||
// for some versions of debian, the cryptopts parameter MUST contain all the following: target,source,key,opts...
|
||||
// see https://salsa.debian.org/cryptsetup-team/cryptsetup/-/blob/debian/buster/debian/functions
|
||||
// and https://cryptsetup-team.pages.debian.net/cryptsetup/README.initramfs.html
|
||||
return b.config.Cmdline(nil, "root=/dev/mapper/root", "cryptopts=target=root,source=UUID="+b.cryptUUID+",key=none,luks", b.cmdLineExtra)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *builder) installBootloader(ctx context.Context) error {
|
||||
|
||||
Reference in New Issue
Block a user