Skip to content

Commit

Permalink
Allow leaving additional disks unformatted
Browse files Browse the repository at this point in the history
Also allow using something else than "ext4", like "xfs".

Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Jul 25, 2023
1 parent 518c429 commit 5db82ac
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ additionalDisks:
# - "data"
# or a list of disk objects with extra parameters, for example:
# - name: "data"
# format: true
# fsType: "ext4"

ssh:
# A localhost port of the host. Forwarded to port 22 of the guest.
Expand Down
14 changes: 10 additions & 4 deletions pkg/cidata/cidata.TEMPLATE.d/boot/05-lima-disks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ get_disk_var() {
for i in $(seq 0 $((LIMA_CIDATA_DISKS - 1))); do
DISK_NAME="$(get_disk_var "$i" "NAME")"
DEVICE_NAME="$(get_disk_var "$i" "DEVICE")"
FORMAT_DISK="$(get_disk_var "$i" "FORMAT")"
FORMAT_FSTYPE="$(get_disk_var "$i" "FSTYPE")"

test -n "$FORMAT_DISK" || FORMAT_DISK=true
test -n "$FORMAT_FSTYPE" || FORMAT_FSTYPE=ext4

# first time setup
if [[ ! -b "/dev/disk/by-label/lima-${DISK_NAME}" ]]; then
# TODO: skip if disk is tagged as "raw"
echo 'type=linux' | sfdisk --label gpt "/dev/${DEVICE_NAME}"
mkfs.ext4 -L "lima-${DISK_NAME}" "/dev/${DEVICE_NAME}1"
if $FORMAT_DISK; then
echo 'type=linux' | sfdisk --label gpt "/dev/${DEVICE_NAME}"
mkfs.$FORMAT_FSTYPE -L "lima-${DISK_NAME}" "/dev/${DEVICE_NAME}1"
fi
fi

mkdir -p "/mnt/lima-${DISK_NAME}"
mount -t ext4 "/dev/${DEVICE_NAME}1" "/mnt/lima-${DISK_NAME}"
mount -t $FORMAT_FSTYPE "/dev/${DEVICE_NAME}1" "/mnt/lima-${DISK_NAME}"
done
2 changes: 2 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/lima.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ LIMA_CIDATA_DISKS={{ len .Disks }}
{{- range $i, $disk := .Disks}}
LIMA_CIDATA_DISK_{{$i}}_NAME={{$disk.Name}}
LIMA_CIDATA_DISK_{{$i}}_DEVICE={{$disk.Device}}
LIMA_CIDATA_DISK_{{$i}}_FORMAT={{$disk.Format}}
LIMA_CIDATA_DISK_{{$i}}_FSTYPE={{$disk.FSType}}
{{- end}}
LIMA_CIDATA_GUEST_INSTALL_PREFIX={{ .GuestInstallPrefix }}
{{- if .Containerd.User}}
Expand Down
10 changes: 10 additions & 0 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,19 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
}

for i, d := range y.AdditionalDisks {
format := true
if d.Format != nil {
format = *d.Format
}
fstype := ""
if d.FSType != nil {
fstype = *d.FSType
}
args.Disks = append(args.Disks, Disk{
Name: d.Name,
Device: diskDeviceNameFromOrder(i),
Format: format,
FSType: fstype,
})
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type BootCmds struct {
type Disk struct {
Name string
Device string
Format bool
FSType string
}
type TemplateArgs struct {
Name string // instance name
Expand Down
2 changes: 2 additions & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type Image struct {

type Disk struct {
Name string `yaml:"name" json:"name"` // REQUIRED
Format *bool `yaml:"format,omitempty" json:"format,omitempty"`
FSType *string `yaml:"fsType,omitempty" json:"fsType,omitEmpty"`
}

type Mount struct {
Expand Down
8 changes: 8 additions & 0 deletions pkg/limayaml/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ additionalDisks:
assert.NilError(t, err)
assert.Equal(t, len(y.AdditionalDisks), 1)
assert.Equal(t, y.AdditionalDisks[0].Name, "name")
assert.Assert(t, y.AdditionalDisks[0].Format == nil)
assert.Assert(t, y.AdditionalDisks[0].FSType == nil)
}

func TestLoadDiskStruct(t *testing.T) {
s := `
additionalDisks:
- name: "name"
format: false
fsType: "xfs"
`
y, err := Load([]byte(s), "disk.yaml")
assert.NilError(t, err)
assert.Assert(t, len(y.AdditionalDisks) == 1)
assert.Equal(t, y.AdditionalDisks[0].Name, "name")
assert.Assert(t, y.AdditionalDisks[0].Format != nil)
assert.Equal(t, *y.AdditionalDisks[0].Format, false)
assert.Assert(t, y.AdditionalDisks[0].FSType != nil)
assert.Equal(t, *y.AdditionalDisks[0].FSType, "xfs")
}

0 comments on commit 5db82ac

Please sign in to comment.