Class: Vmopt::DiskOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/vmopt/disk_operation.rb

Instance Method Summary collapse

Instance Method Details

#chk_format_diskObject

参数:无功能:扫描整个系统磁盘,若存在没有格式化的磁盘则进行格式化返回值:默认



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vmopt/disk_operation.rb', line 85

def chk_format_disk
  colItemindexs = WMI.execquery ("select * from Win32_DiskDrive")
  index = []
       for colItemindex in colItemindexs do
        if colItemindex.Partitions==0
          index.push(colItemindex.Index)
        end       
       end
       if index.empty?
        return false
       end
  for i in index do
    colItems = WMI.execquery ("select * from Win32_DiskDrive where index=#{i}")
    for colItem in colItems do
      size=colItem.size
      size=(size.to_i/1048576).to_s
    end
    
    logicalcolItems = WMI.execquery ("select * from Win32_LogicalDisk")
    str=[]
        for colItem in logicalcolItems do
          str.push(colItem.DeviceID)
          volumename=str.sort!.last.next
        end

    File.open("c:/1.txt","w")do|file|
    file.puts "select disk=#{i}"
    file.puts "create partition primary size=#{size}"
    file.puts "assign letter=#{volumename}"
      end
    unless format_disk(volumename)
      return false
    end 
  File.delete("c:/1.txt")
  end
  return true
end

#delete_partition(index) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/vmopt/disk_operation.rb', line 165

def delete_partition(index)
  indexEffectives = WMI.execquery("select * from Win32_DiskDrive ")
     indexarr=[]
     for indexeff in indexEffectives do
      indexarr.push(indexeff.index)
     end
  if index<0 or index > indexarr.sort!.last 
     return false 
  end

  File.open("c:/2.txt","w") do|file|
  file.puts "select disk=#{index}"
  file.puts "select partition 1"
  file.puts "delete partition override"
  end
  ret = system("diskpart /s c:/2.txt > null 2>&1 ")
  File.delete("c:/2.txt")
  if ret == false
    return false
  end
  return true
end

#format_disk(volumename) ⇒ Object

参数:无功能:格式化磁盘,供其他方法调用返回值:默认



75
76
77
78
79
# File 'lib/vmopt/disk_operation.rb', line 75

def format_disk(volumename)
  ret1= system("diskpart /s c:/1.txt > null 2>&1 ")
  ret2 = system("format /FS:NTFS /force /Q  #{volumename}  >null 2>&1" )
  return ret1 && ret2
end

#format_disk_by_index(index) ⇒ Object

参数:磁盘索引号功能:根据指定的磁盘索引号返回值:指定的磁盘已经被格式化返回false,成功格式化返回true



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vmopt/disk_operation.rb', line 127

def format_disk_by_index(index)

  indexEffectives = WMI.execquery("select * from Win32_DiskDrive ")
     indexarr=[]
     for indexeff in indexEffectives do
      indexarr.push(indexeff.index)
     end
  if index<0 or index > indexarr.sort!.last 
     return false 
  end

  
  colItems = WMI.execquery ("select * from Win32_DiskDrive where index=#{index}")
  for colItem in colItems do
        if colItem.Partitions==0
          size=colItem.size
      size=(size.to_i/1048576).to_s
        else
          return false
        end
       end
       logicalcolItems = WMI.execquery ("select * from Win32_LogicalDisk")
      str=[]
       for colItem in logicalcolItems do
        str.push(colItem.DeviceID)
        volumename=str.sort!.last.next
       end

  File.open("c:/1.txt","w") do|file|
  file.puts "select disk=#{index}"
  file.puts "create partition primary size=#{size}"
  file.puts "assign letter=#{volumename}"
  end
  return false unless format_disk(volumename)
  #File.open("c:/1.txt","w+")

  File.delete("c:/1.txt")
  return true
end

#get_disk_informationObject

参数:无作用:查找物理磁盘的基本信息,并打印出来返回值:默认



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vmopt/disk_operation.rb', line 12

def get_disk_information
  data_value={}
  colItems = WMI.execquery ("select * from Win32_DiskDrive")
       for colItem in colItems do
        size=colItem.size
    size=size.to_i/1048576
        str={ "磁盘ID" => colItem.DeviceID,
            "磁盘索引" => colItem.Index,
            "接口类型" => colItem.InterfaceType,
            "磁盘容量" => size
          }
        data_value["diks#{colItem.Index}"] = str
       end
       return data_value
end

#get_partition_informationObject

参数:无作用:查找磁盘分区的基本信息,并打印出来返回值:默认



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vmopt/disk_operation.rb', line 32

def get_partition_information
  data_value={}
  colItems = WMI.execquery ("select * from Win32_LogicalDisk where DriveType=3")
       for colItem in colItems do
    size=colItem.size
    size=size.to_i/1048576
    freeSpace=colItem.FreeSpace
    freeSpace=freeSpace.to_i/1048576

        str={"逻辑驱动器号" => colItem.DeviceID,
             "文件系统" => colItem.FileSystem ,
             "分区容量" => size,  
             "剩余容量" => freeSpace
            }
   
        data_value["partition#{colItem.DeviceID}"] = str
       end
      return data_value
end

#unformat_diskObject

参数:无功能:判断哪些磁盘没有格式化返回值:没有格式化的磁盘索引号



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vmopt/disk_operation.rb', line 57

def unformat_disk
  data_value = []
  colItems = WMI.execquery ("select * from Win32_DiskDrive")
       for colItem in colItems do
        if colItem.Partitions==0
          #str="#{colItem.Index}"

          #data_value["index#{colItem.Index}"]= str

          data_value << colItem.Index
        end       
       end

       return {"diskIndex" => data_value}
end