我已经阅读了很多相关文章,但是我仍然不确定正确的方法,并假设:1.我有一个默认的Ubuntu 14.04 LTS VM,由Azure创建并在Azure上运行,并且没有交换2。想要使用现有VM存储创建交换,而不是使用其他存储创建新磁盘
我也需要这个(实际上是16.04而不是14.04,但我的答案将适用于我认为的两者)。
我读过的帖子:
但是,当我看到我不得不阅读冗长的文章以致您指出时,我将放弃……但是突然间,我想起了DigitalOcean博客中一篇非常简单的文章:
如何在Ubuntu 14.04上添加交换
非常简单,我什至为此写了一个脚本(至少在最好的情况下还没有交换设置和其他高级内容):
#!/usr/bin/env fsharpi
open System
open System.IO
open System.Net
open System.Diagnostics
#load "InfraTools.fs"
open Gatecoin.Infrastructure
// automation of https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
let NUMBER_OF_GB_FOR_SWAP = 1
let isThereSwapMemoryInTheSystem (): bool =
let _,output,_ = Tools.SafeHiddenExec("swapon", "-s")
(output.Trim().Length > 0)
if (isThereSwapMemoryInTheSystem()) then
Console.WriteLine("Swap already setup")
Environment.Exit(0)
let swapFile = new FileInfo(Path.Combine("/", "swapfile"))
if not (swapFile.Exists) then
Tools.BailIfNotSudoer("Need to use 'fallocate' to create swap file")
Console.WriteLine("Creating swap file...")
Tools.SafeExec("fallocate", String.Format("-l {0}G {1}", NUMBER_OF_GB_FOR_SWAP, swapFile.FullName), true)
let permissionsForSwapFile = 600
if not (Tools.OctalPermissions(swapFile) = permissionsForSwapFile) then
Tools.BailIfNotSudoer("Need to adjust permissions of the swap file")
Tools.SafeExec("chmod", String.Format("{0} {1}", permissionsForSwapFile, swapFile.FullName), true)
Tools.BailIfNotSudoer("Enable swap memory")
Tools.SafeExec("mkswap", swapFile.FullName, true)
Tools.SafeExec("swapon", swapFile.FullName, true)
if not (isThereSwapMemoryInTheSystem()) then
Console.WriteLine("Something went wrong while enabling the swap file")
Environment.Exit(1)
Tools.BailIfNotSudoer("Writing into /etc/fstab")
Tools.SafeHiddenExecBashCommand(String.Format("echo \"{0} none swap sw 0 0\" >> /etc/fstab", swapFile.FullName))
为了使以上功能sudo apt install fsharp
生效,您需要首先(至少Ubuntu 16.04在存储库中具有fsharp,不确定14.04左右)。
您还需要此InfraTools.fs
文件:
open System
open System.IO
open System.Net
namespace Gatecoin.Infrastructure
module Tools =
let HiddenExec (command: string, arguments: string) =
let startInfo = new System.Diagnostics.ProcessStartInfo(command)
startInfo.Arguments <- arguments
startInfo.UseShellExecute <- false
// equivalent to `>/dev/null 2>&1` in unix
startInfo.RedirectStandardError <- true
startInfo.RedirectStandardOutput <- true
use proc = System.Diagnostics.Process.Start(startInfo)
proc.WaitForExit()
(proc.ExitCode,proc.StandardOutput.ReadToEnd(),proc.StandardError.ReadToEnd())
let HiddenExecBashCommand (commandWithArguments: string) =
let args = String.Format("-c \"{0}\"", commandWithArguments.Replace("\"", "\\\""))
HiddenExec("bash", args)
let SafeHiddenExecBashCommand (commandWithArguments: string) =
let exitCode,stdOut,stdErr = HiddenExecBashCommand commandWithArguments
if not (exitCode = 0) then
Console.Error.WriteLine(stdErr)
Console.Error.WriteLine()
Console.Error.WriteLine("Bash command '{0}' failed with exit code {1}.", commandWithArguments, exitCode.ToString())
Environment.Exit(1)
exitCode,stdOut,stdErr
let Exec (command: string, arguments: string, echo: bool) =
let psi = new System.Diagnostics.ProcessStartInfo(command)
psi.Arguments <- arguments
psi.UseShellExecute <- false
if (echo) then
Console.WriteLine("{0} {1}", command, arguments)
let p = System.Diagnostics.Process.Start(psi)
p.WaitForExit()
p.ExitCode
let ExecBashCommand (commandWithArguments: string, echo: bool) =
let args = String.Format("-c \"{0}\"", commandWithArguments.Replace("\"", "\\\""))
if (echo) then
Console.WriteLine(commandWithArguments)
Exec("bash", args, false)
let SafeHiddenExec (command: string, arguments: string) =
let exitCode,stdOut,stdErr = HiddenExec(command, arguments)
if not (exitCode = 0) then
Console.Error.WriteLine(stdErr)
Console.Error.WriteLine()
Console.Error.WriteLine("Command '{0}' failed with exit code {1}. Arguments supplied: '{2}'", command, exitCode.ToString(), arguments)
Environment.Exit(1)
exitCode,stdOut,stdErr
let SafeExec (command: string, arguments: string, echo: bool) =
let exitCode = Exec(command, arguments, echo)
if not (exitCode = 0) then
Console.Error.WriteLine("Command '{0}' failed with exit code {1}. Arguments supplied: '{2}'", command, exitCode.ToString(), arguments)
Environment.Exit(1)
failwith "unreached"
()
let SafeExecBashCommand (commandWithArguments: string, echo: bool) =
let args = String.Format("-c \"{0}\"", commandWithArguments.Replace("\"", "\\\""))
if (echo) then
Console.WriteLine(commandWithArguments)
SafeExec("bash", args, false)
let FirstElementOf3Tuple (a, _, _) = a
let SecondElementOf3Tuple (_, b, _) = b
let SimpleStringSplit (str: string, separator: string): string list =
List.ofSeq(str.Split([|separator|], StringSplitOptions.RemoveEmptyEntries))
let SplitStringInLines (str: string): string list =
SimpleStringSplit(str,Environment.NewLine)
let CommandWorksInShell (command: string): bool =
let exitCode =
try
Some(FirstElementOf3Tuple(HiddenExec(command,String.Empty))
with
| :? System.ComponentModel.Win32Exception -> (); None
if exitCode.IsNone then
false
else
true
let BailIfNotSudoer(reason: string): unit =
if not (CommandWorksInShell "id") then
Console.WriteLine ("'id' unix command is needed for this script to work")
Environment.Exit(2)
()
let _,idOutput,_ = HiddenExec("id","-u")
if not (idOutput.Trim() = "0") then
Console.Error.WriteLine ("Error: needs sudo privilege. Reason: {0}", reason)
Environment.Exit(3)
()
()
let OctalPermissions (file: FileInfo): int =
let output = SecondElementOf3Tuple(SafeHiddenExec("stat", String.Format("-c \"%a\" {0}", file.FullName)))
Int32.Parse(output.Trim())
讨论了许多解决方案,但我似乎找不到能在服务器重新启动后持续存在的解决方案
通过服务器重新引导使我的答案起作用的部分是写入/ etc / fstab文件。
此解决方案的优点是,它应该可以在Azure,DigitalOcean,YouNameIt等环境中运行。
请享用!