<commit_before>/*
Copyright 2016 The Rook Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package osd

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/signal"
	"path/filepath"
	"regexp"
	"strings"
	"syscall"

	"github.com/coreos/pkg/capnslog"
	"github.com/pkg/errors"
	"github.com/rook/rook/pkg/clusterd"
	cephconfig "github.com/rook/rook/pkg/daemon/ceph/config"
	oposd "github.com/rook/rook/pkg/operator/ceph/cluster/osd"
	"github.com/rook/rook/pkg/util/sys"
)

const (
	pvcDataTypeDevice     = "data"
	pvcMetadataTypeDevice = "metadata"
)

var (
	logger = capnslog.NewPackageLogger("github.com/rook/rook", "cephosd")
)

// StartOSD starts an OSD on a device that was provisioned by ceph-volume
func StartOSD(context *clusterd.Context, osdType, osdID, osdUUID, lvPath string, pvcBackedOSD, lvBackedPV bool, cephArgs []string) error {

	// ensure the config mount point exists
	configDir := fmt.Sprintf("/var/lib/ceph/osd/ceph-%s", osdID)
	err := os.Mkdir(configDir, 0755)
	if err != nil {
		logger.Errorf("failed to create config dir %q. %v", configDir, err)
	}

	// Update LVM config at runtime
	if err := updateLVMConfig(context, pvcBackedOSD, lvBackedPV); err != nil {
		return errors.Wrap(err, "failed to update lvm configuration file") // fail return here as validation provided by ceph-volume
	}

	var volumeGroupName string
	if pvcBackedOSD && !lvBackedPV {
		volumeGroupName := getVolumeGroupName(lvPath)
		if volumeGroupName == "" {
			return errors.Wrapf(err, "error fetching volume group name for OSD %q", osdID)
		}

		go handleTerminate(context, lvPath, volumeGroupName)

		// It's fine to continue if deactivate fails since we will return error if activate fails
		if op, err := context.Executor.ExecuteCommandWithCombinedOutput("vgchange", "-anvv", volumeGroupName); err != nil {
			logger.Errorf("failed to deactivate volume group for lv %q. output: %s. %v", lvPath, op, err)
			return nil
		}

		if op, err := context.Executor.ExecuteCommandWithCombinedOutput("vgchange", "-ayvv", volumeGroupName); err != nil {
			return errors.Wrapf(err, "failed to activate volume group for lv %q. output: %s", lvPath, op)
		}
	}

	// activate the osd with ceph-volume
	storeFlag := "--" + osdType
	if err := context.Executor.ExecuteCommand("stdbuf", "-oL", "ceph-volume", "lvm", "activate", "--no-systemd", storeFlag, osdID, osdUUID); err != nil {
		return errors.Wrap(err, "failed to activate osd")
	}

	// run the ceph-osd daemon<commit_msg>ceph: remove invalid verbose params from lv activate<commit_after>/*
Copyright 2016 The Rook Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package osd

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/signal"
	"path/filepath"
	"regexp"
	"strings"
	"syscall"

	"github.com/coreos/pkg/capnslog"
	"github.com/pkg/errors"
	"github.com/rook/rook/pkg/clusterd"
	cephconfig "github.com/rook/rook/pkg/daemon/ceph/config"
	oposd "github.com/rook/rook/pkg/operator/ceph/cluster/osd"
	"github.com/rook/rook/pkg/util/sys"
)

const (
	pvcDataTypeDevice     = "data"
	pvcMetadataTypeDevice = "metadata"
)

var (
	logger = capnslog.NewPackageLogger("github.com/rook/rook", "cephosd")
)

// StartOSD starts an OSD on a device that was provisioned by ceph-volume
func StartOSD(context *clusterd.Context, osdType, osdID, osdUUID, lvPath string, pvcBackedOSD, lvBackedPV bool, cephArgs []string) error {

	// ensure the config mount point exists
	configDir := fmt.Sprintf("/var/lib/ceph/osd/ceph-%s", osdID)
	err := os.Mkdir(configDir, 0755)
	if err != nil {
		logger.Errorf("failed to create config dir %q. %v", configDir, err)
	}

	// Update LVM config at runtime
	if err := updateLVMConfig(context, pvcBackedOSD, lvBackedPV); err != nil {
		return errors.Wrap(err, "failed to update lvm configuration file") // fail return here as validation provided by ceph-volume
	}

	var volumeGroupName string
	if pvcBackedOSD && !lvBackedPV {
		volumeGroupName := getVolumeGroupName(lvPath)
		if volumeGroupName == "" {
			return errors.Wrapf(err, "error fetching volume group name for OSD %q", osdID)
		}

		go handleTerminate(context, lvPath, volumeGroupName)

		// It's fine to continue if deactivate fails since we will return error if activate fails
		if op, err := context.Executor.ExecuteCommandWithCombinedOutput("vgchange", "-an", volumeGroupName); err != nil {
			logger.Errorf("failed to deactivate volume group for lv %q. output: %s. %v", lvPath, op, err)
			return nil
		}

		if op, err := context.Executor.ExecuteCommandWithCombinedOutput("vgchange", "-ay", volumeGroupName); err != nil {
			return errors.Wrapf(err, "failed to activate volume group for lv %q. output: %s", lvPath, op)
		}
	}

	// activate the osd with ceph-volume
	storeFlag := "--" + osdType
	if err := context.Executor.ExecuteCommand("stdbuf", "-oL", "ceph-volume", "lvm", "activate", "--no-systemd", storeFlag, osdID, osdUUID); err != nil {
		return errors.Wrap(err, "failed to activate osd")
	}

	// run the ceph-osd daemon
{'fraction_non_alphanumeric': 0.11684370257966616, 'fraction_numerical': 0.004046535154274153, 'mean_word_length': 4.28698752228164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3283686', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1962, 'n_words': 624}