-
Notifications
You must be signed in to change notification settings - Fork 2
/
mount.rb
126 lines (104 loc) · 2.79 KB
/
mount.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
122
123
124
125
126
# frozen_string_literal: true
def mountImage(img, path)
puts "mounting image #{img} on #{path}"
`mount -o loop #{img} #{path}`
end
def unmountImage(path)
puts "unmounting image on #{path}"
`umount #{path}`
end
# ref: https://wiki.alpinelinux.org/wiki/How_to_make_a_cross_architecture_chroot
def mountDev(path)
puts "mounting /dev on #{path}"
`mount -t proc /proc #{path}/proc`
`mount -t sysfs /sys #{path}/sys`
`mount -o bind /dev #{path}/dev`
`mount -o bind /dev/pts #{path}/dev/pts`
`mount -o bind /run #{path}/run`
end
def unmountDev(path)
puts "unmounting /dev on #{path}"
`umount #{path}/proc`
`umount #{path}/sys`
`umount #{path}/dev/pts`
`umount #{path}/dev`
`umount #{path}/run`
end
# https://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device
def getImageLoops(img)
cmd = "losetup | grep #{img}"
IO.popen(cmd) do |r|
lines = r.readlines
return nil if lines.empty?
loops = []
lines.each do |line|
lo = line.delete_suffix("\n").split(" ").first
loops.push lo
end
return loops
end
end
def mountLoop(img, *opt)
loop_dev = opt[0]
offset = opt[1]
unless loop_dev.nil?
if !offset.nil?
`losetup -o #{offset} #{loop_dev} #{img}`
else
`losetup #{loop_dev} #{img}`
end
return loop_dev
end
output = `losetup -Pf --show #{img}`
return output.delete_suffix("\n")
end
def mountCombinedImage(img, path = nil, *opt)
par = opt[0]
loop_dev = opt[1]
if loop_dev.nil?
loop_dev = mountLoop img
else
mountLoop img, loop_dev
end
if path != nil
system "fdisk -l #{img}"
if par.nil?
print "which partition to mount: "
par = gets.delete_suffix("\n")
# p par
end
loop_par = "#{loop_dev}p#{par}"
puts "mounting #{loop_par} on #{path}"
`mount #{loop_par} #{path}`
else
puts "#{img} mounted on #{loop_dev}"
end
end
def unmountCombinedImage(img)
loops = getImageLoops img
loops.each do |lo|
puts "unmounting combined image on #{lo}"
`losetup -d #{lo}`
end
end
if __FILE__ == $0
# img = "ubuntu-core-gnome.img"
# path = "mars-A1-ub18-core"
img = "vf2.img"
path = "rootfs"
# mountImage img, path
# mountCombinedImage img, path
# mountDev path
# unmountDev path
# puts "waiting for 3s"
# sleep 3
unmountImage path
# unmountCombinedImage img
img2 = 'starfive-jh7110-VF2-VF2_515_v2.3.0-55.img'
# mountCombinedImage img2, "vf-de"
unmountImage "vf-de"
# mountCombinedImage img2
# mountCombinedImage img
unmountCombinedImage img2
unmountCombinedImage img
end